getTableData method

Future<FeatureCollection> getTableData(
  1. TableName tableName, {
  2. Envelope? envelope,
  3. Geometry? geometry,
  4. String? where,
  5. int? limit,
})

Implementation

Future<FeatureCollection> getTableData(TableName tableName,
    {Envelope? envelope,
    Geometry? geometry,
    String? where,
    int? limit}) async {
  FeatureCollection queryResult = FeatureCollection();

  GeometryColumn? geometryColumn =
      await getGeometryColumnsForTable(tableName);
  if (geometryColumn != null) {
    queryResult.geomName = geometryColumn.geometryColumnName;
  }

  String sql = "select * from " + tableName.fixedName;

  if (envelope != null && geometry != null) {
    throw ArgumentError("Only one of envelope and geometry have to be set.");
  }

  List<String> wheresList = [];
  if (geometry != null) {
    String? spatialindexGeometryWherePiece =
        await getSpatialindexGeometryWherePiece(tableName, geometry);
    if (spatialindexGeometryWherePiece != null) {
      wheresList.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) {
      wheresList.add(spatialindexBBoxWherePiece);
    }
  }
  if (where != null && where.isNotEmpty) {
    wheresList.add(where);
  }

  if (wheresList.isNotEmpty) {
    var wheresString = wheresList.join(" AND ");
    sql += " WHERE " + wheresString;
  }

  if (limit != null) {
    sql += " limit $limit";
  }
  var result = await _postgresDb.select(sql);
  if (result != null && queryResult.geomName != null) {
    result.forEach((QueryResultRow map) {
      Feature feature = Feature();

      var geomBytes = map.get(queryResult.geomName!);
      if (geomBytes != null) {
        Geometry geom = BinaryParser().parse(geomBytes);
        feature.geometry = geom;
      }
      map.forEach((k, v) {
        if (k != queryResult.geomName) {
          feature.attributes[k] = v;
        }
      });

      queryResult.features.add(feature);
    });
  }

  return queryResult;
}