getGeometriesIn method

List<Geometry?> getGeometriesIn(
  1. TableName tableName, {
  2. Envelope? envelope,
  3. Geometry? intersectionGeometry,
  4. List<String?>? prePostWhere,
  5. int limit = -1,
  6. String? userDataField,
})

Get the geometries of a table inside a given envelope.

Note that the primary key value is put inside the geom's userdata.

@param tableName the table name. @param envelope the envelope to check. @param prePostWhere an optional set of 3 parameters. The parameters are: a prefix wrapper for geom, a postfix for the same and a where string to apply. They all need to be existing if the parameter is passed. @param limit an optional limit to apply. @return The list of geometries intersecting the envelope. @throws Exception

Implementation

List<Geometry?> getGeometriesIn(
  TableName tableName, {
  Envelope? envelope,
  Geometry? intersectionGeometry,
  List<String?>? prePostWhere,
  int limit = -1,
  String? userDataField,
}) {
  List<String> wheres = [];
  String pre = "";
  String post = "";
  String where = "";
  if (prePostWhere != null && prePostWhere.length == 3) {
    if (prePostWhere[0] != null) pre = prePostWhere[0]!;
    if (prePostWhere[1] != null) post = prePostWhere[1]!;
    if (prePostWhere[2] != null) {
      where = prePostWhere[2]!;
      wheres.add(where);
    }
  }

  String userDataSql = userDataField != null ? ", $userDataField " : "";

  String? pk = _sqliteDb.getPrimaryKey(tableName);
  GeometryColumn? gCol = getGeometryColumnsForTable(tableName);
  if (gCol == null) {
    return [];
  }
  String sql = "SELECT " +
      pre +
      gCol.geometryColumnName +
      post +
      " as the_geom, $pk $userDataSql FROM " +
      tableName.fixedName;

  if (intersectionGeometry != null) {
    envelope = intersectionGeometry.getEnvelopeInternal();
  }

  if (envelope != null) {
    double x1 = envelope.getMinX();
    double y1 = envelope.getMinY();
    double x2 = envelope.getMaxX();
    double y2 = envelope.getMaxY();
    String? spatialindexBBoxWherePiece =
        getSpatialindexBBoxWherePiece(tableName, null, x1, y1, x2, y2);
    if (spatialindexBBoxWherePiece != null)
      wheres.add(spatialindexBBoxWherePiece);
  }

  if (wheres.length > 0) {
    sql += " WHERE " + wheres.join(" AND ");
  }

  if (limit > 0) {
    sql += " limit $limit";
  }

  List<Geometry> geoms = [];
  var res = _sqliteDb.select(sql);
  res.forEach((QueryResultRow map) {
    var geomBytes = map.getAt(0);
    if (geomBytes != null) {
      Geometry geom = GeoPkgGeomReader(geomBytes).get();
      var pkValue = map.getAt(1);
      if (userDataField != null) {
        geom.setUserData(map.getAt(2));
      } else {
        geom.setUserData(pkValue);
      }
      if (_supportsRtree || envelope == null) {
        geoms.add(geom);
      } else if (geom.getEnvelopeInternal().intersectsEnvelope(envelope)) {
        // if no spatial index is available, filter the geoms manually
        // print(pkValue.toString() + ": ${geom.getEnvelopeInternal()}");
        geoms.add(geom);
      }
    }
  });
  if (intersectionGeometry != null) {
    geoms.removeWhere((geom) => !geom.intersects(intersectionGeometry));
  }
  return geoms;
}