riskaware-ltd/open-eaggr

Eaggr.convertDggsCellOutlineToShapeString generates invalid WKT and invalid GeoJSON

aaime opened this issue · 1 comments

aaime commented

Trying out a simple class playing with WKT results in errors:

import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import uk.co.riskaware.eaggr.DggsCell;
import uk.co.riskaware.eaggr.Eaggr;
import uk.co.riskaware.eaggr.EaggrException;
import uk.co.riskaware.eaggr.EaggrLibraryException;
import uk.co.riskaware.eaggr.enums.DggsModel;
import uk.co.riskaware.eaggr.enums.ShapeStringFormat;

public class EaggrDecodeToPolygon {

  public static void main(String[] args)
      throws EaggrException, EaggrLibraryException, ParseException {
    Eaggr dggs = new Eaggr(DggsModel.ISEA4T);

    String wkt =
        dggs.convertDggsCellOutlineToShapeString(new DggsCell("19"), ShapeStringFormat.WKT);
    System.out.println(wkt);
    System.out.println(new WKTReader().read(wkt));
  }
}

The output being:

POLYGON ((143.999999999637282 -26.719369956293967,0 -90,-143.999999997810562 -26.719369955760964))
Exception in thread "main" java.lang.IllegalArgumentException: Points of LinearRing do not form a closed linestring
	at org.locationtech.jts.geom.LinearRing.validateConstruction(LinearRing.java:90)
	at org.locationtech.jts.geom.LinearRing.<init>(LinearRing.java:85)
	at org.locationtech.jts.geom.GeometryFactory.createLinearRing(GeometryFactory.java:356)
	at org.locationtech.jts.io.WKTReader.readLinearRingText(WKTReader.java:879)
	at org.locationtech.jts.io.WKTReader.readPolygonText(WKTReader.java:917)
	at org.locationtech.jts.io.WKTReader.readGeometryTaggedText(WKTReader.java:818)
	at org.locationtech.jts.io.WKTReader.readGeometryTaggedText(WKTReader.java:785)
	at org.locationtech.jts.io.WKTReader.read(WKTReader.java:233)
	at org.locationtech.jts.io.WKTReader.read(WKTReader.java:214)
	at org.geoserver.dggs.eaggr.EaggrDecodeToPolygon.main(EaggrDecodeToPolygon.java:21)

JTS complaint is legit, in WKT the last and first point of a polygon ring must be the same, there is no "shortcut".

The GeoJSON generated output is invalid for the same reason, by spec a polygon ring must have at least 4 points, the first and the last are required to be identical:
https://tools.ietf.org/html/rfc7946#page-9

While I'm here, I don't see any way to just get the outline coordinates, without having to parse a string. Did I overlooked it?

That may be an oversight on the format of the strings, it would be a relatively easy change to update them to close the polygons. In the main we have used the export of cells to KML to view the DGGS cells rather than the WKT or GeoJSON.
There is no method to get the outline coordinates as this was not a requirement when we created this library, however it would be possible to add this in the future easily enough.