【问题标题】:Transform FeatureCollection to a LineString with Geotools使用 Geotools 将 FeatureCollection 转换为 LineString
【发布时间】:2018-08-18 15:02:18
【问题描述】:

我正在使用 Geomesa,我想编写一些适用于 JAVA 的程序,在这个示例中,我试图在一个区域中获取一些点并将其转换为一条线以便稍后绘制它:

Query query=new Query(feature.getTypeName());
        query.setFilter(CQL.toFilter(filtre));
        query.setSortBy(new SortBy[] {CommonFactoryFinder.getFilterFactory2().sort("dtg",SortOrder.ASCENDING) });
        FeatureCollection collection=source.getFeatures(query);
        FeatureIterator iterator=collection.features();

我得到了我需要的所有点,但问题是我不能直接将它转换为 LineString ,我所做的是我迭代了所有集合并收集了 ArrayList 中每个元素的几何形状,然后我转换了将 ArrayList 转换为坐标数组并创建 LineString

   Coordinate[] tab;
        cool.add(((com.vividsolutions.jts.geom.Point)save.getDefaultGeometry()).getCoordinate());
        while (iterator.hasNext()) {
            actuel=iterator.next();
            double distance= ( (com.vividsolutions.jts.geom.Point)save.getDefaultGeometry()).distance((com.vividsolutions.jts.geom.Point)actuel.getDefaultGeometry());
                if(distance<0.3 ){
                    if(distance>0) 
                   cool.add(((com.vividsolutions.jts.geom.Point) actuel.getDefaultGeometry()).getCoordinate());

            }
            else{
                tab=new Coordinate[cool.size()];
                tab=cool.toArray(tab);
                route=factory.createLinearRing(tab);
                System.out.println(route);
                cool=new ArrayList<>();
            }
        }
        tab=new Coordinate[cool.size()+1];
        tab=cool.toArray(tab);
        tab[cool.size()]=cool.get(0);
        route=factory.createLinearRing(tab);
        System.out.println(route);
        iterator.close();

有没有其他方法可以直接获得所有积分而不做所有这些工作?

【问题讨论】:

    标签: geotools geomesa


    【解决方案1】:

    如果我正确理解您的问题,您有 Points 的 SimpleFeatureCollection 并且您想将其转换为 LineString 如果差距超过 0.3(度数?),您开始新队。

    将一组特征转换为一条线没有捷径,因此您需要遍历它们并提取点。我会这样做:

      public List<LineString> toLine(SimpleFeatureCollection features) {
        GeometryFactory factory = new GeometryFactory();
        Point lastPoint = null;
        List<LineString> routes = new ArrayList<>();
        ArrayList<Coordinate> coordList = new ArrayList<>();
        LineString route;
        try (SimpleFeatureIterator iter = features.features()) {
          while (iter.hasNext()) {
            SimpleFeature f = iter.next();
            Point p = (Point) f.getDefaultGeometry();
            double distance;
            if (lastPoint != null) {
              distance = p.distance(lastPoint);
            } else {
              distance = -1;
              lastPoint = p;
            }
            if (distance != -1 && distance < 0.3) {
              if (distance > 0) {
                // skip repeats
                coordList.add(p.getCoordinate());
              }
            } else {
              route = factory.createLineString(coordList.toArray(new Coordinate[] {}));
              routes.add(route);
              System.out.println(route);
              coordList = new ArrayList<>();
            }
          }
        }
        return routes;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多