createTileEntry method

TileEntry createTileEntry(
  1. QueryResultRow row
)

Implementation

TileEntry createTileEntry(QueryResultRow row) {
  TileEntry e = new TileEntry();
  e.setIdentifier(row.get("identifier"));
  e.setDescription(row.get("description"));
  e.setTableName(TableName(row.get("table_name"), schemaSupported: false));
  int srid = (row.get("srs_id") as num).toInt();
  e.setSrid(srid);
  var matrixSetEnvelope = new Envelope(
    (row.get("min_x") as num).toDouble(),
    (row.get("max_x") as num).toDouble(),
    (row.get("min_y") as num).toDouble(),
    (row.get("max_y") as num).toDouble(),
  );

  e.setTileMatrixSetBounds(matrixSetEnvelope);

  int cSrid = (row.get("gsrs_id") as num).toInt();
  var bounds = new Envelope(
    (row.get("gmin_x") as num).toDouble(),
    (row.get("gmax_x") as num).toDouble(),
    (row.get("gmin_y") as num).toDouble(),
    (row.get("gmax_y") as num).toDouble(),
  );
  if (cSrid != srid) {
    // need to reproject
    var from = PROJ.Projection.get("EPSG:$cSrid");
    var to = PROJ.Projection.get("EPSG:$srid");
    if (from != null && to != null) {
      bounds = Proj.transformEnvelope(from, to, bounds);
    } else {
      // TODO at least log error
    }
  }
  e.setBounds(bounds);

  String sql = """
      SELECT *, exists(
          SELECT 1 FROM ${e.getTableName().fixedName} data
          where data.zoom_level = tileMatrix.zoom_level
      ) as has_tiles
      FROM $TABLE_TILE_MATRIX_METADATA as tileMatrix
      WHERE lower(table_name) = lower(?)
      ORDER BY zoom_level ASC
      """;
  // load all the tile matrix entries (and join with the data table to see if a certain level
  // has tiles available, given the indexes in the data table, it should be real quick)
  var res = _sqliteDb.select(sql, [e.getTableName().name]);
  res.forEach((QueryResultRow resRow) {
    var zl = (resRow.get("zoom_level") as num).toInt();
    var mw = (resRow.get("matrix_width") as num).toInt();
    var mh = (resRow.get("matrix_height") as num).toInt();
    var tw = (resRow.get("tile_width") as num).toInt();
    var th = (resRow.get("tile_height") as num).toInt();
    var pxs = (resRow.get("pixel_x_size") as num).toDouble();
    var pys = (resRow.get("pixel_y_size") as num).toDouble();
    var has = resRow.get("has_tiles");

    TileMatrix m = TileMatrix(zl, mw, mh, tw, th, pxs, pys)
      ..setTiles(has == 1 ? true : false);

    e.getTileMatricies().add(m);
  });

  return e;
}