Read geometries from ESRI shapefile 'one-by-one'

1k views Asked by At

I am trying to achieve simple goal. Using https://github.com/Esri/geometry-api-java library I want to read multiple geometries from shapefile, but so far it seems that API doesn't provide ability to do so, by now I managed to impport single geometry from file (first one).

Here are code samples that I tried in order to make it works:

property in = FileInputStream

Geometry geometry = GeometryEngine.geometryFromEsriShape(IOUtils.toByteArray(in), Geometry.Type.Unknown);
System.out.println(geometry);


OperatorImportFromESRIShape importerShape = (OperatorImportFromESRIShape) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ImportFromESRIShape);
ByteBuffer byteBuffer = ByteBuffer.wrap(IOUtils.toByteArray(in));
Geometry geometry = importerShape.execute(0, Geometry.Type.Unknown, byteBuffer);

In both cases API returns only single Geometry. The question is how to read multiple geometries from input stream one by one.

Thank you for any help.

1

There are 1 answers

3
oers On

When you move your java package to:

package com.esri.core.geometry;

You can access a package protected Method, that returns a GeometryCursor:

buffer = ByteBuffer.wrap(Files.readAllBytes(Paths.get("Path_to_Shapefile")));
buffer.order(ByteOrder.LITTLE_ENDIAN);
OperatorImportFromESRIShape op = (OperatorImportFromESRIShape) OperatorFactoryLocal.getInstance().getOperator(
        Type.ImportFromESRIShape);
SimpleByteBufferCursor byteBufferCursor = new SimpleByteBufferCursor(buffer);
GeometryCursor geometryCursor = op.execute(ShapeImportFlags.ShapeImportNonTrusted, Geometry.Type.Unknown,
        byteBufferCursor);
Geometry cursor;
while ((cursor = geometryCursor.next()) != null) {
  System.out.println(cursor);
}