PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 3079 of file lwgeom_functions_basic.c.

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