【问题标题】:read coordinates from shp file and compute distance从 shp 文件中读取坐标并计算距离
【发布时间】:2023-03-23 12:10:01
【问题描述】:

我想根据自然地球数据计算点到 shp 文件 (ports.shp) 的最近距离。

例如我正在加载文件的特征:

...
String filename = "10m_cultural/ne_10m_ports.shp";
...


 public static void Calcs(String filename) 
    throws IOException, NoSuchAuthorityCodeException, FactoryException, TransformException {

    HashMap<String, Object> params = new HashMap<>();
    params.put("url", DataUtilities.fileToURL(new File(filename)));
    DataStore ds = DataStoreFinder.getDataStore(params);

    String name = ds.getTypeNames()[0];
    SimpleFeatureSource source = ds.getFeatureSource(name);
    SimpleFeatureCollection features = source.getFeatures();

}

现在,我想计算距离的一个点是:

GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
Point p = gf.createPoint(new Coordinate(43, 18));

我知道要计算我会做的距离:

     CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");           


      Point start = gf.createPoint(new Coordinate(43, 18));
      Point dest = gf.createPoint(new Coordinate(?????));

      GeodeticCalculator gc = new GeodeticCalculator(crs);
      gc.setStartingPosition(JTS.toDirectPosition(start.getCoordinate(), crs));
      gc.setDestinationPosition(JTS.toDirectPosition(dest.getCoordinate(), crs));


      double distance = gc.getOrthodromicDistance();

但我不知道如何找到目标点的坐标(ports.shp 文件):

Point dest = gf.createPoint(new Coordinate(?????));

我有来自加载文件的features,但它没有任何getCoordinates() 方法。

另外,我可以看到ports.shp 由许多POINT 几何组成。我必须以某种方式计算每个点与参考点,然后选择最近的吗?

【问题讨论】:

  • 这看起来在Geographic Information Systems Stack Exchange 上会更热门。
  • “目标”是指离起始坐标最近的点?如果是这样,您确实必须检查所有其他点。你是如何决定搬家的?
  • @GrayCygnus:是的,我的意思是离起点最近的点(从ports.shp)。我的问题是如何正确读取ports.shp文件中的坐标。然后,我会只需将每个点放到目的地位置,然后找到最接近起点的位置。
  • @polygeo 实际上作为一个编程问题在这里更好

标签: java gis geotools


【解决方案1】:

Feature 有一个getDefaultGeometry 方法,可以为您提供所需的要点。然后您可以从该点获取坐标。

编辑

您的问题是单位不匹配,您将MinDist 设置为边界框的宽度(以度为单位,大约为 360),但将其与以米为单位的距离(大约为 7800000)进行比较,因此您从未找到接近的点足以节省。

我开始尝试通过限制初始搜索范围来提高搜索效率,但即使使用我无法确定是否有帮助的人口稠密地点数据集,它也足够快。

    final double MAX_SEARCH_DISTANCE = Math.max(index.getBounds().getWidth(), index.getBounds().getHeight());
    double searchDist = 0.01;

    while (searchDist < MAX_SEARCH_DISTANCE) {
        // start point (user input)
        Coordinate coordinate = p.getCoordinate();

        ReferencedEnvelope search = new ReferencedEnvelope(new Envelope(coordinate),
                index.getSchema().getCoordinateReferenceSystem());

        search.expandBy(searchDist);
        BBOX bbox = ff.bbox(ff.property(index.getSchema().getGeometryDescriptor().getName()), (BoundingBox) search);
        SimpleFeatureCollection candidates = index.subCollection(bbox);

        double minDist = Double.POSITIVE_INFINITY; // can't use
                                                    // MAX_Search_dist here
                                                    // as it is degrees and
                                                    // dists are meters
        Coordinate minDistPoint = null;
        double dist = 0;
        Point dest = null;
        SimpleFeatureIterator itr = candidates.features();
        CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
        try {
            SimpleFeature feature = null;
            while (itr.hasNext()) {
                feature = itr.next();

                // destination point
                dest = (Point) feature.getDefaultGeometry();
                GeodeticCalculator gc = new GeodeticCalculator(crs);
                gc.setStartingPosition(JTS.toDirectPosition(p.getCoordinate(), crs));
                gc.setDestinationPosition(JTS.toDirectPosition(dest.getCoordinate(), crs));
                // Calculate distance between points
                dist = gc.getOrthodromicDistance();
                // System.out.println(feature.getID()+": "+dist);
                if (dist < minDist) {
                    minDist = dist;
                    minDistPoint = dest.getCoordinate();
                    lastMatched = feature;
                }
            }

        } finally {
            itr.close();
        }
        Point ret = null;

        if (minDistPoint == null) {
            searchDist *= 2.0;
            System.out.println("repeat search");
        } else {
            ret = gf.createPoint(minDistPoint);
            return ret;
        }
    }
    return gf.createPoint(new Coordinate());
}

【讨论】:

  • 好的,谢谢!我看到了。我写了一个小的运行示例。我正在使用“ports.shp”文件。我正在寻找参考点和端口之间的距离(dist)。 shp 点。但是 minDistPoint 是空的,即使 dest.getCoordinate() 不是。如果我在正确的道路上,你能告诉我吗?Codeis here。谢谢!(赞成!)
  • 如果您认为可以帮助我解决上述问题,我将不胜感激! the code is available here now
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多