PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ST_Scale()

Datum ST_Scale ( PG_FUNCTION_ARGS  )

Definition at line 2936 of file lwgeom_functions_basic.c.

2937 {
2938  GSERIALIZED *geom;
2939  GSERIALIZED *geom_scale = PG_GETARG_GSERIALIZED_P(1);
2940  GSERIALIZED *geom_origin = NULL;
2941  LWGEOM *lwg, *lwg_scale, *lwg_origin;
2942  LWPOINT *lwpt_scale, *lwpt_origin;
2943  POINT4D origin;
2944  POINT4D factors;
2945  bool translate = false;
2946  GSERIALIZED *ret;
2947  AFFINE aff;
2948 
2949  /* Make sure we have a valid scale input */
2950  lwg_scale = lwgeom_from_gserialized(geom_scale);
2951  lwpt_scale = lwgeom_as_lwpoint(lwg_scale);
2952  if (!lwpt_scale)
2953  {
2954  lwgeom_free(lwg_scale);
2955  PG_FREE_IF_COPY(geom_scale, 1);
2956  lwpgerror("Scale factor geometry parameter must be a point");
2957  PG_RETURN_NULL();
2958  }
2959 
2960  /* Geom Will be modified in place, so take a copy */
2961  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
2962  lwg = lwgeom_from_gserialized(geom);
2963 
2964  /* Empty point, return input untouched */
2965  if (lwgeom_is_empty(lwg))
2966  {
2967  lwgeom_free(lwg_scale);
2968  lwgeom_free(lwg);
2969  PG_FREE_IF_COPY(geom_scale, 1);
2970  PG_RETURN_POINTER(geom);
2971  }
2972 
2973  /* Once we read the scale data into local static point, we can */
2974  /* free the lwgeom */
2975  lwpoint_getPoint4d_p(lwpt_scale, &factors);
2976  if (!lwgeom_has_z(lwg_scale)) factors.z = 1.0;
2977  if (!lwgeom_has_m(lwg_scale)) factors.m = 1.0;
2978  lwgeom_free(lwg_scale);
2979 
2980  /* Do we have the optional false origin? */
2981  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
2982  {
2983  geom_origin = PG_GETARG_GSERIALIZED_P(2);
2984  lwg_origin = lwgeom_from_gserialized(geom_origin);
2985  lwpt_origin = lwgeom_as_lwpoint(lwg_origin);
2986  if (lwpt_origin)
2987  {
2988  lwpoint_getPoint4d_p(lwpt_origin, &origin);
2989  translate = true;
2990  }
2991  /* Free the false origin inputs */
2992  lwgeom_free(lwg_origin);
2993  PG_FREE_IF_COPY(geom_origin, 2);
2994  }
2995 
2996  /* If we have false origin, translate to it before scaling */
2997  if (translate)
2998  {
2999  /* Initialize affine */
3000  memset(&aff, 0, sizeof(AFFINE));
3001  /* Set rotation/scale/sheer matrix to no-op */
3002  aff.afac = aff.efac = aff.ifac = 1.0;
3003  /* Strip false origin from all coordinates */
3004  aff.xoff = -1 * origin.x;
3005  aff.yoff = -1 * origin.y;
3006  aff.zoff = -1 * origin.z;
3007  lwgeom_affine(lwg, &aff);
3008  }
3009 
3010  lwgeom_scale(lwg, &factors);
3011 
3012  /* Return to original origin after scaling */
3013  if (translate)
3014  {
3015  aff.xoff *= -1;
3016  aff.yoff *= -1;
3017  aff.zoff *= -1;
3018  lwgeom_affine(lwg, &aff);
3019  }
3020 
3021  /* Cleanup and return */
3022  ret = geometry_serialize(lwg);
3023  lwgeom_free(lwg);
3024  PG_FREE_IF_COPY(geom, 0);
3025  PG_FREE_IF_COPY(geom_scale, 1);
3026  PG_RETURN_POINTER(ret);
3027 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
void lwgeom_scale(LWGEOM *geom, const POINT4D *factors)
Definition: lwgeom.c:2038
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
void lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
Definition: lwgeom.c:1984
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwgeom.c:1393
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double zoff
Definition: liblwgeom.h:273
double ifac
Definition: liblwgeom.h:273
double xoff
Definition: liblwgeom.h:273
double afac
Definition: liblwgeom.h:273
double efac
Definition: liblwgeom.h:273
double yoff
Definition: liblwgeom.h:273
double m
Definition: liblwgeom.h:355
double x
Definition: liblwgeom.h:355
double z
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355

References AFFINE::afac, AFFINE::efac, geometry_serialize(), 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: