PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 3163 of file lwgeom_functions_basic.c.

3164 {
3165  GSERIALIZED *geom;
3166  GSERIALIZED *geom_scale = PG_GETARG_GSERIALIZED_P(1);
3167  GSERIALIZED *geom_origin = NULL;
3168  LWGEOM *lwg, *lwg_scale, *lwg_origin;
3169  LWPOINT *lwpt_scale, *lwpt_origin;
3170  POINT4D origin;
3171  POINT4D factors;
3172  bool translate = false;
3173  GSERIALIZED *ret;
3174  AFFINE aff;
3175 
3176  /* Make sure we have a valid scale input */
3177  lwg_scale = lwgeom_from_gserialized(geom_scale);
3178  lwpt_scale = lwgeom_as_lwpoint(lwg_scale);
3179  if (!lwpt_scale)
3180  {
3181  lwgeom_free(lwg_scale);
3182  PG_FREE_IF_COPY(geom_scale, 1);
3183  lwpgerror("Scale factor geometry parameter must be a point");
3184  PG_RETURN_NULL();
3185  }
3186 
3187  /* Geom Will be modified in place, so take a copy */
3188  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
3189  lwg = lwgeom_from_gserialized(geom);
3190 
3191  /* Empty point, return input untouched */
3192  if (lwgeom_is_empty(lwg))
3193  {
3194  lwgeom_free(lwg_scale);
3195  lwgeom_free(lwg);
3196  PG_FREE_IF_COPY(geom_scale, 1);
3197  PG_RETURN_POINTER(geom);
3198  }
3199 
3200  /* Once we read the scale data into local static point, we can */
3201  /* free the lwgeom */
3202  lwpoint_getPoint4d_p(lwpt_scale, &factors);
3203  if (!lwgeom_has_z(lwg_scale))
3204  factors.z = 1.0;
3205  if (!lwgeom_has_m(lwg_scale))
3206  factors.m = 1.0;
3207  lwgeom_free(lwg_scale);
3208 
3209  /* Do we have the optional false origin? */
3210  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
3211  {
3212  geom_origin = PG_GETARG_GSERIALIZED_P(2);
3213  lwg_origin = lwgeom_from_gserialized(geom_origin);
3214  lwpt_origin = lwgeom_as_lwpoint(lwg_origin);
3215  if (lwpt_origin)
3216  {
3217  lwpoint_getPoint4d_p(lwpt_origin, &origin);
3218  translate = true;
3219  }
3220  /* Free the false origin inputs */
3221  lwgeom_free(lwg_origin);
3222  PG_FREE_IF_COPY(geom_origin, 2);
3223  }
3224 
3225  /* If we have false origin, translate to it before scaling */
3226  if (translate)
3227  {
3228  /* Initialize affine */
3229  memset(&aff, 0, sizeof(AFFINE));
3230  /* Set rotation/scale/sheer matrix to no-op */
3231  aff.afac = aff.efac = aff.ifac = 1.0;
3232  /* Strip false origin from all coordinates */
3233  aff.xoff = -1 * origin.x;
3234  aff.yoff = -1 * origin.y;
3235  aff.zoff = -1 * origin.z;
3236  lwgeom_affine(lwg, &aff);
3237  }
3238 
3239  lwgeom_scale(lwg, &factors);
3240 
3241  /* Return to original origin after scaling */
3242  if (translate)
3243  {
3244  aff.xoff *= -1;
3245  aff.yoff *= -1;
3246  aff.zoff *= -1;
3247  lwgeom_affine(lwg, &aff);
3248  }
3249 
3250  /* Cleanup and return */
3251  ret = geometry_serialize(lwg);
3252  lwgeom_free(lwg);
3253  PG_FREE_IF_COPY(geom, 0);
3254  PG_FREE_IF_COPY(geom_scale, 1);
3255  PG_RETURN_POINTER(ret);
3256 }
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:1218
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:2137
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:2083
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: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: