53 rterror(
"rt_raster_new: Out of virtual memory creating an rt_raster");
59 if (width > 65535 || height > 65535) {
60 rterror(
"rt_raster_new: Dimensions requested exceed the maximum (65535 x 65535) permitted for a raster");
107 for (i = 0; i < numband; i++) {
115 rtwarn(
"Changes made to raster geotransform matrix may affect out-db band data. Returned band data may be incorrect");
123 assert(NULL != raster);
125 return raster->
width;
131 assert(NULL != raster);
139 double scaleX,
double scaleY
141 assert(NULL != raster);
153 assert(NULL != raster);
162 assert(NULL != raster);
170 double skewX,
double skewY
172 assert(NULL != raster);
174 raster->
skewX = skewX;
175 raster->
skewY = skewY;
184 assert(NULL != raster);
186 return raster->
skewX;
193 assert(NULL != raster);
195 return raster->
skewY;
204 assert(NULL != raster);
216 assert(NULL != raster);
225 assert(NULL != raster);
232 double *i_mag,
double *j_mag,
double *theta_i,
double *theta_ij)
234 double o11, o12, o21, o22 ;
236 if (rast == NULL)
return ;
237 if ( (i_mag==NULL) || (j_mag==NULL) || (theta_i==NULL) || (theta_ij==NULL))
251 double *i_mag,
double *j_mag,
double *theta_i,
double *theta_ij)
256 if ( (i_mag==NULL) || (j_mag==NULL) || (theta_i==NULL) || (theta_ij==NULL))
260 *i_mag = sqrt(xscale*xscale + yskew*yskew) ;
263 *j_mag = sqrt(xskew*xskew + yscale*yscale) ;
273 *theta_i = acos(xscale/(*i_mag)) ;
274 theta_test = acos(yskew/(*i_mag)) ;
275 if (theta_test < M_PI_2){
276 *theta_i = -(*theta_i) ;
288 *theta_ij = acos(((xscale*xskew) + (yskew*yscale))/((*i_mag)*(*j_mag))) ;
289 theta_test = acos( ((-yskew*xskew)+(xscale*yscale)) /
290 ((*i_mag)*(*j_mag)));
291 if (theta_test > M_PI_2) {
292 *theta_ij = -(*theta_ij) ;
299 double o11, o12, o21, o22 ;
302 if (rast == NULL)
return ;
305 &o11, &o12, &o21, &o22) ;
315 double *xscale,
double *xskew,
double *yskew,
double *yscale)
320 double cos_theta_i, sin_theta_i ;
322 if ( (xscale==NULL) || (xskew==NULL) || (yskew==NULL) || (yscale==NULL)) {
326 if ( (theta_ij == 0.0) || (theta_ij == M_PI)) {
340 k_i = tan(f*M_PI_2 - theta_ij) ;
343 s_j = j_mag / (sqrt(k_i*k_i + 1)) ;
346 cos_theta_i = cos(theta_i) ;
347 sin_theta_i = sin(theta_i) ;
348 *xscale = s_i * cos_theta_i ;
349 *xskew = k_i * s_j * f * cos_theta_i + s_j * f * sin_theta_i ;
350 *yskew = -s_i * sin_theta_i ;
351 *yscale = -k_i * s_j * f * sin_theta_i + s_j * f * cos_theta_i ;
357 assert(NULL != raster);
364 assert(NULL != raster);
375 assert(NULL != raster);
382 assert(NULL != raster);
387 return raster->
bands[n];
411 assert(NULL != raster);
412 assert(NULL != band);
414 RASTER_DEBUGF(3,
"Adding band %p to raster %p", band, raster);
417 rterror(
"rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
428 oldbands = raster->
bands;
438 if (NULL == raster->
bands) {
439 rterror(
"rt_raster_add_band: Out of virtual memory " 440 "reallocating band pointers");
441 raster->
bands = oldbands;
447 for (i = 0; i <= raster->
numBands; ++i) {
449 oldband = raster->
bands[i];
451 }
else if (i > index) {
452 tmpband = raster->
bands[i];
453 raster->
bands[i] = oldband;
487 double initialvalue,
uint32_t hasnodata,
double nodatavalue,
498 int32_t checkvalint = 0;
500 double checkvaldouble = 0;
501 float checkvalfloat = 0;
505 assert(NULL != raster);
511 else if (index > oldnumbands + 1)
512 index = oldnumbands + 1;
517 numval = width * height;
520 mem = (
int *)
rtalloc(datasize);
522 rterror(
"rt_raster_generate_new_band: Could not allocate memory for band");
526 if (
FLT_EQ(initialvalue, 0.0))
527 memset(mem, 0, datasize);
535 for (i = 0; i < numval; i++)
536 ptr[i] = clamped_initval;
537 checkvalint = ptr[0];
544 for (i = 0; i < numval; i++)
545 ptr[i] = clamped_initval;
546 checkvalint = ptr[0];
553 for (i = 0; i < numval; i++)
554 ptr[i] = clamped_initval;
555 checkvalint = ptr[0];
562 for (i = 0; i < numval; i++)
563 ptr[i] = clamped_initval;
564 checkvalint = ptr[0];
571 for (i = 0; i < numval; i++)
572 ptr[i] = clamped_initval;
573 checkvalint = ptr[0];
580 for (i = 0; i < numval; i++)
581 ptr[i] = clamped_initval;
582 checkvalint = ptr[0];
589 for (i = 0; i < numval; i++)
590 ptr[i] = clamped_initval;
591 checkvalint = ptr[0];
598 for (i = 0; i < numval; i++)
599 ptr[i] = clamped_initval;
600 checkvalint = ptr[0];
607 for (i = 0; i < numval; i++)
608 ptr[i] = clamped_initval;
609 checkvaluint = ptr[0];
616 for (i = 0; i < numval; i++)
617 ptr[i] = clamped_initval;
618 checkvalfloat = ptr[0];
624 for (i = 0; i < numval; i++)
625 ptr[i] = initialvalue;
626 checkvaldouble = ptr[0];
631 rterror(
"rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
641 checkvalint, checkvaluint,
642 checkvalfloat, checkvaldouble,
648 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
655 if (numbands == oldnumbands || index == -1) {
656 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
661 if (hasnodata &&
FLT_EQ(initialvalue, nodatavalue))
678 double *
gt,
double *igt
682 assert((raster != NULL || gt != NULL));
688 memcpy(_gt, gt,
sizeof(
double) * 6);
690 if (!GDALInvGeoTransform(_gt, igt)) {
691 rterror(
"rt_raster_get_inverse_geotransform_matrix: Could not compute inverse geotransform matrix");
708 assert(NULL != raster);
713 gt[2] = raster->
skewX;
715 gt[4] = raster->
skewY;
729 assert(NULL != raster);
734 raster->
skewX = gt[2];
736 raster->
skewY = gt[4];
757 double xr,
double yr,
758 double *xw,
double *yw,
763 assert(NULL != raster);
764 assert(NULL != xw && NULL != yw);
767 memcpy(_gt, gt,
sizeof(
double) * 6);
786 GDALApplyGeoTransform(_gt, xr, yr, xw, yw);
787 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (c -> g) for (%f, %f) = (%f, %f)",
808 double xw,
double yw,
809 double *xr,
double *yr,
812 double _igt[6] = {0};
815 assert(NULL != raster);
816 assert(NULL != xr && NULL != yr);
819 memcpy(_igt, igt,
sizeof(
double) * 6);
831 rterror(
"rt_raster_geopoint_to_cell: Could not get inverse geotransform matrix");
836 GDALApplyGeoTransform(_igt, xw, yw, xr, yr);
837 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
852 RASTER_DEBUGF(4,
"Corrected GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
882 double _gt[6] = {0.};
884 assert(raster != NULL);
889 for (i = 0; i < 4; i++) {
900 _r[0] = raster->
width;
904 _r[0] = raster->
width;
916 rterror(
"rt_raster_get_envelope: Could not compute spatial coordinates for raster pixel");
928 if (_w[0] < env->
MinX)
930 else if (_w[0] > env->
MaxX)
933 if (_w[1] < env->
MinY)
935 else if (_w[1] > env->
MaxY)
974 double _igt[6] = {0};
987 GEOSGeometry *sgeom = NULL;
988 GEOSGeometry *ngeom = NULL;
996 else if (tolerance > 1.)
1000 while (dbl_run < 10) {
1008 for (i = 0; i < 2; i++) {
1009 if (
FLT_EQ(scale[i], 0)) {
1010 rterror(
"rt_raster_compute_skewed_raster: Scale cannot be zero");
1015 _gt[1] = fabs(scale[i] * tolerance);
1017 _gt[5] = fabs(scale[i] * tolerance);
1030 (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (fabs(scale[0]) / 2.)) / fabs(scale[0]), 1),
1031 (
int) fmax((fabs(extent.
MaxY - extent.
MinY) + (fabs(scale[1]) / 2.)) / fabs(scale[1]), 1)
1035 if (raster == NULL) {
1036 rterror(
"rt_raster_compute_skewed_raster: Could not create output raster");
1055 _gt[2] = skew[0] * tolerance;
1057 _gt[4] = skew[1] * tolerance;
1059 RASTER_DEBUGF(4,
"Initial geotransform: %f, %f, %f, %f, %f, %f",
1060 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1066 rterror(
"rt_raster_compute_skewed_raster: Out of memory allocating extent raster");
1072 if (!GDALInvGeoTransform(_gt, _igt)) {
1073 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1077 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1078 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1082 for (i = 0; i < 2; i++) {
1086 RASTER_DEBUGF(3,
"Shifting along %s axis", i < 1 ?
"X" :
"Y");
1091 if (run > max_run) {
1092 rterror(
"rt_raster_compute_skewed_raster: Could not compute skewed extent due to check preventing infinite loop");
1101 for (j = 0; j < 4; j++) {
1105 _xy[0] = extent.
MinX;
1106 _xy[1] = extent.
MaxY;
1110 _xy[0] = extent.
MinX;
1111 _xy[1] = extent.
MinY;
1115 _xy[0] = extent.
MaxX;
1116 _xy[1] = extent.
MinY;
1120 _xy[0] = extent.
MaxX;
1121 _xy[1] = extent.
MaxY;
1132 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1137 RASTER_DEBUGF(4,
"Point %d at cell %d x %d", j, (
int) _r[0], (
int) _r[1]);
1140 if ((
int) _r[i] < 0) {
1144 if (_dlastpos != j) {
1145 _dlast = (int) _r[i];
1148 else if ((
int) _r[i] < _dlast) {
1149 RASTER_DEBUG(4,
"Point going in wrong direction. Reversing direction");
1165 x = _d[i] * fabs(_r[i]);
1167 y = _d[i] * fabs(_r[i]);
1176 rterror(
"rt_raster_compute_skewed_raster: Could not compute spatial coordinates for raster pixel");
1187 RASTER_DEBUGF(4,
"Shifted geotransform: %f, %f, %f, %f, %f, %f",
1188 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1192 if (!GDALInvGeoTransform(_gt, _igt)) {
1193 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1197 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1198 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1215 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1220 RASTER_DEBUGF(4,
"geopoint %f x %f at cell %d x %d", extent.
MaxX, extent.
MinY, (
int) _r[0], (
int) _r[1]);
1222 raster->
width = _r[0];
1231 if (npoly == NULL) {
1232 rterror(
"rt_raster_compute_skewed_raster: Could not build extent's geometry for covers test");
1246 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for covers test");
1247 GEOSGeom_destroy(ngeom);
1255 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1256 GEOSGeom_destroy(sgeom);
1259 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test");
1260 GEOSGeom_destroy(ngeom);
1275 raster->
width = (int) ((((
double) raster->
width) * fabs(_gt[1]) + fabs(scale[0] / 2.)) / fabs(scale[0]));
1276 raster->
height = (int) ((((
double) raster->
height) * fabs(_gt[5]) + fabs(scale[1] / 2.)) / fabs(scale[1]));
1277 _gt[1] = fabs(scale[0]);
1278 _gt[5] = -1 * fabs(scale[1]);
1284 for (i = 0; i < 2; i++) {
1294 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for minimizing dimensions");
1295 GEOSGeom_destroy(ngeom);
1303 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1304 GEOSGeom_destroy(sgeom);
1307 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test for minimizing dimensions");
1308 GEOSGeom_destroy(ngeom);
1325 GEOSGeom_destroy(ngeom);
1339 return (NULL == raster || raster->
height <= 0 || raster->
width <= 0);
1352 return !(NULL == raster || nband >= raster->
numBands || nband < 0);
1376 int fromindex,
int toindex
1381 assert(NULL != torast);
1382 assert(NULL != fromrast);
1386 rtwarn(
"rt_raster_copy_band: Attempting to add a band with different width or height");
1392 rtwarn(
"rt_raster_copy_band: Second raster has no band");
1395 else if (fromindex < 0) {
1396 rtwarn(
"rt_raster_copy_band: Band index for second raster < 0. Defaulted to 0");
1399 else if (fromindex >= fromrast->
numBands) {
1400 rtwarn(
"rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex, fromrast->
numBands - 1);
1401 fromindex = fromrast->
numBands - 1;
1405 rtwarn(
"rt_raster_copy_band: Band index for first raster < 0. Defaulted to 0");
1408 else if (toindex > torast->
numBands) {
1409 rtwarn(
"rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex, torast->
numBands);
1445 double gt[6] = {0.};
1447 assert(NULL != raster);
1448 assert(NULL != bandNums);
1450 RASTER_DEBUGF(3,
"rt_raster_from_band: source raster has %d bands",
1456 rterror(
"rt_raster_from_band: Out of memory allocating new raster");
1468 for (i = 0; i <
count; i++) {
1473 rterror(
"rt_raster_from_band: Could not copy band");
1479 RASTER_DEBUGF(3,
"rt_raster_from_band: band created at index %d",
1483 RASTER_DEBUGF(3,
"rt_raster_from_band: new raster has %d bands",
1504 assert(NULL != raster);
1505 assert(NULL != band);
1508 rterror(
"rt_raster_replace_band: Band does not match raster's dimensions: %dx%d band to %dx%d raster",
1513 if (index >= raster->
numBands || index < 0) {
1514 rterror(
"rt_raster_replace_band: Band index is not valid");
1519 RASTER_DEBUGF(3,
"rt_raster_replace_band: old band at %p", oldband);
1520 RASTER_DEBUGF(3,
"rt_raster_replace_band: new band at %p", band);
1548 assert(NULL != raster);
1556 if (nband == NULL) {
1557 rterror(
"rt_raster_clone: Could not allocate memory for deep clone");
1560 for (i = 0; i < numband; i++)
1574 rterror(
"rt_raster_clone: Could not create cloned raster");
1604 char *format,
char **options, uint64_t *gdalsize
1606 GDALDriverH src_drv = NULL;
1607 int destroy_src_drv = 0;
1608 GDALDatasetH
src_ds = NULL;
1610 vsi_l_offset rtn_lenvsi;
1611 uint64_t rtn_len = 0;
1613 GDALDriverH rtn_drv = NULL;
1614 GDALDatasetH rtn_ds = NULL;
1617 assert(NULL != raster);
1618 assert(NULL != gdalsize);
1625 if (format == NULL || !strlen(format))
1631 if (NULL == src_ds) {
1632 rterror(
"rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1637 rtn_drv = GDALGetDriverByName(format);
1638 if (NULL == rtn_drv) {
1639 rterror(
"rt_raster_to_gdal: Could not load the output GDAL driver");
1641 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1647 RASTER_DEBUG(3,
"Copying GDAL MEM raster to memory file in output format");
1648 rtn_ds = GDALCreateCopy(
1660 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1663 if (NULL == rtn_ds) {
1664 rterror(
"rt_raster_to_gdal: Could not create the output GDAL dataset");
1668 RASTER_DEBUGF(4,
"dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1674 RASTER_DEBUG(3,
"Done copying GDAL MEM raster to memory file in output format");
1678 rtn = VSIGetMemFileBuffer(
"/vsimem/out.dat", &rtn_lenvsi,
TRUE);
1679 RASTER_DEBUG(3,
"Done copying GDAL memory file to buffer");
1681 rterror(
"rt_raster_to_gdal: Could not create the output GDAL raster");
1685 rtn_len = (uint64_t) rtn_lenvsi;
1686 *gdalsize = rtn_len;
1709 GDALDriverH *drv = NULL;
1715 assert(drv_count != NULL);
1718 count = GDALGetDriverCount();
1723 rterror(
"rt_raster_gdal_drivers: Could not allocate memory for gdaldriver structure");
1727 for (i = 0, j = 0; i <
count; i++) {
1728 drv = GDALGetDriver(i);
1730 #ifdef GDAL_DCAP_RASTER 1733 state = GDALGetMetadataItem(drv, GDAL_DCAP_RASTER, NULL);
1734 if (state == NULL || !EQUAL(state,
"YES"))
1740 state = GDALGetMetadataItem(drv, GDAL_DCAP_CREATECOPY, NULL);
1741 if (state == NULL)
continue;
1744 state = GDALGetMetadataItem(drv, GDAL_DCAP_VIRTUALIO, NULL);
1745 if (state == NULL)
continue;
1752 txt = GDALGetDriverShortName(drv);
1753 txt_len = strlen(txt);
1756 RASTER_DEBUGF(3,
"driver %s (%d) supports CreateCopy() and VirtualIO()", txt, i);
1759 txt_len = (txt_len + 1) *
sizeof(
char);
1761 memcpy(rtn[j].short_name, txt, txt_len);
1764 txt = GDALGetDriverLongName(drv);
1765 txt_len = strlen(txt);
1767 txt_len = (txt_len + 1) *
sizeof(
char);
1769 memcpy(rtn[j].long_name, txt, txt_len);
1772 txt = GDALGetDriverCreationOptionList(drv);
1773 txt_len = strlen(txt);
1775 txt_len = (txt_len + 1) *
sizeof(
char);
1777 memcpy(rtn[j].create_options, txt, txt_len);
1813 int *excludeNodataValues,
1815 GDALDriverH *rtn_drv,
int *destroy_rtn_drv
1817 GDALDriverH drv = NULL;
1818 GDALDatasetH
ds = NULL;
1819 double gt[6] = {0.0};
1821 GDALDataType gdal_pt = GDT_Unknown;
1822 GDALRasterBandH
band;
1824 char *pszDataPointer;
1825 char szGDALOption[50];
1826 char *apszOptions[4];
1827 double nodata = 0.0;
1828 int allocBandNums = 0;
1829 int allocNodataValues = 0;
1838 assert(NULL != raster);
1839 assert(NULL != rtn_drv);
1840 assert(NULL != destroy_rtn_drv);
1842 *destroy_rtn_drv = 0;
1848 *destroy_rtn_drv = 1;
1850 drv = GDALGetDriverByName(
"MEM");
1852 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1858 if (*destroy_rtn_drv) {
1860 GDALDeregisterDriver(drv);
1871 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1877 cplerr = GDALSetGeoTransform(ds, gt);
1878 if (cplerr != CE_None) {
1879 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1885 if (NULL != srs && strlen(srs)) {
1888 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1893 cplerr = GDALSetProjection(ds, _srs);
1895 if (cplerr != CE_None) {
1896 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1900 RASTER_DEBUGF(3,
"Projection set to: %s", GDALGetProjectionRef(ds));
1905 if (NULL != bandNums && count > 0) {
1906 for (i = 0; i <
count; i++) {
1907 if (bandNums[i] >= numBands) {
1908 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1917 if (NULL == bandNums) {
1918 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1923 for (i = 0; i <
count; i++) bandNums[i] = i;
1927 if (NULL == excludeNodataValues) {
1928 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1929 if (NULL == excludeNodataValues) {
1930 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1934 allocNodataValues = 1;
1935 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1939 for (i = 0; i <
count; i++) {
1941 if (NULL == rtband) {
1942 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1944 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1951 if (gdal_pt == GDT_Unknown)
1952 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1961 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1962 sprintf(pszDataPointer,
"%p", pVoid);
1963 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1966 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1967 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1969 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1971 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1973 apszOptions[0] = szGDALOption;
1974 apszOptions[1] = NULL;
1975 apszOptions[2] = NULL;
1976 apszOptions[3] = NULL;
1982 if (GDALAddBand(ds, gdal_pt, apszOptions) == CE_Failure) {
1983 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1995 if (GDALAddBand(ds, gdal_pt, NULL) == CE_Failure) {
1996 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1998 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2005 if (GDALGetRasterCount(ds) != i + 1) {
2006 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2008 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2015 band = GDALGetRasterBand(ds, i + 1);
2017 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2019 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2026 int nXBlocks, nYBlocks;
2027 int nXBlockSize, nYBlockSize;
2028 int iXBlock, iYBlock;
2029 int nXValid, nYValid;
2035 int16_t *values = NULL;
2039 GDALGetBlockSize(band, &nXBlockSize, &nYBlockSize);
2040 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2041 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2042 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2043 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2048 if (NULL == values) {
2049 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2051 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2056 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2057 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2058 memset(values, 0, valueslen);
2060 x = iXBlock * nXBlockSize;
2061 y = iYBlock * nYBlockSize;
2062 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2066 if ((iXBlock + 1) * nXBlockSize > width)
2067 nXValid = width - (iXBlock * nXBlockSize);
2069 nXValid = nXBlockSize;
2072 if ((iYBlock + 1) * nYBlockSize > height)
2073 nYValid = height - (iYBlock * nYBlockSize);
2075 nYValid = nYBlockSize;
2077 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2081 iYMax = y + nYValid;
2082 iXMax = x + nXValid;
2083 for (iY = y; iY < iYMax; iY++) {
2084 for (iX = x; iX < iXMax; iX++) {
2086 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2089 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2103 values, nXValid, nYValid,
2107 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2110 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2123 if (GDALSetRasterNoDataValue(band, nodata) != CE_None)
2124 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2125 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(band, NULL));
2128 #if POSTGIS_DEBUG_LEVEL > 3 2130 GDALRasterBandH _grb = NULL;
2136 _grb = GDALGetRasterBand(ds, i + 1);
2137 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2138 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2148 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2173 char *authname = NULL;
2174 char *authcode = NULL;
2176 GDALRasterBandH gdband = NULL;
2177 GDALDataType gdpixtype = GDT_Unknown;
2188 int nXBlocks, nYBlocks;
2189 int nXBlockSize, nYBlockSize;
2190 int iXBlock, iYBlock;
2191 int nXValid, nYValid;
2201 width = GDALGetRasterXSize(ds);
2202 height = GDALGetRasterYSize(ds);
2203 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2209 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2215 cplerr = GDALGetGeoTransform(ds, gt);
2216 if (GDALGetGeoTransform(ds, gt) != CE_None) {
2217 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2229 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2230 gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
2236 strcmp(authname,
"EPSG") == 0 &&
2243 if (authname != NULL)
2245 if (authcode != NULL)
2249 numBands = GDALGetRasterCount(ds);
2251 #if POSTGIS_DEBUG_LEVEL > 3 2252 for (i = 1; i <= numBands; i++) {
2253 GDALRasterBandH _grb = NULL;
2259 _grb = GDALGetRasterBand(ds, i);
2260 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2261 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2266 for (i = 1; i <= numBands; i++) {
2269 gdband = GDALGetRasterBand(ds, i);
2270 if (NULL == gdband) {
2271 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2278 gdpixtype = GDALGetRasterDataType(gdband);
2279 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2282 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2289 width = GDALGetRasterBandXSize(gdband);
2290 height = GDALGetRasterBandYSize(gdband);
2291 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2294 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2295 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2300 (hasnodata ? nodataval : 0),
2304 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2312 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2313 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2314 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2315 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2316 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2319 valueslen = ptlen * nXBlockSize * nYBlockSize;
2321 if (values == NULL) {
2322 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2326 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2328 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2329 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2330 x = iXBlock * nXBlockSize;
2331 y = iYBlock * nYBlockSize;
2332 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2335 memset(values, 0, valueslen);
2338 if ((iXBlock + 1) * nXBlockSize > width)
2339 nXValid = width - (iXBlock * nXBlockSize);
2341 nXValid = nXBlockSize;
2344 if ((iYBlock + 1) * nYBlockSize > height)
2345 nYValid = height - (iYBlock * nYBlockSize);
2347 nYValid = nYBlockSize;
2349 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2351 cplerr = GDALRasterIO(
2355 values, nXValid, nYValid,
2359 if (cplerr != CE_None) {
2360 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2367 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2369 y = nYBlockSize * iYBlock;
2371 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels", x, y, nXValid * nYValid);
2376 x = nXBlockSize * iXBlock;
2377 for (iY = 0; iY < nYValid; iY++) {
2378 y = iY + (nYBlockSize * iYBlock);
2380 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels", x, y, nXValid);
2382 ptr += (nXValid * ptlen);
2415 static _rti_rasterize_arg
2417 _rti_rasterize_arg arg = NULL;
2421 rterror(
"_rti_rasterize_arg_init: Could not allocate memory for _rti_rasterize_arg");
2446 if (arg->
init != NULL)
2452 if (arg->
value != NULL)
2460 OSRDestroySpatialReference(arg->
src_sr);
2493 const unsigned char *wkb,
uint32_t wkb_len,
2498 int *width,
int *height,
2499 double *scale_x,
double *scale_y,
2500 double *ul_xw,
double *ul_yw,
2501 double *grid_xw,
double *grid_yw,
2502 double *skew_x,
double *skew_y,
2509 _rti_rasterize_arg arg = NULL;
2512 double _scale[2] = {0};
2513 double _skew[2] = {0};
2516 OGRGeometryH src_geom;
2517 OGREnvelope src_env;
2519 OGRwkbGeometryType wkbtype = wkbUnknown;
2524 double _gt[6] = {0};
2525 GDALDriverH _drv = NULL;
2527 GDALDatasetH _ds = NULL;
2528 GDALRasterBandH _band = NULL;
2530 uint16_t _width = 0;
2531 uint16_t _height = 0;
2535 assert(NULL != wkb);
2536 assert(0 != wkb_len);
2541 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2546 if (num_bands < 1) {
2577 if (NULL != srs && strlen(srs)) {
2578 arg->
src_sr = OSRNewSpatialReference(NULL);
2579 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2580 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2587 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2588 if (ogrerr != OGRERR_NONE) {
2589 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2598 if (OGR_G_IsEmpty(src_geom)) {
2599 rtinfo(
"Geometry provided is empty. Returning empty raster");
2601 OGR_G_DestroyGeometry(src_geom);
2609 OGR_G_GetEnvelope(src_geom, &src_env);
2612 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2617 (NULL != scale_x) &&
2618 (NULL != scale_y) &&
2623 _scale[0] = fabs(*scale_x);
2624 _scale[1] = fabs(*scale_y);
2633 _dim[0] = abs(*width);
2634 _dim[1] = abs(*height);
2637 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2642 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2647 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2649 OGR_G_DestroyGeometry(src_geom);
2655 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2659 if (NULL != skew_x) {
2673 if (NULL != skew_y) {
2695 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2697 (wkbtype == wkbPoint) ||
2698 (wkbtype == wkbMultiPoint) ||
2699 (wkbtype == wkbLineString) ||
2700 (wkbtype == wkbMultiLineString)
2708 GEOSGeometry *egeom = NULL;
2709 GEOSGeometry *geom = NULL;
2711 RASTER_DEBUG(3,
"Testing geometry is properly contained by extent");
2724 if (epoly == NULL) {
2725 rterror(
"rt_raster_gdal_rasterize: Could not create envelope's geometry to test if geometry is properly contained by extent");
2727 OGR_G_DestroyGeometry(src_geom);
2743 result = GEOSRelatePattern(egeom, geom,
"T**FF*FF*");
2744 GEOSGeom_destroy(geom);
2745 GEOSGeom_destroy(egeom);
2748 rterror(
"rt_raster_gdal_rasterize: Could not test if geometry is properly contained by extent for geometry within extent");
2750 OGR_G_DestroyGeometry(src_geom);
2760 #if POSTGIS_GDAL_VERSION > 18 2764 (NULL == ul_xw && NULL == ul_yw) &&
2765 (NULL != grid_xw && NULL != grid_xw) &&
2769 RASTER_DEBUG(3,
"Skipping extent adjustment on X-axis due to upcoming alignment");
2772 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2773 extent.
MinX -= (_scale[0] / 2.);
2774 extent.
MaxX += (_scale[0] / 2.);
2779 (NULL == ul_xw && NULL == ul_yw) &&
2780 (NULL != grid_xw && NULL != grid_xw) &&
2784 RASTER_DEBUG(3,
"Skipping extent adjustment on Y-axis due to upcoming alignment");
2787 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2788 extent.
MinY -= (_scale[1] / 2.);
2789 extent.
MaxY += (_scale[1] / 2.);
2796 (NULL == ul_xw && NULL == ul_yw) &&
2797 (NULL != grid_xw && NULL != grid_xw) &&
2801 RASTER_DEBUG(3,
"Skipping extent adjustment on X-axis due to upcoming alignment");
2804 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2805 extent.
MinX -= _scale[0];
2806 extent.
MaxX += _scale[0];
2812 (NULL == ul_xw && NULL == ul_yw) &&
2813 (NULL != grid_xw && NULL != grid_xw) &&
2817 RASTER_DEBUG(3,
"Skipping extent adjustment on Y-axis due to upcoming alignment");
2820 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2821 extent.
MinY -= _scale[1];
2822 extent.
MaxY += _scale[1];
2851 if (skewedrast == NULL) {
2852 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2854 OGR_G_DestroyGeometry(src_geom);
2861 _dim[0] = skewedrast->
width;
2862 _dim[1] = skewedrast->
height;
2872 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2874 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2879 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2881 OGR_G_DestroyGeometry(src_geom);
2894 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2895 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2896 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2906 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2914 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2915 ((NULL == ul_xw) && (NULL != ul_yw))
2917 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2920 OGR_G_DestroyGeometry(src_geom);
2930 (NULL != grid_xw) || (NULL != grid_yw)
2935 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2936 ((NULL == grid_xw) && (NULL != grid_yw))
2938 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2941 OGR_G_DestroyGeometry(src_geom);
2948 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2956 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2971 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2974 OGR_G_DestroyGeometry(src_geom);
2987 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2990 OGR_G_DestroyGeometry(src_geom);
3001 else if (NULL == scale_x) {
3013 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3016 OGR_G_DestroyGeometry(src_geom);
3023 rast->
scaleX = fabs((_c[0] - _w[0]) / ((
double) rast->
width));
3029 else if (NULL == scale_y) {
3041 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3044 OGR_G_DestroyGeometry(src_geom);
3051 rast->
scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double) rast->
height));
3065 _dim[0] = rast->
width;
3071 (NULL != scale_x) && (*scale_x < 0.)
3073 (NULL != scale_y) && (*scale_y > 0)
3079 (NULL != scale_x) &&
3090 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3093 OGR_G_DestroyGeometry(src_geom);
3104 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0))
3109 (NULL != scale_y) &&
3120 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3123 OGR_G_DestroyGeometry(src_geom);
3134 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0))
3142 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3143 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3144 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3153 _drv = GDALGetDriverByName(
"MEM");
3155 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3157 OGR_G_DestroyGeometry(src_geom);
3167 GDALDeregisterDriver(_drv);
3170 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3172 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3174 OGR_G_DestroyGeometry(src_geom);
3177 if (unload_drv) GDALDestroyDriver(_drv);
3183 cplerr = GDALSetGeoTransform(_ds, _gt);
3184 if (cplerr != CE_None) {
3185 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3187 OGR_G_DestroyGeometry(src_geom);
3192 if (unload_drv) GDALDestroyDriver(_drv);
3198 if (NULL != arg->
src_sr) {
3200 OSRExportToWkt(arg->
src_sr, &_srs);
3202 cplerr = GDALSetProjection(_ds, _srs);
3204 if (cplerr != CE_None) {
3205 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3207 OGR_G_DestroyGeometry(src_geom);
3212 if (unload_drv) GDALDestroyDriver(_drv);
3219 for (i = 0; i < arg->
numbands; i++) {
3225 if (cplerr != CE_None) {
3226 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3231 _band = GDALGetRasterBand(_ds, i + 1);
3232 if (NULL == _band) {
3233 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3241 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3242 if (cplerr != CE_None) {
3243 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3247 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3251 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3252 if (cplerr != CE_None) {
3253 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3262 OGR_G_DestroyGeometry(src_geom);
3268 if (unload_drv) GDALDestroyDriver(_drv);
3278 cplerr = GDALRasterizeGeometries(
3287 if (cplerr != CE_None) {
3288 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3290 OGR_G_DestroyGeometry(src_geom);
3295 if (unload_drv) GDALDestroyDriver(_drv);
3301 GDALFlushCache(_ds);
3305 OGR_G_DestroyGeometry(src_geom);
3309 if (unload_drv) GDALDestroyDriver(_drv);
3312 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3321 for (i = 0; i < arg->
numbands; i++) {
3329 double nodataval = 0;
3334 if (oldband == NULL) {
3335 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3353 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3364 hasnodata, nodataval,
3368 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3379 for (x = 0; x < _width; x++) {
3380 for (y = 0; y < _height; y++) {
3383 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3395 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3406 if (oldband == NULL) {
3407 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3452 double _offset[2][4] = {{0.}};
3453 uint16_t _dim[2][2] = {{0}};
3458 double gt[6] = {0.};
3460 assert(NULL != rast1);
3461 assert(NULL != rast2);
3462 assert(NULL != rtnraster);
3469 rterror(
"rt_raster_from_two_rasters: Could not test for alignment on the two rasters");
3473 rterror(
"rt_raster_from_two_rasters: The two rasters provided do not have the same alignment");
3478 _dim[0][0] = rast1->
width;
3479 _dim[0][1] = rast1->
height;
3480 _dim[1][0] = rast2->width;
3481 _dim[1][1] = rast2->height;
3486 _rast[0]->ipX, _rast[0]->ipY,
3487 &(_offset[1][0]), &(_offset[1][1]),
3490 rterror(
"rt_raster_from_two_rasters: Could not compute offsets of the second raster relative to the first raster");
3493 _offset[1][0] = -1 * _offset[1][0];
3494 _offset[1][1] = -1 * _offset[1][1];
3495 _offset[1][2] = _offset[1][0] + _dim[1][0] - 1;
3496 _offset[1][3] = _offset[1][1] + _dim[1][1] - 1;
3499 switch (extenttype) {
3508 _offset[0][0] = -1 * _offset[1][0];
3509 _offset[0][1] = -1 * _offset[1][1];
3514 dim[0] = _dim[i][0];
3515 dim[1] = _dim[i][1];
3520 if (raster == NULL) {
3521 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3529 double off[4] = {0};
3543 if (_offset[1][0] < 0)
3544 off[0] = _offset[1][0];
3546 if (_offset[1][1] < 0)
3547 off[1] = _offset[1][1];
3550 off[2] = _dim[0][0] - 1;
3551 if ((
int) _offset[1][2] >= _dim[0][0])
3552 off[2] = _offset[1][2];
3553 off[3] = _dim[0][1] - 1;
3554 if ((
int) _offset[1][3] >= _dim[0][1])
3555 off[3] = _offset[1][3];
3564 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3568 dim[0] = off[2] - off[0] + 1;
3569 dim[1] = off[3] - off[1] + 1;
3582 if (raster == NULL) {
3583 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3601 &(_offset[0][0]), &(_offset[0][1]),
3604 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the FIRST raster relative to the output raster");
3608 _offset[0][0] *= -1;
3609 _offset[0][1] *= -1;
3614 &(_offset[1][0]), &(_offset[1][1]),
3617 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the SECOND raster relative to the output raster");
3621 _offset[1][0] *= -1;
3622 _offset[1][1] *= -1;
3626 double off[4] = {0};
3630 (_offset[1][2] < 0 || _offset[1][0] > (_dim[0][0] - 1)) ||
3631 (_offset[1][3] < 0 || _offset[1][1] > (_dim[0][1] - 1))
3633 RASTER_DEBUG(3,
"The two rasters provided have no intersection. Returning no band raster");
3636 if (raster == NULL) {
3637 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3644 if (NULL != offset) {
3645 for (i = 0; i < 4; i++)
3646 offset[i] = _offset[i / 2][i % 2];
3653 if (_offset[1][0] > 0)
3654 off[0] = _offset[1][0];
3655 if (_offset[1][1] > 0)
3656 off[1] = _offset[1][1];
3658 off[2] = _dim[0][0] - 1;
3659 if (_offset[1][2] < _dim[0][0])
3660 off[2] = _offset[1][2];
3661 off[3] = _dim[0][1] - 1;
3662 if (_offset[1][3] < _dim[0][1])
3663 off[3] = _offset[1][3];
3665 dim[0] = off[2] - off[0] + 1;
3666 dim[1] = off[3] - off[1] + 1;
3671 if (raster == NULL) {
3672 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3685 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3696 &(_offset[0][0]), &(_offset[0][1]),
3699 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the FIRST raster relative to the output raster");
3703 _offset[0][0] *= -1;
3704 _offset[0][1] *= -1;
3709 &(_offset[1][0]), &(_offset[1][1]),
3712 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the SECOND raster relative to the output raster");
3716 _offset[1][0] *= -1;
3717 _offset[1][1] *= -1;
3721 rterror(
"rt_raster_from_two_rasters: Extent type ET_CUSTOM is not supported by this function");
3726 if (NULL != offset) {
3727 for (i = 0; i < 4; i++)
3728 offset[i] = _offset[i / 2][i % 2];
void rt_raster_get_phys_params(rt_raster rast, double *i_mag, double *j_mag, double *theta_i, double *theta_ij)
Calculates and returns the physically significant descriptors embodied in the geotransform attached t...
int32_t rt_util_clamp_to_32BSI(double value)
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
void rt_raster_set_phys_params(rt_raster rast, double i_mag, double j_mag, double theta_i, double theta_ij)
Calculates the geotransform coefficients and applies them to the supplied raster. ...
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.
rt_errorstate rt_raster_get_inverse_geotransform_matrix(rt_raster raster, double *gt, double *igt)
Get 6-element array of raster inverse geotransform matrix.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
uint32_t rt_util_clamp_to_32BUI(double value)
int rt_util_gdal_register_all(int force_register_all)
struct _rti_rasterize_arg_t * _rti_rasterize_arg
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Datum covers(PG_FUNCTION_ARGS)
rt_errorstate rt_raster_same_alignment(rt_raster rast1, rt_raster rast2, int *aligned, char **reason)
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
struct rt_raster_t * rt_raster
Types definitions.
void lwgeom_free(LWGEOM *geom)
struct rt_gdaldriver_t * rt_gdaldriver
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
int rt_raster_get_num_bands(rt_raster raster)
int rt_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
void * rtrealloc(void *mem, size_t size)
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope ext)
OGRSpatialReferenceH src_sr
rt_errorstate
Enum definitions.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr raster point.
rt_raster rt_raster_compute_skewed_raster(rt_envelope extent, double *skew, double *scale, double tolerance)
void rt_band_destroy(rt_band band)
Destroy a raster band.
static _rti_rasterize_arg _rti_rasterize_arg_init()
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
#define LW_PARSER_CHECK_NONE
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
uint16_t rt_util_clamp_to_16BUI(double value)
static void _rti_rasterize_arg_destroy(_rti_rasterize_arg arg)
void rt_band_set_ownsdata_flag(rt_band band, int flag)
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
int rt_util_gdal_driver_registered(const char *drv)
void lwgeom_geos_error(const char *fmt,...)
void rtwarn(const char *fmt,...)
void lwpoly_free(LWPOLY *poly)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_raster_copy_band(rt_raster torast, rt_raster fromrast, int fromindex, int toindex)
Copy one band from one raster to another.
#define SRID_UNKNOWN
Unknown SRID value.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
int8_t rt_util_clamp_to_8BSI(double value)
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
void rtinfo(const char *fmt,...)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
#define RASTER_DEBUGF(level, msg,...)
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
static void _rt_raster_geotransform_warn_offline_band(rt_raster raster)
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
uint8_t rt_util_clamp_to_4BUI(double value)
uint8_t rt_util_clamp_to_8BUI(double value)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
uint8_t rt_util_clamp_to_1BB(double value)
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
void rtdealloc(void *mem)
void rt_raster_calc_phys_params(double xscale, double xskew, double yskew, double yscale, double *i_mag, double *j_mag, double *theta_i, double *theta_ij)
Calculates the physically significant descriptors embodied in an arbitrary geotransform.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
#define RASTER_DEBUG(level, msg)
rt_band rt_band_duplicate(rt_band band)
Create a new band duplicated from source band.
This library is the generic raster handling section of PostGIS.
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
int rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
Generate a new inline band and add it to a raster.
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)
uint8_t * rt_raster_to_gdal(rt_raster raster, const char *srs, char *format, char **options, uint64_t *gdalsize)
Return formatted GDAL raster from raster.
uint8_t rt_util_clamp_to_2BUI(double value)
float rt_util_clamp_to_32F(double value)
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
rt_raster rt_raster_from_band(rt_raster raster, uint32_t *bandNums, int count)
Construct a new rt_raster from an existing rt_raster and an array of band numbers.
int rt_raster_calc_gt_coeff(double i_mag, double j_mag, double theta_i, double theta_ij, double *xscale, double *xskew, double *yskew, double *yscale)
Calculates the coefficients of a geotransform given the physically significant parameters describing ...
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
uint16_t rt_raster_get_height(rt_raster raster)
uint16_t rt_raster_get_width(rt_raster raster)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
int16_t rt_util_clamp_to_16BSI(double value)