使用GeoTools

时间:2017-06-16 08:34:16

标签: java geotools

我试图使用GeoTools从LuciadFusion wfs服务器检索数据,但是在查找如何实现目标的示例方面遇到了麻烦。

我的想法是跟踪移动的东西,并希望从我移动的移动区域中检索地图数据(特征),然后计算距离(例如距离最近的道路,距离最近的湖泊有多远)岸)。

我想获取这些功能,将它们放入SpatialIndexFeatureCollection(保存在内存中以便快速访问,因为我跟踪多个移动对象),在那里我可以查询我想知道的内容。

到目前为止,我查询了一些我发现的随机wfs服务器:http://ogc.bgs.ac.uk/digmap625k_gsml32_insp_gs/wfs?我能够阅读这些功能,并从构建我的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 个答案:

答案 0 :(得分:1)

首先澄清术语:

  1. TypeName数据类型架构的标识符。
  2. a Feature是具有位置和属性的实际数据。
  3. 要限制返回的功能数量,您需要使用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);
    
相关问题