使用 GeoTools 通过 WFS 检索地图数据

Retrieve map data via WFS with 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);
}

运行 它打印:

但是,当我自己的 WFS 服务器设置完成后,它将包含更多的类型名称,每个类型名称都描述了一个区域的 fx 道路或湖泊。 对于许多领域。

如何获取与定义区域(边界框 BB)相关的类型名称,或者如果可能的话,甚至可能仅获取 BB 覆盖的类型名称中的特征?

其次,当从上面的例子中提取数据时,所有特征都在错误的 CoordinateReferenceSystem 中 - 我如何强制它成为 EPSG:4326?

谢谢!

首先澄清术语:

  1. a TypeName 数据类型 模式 .
  2. 的标识符
  3. a Feature 是具有位置和属性的实际数据。

要限制返回的功能数量,您需要使用 Query object. This allows you to specify a Filter to restrict the features returned. In the case of a WFSDatastore (and most others) it is converted into something the underlying store understands and handled there. You can create features in all sorts of ways including a FilterFactory but the easiest is to use ECQL which allows you to write more human understandable filters directly. There is a 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);