PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_TileEnvelope()

Datum ST_TileEnvelope ( PG_FUNCTION_ARGS  )

Definition at line 2097 of file lwgeom_functions_basic.c.

2098 {
2099  GSERIALIZED *bounds;
2100  uint32_t zoomu;
2101  int32_t x, y, zoom;
2102  uint32_t worldTileSize;
2103  double tileGeoSizeX, tileGeoSizeY;
2104  double boundsWidth, boundsHeight;
2105  double x1, y1, x2, y2;
2106  double margin;
2107  /* This is broken, since 3857 doesn't mean "web mercator", it means
2108  the contents of the row in spatial_ref_sys with srid = 3857.
2109  For practical purposes this will work, but in good implementation
2110  we should de-reference in spatial ref sys to confirm that the
2111  srid of the object is EPSG:3857. */
2112  int32_t srid;
2113  GBOX bbox;
2114  LWGEOM *g = NULL;
2115 
2116  POSTGIS_DEBUG(2, "ST_TileEnvelope called");
2117 
2118  zoom = PG_GETARG_INT32(0);
2119  x = PG_GETARG_INT32(1);
2120  y = PG_GETARG_INT32(2);
2121 
2122  bounds = PG_GETARG_GSERIALIZED_P(3);
2123  /*
2124  * We deserialize the geometry and recalculate the bounding box here to get
2125  * 64b floating point precision. The serialized bbox has 32b float is not
2126  * precise enough with big numbers such as the ones used in the default
2127  * parameters, e.g: -20037508.3427892 is transformed into -20037510
2128  */
2129  g = lwgeom_from_gserialized(bounds);
2130  if (lwgeom_calculate_gbox(g, &bbox) != LW_SUCCESS)
2131  elog(ERROR, "%s: Unable to compute bbox", __func__);
2132  srid = g->srid;
2133  lwgeom_free(g);
2134 
2135  /* Avoid crashing with old signature (old sql code with 3 args, new C code with 4) */
2136  margin = PG_NARGS() < 4 ? 0 : PG_GETARG_FLOAT8(4);
2137  /* shrinking by more than 50% would eliminate the tile outright */
2138  if (margin < -0.5)
2139  elog(ERROR, "%s: Margin must not be less than -50%%, margin=%f", __func__, margin);
2140 
2141  boundsWidth = bbox.xmax - bbox.xmin;
2142  boundsHeight = bbox.ymax - bbox.ymin;
2143  if (boundsWidth <= 0 || boundsHeight <= 0)
2144  elog(ERROR, "%s: Geometric bounds are too small", __func__);
2145 
2146  if (zoom < 0 || zoom >= 32)
2147  elog(ERROR, "%s: Invalid tile zoom value, %d", __func__, zoom);
2148 
2149  zoomu = (uint32_t)zoom;
2150  worldTileSize = 0x01u << (zoomu > 31 ? 31 : zoomu);
2151 
2152  if (x < 0 || (uint32_t)x >= worldTileSize)
2153  elog(ERROR, "%s: Invalid tile x value, %d", __func__, x);
2154  if (y < 0 || (uint32_t)y >= worldTileSize)
2155  elog(ERROR, "%s: Invalid tile y value, %d", __func__, y);
2156 
2157  tileGeoSizeX = boundsWidth / worldTileSize;
2158  tileGeoSizeY = boundsHeight / worldTileSize;
2159 
2160  /*
2161  * 1 margin (100%) is the same as a single tile width
2162  * if the size of the tile with margins span more than the total number of tiles,
2163  * reset x1/x2 to the bounds
2164  */
2165  if ((1 + margin * 2) > worldTileSize)
2166  {
2167  x1 = bbox.xmin;
2168  x2 = bbox.xmax;
2169  }
2170  else
2171  {
2172  x1 = bbox.xmin + tileGeoSizeX * (x - margin);
2173  x2 = bbox.xmin + tileGeoSizeX * (x + 1 + margin);
2174  }
2175 
2176  y1 = bbox.ymax - tileGeoSizeY * (y + 1 + margin);
2177  y2 = bbox.ymax - tileGeoSizeY * (y - margin);
2178 
2179  /* Clip the final tile bounds to the bounds of the tile plane */
2180  if (y1 < bbox.ymin) y1 = bbox.ymin;
2181  if (y2 > bbox.ymax) y2 = bbox.ymax;
2182  if (x1 < bbox.xmin) x1 = bbox.xmin;
2183  if (x2 > bbox.xmax) x2 = bbox.xmax;
2184 
2185  PG_RETURN_POINTER(
2186  geometry_serialize(
2189  srid, x1, y1, x2, y2))));
2190 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define LW_SUCCESS
Definition: liblwgeom.h:112
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:329
LWPOLY * lwpoly_construct_envelope(int32_t srid, double x1, double y1, double x2, double y2)
Definition: lwpoly.c:98
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:755
double ymax
Definition: liblwgeom.h:372
double xmax
Definition: liblwgeom.h:370
double ymin
Definition: liblwgeom.h:371
double xmin
Definition: liblwgeom.h:369
int32_t srid
Definition: liblwgeom.h:475

References LW_SUCCESS, lwgeom_calculate_gbox(), lwgeom_free(), lwgeom_from_gserialized(), lwpoly_as_lwgeom(), lwpoly_construct_envelope(), LWGEOM::srid, pixval::x, GBOX::xmax, GBOX::xmin, pixval::y, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: