功能部件具有一种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());
}