Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GeometryFixer class #704

Merged
merged 12 commits into from Apr 8, 2021
Merged
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