PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 3190 of file lwgeom_functions_basic.c.

3191 {
3192  GSERIALIZED *geom;
3193  GSERIALIZED *geom_scale = PG_GETARG_GSERIALIZED_P(1);
3194  GSERIALIZED *geom_origin = NULL;
3195  LWGEOM *lwg, *lwg_scale, *lwg_origin;
3196  LWPOINT *lwpt_scale, *lwpt_origin;
3197  POINT4D origin;
3198  POINT4D factors;
3199  bool translate = false;
3200  GSERIALIZED *ret;
3201  AFFINE aff;
3202 
3203  /* Make sure we have a valid scale input */
3204  lwg_scale = lwgeom_from_gserialized(geom_scale);
3205  lwpt_scale = lwgeom_as_lwpoint(lwg_scale);
3206  if (!lwpt_scale)
3207  {
3208  lwgeom_free(lwg_scale);
3209  PG_FREE_IF_COPY(geom_scale, 1);
3210  lwpgerror("Scale factor geometry parameter must be a point");
3211  PG_RETURN_NULL();
3212  }
3213 
3214  /* Geom Will be modified in place, so take a copy */
3215  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
3216  lwg = lwgeom_from_gserialized(geom);
3217 
3218  /* Empty point, return input untouched */
3219  if (lwgeom_is_empty(lwg))
3220  {
3221  lwgeom_free(lwg_scale);
3222  lwgeom_free(lwg);
3223  PG_FREE_IF_COPY(geom_scale, 1);
3224  PG_RETURN_POINTER(geom);
3225  }
3226 
3227  /* Once we read the scale data into local static point, we can */
3228  /* free the lwgeom */
3229  lwpoint_getPoint4d_p(lwpt_scale, &factors);
3230  if (!lwgeom_has_z(lwg_scale))
3231  factors.z = 1.0;
3232  if (!lwgeom_has_m(lwg_scale))
3233  factors.m = 1.0;
3234  lwgeom_free(lwg_scale);
3235 
3236  /* Do we have the optional false origin? */
3237  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
3238  {
3239  geom_origin = PG_GETARG_GSERIALIZED_P(2);
3240  lwg_origin = lwgeom_from_gserialized(geom_origin);
3241  lwpt_origin = lwgeom_as_lwpoint(lwg_origin);
3242  if (lwpt_origin)
3243  {
3244  lwpoint_getPoint4d_p(lwpt_origin, &origin);
3245  translate = true;
3246  }
3247  /* Free the false origin inputs */
3248  lwgeom_free(lwg_origin);
3249  PG_FREE_IF_COPY(geom_origin, 2);
3250  }
3251 
3252  /* If we have false origin, translate to it before scaling */
3253  if (translate)
3254  {
3255  /* Initialize affine */
3256  memset(&aff, 0, sizeof(AFFINE));
3257  /* Set rotation/scale/sheer matrix to no-op */
3258  aff.afac = aff.efac = aff.ifac = 1.0;
3259  /* Strip false origin from all coordinates */
3260  aff.xoff = -1 * origin.x;
3261  aff.yoff = -1 * origin.y;
3262  aff.zoff = -1 * origin.z;
3263  lwgeom_affine(lwg, &aff);
3264  }
3265 
3266  lwgeom_scale(lwg, &factors);
3267 
3268  /* Return to original origin after scaling */
3269  if (translate)
3270  {
3271  aff.xoff *= -1;
3272  aff.yoff *= -1;
3273  aff.zoff *= -1;
3274  lwgeom_affine(lwg, &aff);
3275  }
3276 
3277  /* Cleanup and return */
3278  ret = geometry_serialize(lwg);
3279  lwgeom_free(lwg);
3280  PG_FREE_IF_COPY(geom, 0);
3281  PG_FREE_IF_COPY(geom_scale, 1);
3282  PG_RETURN_POINTER(ret);
3283 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1246
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:962
void lwgeom_scale(LWGEOM *geom, const POINT4D *factors)
Definition: lwgeom.c:2165
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:2111
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:969
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:199
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:127
double zoff
Definition: liblwgeom.h:332
double ifac
Definition: liblwgeom.h:332
double xoff
Definition: liblwgeom.h:332
double afac
Definition: liblwgeom.h:332
double efac
Definition: liblwgeom.h:332
double yoff
Definition: liblwgeom.h:332
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

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: