| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.precision; |
| 13 |
|
| 14 |
import org.locationtech.jts.geom.Coordinate; |
| 15 |
import org.locationtech.jts.geom.CoordinateList; |
| 16 |
import org.locationtech.jts.geom.Geometry; |
| 17 |
import org.locationtech.jts.geom.LineString; |
| 18 |
import org.locationtech.jts.geom.LinearRing; |
| 19 |
import org.locationtech.jts.geom.PrecisionModel; |
| 20 |
import org.locationtech.jts.geom.util.GeometryEditor; |
| 21 |
|
| 22 |
public class PrecisionReducerCoordinateOperation extends |
| 23 |
GeometryEditor.CoordinateOperation |
| 24 |
{ |
| 25 |
private PrecisionModel targetPM; |
| 26 |
private boolean removeCollapsed = true; |
| 27 |
|
| 28 |
public PrecisionReducerCoordinateOperation(PrecisionModel targetPM, boolean removeCollapsed) |
| 29 |
{ |
| 30 |
this.targetPM = targetPM; |
| 31 |
this.removeCollapsed = removeCollapsed; |
| 32 |
} |
| 33 |
|
| 34 |
public Coordinate[] edit(Coordinate[] coordinates, Geometry geom) { |
| 35 |
if (coordinates.length == 0) |
| 36 |
return null; |
| 37 |
|
| 38 |
Coordinate[] reducedCoords = new Coordinate[coordinates.length]; |
| 39 |
|
| 40 |
for (int i = 0; i < coordinates.length; i++) { |
| 41 |
Coordinate coord = new Coordinate(coordinates[i]); |
| 42 |
targetPM.makePrecise(coord); |
| 43 |
reducedCoords[i] = coord; |
| 44 |
} |
| 45 |
|
| 46 |
CoordinateList noRepeatedCoordList = new CoordinateList(reducedCoords, |
| 47 |
false); |
| 48 |
Coordinate[] noRepeatedCoords = noRepeatedCoordList.toCoordinateArray(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Check to see if the removal of repeated points collapsed the coordinate |
| 52 |
* List to an invalid length for the type of the parent geometry. It is not |
| 53 |
* necessary to check for Point collapses, since the coordinate list can |
| 54 |
* never collapse to less than one point. If the length is invalid, return |
| 55 |
* the full-length coordinate array first computed, or null if collapses are |
| 56 |
* being removed. (This may create an invalid geometry - the client must |
| 57 |
* handle this.) |
| 58 |
*/ |
| 59 |
int minLength = 0; |
| 60 |
if (geom instanceof LineString) |
| 61 |
minLength = 2; |
| 62 |
if (geom instanceof LinearRing) |
| 63 |
minLength = 4; |
| 64 |
|
| 65 |
Coordinate[] collapsedCoords = reducedCoords; |
| 66 |
if (removeCollapsed) |
| 67 |
collapsedCoords = null; |
| 68 |
|
| 69 |
|
| 70 |
if (noRepeatedCoords.length < minLength) { |
| 71 |
return collapsedCoords; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
return noRepeatedCoords; |
| 76 |
} |
| 77 |
} |
| 78 |
|