PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 3081 of file lwgeom_functions_basic.c.

3082 {
3083  GSERIALIZED *geom;
3084  GSERIALIZED *geom_scale = PG_GETARG_GSERIALIZED_P(1);
3085  GSERIALIZED *geom_origin = NULL;
3086  LWGEOM *lwg, *lwg_scale, *lwg_origin;
3087  LWPOINT *lwpt_scale, *lwpt_origin;
3088  POINT4D origin;
3089  POINT4D factors;
3090  bool translate = false;
3091  GSERIALIZED *ret;
3092  AFFINE aff;
3093 
3094  /* Make sure we have a valid scale input */
3095  lwg_scale = lwgeom_from_gserialized(geom_scale);
3096  lwpt_scale = lwgeom_as_lwpoint(lwg_scale);
3097  if (!lwpt_scale)
3098  {
3099  lwgeom_free(lwg_scale);
3100  PG_FREE_IF_COPY(geom_scale, 1);
3101  lwpgerror("Scale factor geometry parameter must be a point");
3102  PG_RETURN_NULL();
3103  }
3104 
3105  /* Geom Will be modified in place, so take a copy */
3106  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
3107  lwg = lwgeom_from_gserialized(geom);
3108 
3109  /* Empty point, return input untouched */
3110  if (lwgeom_is_empty(lwg))
3111  {
3112  lwgeom_free(lwg_scale);
3113  lwgeom_free(lwg);
3114  PG_FREE_IF_COPY(geom_scale, 1);
3115  PG_RETURN_POINTER(geom);
3116  }
3117 
3118  /* Once we read the scale data into local static point, we can */
3119  /* free the lwgeom */
3120  lwpoint_getPoint4d_p(lwpt_scale, &factors);
3121  if (!lwgeom_has_z(lwg_scale))
3122  factors.z = 1.0;
3123  if (!lwgeom_has_m(lwg_scale))
3124  factors.m = 1.0;
3125  lwgeom_free(lwg_scale);
3126 
3127  /* Do we have the optional false origin? */
3128  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
3129  {
3130  geom_origin = PG_GETARG_GSERIALIZED_P(2);
3131  lwg_origin = lwgeom_from_gserialized(geom_origin);
3132  lwpt_origin = lwgeom_as_lwpoint(lwg_origin);
3133  if (lwpt_origin)
3134  {
3135  lwpoint_getPoint4d_p(lwpt_origin, &origin);
3136  translate = true;
3137  }
3138  /* Free the false origin inputs */
3139  lwgeom_free(lwg_origin);
3140  PG_FREE_IF_COPY(geom_origin, 2);
3141  }
3142 
3143  /* If we have false origin, translate to it before scaling */
3144  if (translate)
3145  {
3146  /* Initialize affine */
3147  memset(&aff, 0, sizeof(AFFINE));
3148  /* Set rotation/scale/sheer matrix to no-op */
3149  aff.afac = aff.efac = aff.ifac = 1.0;
3150  /* Strip false origin from all coordinates */
3151  aff.xoff = -1 * origin.x;
3152  aff.yoff = -1 * origin.y;
3153  aff.zoff = -1 * origin.z;
3154  lwgeom_affine(lwg, &aff);
3155  }
3156 
3157  lwgeom_scale(lwg, &factors);
3158 
3159  /* Return to original origin after scaling */
3160  if (translate)
3161  {
3162  aff.xoff *= -1;
3163  aff.yoff *= -1;
3164  aff.zoff *= -1;
3165  lwgeom_affine(lwg, &aff);
3166  }
3167 
3168  /* Cleanup and return */
3169  ret = geometry_serialize(lwg);
3170  lwgeom_free(lwg);
3171  PG_FREE_IF_COPY(geom, 0);
3172  PG_FREE_IF_COPY(geom_scale, 1);
3173  PG_RETURN_POINTER(ret);
3174 }
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:1155
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
void lwgeom_scale(LWGEOM *geom, const POINT4D *factors)
Definition: lwgeom.c:2074
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:2020
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
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:347
double ifac
Definition: liblwgeom.h:347
double xoff
Definition: liblwgeom.h:347
double afac
Definition: liblwgeom.h:347
double efac
Definition: liblwgeom.h:347
double yoff
Definition: liblwgeom.h:347
double m
Definition: liblwgeom.h:429
double x
Definition: liblwgeom.h:429
double z
Definition: liblwgeom.h:429
double y
Definition: liblwgeom.h:429

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: