2

I'm using GeoTools JTS libraries in Java and I want to find the nearest point inside polygon shape to some single target location. This is exactly what in PostGis ST_ClosestPoint does.

I found this article discussing "Snap a Point to a Line".

So trivial solution will be:

-Dump polygon to lines
-Snap a Point for each above Line
-Calculate the distance between the point and "the snapped" point
-Take the one with minimum distance

How can I achieve my requirement in GeoTools in more "elegant" way?

EDIT:

Actually I'm not sure if the link to snap a point doing exactly what ST_Closest because I don't understand how it's actually extracting the closest point...

  • 2
    Convert polygon into MultiLineString and it will be enough to take just one closest point. – user30184 Sep 27 '15 at 11:09
  • @user30184 You mean with the method in above documentation link? I think this could be an 'answer'... – michael Sep 27 '15 at 12:49
1

JTS's DistanceOP#nearestPoints might be an easy way. I have not tested it.

http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/operation/distance/DistanceOp.html#nearestPoints%28com.vividsolutions.jts.geom.Geometry,%20com.vividsolutions.jts.geom.Geometry%29

Compute the the nearest points of two geometries. The points are presented in the same order as the input Geometries.

| improve this answer | |
  • Hi, is there any better performance alternative for DistanceOp? I have fixed set of polygons (let's say 100) and random points (let's say 100.000). I need to find min distance between every point and polygons. – Capacytron Mar 21 '18 at 12:57
  • Use a spatial database like PostGIS – bugmenot123 Mar 26 '18 at 9:15
  • thanks for your advice, but I have some environment limitations unfortunately, I won't be able to do it. – Capacytron Mar 27 '18 at 9:49
  • Then use some spatial index like rtree or something (I am not developing Java anymore, so I do not know). – bugmenot123 Mar 27 '18 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.