734 double scale[2] = {0};
735 double *scale_x = NULL;
736 double *scale_y = NULL;
754 text *pixeltypetext = NULL;
755 char *pixeltype = NULL;
758 uint32_t pixtypes_len = 0;
760 double *values = NULL;
761 uint32_t values_len = 0;
763 uint8_t *hasnodatas = NULL;
764 double *nodatavals = NULL;
765 uint32_t nodatavals_len = 0;
768 double *ul_xw = NULL;
769 double *ul_yw = NULL;
771 double gridw[2] = {0};
772 double *grid_xw = NULL;
773 double *grid_yw = NULL;
775 double skew[2] = {0};
776 double *skew_x = NULL;
777 double *skew_y = NULL;
779 char **options = NULL;
782 uint32_t num_bands = 0;
795 gser = PG_GETARG_GSERIALIZED_P(0);
809 PG_FREE_IF_COPY(gser, 0);
821 SET_VARSIZE(pgrast, pgrast->
size);
822 PG_RETURN_POINTER(pgrast);
826 if (!PG_ARGISNULL(1)) {
827 scale[0] = PG_GETARG_FLOAT8(1);
833 if (!PG_ARGISNULL(2)) {
834 scale[1] = PG_GETARG_FLOAT8(2);
838 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: scale (x, y) = %f, %f", scale[0], scale[1]);
841 if (!PG_ARGISNULL(3)) {
842 dim[0] = PG_GETARG_INT32(3);
843 if (dim[0] < 0) dim[0] = 0;
844 if (dim[0] != 0) dim_x = &dim[0];
848 if (!PG_ARGISNULL(4)) {
849 dim[1] = PG_GETARG_INT32(4);
850 if (dim[1] < 0) dim[1] = 0;
851 if (dim[1] != 0) dim_y = &dim[1];
856 if (!PG_ARGISNULL(5)) {
857 array = PG_GETARG_ARRAYTYPE_P(5);
858 etype = ARR_ELEMTYPE(array);
859 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
867 PG_FREE_IF_COPY(gser, 0);
869 elog(ERROR,
"RASTER_asRaster: Invalid data type for pixeltype");
874 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
880 for (i = 0, j = 0; i < n; i++) {
889 pixeltypetext = (text *) DatumGetPointer(e[i]);
890 if (NULL == pixeltypetext)
break;
891 pixeltype = text_to_cstring(pixeltypetext);
899 if (strlen(pixeltype)) {
906 PG_FREE_IF_COPY(gser, 0);
908 elog(ERROR,
"RASTER_asRaster: Invalid pixel type provided: %s", pixeltype);
912 pixtypes[j] = pixtype;
919 pixtypes = repalloc(pixtypes, j *
sizeof(
rt_pixtype));
929 #if POSTGIS_DEBUG_LEVEL > 0
930 for (uint32_t u = 0; u < pixtypes_len; u++)
935 if (!PG_ARGISNULL(6)) {
936 array = PG_GETARG_ARRAYTYPE_P(6);
937 etype = ARR_ELEMTYPE(array);
938 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
946 if (pixtypes_len) pfree(pixtypes);
949 PG_FREE_IF_COPY(gser, 0);
951 elog(ERROR,
"RASTER_asRaster: Invalid data type for value");
956 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
960 values = (
double *) palloc(
sizeof(
double) * n);
961 for (i = 0, j = 0; i < n; i++) {
969 values[j] = (double) DatumGetFloat4(e[i]);
972 values[j] = (double) DatumGetFloat8(e[i]);
982 values = repalloc(values, j *
sizeof(
double));
992 #if POSTGIS_DEBUG_LEVEL > 0
993 for (uint32_t u = 0; u < values_len; u++)
998 if (!PG_ARGISNULL(7)) {
999 array = PG_GETARG_ARRAYTYPE_P(7);
1000 etype = ARR_ELEMTYPE(array);
1001 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1009 if (pixtypes_len) pfree(pixtypes);
1010 if (values_len) pfree(values);
1013 PG_FREE_IF_COPY(gser, 0);
1015 elog(ERROR,
"RASTER_asRaster: Invalid data type for nodataval");
1020 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1024 nodatavals = (
double *) palloc(
sizeof(
double) * n);
1025 hasnodatas = (uint8_t *) palloc(
sizeof(uint8_t) * n);
1026 for (i = 0, j = 0; i < n; i++) {
1037 nodatavals[j] = (double) DatumGetFloat4(e[i]);
1040 nodatavals[j] = (double) DatumGetFloat8(e[i]);
1051 nodatavals = repalloc(nodatavals, j *
sizeof(
double));
1052 hasnodatas = repalloc(hasnodatas, j *
sizeof(uint8_t));
1064 #if POSTGIS_DEBUG_LEVEL > 0
1065 for (uint32_t u = 0; u < nodatavals_len; u++)
1073 if (!PG_ARGISNULL(8)) {
1074 ulw[0] = PG_GETARG_FLOAT8(8);
1079 if (!PG_ARGISNULL(9)) {
1080 ulw[1] = PG_GETARG_FLOAT8(9);
1083 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: upperleft (x, y) = %f, %f", ulw[0], ulw[1]);
1086 if (!PG_ARGISNULL(10)) {
1087 gridw[0] = PG_GETARG_FLOAT8(10);
1088 grid_xw = &gridw[0];
1092 if (!PG_ARGISNULL(11)) {
1093 gridw[1] = PG_GETARG_FLOAT8(11);
1094 grid_yw = &gridw[1];
1096 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: grid (x, y) = %f, %f", gridw[0], gridw[1]);
1103 (scale_x == NULL && scale_y != NULL) ||
1104 (scale_x != NULL && scale_y == NULL)
1106 elog(NOTICE,
"Values must be provided for both X and Y of scale if one is specified");
1113 (dim_x == NULL && dim_y != NULL) ||
1114 (dim_x != NULL && dim_y == NULL)
1116 elog(NOTICE,
"Values must be provided for both width and height if one is specified");
1123 (scale_x != NULL && scale_y != NULL) &&
1124 (dim_x != NULL && dim_y != NULL)
1126 elog(NOTICE,
"Values provided for X and Y of scale and width and height. Using the width and height");
1134 (scale_x == NULL && scale_y == NULL) &&
1135 (dim_x == NULL && dim_y == NULL)
1137 elog(NOTICE,
"Values must be provided for X and Y of scale or width and height");
1144 (ul_xw == NULL && ul_yw != NULL) ||
1145 (ul_xw != NULL && ul_yw == NULL)
1147 elog(NOTICE,
"Values must be provided for both X and Y when specifying the upper-left corner");
1154 (grid_xw == NULL && grid_yw != NULL) ||
1155 (grid_xw != NULL && grid_yw == NULL)
1157 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment");
1164 (ul_xw != NULL && ul_yw != NULL) &&
1165 (grid_xw != NULL && grid_yw != NULL)
1167 elog(NOTICE,
"Values provided for both X and Y of upper-left corner and alignment. Using the values of upper-left corner");
1176 if (pixtypes_len) pfree(pixtypes);
1177 if (values_len) pfree(values);
1178 if (nodatavals_len) {
1184 PG_FREE_IF_COPY(gser, 0);
1190 if (!PG_ARGISNULL(12)) {
1191 skew[0] = PG_GETARG_FLOAT8(12);
1197 if (!PG_ARGISNULL(13)) {
1198 skew[1] = PG_GETARG_FLOAT8(13);
1202 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: skew (x, y) = %f, %f", skew[0], skew[1]);
1205 if (!PG_ARGISNULL(14) && PG_GETARG_BOOL(14) ==
TRUE) {
1206 if (options_len == 0) {
1208 options = (
char **) palloc(
sizeof(
char *) * options_len);
1212 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1215 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
1216 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
1221 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1222 options[options_len - 1] = NULL;
1233 if (pixtypes_len) pfree(pixtypes);
1234 if (values_len) pfree(values);
1235 if (nodatavals_len) {
1239 if (options_len) pfree(options);
1242 PG_FREE_IF_COPY(gser, 0);
1244 elog(ERROR,
"RASTER_asRaster: Could not find srtext for SRID (%d)", srid);
1254 num_bands =
MIN(pixtypes_len, values_len);
1255 num_bands =
MIN(num_bands, nodatavals_len);
1263 (pixtypes_len == values_len) &&
1264 (values_len == nodatavals_len)
1268 "Imbalanced number of values provided for pixeltype (%d), value (%d) and nodataval (%d). Using the first %d values of each parameter",
1280 PG_FREE_IF_COPY(gser, 0);
1306 if (pixtypes_len) pfree(pixtypes);
1307 if (values_len) pfree(values);
1308 if (nodatavals_len) {
1312 if (options_len) pfree(options);
1315 elog(ERROR,
"RASTER_asRaster: Could not rasterize geometry");
1325 if (NULL == pgrast) PG_RETURN_NULL();
1329 SET_VARSIZE(pgrast, pgrast->
size);
1330 PG_RETURN_POINTER(pgrast);
int32_t gserialized_get_srid(const GSERIALIZED *g)
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)
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
#define SRID_UNKNOWN
Unknown SRID value.
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
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.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
char * rtpg_getSR(int32_t srid)
char * rtpg_trim(const char *input)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)