Skip to content

Commit

Permalink
Add GeometryFixer class (#704)
Browse files Browse the repository at this point in the history
* GeometryFixer added

Signed-off-by: Martin Davis <mtnclimb@gmail.com>
  • Loading branch information
dr-jts committed Apr 8, 2021
1 parent 354f657 commit 2e4fef2
Show file tree
Hide file tree
Showing 9 changed files with 963 additions and 27 deletions.
Expand Up @@ -39,8 +39,8 @@ public class BufferFunctions {
public static String bufferDescription = "Buffers a geometry by a distance";

@Metadata(description="Buffer a geometry by a distance")
public static Geometry buffer(Geometry g, double distance) { return g.buffer(distance); }
public static Geometry buffer(Geometry g, double distance) { return g.buffer(distance); }

public static Geometry bufferWithParams(Geometry g,
Double distance,
@Metadata(title="Quadrant Segs")
Expand Down
Expand Up @@ -264,6 +264,19 @@ public void setOrdinate(int ordinateIndex, double value)
}
}

/**
* Tests if the coordinate has valid X and Y ordinate values.
* An ordinate value is valid iff it is finite.
*
* @return true if the coordinate is valid
* @see Double#isFinite(double)
*/
public boolean isValid() {
if (! Double.isFinite(x)) return false;
if (! Double.isFinite(y)) return false;
return true;
}

/**
* Returns whether the planar projections of the two <code>Coordinate</code>s
* are equal.
Expand Down
Expand Up @@ -371,8 +371,11 @@ public static Coordinate[] toCoordinateArray(Collection coordList) {
}

/**
* Returns whether #equals returns true for any two consecutive Coordinates
* Tests whether {@link Coordinate#equals(Object) returns true for any two consecutive Coordinates
* in the given array.
*
* @param coord an array of coordinates
* @return true if the array has repeated points
*/
public static boolean hasRepeatedPoints(Coordinate[] coord) {
for (int i = 1; i < coord.length; i++) {
Expand All @@ -382,7 +385,7 @@ public static boolean hasRepeatedPoints(Coordinate[] coord) {
}
return false;
}

/**
* Returns either the given coordinate array if its length is greater than the
* given amount, or an empty coordinate array.
Expand All @@ -396,6 +399,8 @@ public static Coordinate[] atLeastNCoordinatesOrNothing(int n, Coordinate[] c) {
* constructs a new array containing no repeated points.
* Otherwise, returns the argument.
*
* @param coord an array of coordinates
* @return the array with repeated coordinates removed
* @see #hasRepeatedPoints(Coordinate[])
*/
public static Coordinate[] removeRepeatedPoints(Coordinate[] coord) {
Expand All @@ -404,6 +409,44 @@ public static Coordinate[] removeRepeatedPoints(Coordinate[] coord) {
return coordList.toCoordinateArray();
}

/**
* Tests whether an array has any repeated or invalid coordinates.
*
* @param coord an array of coordinates
* @return true if the array contains repeated or invalid coordinates
* @see Coordinate#isValid()
*/
public static boolean hasRepeatedOrInvalid(Coordinate[] coord) {
for (int i = 1; i < coord.length; i++) {
if (! coord[i].isValid())
return true;
if (coord[i - 1].equals(coord[i])) {
return true;
}
}
return false;
}

/**
* If the coordinate array argument has repeated or invalid points,
* constructs a new array containing no repeated points.
* Otherwise, returns the argument.
*
* @param coord an array of coordinates
* @return the array with repeated and invalid coordinates removed
* @see #hasRepeatedOrInvalid(Coordinate[])
* @see Coordinate#isValid()
*/
public static Coordinate[] removeRepeatedAndInvalidPoints(Coordinate[] coord) {
if (!hasRepeatedOrInvalid(coord)) return coord;
CoordinateList coordList = new CoordinateList();
for (int i = 0; i < coord.length; i++) {
if (! coord[i].isValid()) continue;
coordList.add(coord[i], false);
}
return coordList.toCoordinateArray();
}

/**
* Collapses a coordinate array to remove all null elements.
*
Expand Down

0 comments on commit 2e4fef2

Please sign in to comment.