PostGIS  2.5.2dev-r@@SVN_REVISION@@

◆ mvt_geom()

LWGEOM* mvt_geom ( LWGEOM lwgeom,
const GBOX gbox,
uint32_t  extent,
uint32_t  buffer,
bool  clip_geom 
)

Transform a geometry into vector tile coordinate space.

Makes best effort to keep validity. Might collapse geometry into lower dimension.

NOTE: modifies in place if possible (not currently possible for polygons)

Definition at line 881 of file mvt.c.

References AFFINE::afac, AFFINE::efac, AFFINE::ifac, LW_TRUE, lwgeom_affine(), lwgeom_get_basic_type(), lwgeom_grid_in_place(), lwgeom_is_empty(), lwgeom_remove_repeated_points_in_place(), lwgeom_simplify_in_place(), lwgeom_to_basic_type(), mvt_clip_and_validate_geos(), window::res, GBOX::xmax, GBOX::xmin, AFFINE::xoff, GBOX::ymax, GBOX::ymin, and AFFINE::yoff.

Referenced by ST_AsMVTGeom().

883 {
884  AFFINE affine = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
885  gridspec grid = {0, 0, 0, 0, 1, 1, 0, 0};
886  double width = gbox->xmax - gbox->xmin;
887  double height = gbox->ymax - gbox->ymin;
888  double resx, resy, res, fx, fy;
889  int preserve_collapsed = LW_TRUE;
890  const uint8_t basic_type = lwgeom_get_basic_type(lwgeom);
891  POSTGIS_DEBUG(2, "mvt_geom called");
892 
893  /* Simplify it as soon as possible */
894  lwgeom = lwgeom_to_basic_type(lwgeom, basic_type);
895 
896  /* Short circuit out on EMPTY */
897  if (lwgeom_is_empty(lwgeom))
898  return NULL;
899 
900  if (width == 0 || height == 0)
901  elog(ERROR, "mvt_geom: bounds width or height cannot be 0");
902 
903  if (extent == 0)
904  elog(ERROR, "mvt_geom: extent cannot be 0");
905 
906  resx = width / extent;
907  resy = height / extent;
908  res = (resx < resy ? resx : resy)/2;
909  fx = extent / width;
910  fy = -(extent / height);
911 
912  /* Remove all non-essential points (under the output resolution) */
914  lwgeom_simplify_in_place(lwgeom, res, preserve_collapsed);
915 
916  /* If geometry has disappeared, you're done */
917  if (lwgeom_is_empty(lwgeom))
918  return NULL;
919 
920  /* transform to tile coordinate space */
921  affine.afac = fx;
922  affine.efac = fy;
923  affine.ifac = 1;
924  affine.xoff = -gbox->xmin * fx;
925  affine.yoff = -gbox->ymax * fy;
926  lwgeom_affine(lwgeom, &affine);
927 
928  /* snap to integer precision, removing duplicate points */
929  lwgeom_grid_in_place(lwgeom, &grid);
930 
931  if (lwgeom == NULL || lwgeom_is_empty(lwgeom))
932  return NULL;
933 
934  lwgeom = mvt_clip_and_validate_geos(lwgeom, basic_type, extent, buffer, clip_geom);
935  if (lwgeom == NULL || lwgeom_is_empty(lwgeom))
936  return NULL;
937 
938  return lwgeom;
939 }
void lwgeom_remove_repeated_points_in_place(LWGEOM *in, double tolerance)
Definition: lwgeom.c:1603
tuple res
Definition: window.py:78
double xmax
Definition: liblwgeom.h:296
static LWGEOM * lwgeom_to_basic_type(LWGEOM *geom, uint8 original_type)
In place process a collection to find a concrete geometry object and expose that as the actual object...
Definition: mvt.c:770
double ifac
Definition: liblwgeom.h:273
double xoff
Definition: liblwgeom.h:273
double afac
Definition: liblwgeom.h:273
void lwgeom_simplify_in_place(LWGEOM *igeom, double dist, int preserve_collapsed)
Definition: lwgeom.c:1748
Datum buffer(PG_FUNCTION_ARGS)
double ymin
Definition: liblwgeom.h:297
double xmin
Definition: liblwgeom.h:295
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
static LWGEOM * mvt_clip_and_validate_geos(LWGEOM *lwgeom, uint8_t basic_type, uint32_t extent, uint32_t buffer, bool clip_geom)
Definition: mvt.c:794
double ymax
Definition: liblwgeom.h:298
static uint8 lwgeom_get_basic_type(LWGEOM *geom)
Definition: mvt.c:733
double efac
Definition: liblwgeom.h:273
void lwgeom_grid_in_place(LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2145
double yoff
Definition: liblwgeom.h:273
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
unsigned char uint8_t
Definition: uthash.h:79
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:1969
Snap to grid.
Here is the call graph for this function:
Here is the caller graph for this function: