57 rterror(
"rt_raster_new: Out of virtual memory creating an rt_raster");
63 if (width > 65535 || height > 65535) {
64 rterror(
"rt_raster_new: Dimensions requested exceed the maximum (65535 x 65535) permitted for a raster");
111 for (i = 0; i < numband; i++) {
119 rtwarn(
"Changes made to raster geotransform matrix may affect out-db band data. Returned band data may be incorrect");
143 double scaleX,
double scaleY
174 double skewX,
double skewY
236 double *i_mag,
double *j_mag,
double *theta_i,
double *theta_ij)
238 double o11, o12, o21, o22 ;
241 if ( (i_mag==NULL) || (j_mag==NULL) || (theta_i==NULL) || (theta_ij==NULL))
255 double *i_mag,
double *j_mag,
double *theta_i,
double *theta_ij)
260 if ( (i_mag==NULL) || (j_mag==NULL) || (theta_i==NULL) || (theta_ij==NULL))
264 *i_mag = sqrt(xscale*xscale + yskew*yskew) ;
267 *j_mag = sqrt(xskew*xskew + yscale*yscale) ;
277 *theta_i = acos(xscale/(*i_mag)) ;
278 theta_test = acos(yskew/(*i_mag)) ;
279 if (theta_test < M_PI_2){
280 *theta_i = -(*theta_i) ;
292 *theta_ij = acos(((xscale*xskew) + (yskew*yscale))/((*i_mag)*(*j_mag))) ;
293 theta_test = acos( ((-yskew*xskew)+(xscale*yscale)) /
294 ((*i_mag)*(*j_mag)));
295 if (theta_test > M_PI_2) {
296 *theta_ij = -(*theta_ij) ;
303 double o11, o12, o21, o22 ;
309 &o11, &o12, &o21, &o22) ;
319 double *xscale,
double *xskew,
double *yskew,
double *yscale)
324 double cos_theta_i, sin_theta_i ;
326 if ( (xscale==NULL) || (xskew==NULL) || (yskew==NULL) || (yscale==NULL)) {
330 if ( (theta_ij == 0.0) || (theta_ij == M_PI)) {
344 k_i = tan(f*M_PI_2 - theta_ij) ;
347 s_j = j_mag / (sqrt(k_i*k_i + 1)) ;
350 cos_theta_i = cos(theta_i) ;
351 sin_theta_i = sin(theta_i) ;
352 *xscale = s_i * cos_theta_i ;
353 *xskew = k_i * s_j * f * cos_theta_i + s_j * f * sin_theta_i ;
354 *yskew = -s_i * sin_theta_i ;
355 *yscale = -k_i * s_j * f * sin_theta_i + s_j * f * cos_theta_i ;
388 if (n >=
raster->numBands || n < 0)
416 assert(NULL !=
band);
421 rterror(
"rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
426 if (index >
raster->numBands)
442 if (NULL ==
raster->bands) {
443 rterror(
"rt_raster_add_band: Out of virtual memory "
444 "reallocating band pointers");
451 for (i = 0; i <=
raster->numBands; ++i) {
453 oldband =
raster->bands[i];
455 }
else if (i > index) {
456 tmpband =
raster->bands[i];
457 raster->bands[i] = oldband;
491 double initialvalue, uint32_t hasnodata,
double nodatavalue,
502 int32_t checkvalint = 0;
503 uint32_t checkvaluint = 0;
504 double checkvaldouble = 0;
505 float checkvalfloat = 0;
515 else if (index > oldnumbands + 1)
516 index = oldnumbands + 1;
521 numval = width * height;
524 mem = (
int *)
rtalloc(datasize);
526 rterror(
"rt_raster_generate_new_band: Could not allocate memory for band");
530 if (
FLT_EQ(initialvalue, 0.0))
531 memset(mem, 0, datasize);
539 for (i = 0; i < numval; i++)
540 ptr[i] = clamped_initval;
541 checkvalint = ptr[0];
548 for (i = 0; i < numval; i++)
549 ptr[i] = clamped_initval;
550 checkvalint = ptr[0];
557 for (i = 0; i < numval; i++)
558 ptr[i] = clamped_initval;
559 checkvalint = ptr[0];
566 for (i = 0; i < numval; i++)
567 ptr[i] = clamped_initval;
568 checkvalint = ptr[0];
575 for (i = 0; i < numval; i++)
576 ptr[i] = clamped_initval;
577 checkvalint = ptr[0];
584 for (i = 0; i < numval; i++)
585 ptr[i] = clamped_initval;
586 checkvalint = ptr[0];
593 for (i = 0; i < numval; i++)
594 ptr[i] = clamped_initval;
595 checkvalint = ptr[0];
602 for (i = 0; i < numval; i++)
603 ptr[i] = clamped_initval;
604 checkvalint = ptr[0];
611 for (i = 0; i < numval; i++)
612 ptr[i] = clamped_initval;
613 checkvaluint = ptr[0];
620 for (i = 0; i < numval; i++)
621 ptr[i] = clamped_initval;
622 checkvalfloat = ptr[0];
628 for (i = 0; i < numval; i++)
629 ptr[i] = initialvalue;
630 checkvaldouble = ptr[0];
635 rterror(
"rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
645 checkvalint, checkvaluint,
646 checkvalfloat, checkvaldouble,
652 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
659 if (numbands == oldnumbands || index == -1) {
660 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
665 if (hasnodata &&
FLT_EQ(initialvalue, nodatavalue))
682 double *
gt,
double *igt
686 assert((
raster != NULL ||
gt != NULL));
692 memcpy(_gt,
gt,
sizeof(
double) * 6);
694 if (!GDALInvGeoTransform(_gt, igt)) {
695 rterror(
"rt_raster_get_inverse_geotransform_matrix: Could not compute inverse geotransform matrix");
761 double xr,
double yr,
762 double *xw,
double *yw,
768 assert(NULL != xw && NULL != yw);
771 memcpy(_gt,
gt,
sizeof(
double) * 6);
788 GDALApplyGeoTransform(_gt, xr, yr, xw, yw);
789 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (c -> g) for (%f, %f) = (%f, %f)",
810 double xw,
double yw,
811 double *xr,
double *yr,
833 RASTER_DEBUGF(4,
"Corrected GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
854 double xw,
double yw,
855 double *xr,
double *yr,
858 double _igt[6] = {0};
861 assert(NULL != xr && NULL != yr);
864 memcpy(_igt, igt,
sizeof(
double) * 6);
876 rterror(
"rt_raster_geopoint_to_cell: Could not get inverse geotransform matrix");
881 GDALApplyGeoTransform(_igt, xw, yw, xr, yr);
882 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
913 double _gt[6] = {0.};
920 for (i = 0; i < 4; i++) {
947 rterror(
"rt_raster_get_envelope: Could not compute spatial coordinates for raster pixel");
959 if (_w[0] < env->
MinX)
961 else if (_w[0] > env->
MaxX)
964 if (_w[1] < env->
MinY)
966 else if (_w[1] > env->
MaxY)
998 uint32_t max_run = 1;
1004 double _gt[6] = {0};
1005 double _igt[6] = {0};
1006 int _d[2] = {1, -1};
1011 double _xy[2] = {0};
1018 GEOSGeometry *sgeom = NULL;
1019 GEOSGeometry *ngeom = NULL;
1027 else if (tolerance > 1.)
1030 dbl_run = tolerance;
1031 while (dbl_run < 10) {
1039 for (i = 0; i < 2; i++) {
1040 if (
FLT_EQ(scale[i], 0.0))
1042 rterror(
"rt_raster_compute_skewed_raster: Scale cannot be zero");
1047 _gt[1] = fabs(scale[i] * tolerance);
1049 _gt[5] = fabs(scale[i] * tolerance);
1055 if ((skew == NULL) || (
FLT_EQ(skew[0], 0.0) &&
FLT_EQ(skew[1], 0.0)))
1058 (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (fabs(scale[0]) / 2.)) / fabs(scale[0]), 1),
1059 (
int) fmax((fabs(extent.
MaxY - extent.
MinY) + (fabs(scale[1]) / 2.)) / fabs(scale[1]), 1)
1064 rterror(
"rt_raster_compute_skewed_raster: Could not create output raster");
1083 _gt[2] = skew[0] * tolerance;
1085 _gt[4] = skew[1] * tolerance;
1087 RASTER_DEBUGF(4,
"Initial geotransform: %f, %f, %f, %f, %f, %f",
1088 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1094 rterror(
"rt_raster_compute_skewed_raster: Out of memory allocating extent raster");
1100 if (!GDALInvGeoTransform(_gt, _igt)) {
1101 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1105 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1106 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1110 for (i = 0; i < 2; i++) {
1114 RASTER_DEBUGF(3,
"Shifting along %s axis", i < 1 ?
"X" :
"Y");
1119 if (run > max_run) {
1120 rterror(
"rt_raster_compute_skewed_raster: Could not compute skewed extent due to check preventing infinite loop");
1129 for (j = 0; j < 4; j++) {
1133 _xy[0] = extent.
MinX;
1134 _xy[1] = extent.
MaxY;
1138 _xy[0] = extent.
MinX;
1139 _xy[1] = extent.
MinY;
1143 _xy[0] = extent.
MaxX;
1144 _xy[1] = extent.
MinY;
1148 _xy[0] = extent.
MaxX;
1149 _xy[1] = extent.
MaxY;
1160 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1165 RASTER_DEBUGF(4,
"Point %d at cell %d x %d", j, (
int) _r[0], (
int) _r[1]);
1168 if ((
int) _r[i] < 0) {
1172 if (_dlastpos != j) {
1173 _dlast = (int) _r[i];
1176 else if ((
int) _r[i] < _dlast) {
1177 RASTER_DEBUG(4,
"Point going in wrong direction. Reversing direction");
1193 x = _d[i] * fabs(_r[i]);
1195 y = _d[i] * fabs(_r[i]);
1204 rterror(
"rt_raster_compute_skewed_raster: Could not compute spatial coordinates for raster pixel");
1215 RASTER_DEBUGF(4,
"Shifted geotransform: %f, %f, %f, %f, %f, %f",
1216 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1220 if (!GDALInvGeoTransform(_gt, _igt)) {
1221 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1225 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1226 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1243 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1248 RASTER_DEBUGF(4,
"geopoint %f x %f at cell %d x %d", extent.
MaxX, extent.
MinY, (
int) _r[0], (
int) _r[1]);
1259 if (npoly == NULL) {
1260 rterror(
"rt_raster_compute_skewed_raster: Could not build extent's geometry for covers test");
1274 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for covers test");
1275 GEOSGeom_destroy(ngeom);
1283 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1284 GEOSGeom_destroy(sgeom);
1287 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test");
1288 GEOSGeom_destroy(ngeom);
1303 raster->width = (int) ((((
double)
raster->width) * fabs(_gt[1]) + fabs(scale[0] / 2.)) / fabs(scale[0]));
1304 raster->height = (int) ((((
double)
raster->height) * fabs(_gt[5]) + fabs(scale[1] / 2.)) / fabs(scale[1]));
1305 _gt[1] = fabs(scale[0]);
1306 _gt[5] = -1 * fabs(scale[1]);
1312 for (i = 0; i < 2; i++) {
1322 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for minimizing dimensions");
1323 GEOSGeom_destroy(ngeom);
1331 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1332 GEOSGeom_destroy(sgeom);
1335 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test for minimizing dimensions");
1336 GEOSGeom_destroy(ngeom);
1349 GEOSGeom_destroy(ngeom);
1400 int fromindex,
int toindex
1405 assert(NULL != torast);
1406 assert(NULL != fromrast);
1410 rtwarn(
"rt_raster_copy_band: Attempting to add a band with different width or height");
1416 rtwarn(
"rt_raster_copy_band: Second raster has no band");
1419 else if (fromindex < 0) {
1420 rtwarn(
"rt_raster_copy_band: Band index for second raster < 0. Defaulted to 0");
1423 else if (fromindex >= fromrast->
numBands) {
1424 rtwarn(
"rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex, fromrast->
numBands - 1);
1425 fromindex = fromrast->
numBands - 1;
1429 rtwarn(
"rt_raster_copy_band: Band index for first raster < 0. Defaulted to 0");
1432 else if (toindex > torast->
numBands) {
1433 rtwarn(
"rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex, torast->
numBands);
1469 double gt[6] = {0.};
1472 assert(NULL != bandNums);
1474 RASTER_DEBUGF(3,
"rt_raster_from_band: source raster has %d bands",
1480 rterror(
"rt_raster_from_band: Out of memory allocating new raster");
1492 for (i = 0; i <
count; i++) {
1497 rterror(
"rt_raster_from_band: Could not copy band");
1503 RASTER_DEBUGF(3,
"rt_raster_from_band: band created at index %d",
1507 RASTER_DEBUGF(3,
"rt_raster_from_band: new raster has %d bands",
1529 assert(NULL !=
band);
1532 rterror(
"rt_raster_replace_band: Band does not match raster's dimensions: %dx%d band to %dx%d raster",
1537 if (index >=
raster->numBands || index < 0) {
1538 rterror(
"rt_raster_replace_band: Band index is not valid");
1543 RASTER_DEBUGF(3,
"rt_raster_replace_band: old band at %p", oldband);
1576 uint32_t *
nband = NULL;
1580 if (
nband == NULL) {
1581 rterror(
"rt_raster_clone: Could not allocate memory for deep clone");
1584 for (i = 0; i < numband; i++)
1598 rterror(
"rt_raster_clone: Could not create cloned raster");
1628 double igt[6] = {0};
1631 double nodatavalue = 0.0;
1636 rterror(
"unable to read requested band");
1650 else if (dim ==
'm') {
1659 rterror(
"unknown value for dim");
1668 double xr, yr,
value;
1698 *lwgeom_out = lwgeom;
1722 char *format,
char **options, uint64_t *gdalsize
1727 GDALDriverH src_drv = NULL;
1728 int destroy_src_drv = 0;
1729 GDALDatasetH
src_ds = NULL;
1731 vsi_l_offset rtn_lenvsi;
1732 uint64_t rtn_len = 0;
1734 GDALDriverH rtn_drv = NULL;
1735 GDALDatasetH rtn_ds = NULL;
1736 uint8_t *rtn = NULL;
1739 assert(NULL != gdalsize);
1746 if (format == NULL || !strlen(format))
1753 rterror(
"rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1758 rtn_drv = GDALGetDriverByName(format);
1759 if (NULL == rtn_drv) {
1760 rterror(
"rt_raster_to_gdal: Could not load the output GDAL driver");
1762 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1768 cc = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_CREATECOPY, NULL);
1770 vio = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_VIRTUALIO, NULL);
1772 if (cc == NULL || vio == NULL) {
1773 rterror(
"rt_raster_to_gdal: Output GDAL driver does not support CreateCopy and/or VirtualIO");
1775 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1780 RASTER_DEBUG(3,
"Copying GDAL MEM raster to memory file in output format");
1781 rtn_ds = GDALCreateCopy(
1793 if (destroy_src_drv) GDALDestroyDriver(src_drv);
1796 if (NULL == rtn_ds) {
1797 rterror(
"rt_raster_to_gdal: Could not create the output GDAL dataset");
1801 RASTER_DEBUGF(4,
"dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1807 RASTER_DEBUG(3,
"Done copying GDAL MEM raster to memory file in output format");
1811 rtn = VSIGetMemFileBuffer(
"/vsimem/out.dat", &rtn_lenvsi,
TRUE);
1812 RASTER_DEBUG(3,
"Done copying GDAL memory file to buffer");
1814 rterror(
"rt_raster_to_gdal: Could not create the output GDAL raster");
1818 rtn_len = (uint64_t) rtn_lenvsi;
1819 *gdalsize = rtn_len;
1840 assert(drv_count != NULL);
1841 uint32_t output_driver = 0;
1843 uint32_t
count = (uint32_t)GDALGetDriverCount();
1848 rterror(
"rt_raster_gdal_drivers: Could not allocate memory for gdaldriver structure");
1849 *drv_count = output_driver;
1853 for (uint32_t i = 0; i <
count; i++)
1855 GDALDriverH *drv = GDALGetDriver(i);
1857 #ifdef GDAL_DCAP_RASTER
1860 const char *is_raster;
1861 is_raster = GDALGetMetadataItem(drv, GDAL_DCAP_RASTER, NULL);
1862 if (!is_raster || !EQUAL(is_raster,
"YES"))
1867 const char *cc = GDALGetMetadataItem(drv, GDAL_DCAP_CREATECOPY, NULL);
1868 if (can_write && !cc)
1872 const char *vio = GDALGetMetadataItem(drv, GDAL_DCAP_VIRTUALIO, NULL);
1873 if (can_write && !vio)
1879 rtn[output_driver].
can_write = (cc != NULL && vio != NULL);
1882 rtn[output_driver].
idx = i;
1885 const char *txt = GDALGetDriverShortName(drv);
1886 size_t txt_len = strlen(txt);
1887 txt_len = (txt_len + 1) *
sizeof(
char);
1889 memcpy(rtn[output_driver].short_name, txt, txt_len);
1892 txt = GDALGetDriverLongName(drv);
1893 txt_len = strlen(txt);
1894 txt_len = (txt_len + 1) *
sizeof(
char);
1896 memcpy(rtn[output_driver].long_name, txt, txt_len);
1899 txt = GDALGetDriverCreationOptionList(drv);
1900 txt_len = strlen(txt);
1901 txt_len = (txt_len + 1) *
sizeof(
char);
1903 memcpy(rtn[output_driver].create_options, txt, txt_len);
1910 *drv_count = output_driver;
1939 int *excludeNodataValues,
1941 GDALDriverH *rtn_drv,
int *destroy_rtn_drv
1943 GDALDriverH drv = NULL;
1944 GDALDatasetH
ds = NULL;
1945 double gt[6] = {0.0};
1947 GDALDataType gdal_pt = GDT_Unknown;
1948 GDALRasterBandH
band;
1950 char *pszDataPointer;
1951 char szGDALOption[50];
1952 char *apszOptions[4];
1953 double nodata = 0.0;
1954 int allocBandNums = 0;
1955 int allocNodataValues = 0;
1960 uint32_t height = 0;
1965 assert(NULL != rtn_drv);
1966 assert(NULL != destroy_rtn_drv);
1968 *destroy_rtn_drv = 0;
1974 *destroy_rtn_drv = 1;
1976 drv = GDALGetDriverByName(
"MEM");
1978 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1984 if (*destroy_rtn_drv) {
1986 GDALDeregisterDriver(drv);
1997 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
2003 cplerr = GDALSetGeoTransform(
ds,
gt);
2004 if (cplerr != CE_None) {
2005 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
2011 if (NULL != srs && strlen(srs)) {
2014 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
2019 cplerr = GDALSetProjection(
ds, _srs);
2021 if (cplerr != CE_None) {
2022 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
2031 if (NULL != bandNums &&
count > 0) {
2032 for (i = 0; i <
count; i++) {
2033 if (bandNums[i] >= numBands) {
2034 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
2042 bandNums = (uint32_t *)
rtalloc(
sizeof(uint32_t) *
count);
2043 if (NULL == bandNums) {
2044 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
2049 for (i = 0; i <
count; i++) bandNums[i] = i;
2053 if (NULL == excludeNodataValues) {
2054 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
2055 if (NULL == excludeNodataValues) {
2056 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
2060 allocNodataValues = 1;
2061 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
2065 for (i = 0; i <
count; i++) {
2067 if (NULL == rtband) {
2068 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
2070 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2077 if (gdal_pt == GDT_Unknown)
2078 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
2087 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
2088 sprintf(pszDataPointer,
"%p", pVoid);
2089 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
2092 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
2093 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
2095 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
2097 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
2099 apszOptions[0] = szGDALOption;
2100 apszOptions[1] = NULL;
2101 apszOptions[2] = NULL;
2102 apszOptions[3] = NULL;
2108 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
2109 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2121 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2122 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2124 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2131 if (GDALGetRasterCount(
ds) != i + 1) {
2132 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2134 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2141 band = GDALGetRasterBand(
ds, i + 1);
2143 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2145 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2152 uint32_t nXBlocks, nYBlocks;
2153 int nXBlockSize, nYBlockSize;
2154 uint32_t iXBlock, iYBlock;
2155 int nXValid, nYValid;
2160 uint32_t valueslen = 0;
2161 int16_t *values = NULL;
2165 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2166 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2167 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2168 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2169 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2174 if (NULL == values) {
2175 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2177 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2182 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2183 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2184 memset(values, 0, valueslen);
2186 x = iXBlock * nXBlockSize;
2187 y = iYBlock * nYBlockSize;
2188 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2192 if ((iXBlock + 1) * nXBlockSize > width)
2193 nXValid = width - (iXBlock * nXBlockSize);
2195 nXValid = nXBlockSize;
2198 if ((iYBlock + 1) * nYBlockSize > height)
2199 nYValid = height - (iYBlock * nYBlockSize);
2201 nYValid = nYBlockSize;
2203 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2207 iYMax =
y + nYValid;
2208 iXMax =
x + nXValid;
2209 for (iY =
y; iY < iYMax; iY++) {
2210 for (iX =
x; iX < iXMax; iX++) {
2212 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2215 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2229 values, nXValid, nYValid,
2233 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2236 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2249 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2250 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2251 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2254 #if POSTGIS_DEBUG_LEVEL > 3
2256 GDALRasterBandH _grb = NULL;
2262 _grb = GDALGetRasterBand(
ds, i + 1);
2263 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2264 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2274 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2296 uint32_t height = 0;
2297 uint32_t numBands = 0;
2299 char *authname = NULL;
2300 char *authcode = NULL;
2302 GDALRasterBandH gdband = NULL;
2303 GDALDataType gdpixtype = GDT_Unknown;
2314 uint32_t nXBlocks, nYBlocks;
2315 int nXBlockSize, nYBlockSize;
2316 uint32_t iXBlock, iYBlock;
2317 uint32_t nXValid, nYValid;
2320 uint8_t *values = NULL;
2321 uint32_t valueslen = 0;
2322 uint8_t *ptr = NULL;
2327 width = GDALGetRasterXSize(
ds);
2328 height = GDALGetRasterYSize(
ds);
2329 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2335 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2341 cplerr = GDALGetGeoTransform(
ds,
gt);
2342 if (GDALGetGeoTransform(
ds,
gt) != CE_None) {
2343 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2355 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2362 strcmp(authname,
"EPSG") == 0 &&
2369 if (authname != NULL)
2371 if (authcode != NULL)
2375 numBands = GDALGetRasterCount(
ds);
2377 #if POSTGIS_DEBUG_LEVEL > 3
2378 for (i = 1; i <= numBands; i++) {
2379 GDALRasterBandH _grb = NULL;
2385 _grb = GDALGetRasterBand(
ds, i);
2386 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2387 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2392 for (i = 1; i <= numBands; i++) {
2395 gdband = GDALGetRasterBand(
ds, i);
2396 if (NULL == gdband) {
2397 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2404 gdpixtype = GDALGetRasterDataType(gdband);
2405 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2408 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2415 width = GDALGetRasterBandXSize(gdband);
2416 height = GDALGetRasterBandYSize(gdband);
2417 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2420 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2421 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2426 (hasnodata ? nodataval : 0),
2430 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2438 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2439 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2440 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2441 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2442 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2445 valueslen = ptlen * nXBlockSize * nYBlockSize;
2447 if (values == NULL) {
2448 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2452 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2454 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2455 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2456 x = iXBlock * nXBlockSize;
2457 y = iYBlock * nYBlockSize;
2458 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2461 memset(values, 0, valueslen);
2464 if ((iXBlock + 1) * nXBlockSize > width)
2465 nXValid = width - (iXBlock * nXBlockSize);
2467 nXValid = nXBlockSize;
2470 if ((iYBlock + 1) * nYBlockSize > height)
2471 nYValid = height - (iYBlock * nYBlockSize);
2473 nYValid = nYBlockSize;
2475 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2477 cplerr = GDALRasterIO(
2481 values, nXValid, nYValid,
2485 if (cplerr != CE_None) {
2486 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2493 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2495 y = nYBlockSize * iYBlock;
2497 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels",
x,
y, nXValid * nYValid);
2502 x = nXBlockSize * iXBlock;
2503 for (iY = 0; iY < nYValid; iY++) {
2504 y = iY + (nYBlockSize * iYBlock);
2506 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels",
x,
y, nXValid);
2508 ptr += (nXValid * ptlen);
2547 rterror(
"_rti_rasterize_arg_init: Could not allocate memory for _rti_rasterize_arg");
2572 if (arg->
init != NULL)
2578 if (arg->
value != NULL)
2586 OSRDestroySpatialReference(arg->
src_sr);
2619 const unsigned char *wkb, uint32_t wkb_len,
2622 double *init,
double *
value,
2623 double *nodata, uint8_t *hasnodata,
2624 int *width,
int *height,
2625 double *scale_x,
double *scale_y,
2626 double *ul_xw,
double *ul_yw,
2627 double *grid_xw,
double *grid_yw,
2628 double *skew_x,
double *skew_y,
2638 double _scale[2] = {0};
2639 double _skew[2] = {0};
2642 OGRGeometryH src_geom;
2643 OGREnvelope src_env;
2645 OGRwkbGeometryType wkbtype = wkbUnknown;
2650 double _gt[6] = {0};
2651 GDALDriverH _drv = NULL;
2653 GDALDatasetH _ds = NULL;
2654 GDALRasterBandH _band = NULL;
2656 uint16_t _width = 0;
2657 uint16_t _height = 0;
2661 assert(NULL != wkb);
2662 assert(0 != wkb_len);
2667 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2672 if (num_bands < 1) {
2703 if (NULL != srs && strlen(srs)) {
2704 arg->
src_sr = OSRNewSpatialReference(NULL);
2705 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2706 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2713 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2714 if (ogrerr != OGRERR_NONE) {
2715 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2724 if (OGR_G_IsEmpty(src_geom)) {
2725 rtinfo(
"Geometry provided is empty. Returning empty raster");
2727 OGR_G_DestroyGeometry(src_geom);
2735 OGR_G_GetEnvelope(src_geom, &src_env);
2738 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2743 (NULL != scale_x) &&
2744 (NULL != scale_y) &&
2749 _scale[0] = fabs(*scale_x);
2750 _scale[1] = fabs(*scale_y);
2753 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2755 _dim[0] = abs(*width);
2756 _dim[1] = abs(*height);
2759 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2764 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2769 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2771 OGR_G_DestroyGeometry(src_geom);
2777 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2781 if (NULL != skew_x) {
2795 if (NULL != skew_y) {
2817 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2819 (wkbtype == wkbPoint) ||
2820 (wkbtype == wkbMultiPoint) ||
2821 (wkbtype == wkbLineString) ||
2822 (wkbtype == wkbMultiLineString)
2828 #if POSTGIS_GDAL_VERSION > 18
2830 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2831 extent.
MinX -= (_scale[0] / 2.);
2832 extent.
MaxX += (_scale[0] / 2.);
2834 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2835 extent.
MinY -= (_scale[1] / 2.);
2836 extent.
MaxY += (_scale[1] / 2.);
2840 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2841 extent.
MinX -= _scale[0];
2842 extent.
MaxX += _scale[0];
2844 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2845 extent.
MinY -= _scale[1];
2846 extent.
MaxY += _scale[1];
2871 if (skewedrast == NULL) {
2872 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2874 OGR_G_DestroyGeometry(src_geom);
2881 _dim[0] = skewedrast->
width;
2882 _dim[1] = skewedrast->
height;
2892 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2894 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2899 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2901 OGR_G_DestroyGeometry(src_geom);
2914 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2915 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2916 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2926 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2934 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2935 ((NULL == ul_xw) && (NULL != ul_yw))
2937 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2940 OGR_G_DestroyGeometry(src_geom);
2950 (NULL != grid_xw) || (NULL != grid_yw)
2955 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2956 ((NULL == grid_xw) && (NULL != grid_yw))
2958 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2961 OGR_G_DestroyGeometry(src_geom);
2968 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2976 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2991 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2994 OGR_G_DestroyGeometry(src_geom);
3007 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3010 OGR_G_DestroyGeometry(src_geom);
3021 else if (NULL == scale_x) {
3033 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3036 OGR_G_DestroyGeometry(src_geom);
3043 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
3049 else if (NULL == scale_y) {
3061 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3064 OGR_G_DestroyGeometry(src_geom);
3071 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
3085 _dim[0] =
rast->width;
3086 _dim[1] =
rast->height;
3091 (NULL != scale_x) && (*scale_x < 0.)
3093 (NULL != scale_y) && (*scale_y > 0)
3099 (NULL != scale_x) &&
3110 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3113 OGR_G_DestroyGeometry(src_geom);
3124 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3129 (NULL != scale_y) &&
3140 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3143 OGR_G_DestroyGeometry(src_geom);
3154 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3162 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3163 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3164 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3173 _drv = GDALGetDriverByName(
"MEM");
3175 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3177 OGR_G_DestroyGeometry(src_geom);
3187 GDALDeregisterDriver(_drv);
3190 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3192 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3194 OGR_G_DestroyGeometry(src_geom);
3197 if (unload_drv) GDALDestroyDriver(_drv);
3203 cplerr = GDALSetGeoTransform(_ds, _gt);
3204 if (cplerr != CE_None) {
3205 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3207 OGR_G_DestroyGeometry(src_geom);
3212 if (unload_drv) GDALDestroyDriver(_drv);
3218 if (NULL != arg->
src_sr) {
3220 OSRExportToWkt(arg->
src_sr, &_srs);
3222 cplerr = GDALSetProjection(_ds, _srs);
3224 if (cplerr != CE_None) {
3225 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3227 OGR_G_DestroyGeometry(src_geom);
3232 if (unload_drv) GDALDestroyDriver(_drv);
3239 for (i = 0; i < arg->
numbands; i++) {
3245 if (cplerr != CE_None) {
3246 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3251 _band = GDALGetRasterBand(_ds, i + 1);
3252 if (NULL == _band) {
3253 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3261 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3262 if (cplerr != CE_None) {
3263 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3267 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3271 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3272 if (cplerr != CE_None) {
3273 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3282 OGR_G_DestroyGeometry(src_geom);
3288 if (unload_drv) GDALDestroyDriver(_drv);
3298 cplerr = GDALRasterizeGeometries(
3307 if (cplerr != CE_None) {
3308 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3310 OGR_G_DestroyGeometry(src_geom);
3315 if (unload_drv) GDALDestroyDriver(_drv);
3321 GDALFlushCache(_ds);
3325 OGR_G_DestroyGeometry(src_geom);
3329 if (unload_drv) GDALDestroyDriver(_drv);
3332 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3341 for (i = 0; i < arg->
numbands; i++) {
3342 uint8_t *
data = NULL;
3349 double nodataval = 0;
3354 if (oldband == NULL) {
3355 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3373 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3384 hasnodata, nodataval,
3388 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3399 for (
x = 0;
x < _width;
x++) {
3400 for (
y = 0;
y < _height;
y++) {
3403 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3415 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3426 if (oldband == NULL) {
3427 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3472 double _offset[2][4] = {{0.}};
3473 uint16_t _dim[2][2] = {{0}};
3478 double gt[6] = {0.};
3480 assert(NULL != rast1);
3481 assert(NULL != rast2);
3482 assert(NULL != rtnraster);
3489 rterror(
"rt_raster_from_two_rasters: Could not test for alignment on the two rasters");
3493 rterror(
"rt_raster_from_two_rasters: The two rasters provided do not have the same alignment");
3498 _dim[0][0] = rast1->
width;
3499 _dim[0][1] = rast1->
height;
3500 _dim[1][0] = rast2->
width;
3501 _dim[1][1] = rast2->
height;
3506 _rast[0]->ipX, _rast[0]->ipY,
3507 &(_offset[1][0]), &(_offset[1][1]),
3510 rterror(
"rt_raster_from_two_rasters: Could not compute offsets of the second raster relative to the first raster");
3513 _offset[1][0] = -1 * _offset[1][0];
3514 _offset[1][1] = -1 * _offset[1][1];
3515 _offset[1][2] = _offset[1][0] + _dim[1][0] - 1;
3516 _offset[1][3] = _offset[1][1] + _dim[1][1] - 1;
3519 switch (extenttype) {
3529 _offset[0][0] = -1 * _offset[1][0];
3530 _offset[0][1] = -1 * _offset[1][1];
3535 dim[0] = _dim[i][0];
3536 dim[1] = _dim[i][1];
3542 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3550 double off[4] = {0};
3564 if (_offset[1][0] < 0)
3565 off[0] = _offset[1][0];
3567 if (_offset[1][1] < 0)
3568 off[1] = _offset[1][1];
3571 off[2] = _dim[0][0] - 1;
3572 if ((
int) _offset[1][2] >= _dim[0][0])
3573 off[2] = _offset[1][2];
3574 off[3] = _dim[0][1] - 1;
3575 if ((
int) _offset[1][3] >= _dim[0][1])
3576 off[3] = _offset[1][3];
3585 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3589 dim[0] = off[2] - off[0] + 1;
3590 dim[1] = off[3] - off[1] + 1;
3604 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3622 &(_offset[0][0]), &(_offset[0][1]),
3625 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the FIRST raster relative to the output raster");
3629 _offset[0][0] *= -1;
3630 _offset[0][1] *= -1;
3635 &(_offset[1][0]), &(_offset[1][1]),
3638 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the SECOND raster relative to the output raster");
3642 _offset[1][0] *= -1;
3643 _offset[1][1] *= -1;
3647 double off[4] = {0};
3651 (_offset[1][2] < 0 || _offset[1][0] > (_dim[0][0] - 1)) ||
3652 (_offset[1][3] < 0 || _offset[1][1] > (_dim[0][1] - 1))
3654 RASTER_DEBUG(3,
"The two rasters provided have no intersection. Returning no band raster");
3658 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3665 if (NULL != offset) {
3666 for (i = 0; i < 4; i++)
3667 offset[i] = _offset[i / 2][i % 2];
3674 if (_offset[1][0] > 0)
3675 off[0] = _offset[1][0];
3676 if (_offset[1][1] > 0)
3677 off[1] = _offset[1][1];
3679 off[2] = _dim[0][0] - 1;
3680 if (_offset[1][2] < _dim[0][0])
3681 off[2] = _offset[1][2];
3682 off[3] = _dim[0][1] - 1;
3683 if (_offset[1][3] < _dim[0][1])
3684 off[3] = _offset[1][3];
3686 dim[0] = off[2] - off[0] + 1;
3687 dim[1] = off[3] - off[1] + 1;
3693 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3706 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3717 &(_offset[0][0]), &(_offset[0][1]),
3720 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the FIRST raster relative to the output raster");
3724 _offset[0][0] *= -1;
3725 _offset[0][1] *= -1;
3730 &(_offset[1][0]), &(_offset[1][1]),
3733 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the SECOND raster relative to the output raster");
3737 _offset[1][0] *= -1;
3738 _offset[1][1] *= -1;
3742 rterror(
"rt_raster_from_two_rasters: Extent type ET_CUSTOM is not supported by this function");
3747 if (NULL != offset) {
3748 for (i = 0; i < 4; i++)
3749 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 lwpointiterator_peek(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assigns the next point in the iterator to p.
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
LWGEOM * lwgeom_force_3dm(const LWGEOM *geom, double mval)
LWGEOM * lwgeom_force_4d(const LWGEOM *geom, double zval, double mval)
LWGEOM * lwgeom_force_3dz(const LWGEOM *geom, double zval)
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
int lwpointiterator_modify_next(LWPOINTITERATOR *s, const POINT4D *p)
Attempts to replace the next point int the iterator with p, and advances the iterator to the next poi...
LWPOINTITERATOR * lwpointiterator_create_rw(LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM* Supports modification of coordinates during iterat...
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
int lwpointiterator_has_next(LWPOINTITERATOR *s)
Returns LW_TRUE if there is another point available in the iterator.
void lwpoly_free(LWPOLY *poly)
#define SRID_UNKNOWN
Unknown SRID value.
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
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_pixel_resample(rt_band band, double xr, double yr, rt_resample_type resample, double *r_value, int *r_nodata)
Retrieve a point value from the raster using a world coordinate and selected resampling method.
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 cell coordinate.
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_errorstate rt_raster_geopoint_to_rasterpoint(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr raster point.
rt_raster rt_raster_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.
rt_errorstate rt_raster_copy_to_geometry(rt_raster raster, uint32_t bandnum, char dim, rt_resample_type resample, const LWGEOM *lwgeom_in, LWGEOM **lwgeom_out)
Copy values from a raster to the points on a geometry using the requested interpolation type.
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