Hi all,
I have been trying to read a shapefile using the Geotools libraries, but I get an exception like this when I attempt to iterate the features:
java.lang.IllegalArgumentException: Invalid ordinate index: 3
at org.locationtech.jts.geom.CoordinateXYM.setOrdinate(CoordinateXYM.java:146)
at org.locationtech.jts.geom.impl.CoordinateArraySequence.setOrdinate(CoordinateArraySequence.java:305)
at org.geotools.data.shapefile.shp.PolygonHandler.readCoordinates(PolygonHandler.java:311)
at org.geotools.data.shapefile.shp.PolygonHandler.read(PolygonHandler.java:142)
at org.geotools.data.shapefile.shp.ShapefileReader$Record.shape(ShapefileReader.java:113)
at org.geotools.data.shapefile.ShapefileFeatureReader.getGeometry(ShapefileFeatureReader.java:238)
at org.geotools.data.shapefile.ShapefileFeatureReader.hasNext(ShapefileFeatureReader.java:181)
I have been using Geotools (and JTS) successfully for 8-10 years, so I don't consider myself a rookie as such, but still there might be something I have misunderstood here, and I am by far any expert on GIS matters. But this is the first time I have met such an exception despite having processed hundreds of shapefiles before.
I attempted to debug this case, and doing so I found something which I wonder if could be the cause of the problem above.
First of all, the library versions in play:
- JTS: 1.17.1
- Geotools: 24.0
Then, some parameters pulled out from the operation in question:
flatFeature: false
dimensions: 2
numParts: 1
numPoints: 1099
shapeType = PolygonM
Then, when the PolygonHandler of Geotools is going to read the coordinates, it invokes the following:
So when setting the ordinateIndex, Geotools makes use of CoordinateSequence.M from JTS.
It is defined like:
int M = 3;
But then when the CoordinateXYM.class is going to handle this setOrdinate() operation, despite M (with value 3) being passed from Geotools, we end up in the default case and throw an exception. And if we check the definition of "M" in the CoordinateXYM.class, it is defined as
public static final int M = 2;
So this is what my question boils down to, the definition of M is different in these two classes:
org.locationtech.jts.geom.CoordinateXYM.CLASS
public static final int M = 2;
org.locationtech.jts.geom.CoordinateSequence.CLASS
int M = 3;
Is that correct?
Unfortunately I cannot supply the shapefile in question as it holds sensitive data.
Kind Regards,
Eivind Rønnevik
_______________________________________________