您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从shp文件读取坐标并计算距离

从shp文件读取坐标并计算距离

功能部件具有一种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);
        B@R_240_2419@ b@R_240_2419@ = ff.b@R_240_2419@(ff.property(index.getSchema().getGeometryDescriptor().getName()), (Bounding@R_240_2419@) search);
        SimpleFeatureCollection candidates = index.subCollection(b@R_240_2419@);

        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());
}
其他 2022/1/1 18:25:43 有526人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶