738 double scale[2] = {0};
739 double *scale_x = NULL;
740 double *scale_y = NULL;
758 text *pixeltypetext = NULL;
759 char *pixeltype = NULL;
764 double *values = NULL;
768 double *nodatavals = NULL;
772 double *ul_xw = NULL;
773 double *ul_yw = NULL;
775 double gridw[2] = {0};
776 double *grid_xw = NULL;
777 double *grid_yw = NULL;
779 double skew[2] = {0};
780 double *skew_x = NULL;
781 double *skew_y = NULL;
783 char **options = NULL;
799 gser = PG_GETARG_GSERIALIZED_P(0);
813 PG_FREE_IF_COPY(gser, 0);
825 SET_VARSIZE(pgrast, pgrast->
size);
826 PG_RETURN_POINTER(pgrast);
830 if (!PG_ARGISNULL(1)) {
831 scale[0] = PG_GETARG_FLOAT8(1);
832 if (
FLT_NEQ(scale[0], 0)) scale_x = &scale[0];
836 if (!PG_ARGISNULL(2)) {
837 scale[1] = PG_GETARG_FLOAT8(2);
838 if (
FLT_NEQ(scale[1], 0)) scale_y = &scale[1];
840 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: scale (x, y) = %f, %f", scale[0], scale[1]);
843 if (!PG_ARGISNULL(3)) {
844 dim[0] = PG_GETARG_INT32(3);
845 if (dim[0] < 0) dim[0] = 0;
846 if (dim[0] != 0) dim_x = &dim[0];
850 if (!PG_ARGISNULL(4)) {
851 dim[1] = PG_GETARG_INT32(4);
852 if (dim[1] < 0) dim[1] = 0;
853 if (dim[1] != 0) dim_y = &dim[1];
858 if (!PG_ARGISNULL(5)) {
859 array = PG_GETARG_ARRAYTYPE_P(5);
860 etype = ARR_ELEMTYPE(array);
861 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
869 PG_FREE_IF_COPY(gser, 0);
871 elog(ERROR,
"RASTER_asRaster: Invalid data type for pixeltype");
876 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
882 for (i = 0, j = 0; i < n; i++) {
891 pixeltypetext = (text *) DatumGetPointer(e[i]);
892 if (NULL == pixeltypetext)
break;
901 if (strlen(pixeltype)) {
908 PG_FREE_IF_COPY(gser, 0);
910 elog(ERROR,
"RASTER_asRaster: Invalid pixel type provided: %s", pixeltype);
914 pixtypes[j] = pixtype;
921 pixtypes = repalloc(pixtypes, j *
sizeof(
rt_pixtype));
931 #if POSTGIS_DEBUG_LEVEL > 0
932 for (i = 0; i < pixtypes_len; i++)
933 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: pixtypes[%d] = %d", i, (
int) pixtypes[i]);
937 if (!PG_ARGISNULL(6)) {
938 array = PG_GETARG_ARRAYTYPE_P(6);
939 etype = ARR_ELEMTYPE(array);
940 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
948 if (pixtypes_len) pfree(pixtypes);
951 PG_FREE_IF_COPY(gser, 0);
953 elog(ERROR,
"RASTER_asRaster: Invalid data type for value");
958 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
962 values = (
double *) palloc(
sizeof(
double) * n);
963 for (i = 0, j = 0; i < n; i++) {
971 values[j] = (double) DatumGetFloat4(e[i]);
974 values[j] = (double) DatumGetFloat8(e[i]);
984 values = repalloc(values, j *
sizeof(
double));
994 #if POSTGIS_DEBUG_LEVEL > 0
995 for (i = 0; i < values_len; i++)
1000 if (!PG_ARGISNULL(7)) {
1001 array = PG_GETARG_ARRAYTYPE_P(7);
1002 etype = ARR_ELEMTYPE(array);
1003 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1011 if (pixtypes_len) pfree(pixtypes);
1012 if (values_len) pfree(values);
1015 PG_FREE_IF_COPY(gser, 0);
1017 elog(ERROR,
"RASTER_asRaster: Invalid data type for nodataval");
1022 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1026 nodatavals = (
double *) palloc(
sizeof(
double) * n);
1028 for (i = 0, j = 0; i < n; i++) {
1039 nodatavals[j] = (double) DatumGetFloat4(e[i]);
1042 nodatavals[j] = (double) DatumGetFloat8(e[i]);
1053 nodatavals = repalloc(nodatavals, j *
sizeof(
double));
1054 hasnodatas = repalloc(hasnodatas, j *
sizeof(
uint8_t));
1066 #if POSTGIS_DEBUG_LEVEL > 0
1067 for (i = 0; i < nodatavals_len; i++) {
1074 if (!PG_ARGISNULL(8)) {
1075 ulw[0] = PG_GETARG_FLOAT8(8);
1080 if (!PG_ARGISNULL(9)) {
1081 ulw[1] = PG_GETARG_FLOAT8(9);
1084 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: upperleft (x, y) = %f, %f", ulw[0], ulw[1]);
1087 if (!PG_ARGISNULL(10)) {
1088 gridw[0] = PG_GETARG_FLOAT8(10);
1089 grid_xw = &gridw[0];
1093 if (!PG_ARGISNULL(11)) {
1094 gridw[1] = PG_GETARG_FLOAT8(11);
1095 grid_yw = &gridw[1];
1097 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: grid (x, y) = %f, %f", gridw[0], gridw[1]);
1104 (scale_x == NULL && scale_y != NULL) ||
1105 (scale_x != NULL && scale_y == NULL)
1107 elog(NOTICE,
"Values must be provided for both X and Y of scale if one is specified");
1114 (dim_x == NULL && dim_y != NULL) ||
1115 (dim_x != NULL && dim_y == NULL)
1117 elog(NOTICE,
"Values must be provided for both width and height if one is specified");
1124 (scale_x != NULL && scale_y != NULL) &&
1125 (dim_x != NULL && dim_y != NULL)
1127 elog(NOTICE,
"Values provided for X and Y of scale and width and height. Using the width and height");
1135 (scale_x == NULL && scale_y == NULL) &&
1136 (dim_x == NULL && dim_y == NULL)
1138 elog(NOTICE,
"Values must be provided for X and Y of scale or width and height");
1145 (ul_xw == NULL && ul_yw != NULL) ||
1146 (ul_xw != NULL && ul_yw == NULL)
1148 elog(NOTICE,
"Values must be provided for both X and Y when specifying the upper-left corner");
1155 (grid_xw == NULL && grid_yw != NULL) ||
1156 (grid_xw != NULL && grid_yw == NULL)
1158 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment");
1165 (ul_xw != NULL && ul_yw != NULL) &&
1166 (grid_xw != NULL && grid_yw != NULL)
1168 elog(NOTICE,
"Values provided for both X and Y of upper-left corner and alignment. Using the values of upper-left corner");
1177 if (pixtypes_len) pfree(pixtypes);
1178 if (values_len) pfree(values);
1179 if (nodatavals_len) {
1185 PG_FREE_IF_COPY(gser, 0);
1191 if (!PG_ARGISNULL(12)) {
1192 skew[0] = PG_GETARG_FLOAT8(12);
1193 if (
FLT_NEQ(skew[0], 0)) skew_x = &skew[0];
1197 if (!PG_ARGISNULL(13)) {
1198 skew[1] = PG_GETARG_FLOAT8(13);
1199 if (
FLT_NEQ(skew[1], 0)) skew_y = &skew[1];
1201 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: skew (x, y) = %f, %f", skew[0], skew[1]);
1204 if (!PG_ARGISNULL(14) && PG_GETARG_BOOL(14) ==
TRUE) {
1205 if (options_len == 0) {
1207 options = (
char **) palloc(
sizeof(
char *) * options_len);
1211 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1214 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
1215 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
1220 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1221 options[options_len - 1] = NULL;
1232 if (pixtypes_len) pfree(pixtypes);
1233 if (values_len) pfree(values);
1234 if (nodatavals_len) {
1238 if (options_len) pfree(options);
1241 PG_FREE_IF_COPY(gser, 0);
1243 elog(ERROR,
"RASTER_asRaster: Could not find srtext for SRID (%d)", srid);
1253 num_bands =
MIN(pixtypes_len, values_len);
1254 num_bands =
MIN(num_bands, nodatavals_len);
1262 (pixtypes_len == values_len) &&
1263 (values_len == nodatavals_len)
1267 "Imbalanced number of values provided for pixeltype (%d), value (%d) and nodataval (%d). Using the first %d values of each parameter",
1279 PG_FREE_IF_COPY(gser, 0);
1286 num_bands, pixtypes,
1288 nodatavals, hasnodatas,
1297 if (pixtypes_len) pfree(pixtypes);
1298 if (values_len) pfree(values);
1299 if (nodatavals_len) {
1303 if (options_len) pfree(options);
1306 elog(ERROR,
"RASTER_asRaster: Could not rasterize geometry");
1316 if (NULL == pgrast) PG_RETURN_NULL();
1320 SET_VARSIZE(pgrast, pgrast->
size);
1321 PG_RETURN_POINTER(pgrast);
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
void lwgeom_free(LWGEOM *geom)
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
#define SRID_UNKNOWN
Unknown SRID value.
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_raster rt_raster_gdal_rasterize(const unsigned char *wkb, uint32_t wkb_len, const char *srs, uint32_t num_bands, rt_pixtype *pixtype, double *init, double *value, double *nodata, uint8_t *hasnodata, int *width, int *height, double *scale_x, double *scale_y, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, char **options)
Return a raster of the provided geometry.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
char * text_to_cstring(const text *textptr)
char * rtpg_getSR(int srid)
char * rtpg_trim(const char *input)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)