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;
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);
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};
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.};
889 for (i = 0; i < 4; i++) {
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)
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]);
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);
1321 GEOSGeom_destroy(ngeom);
1372 int fromindex,
int toindex
1377 assert(NULL != torast);
1378 assert(NULL != fromrast);
1382 rtwarn(
"rt_raster_copy_band: Attempting to add a band with different width or height");
1388 rtwarn(
"rt_raster_copy_band: Second raster has no band");
1391 else if (fromindex < 0) {
1392 rtwarn(
"rt_raster_copy_band: Band index for second raster < 0. Defaulted to 0");
1395 else if (fromindex >= fromrast->
numBands) {
1396 rtwarn(
"rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex, fromrast->
numBands - 1);
1397 fromindex = fromrast->
numBands - 1;
1401 rtwarn(
"rt_raster_copy_band: Band index for first raster < 0. Defaulted to 0");
1404 else if (toindex > torast->
numBands) {
1405 rtwarn(
"rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex, torast->
numBands);
1441 double gt[6] = {0.};
1444 assert(NULL != bandNums);
1446 RASTER_DEBUGF(3,
"rt_raster_from_band: source raster has %d bands",
1452 rterror(
"rt_raster_from_band: Out of memory allocating new raster");
1464 for (i = 0; i <
count; i++) {
1469 rterror(
"rt_raster_from_band: Could not copy band");
1475 RASTER_DEBUGF(3,
"rt_raster_from_band: band created at index %d",
1479 RASTER_DEBUGF(3,
"rt_raster_from_band: new raster has %d bands",
1501 assert(NULL !=
band);
1504 rterror(
"rt_raster_replace_band: Band does not match raster's dimensions: %dx%d band to %dx%d raster",
1509 if (index >=
raster->numBands || index < 0) {
1510 rterror(
"rt_raster_replace_band: Band index is not valid");
1515 RASTER_DEBUGF(3,
"rt_raster_replace_band: old band at %p", oldband);
1552 if (
nband == NULL) {
1553 rterror(
"rt_raster_clone: Could not allocate memory for deep clone");
1556 for (i = 0; i < numband; i++)
1570 rterror(
"rt_raster_clone: Could not create cloned raster");
1600 char *format,
char **options, uint64_t *gdalsize
1605 GDALDriverH src_drv = NULL;
1606 int destroy_src_drv = 0;
1607 GDALDatasetH
src_ds = NULL;
1609 vsi_l_offset rtn_lenvsi;
1610 uint64_t rtn_len = 0;
1612 GDALDriverH rtn_drv = NULL;
1613 GDALDatasetH rtn_ds = NULL;
1617 assert(NULL != gdalsize);
1624 if (format == NULL || !strlen(format))
1631 rterror(
"rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1636 rtn_drv = GDALGetDriverByName(format);
1637 if (NULL == rtn_drv) {
1638 rterror(
"rt_raster_to_gdal: Could not load the output GDAL driver");
1640 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1646 cc = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_CREATECOPY, NULL);
1648 vio = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_VIRTUALIO, NULL);
1650 if (cc == NULL || vio == NULL) {
1651 rterror(
"rt_raster_to_gdal: Output GDAL driver does not support CreateCopy and/or VirtualIO");
1653 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1658 RASTER_DEBUG(3,
"Copying GDAL MEM raster to memory file in output format");
1659 rtn_ds = GDALCreateCopy(
1671 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1674 if (NULL == rtn_ds) {
1675 rterror(
"rt_raster_to_gdal: Could not create the output GDAL dataset");
1679 RASTER_DEBUGF(4,
"dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1685 RASTER_DEBUG(3,
"Done copying GDAL MEM raster to memory file in output format");
1689 rtn = VSIGetMemFileBuffer(
"/vsimem/out.dat", &rtn_lenvsi,
TRUE);
1690 RASTER_DEBUG(3,
"Done copying GDAL memory file to buffer");
1692 rterror(
"rt_raster_to_gdal: Could not create the output GDAL raster");
1696 rtn_len = (uint64_t) rtn_lenvsi;
1697 *gdalsize = rtn_len;
1721 GDALDriverH *drv = NULL;
1727 assert(drv_count != NULL);
1730 count = GDALGetDriverCount();
1735 rterror(
"rt_raster_gdal_drivers: Could not allocate memory for gdaldriver structure");
1739 for (i = 0, j = 0; i <
count; i++) {
1740 drv = GDALGetDriver(i);
1742 #ifdef GDAL_DCAP_RASTER
1745 const char *is_raster;
1746 is_raster = GDALGetMetadataItem(drv, GDAL_DCAP_RASTER, NULL);
1747 if (is_raster == NULL || !EQUAL(is_raster,
"YES"))
1752 cc = GDALGetMetadataItem(drv, GDAL_DCAP_CREATECOPY, NULL);
1755 vio = GDALGetMetadataItem(drv, GDAL_DCAP_VIRTUALIO, NULL);
1757 if (can_write && (cc == NULL || vio == NULL))
1763 rtn[j].
can_write = (cc != NULL && vio != NULL);
1765 if (rtn[j].can_write) {
1766 RASTER_DEBUGF(3,
"driver %s (%d) supports CreateCopy() and VirtualIO()", txt, i);
1773 txt = GDALGetDriverShortName(drv);
1774 txt_len = strlen(txt);
1776 txt_len = (txt_len + 1) *
sizeof(
char);
1778 memcpy(rtn[j].short_name, txt, txt_len);
1781 txt = GDALGetDriverLongName(drv);
1782 txt_len = strlen(txt);
1784 txt_len = (txt_len + 1) *
sizeof(
char);
1786 memcpy(rtn[j].long_name, txt, txt_len);
1789 txt = GDALGetDriverCreationOptionList(drv);
1790 txt_len = strlen(txt);
1792 txt_len = (txt_len + 1) *
sizeof(
char);
1794 memcpy(rtn[j].create_options, txt, txt_len);
1830 int *excludeNodataValues,
1832 GDALDriverH *rtn_drv,
int *destroy_rtn_drv
1834 GDALDriverH drv = NULL;
1835 GDALDatasetH
ds = NULL;
1836 double gt[6] = {0.0};
1838 GDALDataType gdal_pt = GDT_Unknown;
1839 GDALRasterBandH
band;
1841 char *pszDataPointer;
1842 char szGDALOption[50];
1843 char *apszOptions[4];
1844 double nodata = 0.0;
1845 int allocBandNums = 0;
1846 int allocNodataValues = 0;
1856 assert(NULL != rtn_drv);
1857 assert(NULL != destroy_rtn_drv);
1859 *destroy_rtn_drv = 0;
1865 *destroy_rtn_drv = 1;
1867 drv = GDALGetDriverByName(
"MEM");
1869 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1875 if (*destroy_rtn_drv) {
1877 GDALDeregisterDriver(drv);
1888 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1894 cplerr = GDALSetGeoTransform(
ds,
gt);
1895 if (cplerr != CE_None) {
1896 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1902 if (NULL != srs && strlen(srs)) {
1905 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1910 cplerr = GDALSetProjection(
ds, _srs);
1912 if (cplerr != CE_None) {
1913 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1922 if (NULL != bandNums &&
count > 0) {
1923 for (i = 0; i <
count; i++) {
1924 if (bandNums[i] >= numBands) {
1925 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1934 if (NULL == bandNums) {
1935 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1940 for (i = 0; i <
count; i++) bandNums[i] = i;
1944 if (NULL == excludeNodataValues) {
1945 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1946 if (NULL == excludeNodataValues) {
1947 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1951 allocNodataValues = 1;
1952 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1956 for (i = 0; i <
count; i++) {
1958 if (NULL == rtband) {
1959 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1961 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1968 if (gdal_pt == GDT_Unknown)
1969 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1978 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1979 sprintf(pszDataPointer,
"%p", pVoid);
1980 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1983 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1984 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1986 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1988 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1990 apszOptions[0] = szGDALOption;
1991 apszOptions[1] = NULL;
1992 apszOptions[2] = NULL;
1993 apszOptions[3] = NULL;
1999 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
2000 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2012 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2013 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2015 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2022 if (GDALGetRasterCount(
ds) != i + 1) {
2023 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2025 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2032 band = GDALGetRasterBand(
ds, i + 1);
2034 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2036 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2044 int nXBlockSize, nYBlockSize;
2046 int nXValid, nYValid;
2052 int16_t *values = NULL;
2056 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2057 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2058 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2059 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2060 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2065 if (NULL == values) {
2066 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2068 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2073 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2074 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2075 memset(values, 0, valueslen);
2077 x = iXBlock * nXBlockSize;
2078 y = iYBlock * nYBlockSize;
2079 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2083 if ((iXBlock + 1) * nXBlockSize > width)
2084 nXValid = width - (iXBlock * nXBlockSize);
2086 nXValid = nXBlockSize;
2089 if ((iYBlock + 1) * nYBlockSize > height)
2090 nYValid = height - (iYBlock * nYBlockSize);
2092 nYValid = nYBlockSize;
2094 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2098 iYMax =
y + nYValid;
2099 iXMax =
x + nXValid;
2100 for (iY =
y; iY < iYMax; iY++) {
2101 for (iX =
x; iX < iXMax; iX++) {
2103 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2106 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2120 values, nXValid, nYValid,
2124 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2127 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2140 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2141 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2142 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2145 #if POSTGIS_DEBUG_LEVEL > 3
2147 GDALRasterBandH _grb = NULL;
2153 _grb = GDALGetRasterBand(
ds, i + 1);
2154 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2155 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2165 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2190 char *authname = NULL;
2191 char *authcode = NULL;
2193 GDALRasterBandH gdband = NULL;
2194 GDALDataType gdpixtype = GDT_Unknown;
2206 int nXBlockSize, nYBlockSize;
2218 width = GDALGetRasterXSize(
ds);
2219 height = GDALGetRasterYSize(
ds);
2220 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2226 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2232 cplerr = GDALGetGeoTransform(
ds,
gt);
2233 if (GDALGetGeoTransform(
ds,
gt) != CE_None) {
2234 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2246 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2253 strcmp(authname,
"EPSG") == 0 &&
2260 if (authname != NULL)
2262 if (authcode != NULL)
2266 numBands = GDALGetRasterCount(
ds);
2268 #if POSTGIS_DEBUG_LEVEL > 3
2269 for (i = 1; i <= numBands; i++) {
2270 GDALRasterBandH _grb = NULL;
2276 _grb = GDALGetRasterBand(
ds, i);
2277 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2278 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2283 for (i = 1; i <= numBands; i++) {
2286 gdband = GDALGetRasterBand(
ds, i);
2287 if (NULL == gdband) {
2288 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2295 gdpixtype = GDALGetRasterDataType(gdband);
2296 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2299 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2306 width = GDALGetRasterBandXSize(gdband);
2307 height = GDALGetRasterBandYSize(gdband);
2308 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2311 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2312 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2317 (hasnodata ? nodataval : 0),
2321 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2329 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2330 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2331 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2332 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2333 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2336 valueslen = ptlen * nXBlockSize * nYBlockSize;
2338 if (values == NULL) {
2339 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2343 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2345 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2346 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2347 x = iXBlock * nXBlockSize;
2348 y = iYBlock * nYBlockSize;
2349 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2352 memset(values, 0, valueslen);
2355 if ((iXBlock + 1) * nXBlockSize > width)
2356 nXValid = width - (iXBlock * nXBlockSize);
2358 nXValid = nXBlockSize;
2361 if ((iYBlock + 1) * nYBlockSize > height)
2362 nYValid = height - (iYBlock * nYBlockSize);
2364 nYValid = nYBlockSize;
2366 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2368 cplerr = GDALRasterIO(
2372 values, nXValid, nYValid,
2376 if (cplerr != CE_None) {
2377 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2384 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2386 y = nYBlockSize * iYBlock;
2388 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels",
x,
y, nXValid * nYValid);
2393 x = nXBlockSize * iXBlock;
2394 for (iY = 0; iY < nYValid; iY++) {
2395 y = iY + (nYBlockSize * iYBlock);
2397 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels",
x,
y, nXValid);
2399 ptr += (nXValid * ptlen);
2438 rterror(
"_rti_rasterize_arg_init: Could not allocate memory for _rti_rasterize_arg");
2463 if (arg->
init != NULL)
2469 if (arg->
value != NULL)
2477 OSRDestroySpatialReference(arg->
src_sr);
2510 const unsigned char *wkb,
uint32_t wkb_len,
2513 double *init,
double *
value,
2514 double *nodata,
uint8_t *hasnodata,
2515 int *width,
int *height,
2516 double *scale_x,
double *scale_y,
2517 double *ul_xw,
double *ul_yw,
2518 double *grid_xw,
double *grid_yw,
2519 double *skew_x,
double *skew_y,
2529 double _scale[2] = {0};
2530 double _skew[2] = {0};
2533 OGRGeometryH src_geom;
2534 OGREnvelope src_env;
2536 OGRwkbGeometryType wkbtype = wkbUnknown;
2541 double _gt[6] = {0};
2542 GDALDriverH _drv = NULL;
2544 GDALDatasetH _ds = NULL;
2545 GDALRasterBandH _band = NULL;
2547 uint16_t _width = 0;
2548 uint16_t _height = 0;
2552 assert(NULL != wkb);
2553 assert(0 != wkb_len);
2558 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2563 if (num_bands < 1) {
2594 if (NULL != srs && strlen(srs)) {
2595 arg->
src_sr = OSRNewSpatialReference(NULL);
2596 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2597 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2604 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2605 if (ogrerr != OGRERR_NONE) {
2606 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2615 if (OGR_G_IsEmpty(src_geom)) {
2616 rtinfo(
"Geometry provided is empty. Returning empty raster");
2618 OGR_G_DestroyGeometry(src_geom);
2626 OGR_G_GetEnvelope(src_geom, &src_env);
2629 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2634 (NULL != scale_x) &&
2635 (NULL != scale_y) &&
2640 _scale[0] = fabs(*scale_x);
2641 _scale[1] = fabs(*scale_y);
2650 _dim[0] = abs(*width);
2651 _dim[1] = abs(*height);
2654 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2659 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2664 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2666 OGR_G_DestroyGeometry(src_geom);
2672 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2676 if (NULL != skew_x) {
2690 if (NULL != skew_y) {
2712 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2714 (wkbtype == wkbPoint) ||
2715 (wkbtype == wkbMultiPoint) ||
2716 (wkbtype == wkbLineString) ||
2717 (wkbtype == wkbMultiLineString)
2723 #if POSTGIS_GDAL_VERSION > 18
2725 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2726 extent.
MinX -= (_scale[0] / 2.);
2727 extent.
MaxX += (_scale[0] / 2.);
2729 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2730 extent.
MinY -= (_scale[1] / 2.);
2731 extent.
MaxY += (_scale[1] / 2.);
2735 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2736 extent.
MinX -= _scale[0];
2737 extent.
MaxX += _scale[0];
2739 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2740 extent.
MinY -= _scale[1];
2741 extent.
MaxY += _scale[1];
2768 if (skewedrast == NULL) {
2769 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2771 OGR_G_DestroyGeometry(src_geom);
2778 _dim[0] = skewedrast->
width;
2779 _dim[1] = skewedrast->
height;
2789 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2791 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2796 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2798 OGR_G_DestroyGeometry(src_geom);
2811 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2812 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2813 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2823 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2831 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2832 ((NULL == ul_xw) && (NULL != ul_yw))
2834 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2837 OGR_G_DestroyGeometry(src_geom);
2847 (NULL != grid_xw) || (NULL != grid_yw)
2852 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2853 ((NULL == grid_xw) && (NULL != grid_yw))
2855 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2858 OGR_G_DestroyGeometry(src_geom);
2865 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2873 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2888 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2891 OGR_G_DestroyGeometry(src_geom);
2904 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2907 OGR_G_DestroyGeometry(src_geom);
2918 else if (NULL == scale_x) {
2930 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2933 OGR_G_DestroyGeometry(src_geom);
2940 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2946 else if (NULL == scale_y) {
2958 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2961 OGR_G_DestroyGeometry(src_geom);
2968 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2982 _dim[0] =
rast->width;
2983 _dim[1] =
rast->height;
2988 (NULL != scale_x) && (*scale_x < 0.)
2990 (NULL != scale_y) && (*scale_y > 0)
2996 (NULL != scale_x) &&
3007 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3010 OGR_G_DestroyGeometry(src_geom);
3021 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0))
3026 (NULL != scale_y) &&
3037 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3040 OGR_G_DestroyGeometry(src_geom);
3051 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0))
3059 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3060 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3061 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3070 _drv = GDALGetDriverByName(
"MEM");
3072 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3074 OGR_G_DestroyGeometry(src_geom);
3084 GDALDeregisterDriver(_drv);
3087 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3089 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3091 OGR_G_DestroyGeometry(src_geom);
3094 if (unload_drv) GDALDestroyDriver(_drv);
3100 cplerr = GDALSetGeoTransform(_ds, _gt);
3101 if (cplerr != CE_None) {
3102 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3104 OGR_G_DestroyGeometry(src_geom);
3109 if (unload_drv) GDALDestroyDriver(_drv);
3115 if (NULL != arg->
src_sr) {
3117 OSRExportToWkt(arg->
src_sr, &_srs);
3119 cplerr = GDALSetProjection(_ds, _srs);
3121 if (cplerr != CE_None) {
3122 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3124 OGR_G_DestroyGeometry(src_geom);
3129 if (unload_drv) GDALDestroyDriver(_drv);
3136 for (i = 0; i < arg->
numbands; i++) {
3142 if (cplerr != CE_None) {
3143 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3148 _band = GDALGetRasterBand(_ds, i + 1);
3149 if (NULL == _band) {
3150 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3158 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3159 if (cplerr != CE_None) {
3160 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3164 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3168 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3169 if (cplerr != CE_None) {
3170 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3179 OGR_G_DestroyGeometry(src_geom);
3185 if (unload_drv) GDALDestroyDriver(_drv);
3195 cplerr = GDALRasterizeGeometries(
3204 if (cplerr != CE_None) {
3205 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3207 OGR_G_DestroyGeometry(src_geom);
3212 if (unload_drv) GDALDestroyDriver(_drv);
3218 GDALFlushCache(_ds);
3222 OGR_G_DestroyGeometry(src_geom);
3226 if (unload_drv) GDALDestroyDriver(_drv);
3229 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3238 for (i = 0; i < arg->
numbands; i++) {
3246 double nodataval = 0;
3251 if (oldband == NULL) {
3252 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3270 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3281 hasnodata, nodataval,
3285 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3296 for (
x = 0;
x < _width;
x++) {
3297 for (
y = 0;
y < _height;
y++) {
3300 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3312 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3323 if (oldband == NULL) {
3324 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3369 double _offset[2][4] = {{0.}};
3370 uint16_t _dim[2][2] = {{0}};
3375 double gt[6] = {0.};
3377 assert(NULL != rast1);
3378 assert(NULL != rast2);
3379 assert(NULL != rtnraster);
3386 rterror(
"rt_raster_from_two_rasters: Could not test for alignment on the two rasters");
3390 rterror(
"rt_raster_from_two_rasters: The two rasters provided do not have the same alignment");
3395 _dim[0][0] = rast1->
width;
3396 _dim[0][1] = rast1->
height;
3397 _dim[1][0] = rast2->
width;
3398 _dim[1][1] = rast2->
height;
3403 _rast[0]->ipX, _rast[0]->ipY,
3404 &(_offset[1][0]), &(_offset[1][1]),
3407 rterror(
"rt_raster_from_two_rasters: Could not compute offsets of the second raster relative to the first raster");
3410 _offset[1][0] = -1 * _offset[1][0];
3411 _offset[1][1] = -1 * _offset[1][1];
3412 _offset[1][2] = _offset[1][0] + _dim[1][0] - 1;
3413 _offset[1][3] = _offset[1][1] + _dim[1][1] - 1;
3416 switch (extenttype) {
3426 _offset[0][0] = -1 * _offset[1][0];
3427 _offset[0][1] = -1 * _offset[1][1];
3432 dim[0] = _dim[i][0];
3433 dim[1] = _dim[i][1];
3439 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3447 double off[4] = {0};
3461 if (_offset[1][0] < 0)
3462 off[0] = _offset[1][0];
3464 if (_offset[1][1] < 0)
3465 off[1] = _offset[1][1];
3468 off[2] = _dim[0][0] - 1;
3469 if ((
int) _offset[1][2] >= _dim[0][0])
3470 off[2] = _offset[1][2];
3471 off[3] = _dim[0][1] - 1;
3472 if ((
int) _offset[1][3] >= _dim[0][1])
3473 off[3] = _offset[1][3];
3482 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3486 dim[0] = off[2] - off[0] + 1;
3487 dim[1] = off[3] - off[1] + 1;
3501 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3519 &(_offset[0][0]), &(_offset[0][1]),
3522 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the FIRST raster relative to the output raster");
3526 _offset[0][0] *= -1;
3527 _offset[0][1] *= -1;
3532 &(_offset[1][0]), &(_offset[1][1]),
3535 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the SECOND raster relative to the output raster");
3539 _offset[1][0] *= -1;
3540 _offset[1][1] *= -1;
3544 double off[4] = {0};
3548 (_offset[1][2] < 0 || _offset[1][0] > (_dim[0][0] - 1)) ||
3549 (_offset[1][3] < 0 || _offset[1][1] > (_dim[0][1] - 1))
3551 RASTER_DEBUG(3,
"The two rasters provided have no intersection. Returning no band raster");
3555 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3562 if (NULL != offset) {
3563 for (i = 0; i < 4; i++)
3564 offset[i] = _offset[i / 2][i % 2];
3571 if (_offset[1][0] > 0)
3572 off[0] = _offset[1][0];
3573 if (_offset[1][1] > 0)
3574 off[1] = _offset[1][1];
3576 off[2] = _dim[0][0] - 1;
3577 if (_offset[1][2] < _dim[0][0])
3578 off[2] = _offset[1][2];
3579 off[3] = _dim[0][1] - 1;
3580 if (_offset[1][3] < _dim[0][1])
3581 off[3] = _offset[1][3];
3583 dim[0] = off[2] - off[0] + 1;
3584 dim[1] = off[3] - off[1] + 1;
3590 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3603 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3614 &(_offset[0][0]), &(_offset[0][1]),
3617 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the FIRST raster relative to the output raster");
3621 _offset[0][0] *= -1;
3622 _offset[0][1] *= -1;
3627 &(_offset[1][0]), &(_offset[1][1]),
3630 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the SECOND raster relative to the output raster");
3634 _offset[1][0] *= -1;
3635 _offset[1][1] *= -1;
3639 rterror(
"rt_raster_from_two_rasters: Extent type ET_CUSTOM is not supported by this function");
3644 if (NULL != offset) {
3645 for (i = 0; i < 4; i++)
3646 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)
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 lwpoly_free(LWPOLY *poly)
#define SRID_UNKNOWN
Unknown SRID value.
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