1056 double scale[2] = {0};
1057 double *scale_x = NULL;
1058 double *scale_y = NULL;
1076 text *pixeltypetext = NULL;
1077 char *pixeltype = NULL;
1080 uint32_t pixtypes_len = 0;
1082 double *values = NULL;
1083 uint32_t values_len = 0;
1085 uint8_t *hasnodatas = NULL;
1086 double *nodatavals = NULL;
1087 uint32_t nodatavals_len = 0;
1089 double ulw[2] = {0};
1090 double *ul_xw = NULL;
1091 double *ul_yw = NULL;
1093 double gridw[2] = {0};
1094 double *grid_xw = NULL;
1095 double *grid_yw = NULL;
1097 double skew[2] = {0};
1098 double *skew_x = NULL;
1099 double *skew_y = NULL;
1101 char **options = NULL;
1102 int options_len = 0;
1104 uint32_t num_bands = 0;
1114 if (PG_ARGISNULL(0))
1117 gser = PG_GETARG_GSERIALIZED_P(0);
1131 PG_FREE_IF_COPY(gser, 0);
1143 SET_VARSIZE(pgrast, pgrast->
size);
1144 PG_RETURN_POINTER(pgrast);
1148 if (!PG_ARGISNULL(1)) {
1149 scale[0] = PG_GETARG_FLOAT8(1);
1151 scale_x = &scale[0];
1155 if (!PG_ARGISNULL(2)) {
1156 scale[1] = PG_GETARG_FLOAT8(2);
1158 scale_y = &scale[1];
1160 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: scale (x, y) = %f, %f", scale[0], scale[1]);
1163 if (!PG_ARGISNULL(3)) {
1164 dim[0] = PG_GETARG_INT32(3);
1165 if (dim[0] < 0) dim[0] = 0;
1166 if (dim[0] != 0) dim_x = &dim[0];
1170 if (!PG_ARGISNULL(4)) {
1171 dim[1] = PG_GETARG_INT32(4);
1172 if (dim[1] < 0) dim[1] = 0;
1173 if (dim[1] != 0) dim_y = &dim[1];
1178 if (!PG_ARGISNULL(5)) {
1179 array = PG_GETARG_ARRAYTYPE_P(5);
1180 etype = ARR_ELEMTYPE(array);
1181 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1189 PG_FREE_IF_COPY(gser, 0);
1191 elog(ERROR,
"RASTER_asRaster: Invalid data type for pixeltype");
1196 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1202 for (i = 0, j = 0; i < n; i++) {
1211 pixeltypetext = (text *) DatumGetPointer(e[i]);
1212 if (NULL == pixeltypetext)
break;
1213 pixeltype = text_to_cstring(pixeltypetext);
1221 if (strlen(pixeltype)) {
1228 PG_FREE_IF_COPY(gser, 0);
1230 elog(ERROR,
"RASTER_asRaster: Invalid pixel type provided: %s", pixeltype);
1234 pixtypes[j] = pixtype;
1241 pixtypes = repalloc(pixtypes, j *
sizeof(
rt_pixtype));
1251 #if POSTGIS_DEBUG_LEVEL > 0
1252 for (uint32_t u = 0; u < pixtypes_len; u++)
1257 if (!PG_ARGISNULL(6)) {
1258 array = PG_GETARG_ARRAYTYPE_P(6);
1259 etype = ARR_ELEMTYPE(array);
1260 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1268 if (pixtypes_len) pfree(pixtypes);
1271 PG_FREE_IF_COPY(gser, 0);
1273 elog(ERROR,
"RASTER_asRaster: Invalid data type for value");
1278 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1282 values = (
double *) palloc(
sizeof(
double) * n);
1283 for (i = 0, j = 0; i < n; i++) {
1291 values[j] = (double) DatumGetFloat4(e[i]);
1294 values[j] = (double) DatumGetFloat8(e[i]);
1304 values = repalloc(values, j *
sizeof(
double));
1314 #if POSTGIS_DEBUG_LEVEL > 0
1315 for (uint32_t u = 0; u < values_len; u++)
1320 if (!PG_ARGISNULL(7)) {
1321 array = PG_GETARG_ARRAYTYPE_P(7);
1322 etype = ARR_ELEMTYPE(array);
1323 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1331 if (pixtypes_len) pfree(pixtypes);
1332 if (values_len) pfree(values);
1335 PG_FREE_IF_COPY(gser, 0);
1337 elog(ERROR,
"RASTER_asRaster: Invalid data type for nodataval");
1342 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1346 nodatavals = (
double *) palloc(
sizeof(
double) * n);
1347 hasnodatas = (uint8_t *) palloc(
sizeof(uint8_t) * n);
1348 for (i = 0, j = 0; i < n; i++) {
1359 nodatavals[j] = (double) DatumGetFloat4(e[i]);
1362 nodatavals[j] = (double) DatumGetFloat8(e[i]);
1373 nodatavals = repalloc(nodatavals, j *
sizeof(
double));
1374 hasnodatas = repalloc(hasnodatas, j *
sizeof(uint8_t));
1386 #if POSTGIS_DEBUG_LEVEL > 0
1387 for (uint32_t u = 0; u < nodatavals_len; u++)
1395 if (!PG_ARGISNULL(8)) {
1396 ulw[0] = PG_GETARG_FLOAT8(8);
1401 if (!PG_ARGISNULL(9)) {
1402 ulw[1] = PG_GETARG_FLOAT8(9);
1405 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: upperleft (x, y) = %f, %f", ulw[0], ulw[1]);
1408 if (!PG_ARGISNULL(10)) {
1409 gridw[0] = PG_GETARG_FLOAT8(10);
1410 grid_xw = &gridw[0];
1414 if (!PG_ARGISNULL(11)) {
1415 gridw[1] = PG_GETARG_FLOAT8(11);
1416 grid_yw = &gridw[1];
1418 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: grid (x, y) = %f, %f", gridw[0], gridw[1]);
1425 (scale_x == NULL && scale_y != NULL) ||
1426 (scale_x != NULL && scale_y == NULL)
1428 elog(NOTICE,
"Values must be provided for both X and Y of scale if one is specified");
1435 (dim_x == NULL && dim_y != NULL) ||
1436 (dim_x != NULL && dim_y == NULL)
1438 elog(NOTICE,
"Values must be provided for both width and height if one is specified");
1445 (scale_x != NULL && scale_y != NULL) &&
1446 (dim_x != NULL && dim_y != NULL)
1448 elog(NOTICE,
"Values provided for X and Y of scale and width and height. Using the width and height");
1456 (scale_x == NULL && scale_y == NULL) &&
1457 (dim_x == NULL && dim_y == NULL)
1459 elog(NOTICE,
"Values must be provided for X and Y of scale or width and height");
1466 (ul_xw == NULL && ul_yw != NULL) ||
1467 (ul_xw != NULL && ul_yw == NULL)
1469 elog(NOTICE,
"Values must be provided for both X and Y when specifying the upper-left corner");
1476 (grid_xw == NULL && grid_yw != NULL) ||
1477 (grid_xw != NULL && grid_yw == NULL)
1479 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment");
1486 (ul_xw != NULL && ul_yw != NULL) &&
1487 (grid_xw != NULL && grid_yw != NULL)
1489 elog(NOTICE,
"Values provided for both X and Y of upper-left corner and alignment. Using the values of upper-left corner");
1498 if (pixtypes_len) pfree(pixtypes);
1499 if (values_len) pfree(values);
1500 if (nodatavals_len) {
1506 PG_FREE_IF_COPY(gser, 0);
1512 if (!PG_ARGISNULL(12)) {
1513 skew[0] = PG_GETARG_FLOAT8(12);
1519 if (!PG_ARGISNULL(13)) {
1520 skew[1] = PG_GETARG_FLOAT8(13);
1524 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: skew (x, y) = %f, %f", skew[0], skew[1]);
1527 if (!PG_ARGISNULL(14) && PG_GETARG_BOOL(14) ==
TRUE) {
1528 if (options_len == 0) {
1530 options = (
char **) palloc(
sizeof(
char *) * options_len);
1534 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1537 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
1538 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
1543 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1544 options[options_len - 1] = NULL;
1555 if (pixtypes_len) pfree(pixtypes);
1556 if (values_len) pfree(values);
1557 if (nodatavals_len) {
1561 if (options_len) pfree(options);
1564 PG_FREE_IF_COPY(gser, 0);
1566 elog(ERROR,
"RASTER_asRaster: Could not find srtext for SRID (%d)", srid);
1576 num_bands =
MIN(pixtypes_len, values_len);
1577 num_bands =
MIN(num_bands, nodatavals_len);
1585 (pixtypes_len == values_len) &&
1586 (values_len == nodatavals_len)
1590 "Imbalanced number of values provided for pixeltype (%d), value (%d) and nodataval (%d). Using the first %d values of each parameter",
1602 PG_FREE_IF_COPY(gser, 0);
1628 if (pixtypes_len) pfree(pixtypes);
1629 if (values_len) pfree(values);
1630 if (nodatavals_len) {
1634 if (options_len) pfree(options);
1637 elog(ERROR,
"RASTER_asRaster: Could not rasterize geometry");
1647 if (NULL == pgrast) PG_RETURN_NULL();
1651 SET_VARSIZE(pgrast, pgrast->
size);
1652 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,...)