PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 2969 of file lwgeom_functions_basic.c.

2970 {
2971  GSERIALIZED *geom;
2972  GSERIALIZED *geom_scale = PG_GETARG_GSERIALIZED_P(1);
2973  GSERIALIZED *geom_origin = NULL;
2974  LWGEOM *lwg, *lwg_scale, *lwg_origin;
2975  LWPOINT *lwpt_scale, *lwpt_origin;
2976  POINT4D origin;
2977  POINT4D factors;
2978  bool translate = false;
2979  GSERIALIZED *ret;
2980  AFFINE aff;
2981 
2982  /* Make sure we have a valid scale input */
2983  lwg_scale = lwgeom_from_gserialized(geom_scale);
2984  lwpt_scale = lwgeom_as_lwpoint(lwg_scale);
2985  if (!lwpt_scale)
2986  {
2987  lwgeom_free(lwg_scale);
2988  PG_FREE_IF_COPY(geom_scale, 1);
2989  lwpgerror("Scale factor geometry parameter must be a point");
2990  PG_RETURN_NULL();
2991  }
2992 
2993  /* Geom Will be modified in place, so take a copy */
2994  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
2995  lwg = lwgeom_from_gserialized(geom);
2996 
2997  /* Empty point, return input untouched */
2998  if (lwgeom_is_empty(lwg))
2999  {
3000  lwgeom_free(lwg_scale);
3001  lwgeom_free(lwg);
3002  PG_FREE_IF_COPY(geom_scale, 1);
3003  PG_RETURN_POINTER(geom);
3004  }
3005 
3006  /* Once we read the scale data into local static point, we can */
3007  /* free the lwgeom */
3008  lwpoint_getPoint4d_p(lwpt_scale, &factors);
3009  if (!lwgeom_has_z(lwg_scale))
3010  factors.z = 1.0;
3011  if (!lwgeom_has_m(lwg_scale))
3012  factors.m = 1.0;
3013  lwgeom_free(lwg_scale);
3014 
3015  /* Do we have the optional false origin? */
3016  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
3017  {
3018  geom_origin = PG_GETARG_GSERIALIZED_P(2);
3019  lwg_origin = lwgeom_from_gserialized(geom_origin);
3020  lwpt_origin = lwgeom_as_lwpoint(lwg_origin);
3021  if (lwpt_origin)
3022  {
3023  lwpoint_getPoint4d_p(lwpt_origin, &origin);
3024  translate = true;
3025  }
3026  /* Free the false origin inputs */
3027  lwgeom_free(lwg_origin);
3028  PG_FREE_IF_COPY(geom_origin, 2);
3029  }
3030 
3031  /* If we have false origin, translate to it before scaling */
3032  if (translate)
3033  {
3034  /* Initialize affine */
3035  memset(&aff, 0, sizeof(AFFINE));
3036  /* Set rotation/scale/sheer matrix to no-op */
3037  aff.afac = aff.efac = aff.ifac = 1.0;
3038  /* Strip false origin from all coordinates */
3039  aff.xoff = -1 * origin.x;
3040  aff.yoff = -1 * origin.y;
3041  aff.zoff = -1 * origin.z;
3042  lwgeom_affine(lwg, &aff);
3043  }
3044 
3045  lwgeom_scale(lwg, &factors);
3046 
3047  /* Return to original origin after scaling */
3048  if (translate)
3049  {
3050  aff.xoff *= -1;
3051  aff.yoff *= -1;
3052  aff.zoff *= -1;
3053  lwgeom_affine(lwg, &aff);
3054  }
3055 
3056  /* Cleanup and return */
3057  ret = geometry_serialize(lwg);
3058  lwgeom_free(lwg);
3059  PG_FREE_IF_COPY(geom, 0);
3060  PG_FREE_IF_COPY(geom_scale, 1);
3061  PG_RETURN_POINTER(ret);
3062 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
void lwgeom_scale(LWGEOM *geom, const POINT4D *factors)
Definition: lwgeom.c:2028
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:1974
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:924
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
double zoff
Definition: liblwgeom.h:346
double ifac
Definition: liblwgeom.h:346
double xoff
Definition: liblwgeom.h:346
double afac
Definition: liblwgeom.h:346
double efac
Definition: liblwgeom.h:346
double yoff
Definition: liblwgeom.h:346
double m
Definition: liblwgeom.h:428
double x
Definition: liblwgeom.h:428
double z
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428

References AFFINE::afac, AFFINE::efac, AFFINE::ifac, lwgeom_affine(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_is_empty(), lwgeom_scale(), lwpoint_getPoint4d_p(), POINT4D::m, POINT4D::x, AFFINE::xoff, POINT4D::y, AFFINE::yoff, POINT4D::z, and AFFINE::zoff.

Here is the call graph for this function: