write method

List<int> write(
  1. Geometry geom, {
  2. dynamic doSpatialite = false,
})

Writes a {@link Geometry} into a byte array.

@param geom the geometry to write @return the byte array containing the WKB

Implementation

List<int> write(Geometry geom, {doSpatialite = false}) {
  this.doSpatialite = doSpatialite;
  try {
    byteArrayOutStream = [];

    if (doSpatialite) {
      // geom starts with byte 0x00
      byteArrayOutStream.add(0x00);
      writeByteOrder(byteArrayOutStream);
      writeInt(geom.getSRID(), byteArrayOutStream);

      // 6 - 13 MBR_MIN_X a double value [little- big-endian ordered, accordingly with the
      // precedent one]
      // corresponding to the MBR minimum X coordinate for this GEOMETRY
      // 14 - 21 MBR_MIN_Y a double value corresponding to the MBR minimum Y coordinate
      // 22 - 29 MBR_MAX_X a double value corresponding to the MBR maximum X coordinate
      // 30 - 37 MBR_MAX_Y a double value corresponding to the MBR maximum Y coordinate
      double mbrMinX = geom.getEnvelopeInternal().getMinX();
      double mbrMinY = geom.getEnvelopeInternal().getMinY();
      double mbrMaxX = geom.getEnvelopeInternal().getMaxX();
      double mbrMaxY = geom.getEnvelopeInternal().getMaxY();

      Byteutils.putFloat64(mbrMinX, buf, byteOrder);
      byteArrayOutStream.addAll(buf);
      Byteutils.putFloat64(mbrMinY, buf, byteOrder);
      byteArrayOutStream.addAll(buf);
      Byteutils.putFloat64(mbrMaxX, buf, byteOrder);
      byteArrayOutStream.addAll(buf);
      Byteutils.putFloat64(mbrMaxY, buf, byteOrder);
      byteArrayOutStream.addAll(buf);

      // 38 MBR_END [hex 7C] a GEOMETRY encoded BLOB value must always have an 0x7C byte in
      // this position
      byteArrayOutStream.add(0x7C);
    }
    writeStream(geom, byteArrayOutStream, true);
  } catch (ex) {
    throw RuntimeException("Unexpected IO exception: $ex");
  }
  return byteArrayOutStream;
}