从shp文件读取坐标并计算距离 [英] read coordinates from shp file and compute distance

查看:252
本文介绍了从shp文件读取坐标并计算距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to compute the nearest distance from a point to a shp file (ports.shp) from natural earth data.

例如,我正在加载文件的功能:

For example, I am loading the features of the file:

...
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();

}

现在,例如,我要从中计算距离的一个点是:

Now, a point for example from which I want to compute the distance is:

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文件):

but I don't know how to find the coordinates of the destination point (the ports.shp file):

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

我从加载文件中得到了features,但是它没有任何getCoordinates()方法.

I have the features from loading the file but it doesn't have any getCoordinates() method.

此外,正如我所见,ports.shp由许多POINT几何组成.我是否必须以某种方式计算参考点的每个点,然后选择最接近的点?

Also, as i can see ports.shp consists of many POINT geometry.Do I have to compute somehow every point with the reference point and then select the nearest?

推荐答案

功能部件具有getDefaultGeometry方法,该方法将为您提供所需的要点.然后,您可以从该点获取坐标.

Feature has a getDefaultGeometry method which will give you the point you need. Then you can get the coordinates from the point.

编辑

您的问题是单位不匹配,您正在将MinDist设置为边界框的宽度(以度为单位,大约为360),但将其与以米为单位的距离(大约为7800000)进行比较,因此您永远找不到一个关闭的点足以保存.

Your problem was a units mismatch, you were setting MinDist to the width of the bounding box (in degrees, so about 360) but comparing it to distances in metres (so about 7800000) so you never found a point close enough to save.

我开始通过限制初始搜索范围来提高搜索的效率,但是即使使用了我无法确定是否有帮助的人口统计数据集,它也足够快.

I started playing with making the search more efficient by limiting the initial search bounds but it is sufficiently fast even when using the populated places data set that I can't really tell if it helps.

    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());
}

这篇关于从shp文件读取坐标并计算距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆