| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.algorithm; |
| 14 |
|
| 15 |
import org.locationtech.jts.geom.Coordinate; |
| 16 |
import org.locationtech.jts.geom.Geometry; |
| 17 |
import org.locationtech.jts.geom.GeometryCollection; |
| 18 |
import org.locationtech.jts.geom.Point; |
| 19 |
|
| 20 |
/** |
| 21 |
* Computes a point in the interior of an point geometry. |
| 22 |
* <h2>Algorithm</h2> |
| 23 |
* Find a point which is closest to the centroid of the geometry. |
| 24 |
* |
| 25 |
* @version 1.7 |
| 26 |
*/ |
| 27 |
public class InteriorPointPoint { |
| 28 |
|
| 29 |
/** |
| 30 |
* Computes an interior point for the |
| 31 |
* puntal components of a Geometry. |
| 32 |
* |
| 33 |
* @param geom the geometry to compute |
| 34 |
* @return the computed interior point, |
| 35 |
* or <code>null</code> if the geometry has no puntal components |
| 36 |
*/ |
| 37 |
public static Coordinate getInteriorPoint(Geometry geom) { |
| 38 |
InteriorPointPoint intPt = new InteriorPointPoint(geom); |
| 39 |
return intPt.getInteriorPoint(); |
| 40 |
} |
| 41 |
|
| 42 |
private Coordinate centroid; |
| 43 |
private double minDistance = Double.MAX_VALUE; |
| 44 |
|
| 45 |
private Coordinate interiorPoint = null; |
| 46 |
|
| 47 |
public InteriorPointPoint(Geometry g) |
| 48 |
{ |
| 49 |
centroid = g.getCentroid().getCoordinate(); |
| 50 |
add(g); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Tests the point(s) defined by a Geometry for the best inside point. |
| 55 |
* If a Geometry is not of dimension 0 it is not tested. |
| 56 |
* @param geom the geometry to add |
| 57 |
*/ |
| 58 |
private void add(Geometry geom) |
| 59 |
{ |
| 60 |
if (geom instanceof Point) { |
| 61 |
add(geom.getCoordinate()); |
| 62 |
} |
| 63 |
else if (geom instanceof GeometryCollection) { |
| 64 |
GeometryCollection gc = (GeometryCollection) geom; |
| 65 |
for (int i = 0; i < gc.getNumGeometries(); i++) { |
| 66 |
add(gc.getGeometryN(i)); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
private void add(Coordinate point) |
| 71 |
{ |
| 72 |
double dist = point.distance(centroid); |
| 73 |
if (dist < minDistance) { |
| 74 |
interiorPoint = new Coordinate(point); |
| 75 |
minDistance = dist; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public Coordinate getInteriorPoint() |
| 80 |
{ |
| 81 |
return interiorPoint; |
| 82 |
} |
| 83 |
} |
| 84 |
|