write method
Write the geometry into the ByteBuffer. The position, byteOrder, and limit are all set. The handler is not responsible for writing the record or shape type integer.
@param buffer The ByteBuffer to write to. @param geometry The geometry to write.
Implementation
@override
void write(LByteBuffer buffer, Object geometry) {
MultiPolygon multi;
if (geometry is MultiPolygon) {
multi = geometry;
} else {
multi =
geometryFactory.createMultiPolygon(<Polygon>[geometry as Polygon]);
}
Envelope box = multi.getEnvelopeInternal();
buffer.putDouble64(box.getMinX());
buffer.putDouble64(box.getMinY());
buffer.putDouble64(box.getMaxX());
buffer.putDouble64(box.getMaxY());
// need to find the total number of rings and points
List<CoordinateSequence> coordinates = [];
for (int t = 0; t < multi.getNumGeometries(); t++) {
Polygon p = multi.getGeometryN(t) as Polygon;
coordinates.add(p.getExteriorRing().getCoordinateSequence());
for (int ringN = 0; ringN < p.getNumInteriorRing(); ringN++) {
coordinates.add(p.getInteriorRingN(ringN).getCoordinateSequence());
}
}
int nrings = coordinates.length;
final int npoints = multi.getNumPoints();
buffer.putInt32(nrings);
buffer.putInt32(npoints);
int count = 0;
for (int t = 0; t < nrings; t++) {
buffer.putInt32(count);
count = count + coordinates[t].size();
}
final List<double> zExtreame = [double.nan, double.nan];
// write out points here!.. and gather up min and max z values
for (int ringN = 0; ringN < nrings; ringN++) {
CoordinateSequence coords = coordinates[ringN];
Shapeutils.zMinMax(coords, zExtreame);
final int seqSize = coords.size();
for (int coordN = 0; coordN < seqSize; coordN++) {
buffer.putDouble64(coords.getOrdinate(coordN, 0));
buffer.putDouble64(coords.getOrdinate(coordN, 1));
}
}
if (shapeType == ShapeType.POLYGONZ) {
// z
if (zExtreame[0].isNaN) {
buffer.putDouble64(0.0);
buffer.putDouble64(0.0);
} else {
buffer.putDouble64(zExtreame[0]);
buffer.putDouble64(zExtreame[1]);
}
for (int ringN = 0; ringN < nrings; ringN++) {
CoordinateSequence coords = coordinates[ringN];
final int seqSize = coords.size();
double z;
for (int coordN = 0; coordN < seqSize; coordN++) {
z = coords.getOrdinate(coordN, 2);
if (z.isNaN) {
buffer.putDouble64(0.0);
} else {
buffer.putDouble64(z);
}
}
}
}
if (shapeType == ShapeType.POLYGONM || shapeType == ShapeType.POLYGONZ) {
// obtain all M values
List<double> values = [];
for (int ringN = 0; ringN < nrings; ringN++) {
CoordinateSequence coords = coordinates[ringN];
final int seqSize = coords.size();
double m;
for (int coordN = 0; coordN < seqSize; coordN++) {
m = coords.getM(coordN);
values.add(m);
}
}
// m min
double edge =
values.reduce(math.min); // stream().min(Double::compare).get();
buffer.putDouble64(!edge.isNaN ? edge : -10E40);
// m max
edge = values.reduce(math.max); // stream().max(Double::compare).get();
buffer.putDouble64(!edge.isNaN ? edge : -10E40);
// m values
values.forEach((x) {
buffer.putDouble64(x.isNaN ? -10E40 : x);
});
}
}