readGeometry method

Geometry readGeometry()

Implementation

Geometry readGeometry() {
// determine byte order
  int byteOrderWKB = dis.readByte();

// always set byte order, since it may change from geometry to geometry
  if (byteOrderWKB == WKBConstants.wkbNDR) {
    dis.setOrder(Endian.little);
  } else if (byteOrderWKB == WKBConstants.wkbXDR) {
    dis.setOrder(Endian.big);
  } else if (isStrict) {
    throw new ParseException(
        "Unknown geometry byte order (not NDR or XDR): $byteOrderWKB");
  }
//if not strict and not XDR or NDR, then we just use the dis default set at the
//start of the geometry (if a multi-geometry).  This  allows WBKReader to work
//with Spatialite native BLOB WKB, as well as other WKB variants that might just
//specify endian-ness at the start of the multigeometry.

  int typeInt = dis.readInt();
// Adds %1000 to make it compatible with OGC 06-103r4
  int geometryType = (typeInt & 0xffff) % 1000;

// handle 3D and 4D WKB geometries
// geometries with Z coordinates have the 0x80 flag (postgis EWKB)
// or are in the 1000 range (Z) or in the 3000 range (ZM) of geometry type (OGC 06-103r4)
  bool hasZ = ((typeInt & 0x80000000).toInt() != 0 ||
      (typeInt & 0xffff) ~/ 1000 == 1 ||
      (typeInt & 0xffff) ~/ 1000 == 3);
// geometries with M coordinates have the 0x40 flag (postgis EWKB)
// or are in the 1000 range (M) or in the 3000 range (ZM) of geometry type (OGC 06-103r4)
  bool hasM = ((typeInt & 0x40000000).toInt() != 0 ||
      (typeInt & 0xffff) ~/ 1000 == 2 ||
      (typeInt & 0xffff) ~/ 1000 == 3);
//System.out.println(typeInt + " - " + geometryType + " - hasZ:" + hasZ);
  inputDimension = 2 + (hasZ ? 1 : 0) + (hasM ? 1 : 0);

// determine if SRIDs are present
  hasSRID = (typeInt & 0x20000000) != 0;
  int SRID = 0;
  if (hasSRID) {
    SRID = dis.readInt();
  }

// only allocate ordValues buffer if necessary
  if (ordValues == null || ordValues!.length < inputDimension)
    ordValues = List.filled(inputDimension, 0.0);

  late Geometry geom;
  switch (geometryType) {
    case WKBConstants.wkbPoint:
      geom = readPoint();
      break;
    case WKBConstants.wkbLineString:
      geom = readLineString();
      break;
    case WKBConstants.wkbPolygon:
      geom = readPolygon();
      break;
    case WKBConstants.wkbMultiPoint:
      geom = readMultiPoint();
      break;
    case WKBConstants.wkbMultiLineString:
      geom = readMultiLineString();
      break;
    case WKBConstants.wkbMultiPolygon:
      geom = readMultiPolygon();
      break;
    case WKBConstants.wkbGeometryCollection:
      geom = readGeometryCollection();
      break;
    default:
      throw new ParseException("Unknown WKB type $geometryType");
  }
  setSRID(geom, SRID);
  return geom;
}