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 WKB support for POINT EMPTY #567

Merged
merged 1 commit into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
* with arbitrary byte stream sources.
* <p>
* This class reads the format describe in {@link WKBWriter}.
* It also partially handles
* It partially handles
* the <b>Extended WKB</b> format used by PostGIS,
* by parsing and storing SRID values.
* Although not defined in the WKB spec, empty points
* are handled if they are represented as a Point with <code>NaN</code> X and Y ordinates.
* <p>
* The reader repairs structurally-invalid input
* (specifically, LineStrings and LinearRings which contain
* too few points have vertices added,
Expand Down Expand Up @@ -254,6 +257,10 @@ private Geometry setSRID(Geometry g, int SRID)
private Point readPoint() throws IOException
{
CoordinateSequence pts = readCoordinateSequence(1);
// If X and Y are NaN create a empty point
if (Double.isNaN(pts.getX(0)) || Double.isNaN(pts.getY(0))) {
return factory.createPoint();
}
return factory.createPoint(pts);
}

Expand Down
21 changes: 15 additions & 6 deletions modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
* by setting the third bit of the <tt>wkbType</tt> word.
* EWKB format is upward compatible with the original SFS WKB format.
* <p>
* Empty Points cannot be represented in WKB; an
* {@link IllegalArgumentException} will be thrown if one is
* written.
* Empty Points are output as a Point with <code>NaN</code> X and Y ordinate values.
* <p>
* The WKB specification does not support representing {@link LinearRing}s;
* they will be written as {@link LineString}s.
Expand Down Expand Up @@ -330,11 +328,13 @@ else if (geom instanceof GeometryCollection)

private void writePoint(Point pt, OutStream os) throws IOException
{
if (pt.getCoordinateSequence().size() == 0)
throw new IllegalArgumentException("Empty Points cannot be represented in WKB");
writeByteOrder(os);
writeGeometryType(WKBConstants.wkbPoint, pt, os);
writeCoordinateSequence(pt.getCoordinateSequence(), false, os);
if (pt.getCoordinateSequence().size() == 0) {
writeNaNs(2, os);
} else {
writeCoordinateSequence(pt.getCoordinateSequence(), false, os);
}
}

private void writeLineString(LineString line, OutStream os)
Expand Down Expand Up @@ -424,4 +424,13 @@ private void writeCoordinate(CoordinateSequence seq, int index, OutStream os)
os.write(buf, 8);
}
}

private void writeNaNs(int numNaNs, OutStream os)
throws IOException
{
for (int i = 0; i < numNaNs; i++) {
ByteOrderValues.putDouble(Double.NaN, buf, byteOrder);
os.write(buf, 8);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public void testPointPCS() throws IOException, ParseException {
public void testPoint() throws IOException, ParseException {
runWKBTest("POINT (1 2)");
}


public void testPointEmpty() throws IOException, ParseException {
runWKBTest("POINT EMPTY");
}

public void testLineString()
throws IOException, ParseException
{
Expand Down