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

从shp文件读取坐标并计算距离,第1张

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5020969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存