gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:36:03 27 4
gpt4 key购买 nike

我想根据自然地球数据计算从一个点到 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(新坐标(??????));

我有加载文件的功能,但它没有任何getCooperatives()方法。

另外,正如我所看到的ports.shp由许多POINT几何图形组成。我是否必须以某种方式计算引用点的每个点,然后选择最近的点?

最佳答案

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

关于java - 从 shp 文件读取坐标并计算距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com