【问题标题】:Retrieve map data via WFS with GeoTools使用 GeoTools 通过 WFS 检索地图数据
【发布时间】:2017-06-16 08:34:15
【问题描述】:

我正在尝试使用 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
  • 参考信封[-132576.7891571155:743466.624998733, -15669.960592884949:1248847.1762802668]

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

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

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

谢谢!

【问题讨论】:

    标签: java geotools


    【解决方案1】:

    首先澄清术语:

    1. TypeName数据类型模式的标识符。
    2. 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);
    

    【讨论】:

    • 非常感谢您的回答! :-) 我们的 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'不存在。 (是的,类型名似乎也很随机)。 ...
    • 我在原始问题中所述的 wfs 服务器上没有看到此架构错误。如果我将浏览器指向“172.20.6.150:8085/LuciadFusion/…”,我会得到一些充满数据的 xml。使用地理工具时,我是否需要架构来访问这些数据?还是我仍然可以读取数据(以防那些试图设置 Luciad 服务器的人不知道如何将架构放置到位)?再次感谢 - 非常感谢您的帮助!
    • 您确实需要知道要获取的 featureCollection 的 typeName,您可以直接读取 getCapabilities 响应以查看可用的内容。 GeoTools 应该自动获取并找出架构。
    • 所以我成功地从我的服务器创建了一个 WFSDataStore,并且能够列出所有类型名称。但是当试图说 dataStore.getFeatureSource(aTypeName);它因“java.io.IOException:架构'_772ced6e_45_54b5_45_4295_45_bb01_45_a50b9179f1ecWorldType'不存在而失败。”-异常。我做错了什么?
    • 你用的typename是什么?
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2020-02-06
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多