2017-06-16 21 views
1

我試圖使用GeoTools從LuciadFusion wfs服務器中檢索數據,但遇到了如何實現我的目標的示例。 這個想法是,我正在跟蹤移動的東西,並且想要從我移動的t is正在移動到的區域中檢索地圖數據(特徵),然後計算距離(如離最近的道路,最近的湖岸沿岸多遠)。 我想獲得的功能,把它們放到一個SpatialIndexFeatureCollection(保存在內存中快速訪問,因爲我跟蹤多個移動對象),在那裏我可以查詢我想知道的東西。使用GeoTools通過WFS回收地圖數據

到目前爲止,我正在查詢一些隨數據發現的隨機wfs服務器:http://ogc.bgs.ac.uk/digmap625k_gsml32_insp_gs/wfs?我能夠讀取能力,並且從typenames的一個建設我SpatialIndexFeatureCollection:

String url = "http://ogc.bgs.ac.uk/digmap625k_gsml32_insp_gs/wfs?"; 

Map connectionParameters = new HashMap(); 
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", url) 
connectionParameters.put(WFSDataStoreFactory.TIMEOUT.key, 100000); 
WFSDataStoreFactory dsf = new WFSDataStoreFactory(); 

try { 
    WFSDataStore dataStore = dsf.createDataStore(connectionParameters); 

    for(String s : dataStore.getTypeNames()){ 
     System.out.println(s); 

    } 

    SimpleFeatureSource source = dataStore.getFeatureSource("test:uk_625k_mapped_feature"); 
    SimpleFeatureCollection fc = source.getFeatures(); 
    System.out.println(fc.getBounds()); 

    SpatialIndexFeatureCollection index = new SpatialIndexFeatureCollection(); 
    fc.accepts(new FeatureVisitor() { 
     @Override 
     public void visit(Feature feature) { 
     SimpleFeature simpleFeature = (SimpleFeature) feature; 
     Geometry geom = (MultiPolygon) simpleFeature.getDefaultGeometry(); 

     if(geom != null) { 
      Envelope env = geom.getEnvelopeInternal(); 

      if(!env.isNull()) { 
       index.add(simpleFeature); 
      } 
     } 
     } 
    }, new NullProgressListener()); 

catch (FactoryException e) { 
    aLog.error("", e); 
} 

運行它打印:

  • GSML:MappedFeature
  • gsmlgu:GeologicUnit
  • 測試: uk_625k_mapped_feature
  • ReferencedEnvelope [-132576.7891571155:743466.624998733, -15669.960592884949:1248847.1762802668]

但是,當我自己的WFS服務器已經建立時,它將包含更多的類型名稱,每個類型名稱都描述某個區域的fx道路或湖泊。 對於很多領域。

如何獲取與定義區域(邊界框BB)相關的類型名稱,或者甚至可能只有BB所覆蓋的類型名稱中的功能(如果可能)?

其次,從上面的例子中提取數據時,所有的功能都在一個錯誤的CoordinateReferenceSystem - 我如何強制它是EPSG:4326?

謝謝!

回答

1

首先闡明術語:

  1. 一個TypeName類型數據的或模式的標識符。
  2. a Feature是具有位置和屬性的實際數據。

要限制返回的功能數量,您需要使用Query object。這允許您指定Filter來限制返回的功能。在WFSDatastore(和其他大多數)的情況下,它被轉換成底層商店理解和處理的內容。您可以使用各種方式創建功能,包括FilterFactory,但最簡單的方法是使用ECQL,它允許您直接編寫更多人性化的可理解過濾器。有一個helpful tutorial here

Filter filter = ECQL.toFilter("BBOX(the_geom, 500000, 700000, 501000, 701000)"); 
    query.setFilter(filter); 
    query.setMaxFeatures(10); 
    SimpleFeatureCollection fc = source.getFeatures(query); 

至於重投影WFS不處理直接,但你可以使用一個ReprojectingFeatureCollection來包裝你的結果。

ReprojectingFeatureCollection rfc = new ReprojectingFeatureCollection(fc, DefaultGeographicCRS.WGS84); 

除非你期待了很多無效的多邊形,那麼你應該能夠使用建立一個空間索引集合:

SpatialIndexFeatureCollection index = new SpatialIndexFeatureCollection(fc.getSchema()); 
index.addAll(fc); 
+0

非常感謝你的回答! :-) 我們的WFS服務器可能有些問題,我想要獲得_source_對象,我仍然需要說''SimpleFeatureSource source = dataStore.getFeatureSource(「test:uk_625k_mapped_feature」);'因此知道我想檢查的typeName,對吧? 當我在自己的服務器上執行此操作時,出現'java.io.IOException:Schema'_772ced6e_45_54b5_45_4295_45_bb01_45_a50b9179f1ecWorldType'不存在。' (是的,typename看起來也很隨意)。 ... – Mars

+0

我在原始問題中所述的wfs服務器上沒有看到此架構錯誤。 如果我將瀏覽器指向'http://172.20.6.150:8085/LuciadFusion/wfs?REQUEST=GetFeature&SERVICE=WFS&VERSION=1.1.0&TYPENAME=_772ced6e_45_54b5_45_4295_45_bb01_45_a50b9179f1ecWorldType'我確實收到了一些xml完整的數據。 使用geotools時,我是否需要架構來訪問這些數據?或者我可以讀取數據(如果試圖設置Luciad服務器的人無法弄清楚如何獲取架構)? 再次感謝 - 幫助非常感謝! – Mars

+0

您確實需要知道要獲取的featureCollection的typeName,您可以直接閱讀getCapabilities響應以查看可用的功能。 GeoTools應該自動獲取並找出模式。 –

相關問題