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));
I know that to compute the distance I will do:
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();
but I don't know how to find the coordinates of the destination point (the ports.shp file):
Point dest = gf.createPoint(new Coordinate(?????));
I have the features
from loading the file but it doesn't have any getCoordinates()
method.
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?