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");
139 double scaleX,
double scaleY
170 double skewX,
double skewY
232 double *i_mag,
double *j_mag,
double *theta_i,
double *theta_ij)
234 double o11, o12, o21, o22 ;
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 ;
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 ;
384 if (n >=
raster->numBands || n < 0)
412 assert(NULL !=
band);
417 rterror(
"rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
422 if (index >
raster->numBands)
438 if (NULL ==
raster->bands) {
439 rterror(
"rt_raster_add_band: Out of virtual memory "
440 "reallocating band pointers");
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;
499 uint32_t checkvaluint = 0;
500 double checkvaldouble = 0;
501 float checkvalfloat = 0;
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");
757 double xr,
double yr,
758 double *xw,
double *yw,
764 assert(NULL != xw && NULL != yw);
767 memcpy(_gt,
gt,
sizeof(
double) * 6);
784 GDALApplyGeoTransform(_gt, xr, yr, xw, yw);
785 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (c -> g) for (%f, %f) = (%f, %f)",
806 double xw,
double yw,
807 double *xr,
double *yr,
810 double _igt[6] = {0};
814 assert(NULL != xr && NULL != yr);
817 memcpy(_igt, igt,
sizeof(
double) * 6);
829 rterror(
"rt_raster_geopoint_to_cell: Could not get inverse geotransform matrix");
834 GDALApplyGeoTransform(_igt, xw, yw, xr, yr);
835 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
850 RASTER_DEBUGF(4,
"Corrected GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
880 double _gt[6] = {0.};
887 for (i = 0; i < 4; i++) {
914 rterror(
"rt_raster_get_envelope: Could not compute spatial coordinates for raster pixel");
926 if (_w[0] < env->
MinX)
928 else if (_w[0] > env->
MaxX)
931 if (_w[1] < env->
MinY)
933 else if (_w[1] > env->
MaxY)
965 uint32_t max_run = 1;
972 double _igt[6] = {0};
985 GEOSGeometry *sgeom = NULL;
986 GEOSGeometry *ngeom = NULL;
994 else if (tolerance > 1.)
998 while (dbl_run < 10) {
1006 for (i = 0; i < 2; i++) {
1007 if (
FLT_EQ(scale[i], 0.0))
1009 rterror(
"rt_raster_compute_skewed_raster: Scale cannot be zero");
1014 _gt[1] = fabs(scale[i] * tolerance);
1016 _gt[5] = fabs(scale[i] * tolerance);
1022 if ((skew == NULL) || (
FLT_EQ(skew[0], 0.0) &&
FLT_EQ(skew[1], 0.0)))
1025 (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (fabs(scale[0]) / 2.)) / fabs(scale[0]), 1),
1026 (
int) fmax((fabs(extent.
MaxY - extent.
MinY) + (fabs(scale[1]) / 2.)) / fabs(scale[1]), 1)
1031 rterror(
"rt_raster_compute_skewed_raster: Could not create output raster");
1050 _gt[2] = skew[0] * tolerance;
1052 _gt[4] = skew[1] * tolerance;
1054 RASTER_DEBUGF(4,
"Initial geotransform: %f, %f, %f, %f, %f, %f",
1055 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1061 rterror(
"rt_raster_compute_skewed_raster: Out of memory allocating extent raster");
1067 if (!GDALInvGeoTransform(_gt, _igt)) {
1068 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1072 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1073 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1077 for (i = 0; i < 2; i++) {
1081 RASTER_DEBUGF(3,
"Shifting along %s axis", i < 1 ?
"X" :
"Y");
1086 if (run > max_run) {
1087 rterror(
"rt_raster_compute_skewed_raster: Could not compute skewed extent due to check preventing infinite loop");
1096 for (j = 0; j < 4; j++) {
1100 _xy[0] = extent.
MinX;
1101 _xy[1] = extent.
MaxY;
1105 _xy[0] = extent.
MinX;
1106 _xy[1] = extent.
MinY;
1110 _xy[0] = extent.
MaxX;
1111 _xy[1] = extent.
MinY;
1115 _xy[0] = extent.
MaxX;
1116 _xy[1] = extent.
MaxY;
1127 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1132 RASTER_DEBUGF(4,
"Point %d at cell %d x %d", j, (
int) _r[0], (
int) _r[1]);
1135 if ((
int) _r[i] < 0) {
1139 if (_dlastpos != j) {
1140 _dlast = (int) _r[i];
1143 else if ((
int) _r[i] < _dlast) {
1144 RASTER_DEBUG(4,
"Point going in wrong direction. Reversing direction");
1160 x = _d[i] * fabs(_r[i]);
1162 y = _d[i] * fabs(_r[i]);
1171 rterror(
"rt_raster_compute_skewed_raster: Could not compute spatial coordinates for raster pixel");
1182 RASTER_DEBUGF(4,
"Shifted geotransform: %f, %f, %f, %f, %f, %f",
1183 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1187 if (!GDALInvGeoTransform(_gt, _igt)) {
1188 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1192 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1193 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1210 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1215 RASTER_DEBUGF(4,
"geopoint %f x %f at cell %d x %d", extent.
MaxX, extent.
MinY, (
int) _r[0], (
int) _r[1]);
1226 if (npoly == NULL) {
1227 rterror(
"rt_raster_compute_skewed_raster: Could not build extent's geometry for covers test");
1241 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for covers test");
1242 GEOSGeom_destroy(ngeom);
1250 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1251 GEOSGeom_destroy(sgeom);
1254 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test");
1255 GEOSGeom_destroy(ngeom);
1270 raster->width = (int) ((((
double)
raster->width) * fabs(_gt[1]) + fabs(scale[0] / 2.)) / fabs(scale[0]));
1271 raster->height = (int) ((((
double)
raster->height) * fabs(_gt[5]) + fabs(scale[1] / 2.)) / fabs(scale[1]));
1272 _gt[1] = fabs(scale[0]);
1273 _gt[5] = -1 * fabs(scale[1]);
1279 for (i = 0; i < 2; i++) {
1289 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for minimizing dimensions");
1290 GEOSGeom_destroy(ngeom);
1298 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1299 GEOSGeom_destroy(sgeom);
1302 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test for minimizing dimensions");
1303 GEOSGeom_destroy(ngeom);
1316 GEOSGeom_destroy(ngeom);
1367 int fromindex,
int toindex
1372 assert(NULL != torast);
1373 assert(NULL != fromrast);
1377 rtwarn(
"rt_raster_copy_band: Attempting to add a band with different width or height");
1383 rtwarn(
"rt_raster_copy_band: Second raster has no band");
1386 else if (fromindex < 0) {
1387 rtwarn(
"rt_raster_copy_band: Band index for second raster < 0. Defaulted to 0");
1390 else if (fromindex >= fromrast->
numBands) {
1391 rtwarn(
"rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex, fromrast->
numBands - 1);
1392 fromindex = fromrast->
numBands - 1;
1396 rtwarn(
"rt_raster_copy_band: Band index for first raster < 0. Defaulted to 0");
1399 else if (toindex > torast->
numBands) {
1400 rtwarn(
"rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex, torast->
numBands);
1436 double gt[6] = {0.};
1439 assert(NULL != bandNums);
1441 RASTER_DEBUGF(3,
"rt_raster_from_band: source raster has %d bands",
1447 rterror(
"rt_raster_from_band: Out of memory allocating new raster");
1459 for (i = 0; i <
count; i++) {
1464 rterror(
"rt_raster_from_band: Could not copy band");
1470 RASTER_DEBUGF(3,
"rt_raster_from_band: band created at index %d",
1474 RASTER_DEBUGF(3,
"rt_raster_from_band: new raster has %d bands",
1496 assert(NULL !=
band);
1499 rterror(
"rt_raster_replace_band: Band does not match raster's dimensions: %dx%d band to %dx%d raster",
1504 if (index >=
raster->numBands || index < 0) {
1505 rterror(
"rt_raster_replace_band: Band index is not valid");
1510 RASTER_DEBUGF(3,
"rt_raster_replace_band: old band at %p", oldband);
1543 uint32_t *
nband = NULL;
1547 if (
nband == NULL) {
1548 rterror(
"rt_raster_clone: Could not allocate memory for deep clone");
1551 for (i = 0; i < numband; i++)
1565 rterror(
"rt_raster_clone: Could not create cloned raster");
1595 char *format,
char **options, uint64_t *gdalsize
1600 GDALDriverH src_drv = NULL;
1601 int destroy_src_drv = 0;
1602 GDALDatasetH
src_ds = NULL;
1604 vsi_l_offset rtn_lenvsi;
1605 uint64_t rtn_len = 0;
1607 GDALDriverH rtn_drv = NULL;
1608 GDALDatasetH rtn_ds = NULL;
1609 uint8_t *rtn = NULL;
1612 assert(NULL != gdalsize);
1619 if (format == NULL || !strlen(format))
1626 rterror(
"rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1631 rtn_drv = GDALGetDriverByName(format);
1632 if (NULL == rtn_drv) {
1633 rterror(
"rt_raster_to_gdal: Could not load the output GDAL driver");
1635 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1641 cc = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_CREATECOPY, NULL);
1643 vio = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_VIRTUALIO, NULL);
1645 if (cc == NULL || vio == NULL) {
1646 rterror(
"rt_raster_to_gdal: Output GDAL driver does not support CreateCopy and/or VirtualIO");
1648 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1653 RASTER_DEBUG(3,
"Copying GDAL MEM raster to memory file in output format");
1654 rtn_ds = GDALCreateCopy(
1666 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1669 if (NULL == rtn_ds) {
1670 rterror(
"rt_raster_to_gdal: Could not create the output GDAL dataset");
1674 RASTER_DEBUGF(4,
"dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1680 RASTER_DEBUG(3,
"Done copying GDAL MEM raster to memory file in output format");
1684 rtn = VSIGetMemFileBuffer(
"/vsimem/out.dat", &rtn_lenvsi,
TRUE);
1685 RASTER_DEBUG(3,
"Done copying GDAL memory file to buffer");
1687 rterror(
"rt_raster_to_gdal: Could not create the output GDAL raster");
1691 rtn_len = (uint64_t) rtn_lenvsi;
1692 *gdalsize = rtn_len;
1716 GDALDriverH *drv = NULL;
1722 assert(drv_count != NULL);
1725 count = GDALGetDriverCount();
1730 rterror(
"rt_raster_gdal_drivers: Could not allocate memory for gdaldriver structure");
1734 for (i = 0, j = 0; i <
count; i++) {
1735 drv = GDALGetDriver(i);
1737 #ifdef GDAL_DCAP_RASTER
1740 const char *is_raster;
1741 is_raster = GDALGetMetadataItem(drv, GDAL_DCAP_RASTER, NULL);
1742 if (is_raster == NULL || !EQUAL(is_raster,
"YES"))
1747 cc = GDALGetMetadataItem(drv, GDAL_DCAP_CREATECOPY, NULL);
1750 vio = GDALGetMetadataItem(drv, GDAL_DCAP_VIRTUALIO, NULL);
1752 if (can_write && (cc == NULL || vio == NULL))
1758 rtn[j].
can_write = (cc != NULL && vio != NULL);
1760 if (rtn[j].can_write) {
1761 RASTER_DEBUGF(3,
"driver %s (%d) supports CreateCopy() and VirtualIO()", txt, i);
1768 txt = GDALGetDriverShortName(drv);
1769 txt_len = strlen(txt);
1771 txt_len = (txt_len + 1) *
sizeof(
char);
1773 memcpy(rtn[j].short_name, txt, txt_len);
1776 txt = GDALGetDriverLongName(drv);
1777 txt_len = strlen(txt);
1779 txt_len = (txt_len + 1) *
sizeof(
char);
1781 memcpy(rtn[j].long_name, txt, txt_len);
1784 txt = GDALGetDriverCreationOptionList(drv);
1785 txt_len = strlen(txt);
1787 txt_len = (txt_len + 1) *
sizeof(
char);
1789 memcpy(rtn[j].create_options, txt, txt_len);
1825 int *excludeNodataValues,
1827 GDALDriverH *rtn_drv,
int *destroy_rtn_drv
1829 GDALDriverH drv = NULL;
1830 GDALDatasetH
ds = NULL;
1831 double gt[6] = {0.0};
1833 GDALDataType gdal_pt = GDT_Unknown;
1834 GDALRasterBandH
band;
1836 char *pszDataPointer;
1837 char szGDALOption[50];
1838 char *apszOptions[4];
1839 double nodata = 0.0;
1840 int allocBandNums = 0;
1841 int allocNodataValues = 0;
1846 uint32_t height = 0;
1851 assert(NULL != rtn_drv);
1852 assert(NULL != destroy_rtn_drv);
1854 *destroy_rtn_drv = 0;
1860 *destroy_rtn_drv = 1;
1862 drv = GDALGetDriverByName(
"MEM");
1864 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1870 if (*destroy_rtn_drv) {
1872 GDALDeregisterDriver(drv);
1883 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1889 cplerr = GDALSetGeoTransform(
ds,
gt);
1890 if (cplerr != CE_None) {
1891 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1897 if (NULL != srs && strlen(srs)) {
1900 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1905 cplerr = GDALSetProjection(
ds, _srs);
1907 if (cplerr != CE_None) {
1908 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1917 if (NULL != bandNums &&
count > 0) {
1918 for (i = 0; i <
count; i++) {
1919 if (bandNums[i] >= numBands) {
1920 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1928 bandNums = (uint32_t *)
rtalloc(
sizeof(uint32_t) *
count);
1929 if (NULL == bandNums) {
1930 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1935 for (i = 0; i <
count; i++) bandNums[i] = i;
1939 if (NULL == excludeNodataValues) {
1940 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1941 if (NULL == excludeNodataValues) {
1942 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1946 allocNodataValues = 1;
1947 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1951 for (i = 0; i <
count; i++) {
1953 if (NULL == rtband) {
1954 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1956 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1963 if (gdal_pt == GDT_Unknown)
1964 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1973 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1974 sprintf(pszDataPointer,
"%p", pVoid);
1975 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1978 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1979 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1981 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1983 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1985 apszOptions[0] = szGDALOption;
1986 apszOptions[1] = NULL;
1987 apszOptions[2] = NULL;
1988 apszOptions[3] = NULL;
1994 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
1995 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2007 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2008 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2010 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2017 if (GDALGetRasterCount(
ds) != i + 1) {
2018 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2020 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2027 band = GDALGetRasterBand(
ds, i + 1);
2029 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2031 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2038 uint32_t nXBlocks, nYBlocks;
2039 int nXBlockSize, nYBlockSize;
2040 uint32_t iXBlock, iYBlock;
2041 int nXValid, nYValid;
2046 uint32_t valueslen = 0;
2047 int16_t *values = NULL;
2051 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2052 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2053 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2054 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2055 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2060 if (NULL == values) {
2061 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2063 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2068 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2069 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2070 memset(values, 0, valueslen);
2072 x = iXBlock * nXBlockSize;
2073 y = iYBlock * nYBlockSize;
2074 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2078 if ((iXBlock + 1) * nXBlockSize > width)
2079 nXValid = width - (iXBlock * nXBlockSize);
2081 nXValid = nXBlockSize;
2084 if ((iYBlock + 1) * nYBlockSize > height)
2085 nYValid = height - (iYBlock * nYBlockSize);
2087 nYValid = nYBlockSize;
2089 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2093 iYMax =
y + nYValid;
2094 iXMax =
x + nXValid;
2095 for (iY =
y; iY < iYMax; iY++) {
2096 for (iX =
x; iX < iXMax; iX++) {
2098 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2101 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2115 values, nXValid, nYValid,
2119 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2122 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2135 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2136 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2137 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2140 #if POSTGIS_DEBUG_LEVEL > 3
2142 GDALRasterBandH _grb = NULL;
2148 _grb = GDALGetRasterBand(
ds, i + 1);
2149 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2150 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2160 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2182 uint32_t height = 0;
2183 uint32_t numBands = 0;
2185 char *authname = NULL;
2186 char *authcode = NULL;
2188 GDALRasterBandH gdband = NULL;
2189 GDALDataType gdpixtype = GDT_Unknown;
2200 uint32_t nXBlocks, nYBlocks;
2201 int nXBlockSize, nYBlockSize;
2202 uint32_t iXBlock, iYBlock;
2203 uint32_t nXValid, nYValid;
2206 uint8_t *values = NULL;
2207 uint32_t valueslen = 0;
2208 uint8_t *ptr = NULL;
2213 width = GDALGetRasterXSize(
ds);
2214 height = GDALGetRasterYSize(
ds);
2215 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2221 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2227 cplerr = GDALGetGeoTransform(
ds,
gt);
2228 if (GDALGetGeoTransform(
ds,
gt) != CE_None) {
2229 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2241 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2248 strcmp(authname,
"EPSG") == 0 &&
2255 if (authname != NULL)
2257 if (authcode != NULL)
2261 numBands = GDALGetRasterCount(
ds);
2263 #if POSTGIS_DEBUG_LEVEL > 3
2264 for (i = 1; i <= numBands; i++) {
2265 GDALRasterBandH _grb = NULL;
2271 _grb = GDALGetRasterBand(
ds, i);
2272 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2273 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2278 for (i = 1; i <= numBands; i++) {
2281 gdband = GDALGetRasterBand(
ds, i);
2282 if (NULL == gdband) {
2283 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2290 gdpixtype = GDALGetRasterDataType(gdband);
2291 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2294 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2301 width = GDALGetRasterBandXSize(gdband);
2302 height = GDALGetRasterBandYSize(gdband);
2303 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2306 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2307 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2312 (hasnodata ? nodataval : 0),
2316 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2324 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2325 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2326 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2327 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2328 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2331 valueslen = ptlen * nXBlockSize * nYBlockSize;
2333 if (values == NULL) {
2334 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2338 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2340 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2341 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2342 x = iXBlock * nXBlockSize;
2343 y = iYBlock * nYBlockSize;
2344 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2347 memset(values, 0, valueslen);
2350 if ((iXBlock + 1) * nXBlockSize > width)
2351 nXValid = width - (iXBlock * nXBlockSize);
2353 nXValid = nXBlockSize;
2356 if ((iYBlock + 1) * nYBlockSize > height)
2357 nYValid = height - (iYBlock * nYBlockSize);
2359 nYValid = nYBlockSize;
2361 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2363 cplerr = GDALRasterIO(
2367 values, nXValid, nYValid,
2371 if (cplerr != CE_None) {
2372 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2379 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2381 y = nYBlockSize * iYBlock;
2383 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels",
x,
y, nXValid * nYValid);
2388 x = nXBlockSize * iXBlock;
2389 for (iY = 0; iY < nYValid; iY++) {
2390 y = iY + (nYBlockSize * iYBlock);
2392 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels",
x,
y, nXValid);
2394 ptr += (nXValid * ptlen);
2433 rterror(
"_rti_rasterize_arg_init: Could not allocate memory for _rti_rasterize_arg");
2458 if (arg->
init != NULL)
2464 if (arg->
value != NULL)
2472 OSRDestroySpatialReference(arg->
src_sr);
2505 const unsigned char *wkb, uint32_t wkb_len,
2508 double *init,
double *
value,
2509 double *nodata, uint8_t *hasnodata,
2510 int *width,
int *height,
2511 double *scale_x,
double *scale_y,
2512 double *ul_xw,
double *ul_yw,
2513 double *grid_xw,
double *grid_yw,
2514 double *skew_x,
double *skew_y,
2524 double _scale[2] = {0};
2525 double _skew[2] = {0};
2528 OGRGeometryH src_geom;
2529 OGREnvelope src_env;
2531 OGRwkbGeometryType wkbtype = wkbUnknown;
2536 double _gt[6] = {0};
2537 GDALDriverH _drv = NULL;
2539 GDALDatasetH _ds = NULL;
2540 GDALRasterBandH _band = NULL;
2542 uint16_t _width = 0;
2543 uint16_t _height = 0;
2547 assert(NULL != wkb);
2548 assert(0 != wkb_len);
2553 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2558 if (num_bands < 1) {
2589 if (NULL != srs && strlen(srs)) {
2590 arg->
src_sr = OSRNewSpatialReference(NULL);
2591 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2592 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2599 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2600 if (ogrerr != OGRERR_NONE) {
2601 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2610 if (OGR_G_IsEmpty(src_geom)) {
2611 rtinfo(
"Geometry provided is empty. Returning empty raster");
2613 OGR_G_DestroyGeometry(src_geom);
2621 OGR_G_GetEnvelope(src_geom, &src_env);
2624 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2629 (NULL != scale_x) &&
2630 (NULL != scale_y) &&
2635 _scale[0] = fabs(*scale_x);
2636 _scale[1] = fabs(*scale_y);
2639 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2641 _dim[0] = abs(*width);
2642 _dim[1] = abs(*height);
2645 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2650 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2655 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2657 OGR_G_DestroyGeometry(src_geom);
2663 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2667 if (NULL != skew_x) {
2681 if (NULL != skew_y) {
2703 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2705 (wkbtype == wkbPoint) ||
2706 (wkbtype == wkbMultiPoint) ||
2707 (wkbtype == wkbLineString) ||
2708 (wkbtype == wkbMultiLineString)
2714 #if POSTGIS_GDAL_VERSION > 18
2716 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2717 extent.
MinX -= (_scale[0] / 2.);
2718 extent.
MaxX += (_scale[0] / 2.);
2720 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2721 extent.
MinY -= (_scale[1] / 2.);
2722 extent.
MaxY += (_scale[1] / 2.);
2726 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2727 extent.
MinX -= _scale[0];
2728 extent.
MaxX += _scale[0];
2730 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2731 extent.
MinY -= _scale[1];
2732 extent.
MaxY += _scale[1];
2757 if (skewedrast == NULL) {
2758 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2760 OGR_G_DestroyGeometry(src_geom);
2767 _dim[0] = skewedrast->
width;
2768 _dim[1] = skewedrast->
height;
2778 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2780 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2785 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2787 OGR_G_DestroyGeometry(src_geom);
2800 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2801 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2802 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2812 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2820 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2821 ((NULL == ul_xw) && (NULL != ul_yw))
2823 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2826 OGR_G_DestroyGeometry(src_geom);
2836 (NULL != grid_xw) || (NULL != grid_yw)
2841 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2842 ((NULL == grid_xw) && (NULL != grid_yw))
2844 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2847 OGR_G_DestroyGeometry(src_geom);
2854 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2862 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2877 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2880 OGR_G_DestroyGeometry(src_geom);
2893 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2896 OGR_G_DestroyGeometry(src_geom);
2907 else if (NULL == scale_x) {
2919 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2922 OGR_G_DestroyGeometry(src_geom);
2929 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2935 else if (NULL == scale_y) {
2947 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2950 OGR_G_DestroyGeometry(src_geom);
2957 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2971 _dim[0] =
rast->width;
2972 _dim[1] =
rast->height;
2977 (NULL != scale_x) && (*scale_x < 0.)
2979 (NULL != scale_y) && (*scale_y > 0)
2985 (NULL != scale_x) &&
2996 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2999 OGR_G_DestroyGeometry(src_geom);
3010 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3015 (NULL != scale_y) &&
3026 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3029 OGR_G_DestroyGeometry(src_geom);
3040 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3048 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3049 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3050 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3059 _drv = GDALGetDriverByName(
"MEM");
3061 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3063 OGR_G_DestroyGeometry(src_geom);
3073 GDALDeregisterDriver(_drv);
3076 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3078 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3080 OGR_G_DestroyGeometry(src_geom);
3083 if (unload_drv) GDALDestroyDriver(_drv);
3089 cplerr = GDALSetGeoTransform(_ds, _gt);
3090 if (cplerr != CE_None) {
3091 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3093 OGR_G_DestroyGeometry(src_geom);
3098 if (unload_drv) GDALDestroyDriver(_drv);
3104 if (NULL != arg->
src_sr) {
3106 OSRExportToWkt(arg->
src_sr, &_srs);
3108 cplerr = GDALSetProjection(_ds, _srs);
3110 if (cplerr != CE_None) {
3111 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3113 OGR_G_DestroyGeometry(src_geom);
3118 if (unload_drv) GDALDestroyDriver(_drv);
3125 for (i = 0; i < arg->
numbands; i++) {
3131 if (cplerr != CE_None) {
3132 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3137 _band = GDALGetRasterBand(_ds, i + 1);
3138 if (NULL == _band) {
3139 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3147 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3148 if (cplerr != CE_None) {
3149 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3153 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3157 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3158 if (cplerr != CE_None) {
3159 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3168 OGR_G_DestroyGeometry(src_geom);
3174 if (unload_drv) GDALDestroyDriver(_drv);
3184 cplerr = GDALRasterizeGeometries(
3193 if (cplerr != CE_None) {
3194 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3196 OGR_G_DestroyGeometry(src_geom);
3201 if (unload_drv) GDALDestroyDriver(_drv);
3207 GDALFlushCache(_ds);
3211 OGR_G_DestroyGeometry(src_geom);
3215 if (unload_drv) GDALDestroyDriver(_drv);
3218 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3227 for (i = 0; i < arg->
numbands; i++) {
3228 uint8_t *
data = NULL;
3235 double nodataval = 0;
3240 if (oldband == NULL) {
3241 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3259 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3270 hasnodata, nodataval,
3274 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3285 for (
x = 0;
x < _width;
x++) {
3286 for (
y = 0;
y < _height;
y++) {
3289 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3301 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3312 if (oldband == NULL) {
3313 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3358 double _offset[2][4] = {{0.}};
3359 uint16_t _dim[2][2] = {{0}};
3364 double gt[6] = {0.};
3366 assert(NULL != rast1);
3367 assert(NULL != rast2);
3368 assert(NULL != rtnraster);
3375 rterror(
"rt_raster_from_two_rasters: Could not test for alignment on the two rasters");
3379 rterror(
"rt_raster_from_two_rasters: The two rasters provided do not have the same alignment");
3384 _dim[0][0] = rast1->
width;
3385 _dim[0][1] = rast1->
height;
3386 _dim[1][0] = rast2->
width;
3387 _dim[1][1] = rast2->
height;
3392 _rast[0]->ipX, _rast[0]->ipY,
3393 &(_offset[1][0]), &(_offset[1][1]),
3396 rterror(
"rt_raster_from_two_rasters: Could not compute offsets of the second raster relative to the first raster");
3399 _offset[1][0] = -1 * _offset[1][0];
3400 _offset[1][1] = -1 * _offset[1][1];
3401 _offset[1][2] = _offset[1][0] + _dim[1][0] - 1;
3402 _offset[1][3] = _offset[1][1] + _dim[1][1] - 1;
3405 switch (extenttype) {
3415 _offset[0][0] = -1 * _offset[1][0];
3416 _offset[0][1] = -1 * _offset[1][1];
3421 dim[0] = _dim[i][0];
3422 dim[1] = _dim[i][1];
3428 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3436 double off[4] = {0};
3450 if (_offset[1][0] < 0)
3451 off[0] = _offset[1][0];
3453 if (_offset[1][1] < 0)
3454 off[1] = _offset[1][1];
3457 off[2] = _dim[0][0] - 1;
3458 if ((
int) _offset[1][2] >= _dim[0][0])
3459 off[2] = _offset[1][2];
3460 off[3] = _dim[0][1] - 1;
3461 if ((
int) _offset[1][3] >= _dim[0][1])
3462 off[3] = _offset[1][3];
3471 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3475 dim[0] = off[2] - off[0] + 1;
3476 dim[1] = off[3] - off[1] + 1;
3490 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3508 &(_offset[0][0]), &(_offset[0][1]),
3511 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the FIRST raster relative to the output raster");
3515 _offset[0][0] *= -1;
3516 _offset[0][1] *= -1;
3521 &(_offset[1][0]), &(_offset[1][1]),
3524 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the SECOND raster relative to the output raster");
3528 _offset[1][0] *= -1;
3529 _offset[1][1] *= -1;
3533 double off[4] = {0};
3537 (_offset[1][2] < 0 || _offset[1][0] > (_dim[0][0] - 1)) ||
3538 (_offset[1][3] < 0 || _offset[1][1] > (_dim[0][1] - 1))
3540 RASTER_DEBUG(3,
"The two rasters provided have no intersection. Returning no band raster");
3544 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3551 if (NULL != offset) {
3552 for (i = 0; i < 4; i++)
3553 offset[i] = _offset[i / 2][i % 2];
3560 if (_offset[1][0] > 0)
3561 off[0] = _offset[1][0];
3562 if (_offset[1][1] > 0)
3563 off[1] = _offset[1][1];
3565 off[2] = _dim[0][0] - 1;
3566 if (_offset[1][2] < _dim[0][0])
3567 off[2] = _offset[1][2];
3568 off[3] = _dim[0][1] - 1;
3569 if (_offset[1][3] < _dim[0][1])
3570 off[3] = _offset[1][3];
3572 dim[0] = off[2] - off[0] + 1;
3573 dim[1] = off[3] - off[1] + 1;
3579 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3592 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3603 &(_offset[0][0]), &(_offset[0][1]),
3606 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the FIRST raster relative to the output raster");
3610 _offset[0][0] *= -1;
3611 _offset[0][1] *= -1;
3616 &(_offset[1][0]), &(_offset[1][1]),
3619 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the SECOND raster relative to the output raster");
3623 _offset[1][0] *= -1;
3624 _offset[1][1] *= -1;
3628 rterror(
"rt_raster_from_two_rasters: Extent type ET_CUSTOM is not supported by this function");
3633 if (NULL != offset) {
3634 for (i = 0; i < 4; i++)
3635 offset[i] = _offset[i / 2][i % 2];
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
void lwgeom_geos_error(const char *fmt,...)
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
void lwpoly_free(LWPOLY *poly)
#define SRID_UNKNOWN
Unknown SRID value.
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...
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.
void rt_band_set_ownsdata_flag(rt_band band, int flag)
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
rt_band rt_band_duplicate(rt_band band)
Create a new band duplicated from source band.
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
#define RASTER_DEBUG(level, msg)
int rt_util_gdal_register_all(int force_register_all)
#define RASTER_DEBUGF(level, msg,...)
int8_t rt_util_clamp_to_8BSI(double value)
uint8_t rt_util_clamp_to_1BB(double value)
void rtinfo(const char *fmt,...)
int32_t rt_util_clamp_to_32BSI(double value)
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
uint8_t rt_util_clamp_to_2BUI(double value)
void rtwarn(const char *fmt,...)
uint8_t rt_util_clamp_to_8BUI(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_errorstate
Enum definitions.
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
void rt_band_destroy(rt_band band)
Destroy a raster band.
int16_t rt_util_clamp_to_16BSI(double value)
void * rtrealloc(void *mem, size_t size)
struct rt_gdaldriver_t * rt_gdaldriver
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
int rt_util_gdal_driver_registered(const char *drv)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
uint8_t rt_util_clamp_to_4BUI(double value)
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
void rtdealloc(void *mem)
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
uint16_t rt_util_clamp_to_16BUI(double value)
uint32_t rt_util_clamp_to_32BUI(double value)
int rt_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope ext)
struct rt_raster_t * rt_raster
Types definitions.
float rt_util_clamp_to_32F(double value)
rt_errorstate rt_raster_same_alignment(rt_raster rast1, rt_raster rast2, int *aligned, char **reason)
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
This library is the generic raster handling section of PostGIS.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Datum covers(PG_FUNCTION_ARGS)
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.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
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 ...
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.
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
rt_errorstate rt_raster_get_inverse_geotransform_matrix(rt_raster raster, double *gt, double *igt)
Get 6-element array of raster inverse geotransform matrix.
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.
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.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
rt_raster rt_raster_compute_skewed_raster(rt_envelope extent, double *skew, double *scale, double tolerance)
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t can_write)
Returns a set of available GDAL drivers.
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
static void _rti_rasterize_arg_destroy(_rti_rasterize_arg arg)
double rt_raster_get_x_scale(rt_raster raster)
Get scale X 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.
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_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...
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
uint16_t rt_raster_get_height(rt_raster raster)
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
static void _rt_raster_geotransform_warn_offline_band(rt_raster raster)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
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_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.
uint16_t rt_raster_get_width(rt_raster raster)
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.
struct _rti_rasterize_arg_t * _rti_rasterize_arg
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
int rt_raster_copy_band(rt_raster torast, rt_raster fromrast, int fromindex, int toindex)
Copy one band from one raster to another.
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
static _rti_rasterize_arg _rti_rasterize_arg_init()
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
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.
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
OGRSpatialReferenceH src_sr