getTableData method

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

Implementation

FeatureCollection getTableData(TableName tableName,
    {Envelope? envelope, Geometry? geometry, String? where, int? limit}) {
  FeatureCollection queryResult = FeatureCollection();
  String sql = "select * from " + tableName.fixedName;
  List<String> wheresList = [];

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

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

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

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

  bool hasBoundsfilter = envelope != null || geometry != null;

  if (limit != null) {
    sql += " limit $limit";
  }
  var result = _sqliteDb.select(sql);
  result.forEach((QueryResultRow map) {
    Feature feature = Feature();

    bool doAdd = true;
    if (hasGeom) {
      var geomBytes = map.get(queryResult.geomName!);
      if (geomBytes != null) {
        Geometry geom = GeoPkgGeomReader(geomBytes).get();
        if (_supportsRtree && geometry == null) {
          feature.geometry = geom;
        } else {
          // if no spatial index is available, filter the geoms manually
          if (!hasBoundsfilter) {
            // no filter, take them all
            feature.geometry = geom;
          } else if (envelope != null &&
              geom.getEnvelopeInternal().intersectsEnvelope(envelope)) {
            feature.geometry = geom;
          } else if (geometry != null &&
              geom
                  .getEnvelopeInternal()
                  .intersectsEnvelope(geometry.getEnvelopeInternal()) &&
              geom.intersects(geometry)) {
            feature.geometry = geom;
          } else {
            doAdd = false;
          }
        }
      }
    }
    if (doAdd) {
      map
        ..forEach((k, v) {
          if (k != queryResult.geomName) {
            feature.attributes[k] = v;
          }
        });
      queryResult.features.add(feature);
    }
  });

  return queryResult;
}