getGeometriesIn method

Future<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

Future<List<Geometry>> getGeometriesIn(TableName tableName,
    {Envelope? envelope,
    Geometry? intersectionGeometry,
    List<String?>? prePostWhere,
    int limit = -1,
    String? userDataField}) async {
  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 = await _postgresDb.getPrimaryKey(tableName);
  var gcAndSrid = await getGeometryColumnNameAndSridForTable(tableName);
  if (gcAndSrid == null) {
    return [];
  }
  String sql = "SELECT " +
      pre +
      gcAndSrid[0] +
      post +
      " as the_geom, $pk $userDataSql FROM " +
      tableName.fixedName;

  if (intersectionGeometry != null) {
    intersectionGeometry.setSRID(gcAndSrid[1]);
    String? spatialindexGeometryWherePiece =
        await getSpatialindexGeometryWherePiece(
            tableName, intersectionGeometry);
    if (spatialindexGeometryWherePiece != null) {
      wheres.add(spatialindexGeometryWherePiece);
    }
  } else if (envelope != null) {
    double x1 = envelope.getMinX();
    double y1 = envelope.getMinY();
    double x2 = envelope.getMaxX();
    double y2 = envelope.getMaxY();
    String? spatialindexBBoxWherePiece =
        await getSpatialindexBBoxWherePiece(tableName, x1, y1, x2, y2);
    if (spatialindexBBoxWherePiece != null) {
      wheres.add(spatialindexBBoxWherePiece);
    }
  }

  if (wheres.isNotEmpty) {
    sql += " WHERE " + wheres.join(" AND ");
  }

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

  List<Geometry> geoms = [];
  var res = await _postgresDb.select(sql);
  if (res != null) {
    res.forEach((QueryResultRow map) {
      var geomBytes = map.getAt(0);
      if (geomBytes != null) {
        Geometry geom = BinaryParser().parse(geomBytes);
        var pkValue = map.getAt(1);
        if (userDataField != null) {
          geom.setUserData(map.getAt(2));
        } else {
          geom.setUserData(pkValue);
        }
        geoms.add(geom);
      }
    });
  }
  return geoms;
}