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,
509 else if (index > oldnumbands + 1)
510 index = oldnumbands + 1;
515 numval = width * height;
518 mem = (
int *)
rtalloc(datasize);
520 rterror(
"rt_raster_generate_new_band: Could not allocate memory for band");
526 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
536 if (numbands == oldnumbands || index == -1) {
537 rterror(
"rt_raster_generate_new_band: Could not add band to raster. Aborting");
543 if (hasnodata &&
FLT_EQ(initialvalue, nodatavalue))
560 double *
gt,
double *igt
564 assert((
raster != NULL ||
gt != NULL));
570 memcpy(_gt,
gt,
sizeof(
double) * 6);
572 if (!GDALInvGeoTransform(_gt, igt)) {
573 rterror(
"rt_raster_get_inverse_geotransform_matrix: Could not compute inverse geotransform matrix");
639 double xr,
double yr,
640 double *xw,
double *yw,
646 assert(NULL != xw && NULL != yw);
649 memcpy(_gt,
gt,
sizeof(
double) * 6);
666 GDALApplyGeoTransform(_gt, xr, yr, xw, yw);
667 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (c -> g) for (%f, %f) = (%f, %f)",
688 double xw,
double yw,
689 double *xr,
double *yr,
711 RASTER_DEBUGF(4,
"Corrected GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
732 double xw,
double yw,
733 double *xr,
double *yr,
736 double _igt[6] = {0};
739 assert(NULL != xr && NULL != yr);
742 memcpy(_igt, igt,
sizeof(
double) * 6);
754 rterror(
"rt_raster_geopoint_to_cell: Could not get inverse geotransform matrix");
759 GDALApplyGeoTransform(_igt, xw, yw, xr, yr);
760 RASTER_DEBUGF(4,
"GDALApplyGeoTransform (g -> c) for (%f, %f) = (%f, %f)",
791 double _gt[6] = {0.};
798 for (i = 0; i < 4; i++) {
825 rterror(
"rt_raster_get_envelope: Could not compute spatial coordinates for raster pixel");
837 if (_w[0] < env->
MinX)
839 else if (_w[0] > env->
MaxX)
842 if (_w[1] < env->
MinY)
844 else if (_w[1] > env->
MaxY)
876 uint32_t max_run = 1;
883 double _igt[6] = {0};
896 GEOSGeometry *sgeom = NULL;
897 GEOSGeometry *ngeom = NULL;
905 else if (tolerance > 1.)
909 while (dbl_run < 10) {
917 for (i = 0; i < 2; i++) {
918 if (
FLT_EQ(scale[i], 0.0))
920 rterror(
"rt_raster_compute_skewed_raster: Scale cannot be zero");
925 _gt[1] = fabs(scale[i] * tolerance);
927 _gt[5] = fabs(scale[i] * tolerance);
933 if ((skew == NULL) || (
FLT_EQ(skew[0], 0.0) &&
FLT_EQ(skew[1], 0.0)))
936 (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (fabs(scale[0]) / 2.)) / fabs(scale[0]), 1),
937 (
int) fmax((fabs(extent.
MaxY - extent.
MinY) + (fabs(scale[1]) / 2.)) / fabs(scale[1]), 1)
942 rterror(
"rt_raster_compute_skewed_raster: Could not create output raster");
961 _gt[2] = skew[0] * tolerance;
963 _gt[4] = skew[1] * tolerance;
965 RASTER_DEBUGF(4,
"Initial geotransform: %f, %f, %f, %f, %f, %f",
966 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
972 rterror(
"rt_raster_compute_skewed_raster: Out of memory allocating extent raster");
978 if (!GDALInvGeoTransform(_gt, _igt)) {
979 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
983 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
984 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
988 for (i = 0; i < 2; i++) {
992 RASTER_DEBUGF(3,
"Shifting along %s axis", i < 1 ?
"X" :
"Y");
998 rterror(
"rt_raster_compute_skewed_raster: Could not compute skewed extent due to check preventing infinite loop");
1007 for (j = 0; j < 4; j++) {
1011 _xy[0] = extent.
MinX;
1012 _xy[1] = extent.
MaxY;
1016 _xy[0] = extent.
MinX;
1017 _xy[1] = extent.
MinY;
1021 _xy[0] = extent.
MaxX;
1022 _xy[1] = extent.
MinY;
1026 _xy[0] = extent.
MaxX;
1027 _xy[1] = extent.
MaxY;
1038 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1043 RASTER_DEBUGF(4,
"Point %d at cell %d x %d", j, (
int) _r[0], (
int) _r[1]);
1046 if ((
int) _r[i] < 0) {
1050 if (_dlastpos != j) {
1051 _dlast = (int) _r[i];
1054 else if ((
int) _r[i] < _dlast) {
1055 RASTER_DEBUG(4,
"Point going in wrong direction. Reversing direction");
1071 x = _d[i] * fabs(_r[i]);
1073 y = _d[i] * fabs(_r[i]);
1082 rterror(
"rt_raster_compute_skewed_raster: Could not compute spatial coordinates for raster pixel");
1093 RASTER_DEBUGF(4,
"Shifted geotransform: %f, %f, %f, %f, %f, %f",
1094 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1098 if (!GDALInvGeoTransform(_gt, _igt)) {
1099 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1103 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1104 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1121 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1126 RASTER_DEBUGF(4,
"geopoint %f x %f at cell %d x %d", extent.
MaxX, extent.
MinY, (
int) _r[0], (
int) _r[1]);
1137 if (npoly == NULL) {
1138 rterror(
"rt_raster_compute_skewed_raster: Could not build extent's geometry for covers test");
1152 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for covers test");
1153 GEOSGeom_destroy(ngeom);
1161 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1162 GEOSGeom_destroy(sgeom);
1165 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test");
1166 GEOSGeom_destroy(ngeom);
1181 raster->width = (int) ((((
double)
raster->width) * fabs(_gt[1]) + fabs(scale[0] / 2.)) / fabs(scale[0]));
1182 raster->height = (int) ((((
double)
raster->height) * fabs(_gt[5]) + fabs(scale[1] / 2.)) / fabs(scale[1]));
1183 _gt[1] = fabs(scale[0]);
1184 _gt[5] = -1 * fabs(scale[1]);
1190 for (i = 0; i < 2; i++) {
1200 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for minimizing dimensions");
1201 GEOSGeom_destroy(ngeom);
1209 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1210 GEOSGeom_destroy(sgeom);
1213 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test for minimizing dimensions");
1214 GEOSGeom_destroy(ngeom);
1227 GEOSGeom_destroy(ngeom);
1278 int fromindex,
int toindex
1283 assert(NULL != torast);
1284 assert(NULL != fromrast);
1288 rtwarn(
"rt_raster_copy_band: Attempting to add a band with different width or height");
1294 rtwarn(
"rt_raster_copy_band: Second raster has no band");
1297 else if (fromindex < 0) {
1298 rtwarn(
"rt_raster_copy_band: Band index for second raster < 0. Defaulted to 0");
1301 else if (fromindex >= fromrast->
numBands) {
1302 rtwarn(
"rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex, fromrast->
numBands - 1);
1303 fromindex = fromrast->
numBands - 1;
1307 rtwarn(
"rt_raster_copy_band: Band index for first raster < 0. Defaulted to 0");
1310 else if (toindex > torast->
numBands) {
1311 rtwarn(
"rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex, torast->
numBands);
1347 double gt[6] = {0.};
1350 assert(NULL != bandNums);
1352 RASTER_DEBUGF(3,
"rt_raster_from_band: source raster has %d bands",
1358 rterror(
"rt_raster_from_band: Out of memory allocating new raster");
1370 for (i = 0; i <
count; i++) {
1375 rterror(
"rt_raster_from_band: Could not copy band");
1381 RASTER_DEBUGF(3,
"rt_raster_from_band: band created at index %d",
1385 RASTER_DEBUGF(3,
"rt_raster_from_band: new raster has %d bands",
1407 assert(NULL !=
band);
1410 rterror(
"rt_raster_replace_band: Band does not match raster's dimensions: %dx%d band to %dx%d raster",
1415 if (index >=
raster->numBands || index < 0) {
1416 rterror(
"rt_raster_replace_band: Band index is not valid");
1421 RASTER_DEBUGF(3,
"rt_raster_replace_band: old band at %p", oldband);
1454 uint32_t *
nband = NULL;
1458 if (
nband == NULL) {
1459 rterror(
"rt_raster_clone: Could not allocate memory for deep clone");
1462 for (i = 0; i < numband; i++)
1476 rterror(
"rt_raster_clone: Could not create cloned raster");
1506 double igt[6] = {0};
1509 double nodatavalue = 0.0;
1514 rterror(
"unable to read requested band");
1528 else if (dim ==
'm') {
1537 rterror(
"unknown value for dim");
1546 double xr, yr,
value;
1576 *lwgeom_out = lwgeom;
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;
1614 uint8_t *rtn = 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;
1718 assert(drv_count != NULL);
1719 uint32_t output_driver = 0;
1721 uint32_t
count = (uint32_t)GDALGetDriverCount();
1726 rterror(
"rt_raster_gdal_drivers: Could not allocate memory for gdaldriver structure");
1727 *drv_count = output_driver;
1731 for (uint32_t i = 0; i <
count; i++)
1733 GDALDriverH *drv = GDALGetDriver(i);
1735 #ifdef GDAL_DCAP_RASTER
1738 const char *is_raster;
1739 is_raster = GDALGetMetadataItem(drv, GDAL_DCAP_RASTER, NULL);
1740 if (!is_raster || !EQUAL(is_raster,
"YES"))
1745 const char *cc = GDALGetMetadataItem(drv, GDAL_DCAP_CREATECOPY, NULL);
1746 if (can_write && !cc)
1750 const char *vio = GDALGetMetadataItem(drv, GDAL_DCAP_VIRTUALIO, NULL);
1751 if (can_write && !vio)
1757 rtn[output_driver].
can_write = (cc != NULL && vio != NULL);
1760 rtn[output_driver].
idx = i;
1763 const char *txt = GDALGetDriverShortName(drv);
1764 size_t txt_len = strlen(txt);
1765 txt_len = (txt_len + 1) *
sizeof(
char);
1767 memcpy(rtn[output_driver].short_name, txt, txt_len);
1770 txt = GDALGetDriverLongName(drv);
1771 txt_len = strlen(txt);
1772 txt_len = (txt_len + 1) *
sizeof(
char);
1774 memcpy(rtn[output_driver].long_name, txt, txt_len);
1777 txt = GDALGetDriverCreationOptionList(drv);
1778 txt_len = strlen(txt);
1779 txt_len = (txt_len + 1) *
sizeof(
char);
1781 memcpy(rtn[output_driver].create_options, txt, txt_len);
1788 *drv_count = output_driver;
1817 int *excludeNodataValues,
1819 GDALDriverH *rtn_drv,
int *destroy_rtn_drv
1821 GDALDriverH drv = NULL;
1822 GDALDatasetH
ds = NULL;
1823 double gt[6] = {0.0};
1825 GDALDataType gdal_pt = GDT_Unknown;
1826 GDALRasterBandH
band;
1828 char *pszDataPointer;
1829 char szGDALOption[50];
1830 char *apszOptions[4];
1831 double nodata = 0.0;
1832 int allocBandNums = 0;
1833 int allocNodataValues = 0;
1838 uint32_t height = 0;
1843 assert(NULL != rtn_drv);
1844 assert(NULL != destroy_rtn_drv);
1846 *destroy_rtn_drv = 0;
1852 *destroy_rtn_drv = 1;
1854 drv = GDALGetDriverByName(
"MEM");
1856 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1862 if (*destroy_rtn_drv) {
1864 GDALDeregisterDriver(drv);
1875 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1881 cplerr = GDALSetGeoTransform(
ds,
gt);
1882 if (cplerr != CE_None) {
1883 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1889 if (NULL != srs && strlen(srs)) {
1892 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1897 cplerr = GDALSetProjection(
ds, _srs);
1899 if (cplerr != CE_None) {
1900 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1909 if (NULL != bandNums &&
count > 0) {
1910 for (i = 0; i <
count; i++) {
1911 if (bandNums[i] >= numBands) {
1912 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1920 bandNums = (uint32_t *)
rtalloc(
sizeof(uint32_t) *
count);
1921 if (NULL == bandNums) {
1922 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1927 for (i = 0; i <
count; i++) bandNums[i] = i;
1931 if (NULL == excludeNodataValues) {
1932 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1933 if (NULL == excludeNodataValues) {
1934 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1938 allocNodataValues = 1;
1939 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1943 for (i = 0; i <
count; i++) {
1945 if (NULL == rtband) {
1946 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1948 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1955 if (gdal_pt == GDT_Unknown)
1956 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1961 #if POSTGIS_GDAL_VERSION < 30700
1967 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1968 sprintf(pszDataPointer,
"%p", pVoid);
1969 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1972 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1973 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1975 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1977 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1979 apszOptions[0] = szGDALOption;
1980 apszOptions[1] = NULL;
1981 apszOptions[2] = NULL;
1982 apszOptions[3] = NULL;
1988 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
1989 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1994 #if POSTGIS_GDAL_VERSION < 30700
2001 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2002 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2004 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2012 if (GDALGetRasterCount(
ds) != i + 1) {
2013 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2015 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2022 band = GDALGetRasterBand(
ds, i + 1);
2024 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2026 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2031 #if POSTGIS_GDAL_VERSION < 30700
2035 uint32_t nXBlocks, nYBlocks;
2036 int nXBlockSize, nYBlockSize;
2037 uint32_t iXBlock, iYBlock;
2038 int nXValid, nYValid;
2043 uint32_t valueslen = 0;
2044 int16_t *values = NULL;
2048 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2049 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2050 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2051 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2052 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2057 if (NULL == values) {
2058 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2060 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2065 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2066 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2067 memset(values, 0, valueslen);
2069 x = iXBlock * nXBlockSize;
2070 y = iYBlock * nYBlockSize;
2071 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2075 if ((iXBlock + 1) * nXBlockSize > width)
2076 nXValid = width - (iXBlock * nXBlockSize);
2078 nXValid = nXBlockSize;
2081 if ((iYBlock + 1) * nYBlockSize > height)
2082 nYValid = height - (iYBlock * nYBlockSize);
2084 nYValid = nYBlockSize;
2086 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2090 iYMax =
y + nYValid;
2091 iXMax =
x + nXValid;
2092 for (iY =
y; iY < iYMax; iY++) {
2093 for (iX =
x; iX < iXMax; iX++) {
2095 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2098 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2112 values, nXValid, nYValid,
2116 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2119 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2133 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2134 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2135 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2138 #if POSTGIS_DEBUG_LEVEL > 3
2140 GDALRasterBandH _grb = NULL;
2146 _grb = GDALGetRasterBand(
ds, i + 1);
2147 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2148 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2158 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2180 uint32_t height = 0;
2181 uint32_t numBands = 0;
2183 char *authname = NULL;
2184 char *authcode = NULL;
2186 GDALRasterBandH gdband = NULL;
2187 GDALDataType gdpixtype = GDT_Unknown;
2198 uint32_t nXBlocks, nYBlocks;
2199 int nXBlockSize, nYBlockSize;
2200 uint32_t iXBlock, iYBlock;
2201 uint32_t nXValid, nYValid;
2204 uint8_t *values = NULL;
2205 uint32_t valueslen = 0;
2206 uint8_t *ptr = NULL;
2211 width = GDALGetRasterXSize(
ds);
2212 height = GDALGetRasterYSize(
ds);
2213 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2219 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2225 cplerr = GDALGetGeoTransform(
ds,
gt);
2226 if (GDALGetGeoTransform(
ds,
gt) != CE_None) {
2227 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2239 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2246 strcmp(authname,
"EPSG") == 0 &&
2253 if (authname != NULL)
2255 if (authcode != NULL)
2259 numBands = GDALGetRasterCount(
ds);
2261 #if POSTGIS_DEBUG_LEVEL > 3
2262 for (i = 1; i <= numBands; i++) {
2263 GDALRasterBandH _grb = NULL;
2269 _grb = GDALGetRasterBand(
ds, i);
2270 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2271 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2276 for (i = 1; i <= numBands; i++) {
2279 gdband = GDALGetRasterBand(
ds, i);
2280 if (NULL == gdband) {
2281 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2288 gdpixtype = GDALGetRasterDataType(gdband);
2289 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSizeBytes(gdpixtype));
2292 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2299 width = GDALGetRasterBandXSize(gdband);
2300 height = GDALGetRasterBandYSize(gdband);
2301 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2304 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2305 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2310 (hasnodata ? nodataval : 0),
2314 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2322 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2323 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2324 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2325 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2326 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2329 valueslen = ptlen * nXBlockSize * nYBlockSize;
2331 if (values == NULL) {
2332 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2336 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2338 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2339 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2340 x = iXBlock * nXBlockSize;
2341 y = iYBlock * nYBlockSize;
2342 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2345 memset(values, 0, valueslen);
2348 if ((iXBlock + 1) * nXBlockSize > width)
2349 nXValid = width - (iXBlock * nXBlockSize);
2351 nXValid = nXBlockSize;
2354 if ((iYBlock + 1) * nYBlockSize > height)
2355 nYValid = height - (iYBlock * nYBlockSize);
2357 nYValid = nYBlockSize;
2359 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2361 cplerr = GDALRasterIO(
2365 values, nXValid, nYValid,
2369 if (cplerr != CE_None) {
2370 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2377 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2379 y = nYBlockSize * iYBlock;
2381 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels",
x,
y, nXValid * nYValid);
2386 x = nXBlockSize * iXBlock;
2387 for (iY = 0; iY < nYValid; iY++) {
2388 y = iY + (nYBlockSize * iYBlock);
2390 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels",
x,
y, nXValid);
2392 ptr += (nXValid * ptlen);
2431 rterror(
"_rti_rasterize_arg_init: Could not allocate memory for _rti_rasterize_arg");
2456 if (arg->
init != NULL)
2462 if (arg->
value != NULL)
2470 OSRDestroySpatialReference(arg->
src_sr);
2503 const unsigned char *wkb, uint32_t wkb_len,
2506 double *init,
double *
value,
2507 double *nodata, uint8_t *hasnodata,
2508 int *width,
int *height,
2509 double *scale_x,
double *scale_y,
2510 double *ul_xw,
double *ul_yw,
2511 double *grid_xw,
double *grid_yw,
2512 double *skew_x,
double *skew_y,
2522 double _scale[2] = {0};
2523 double _skew[2] = {0};
2526 OGRGeometryH src_geom;
2527 OGREnvelope src_env;
2529 OGRwkbGeometryType wkbtype = wkbUnknown;
2534 double _gt[6] = {0};
2535 GDALDriverH _drv = NULL;
2537 GDALDatasetH _ds = NULL;
2538 GDALRasterBandH _band = NULL;
2540 uint16_t _width = 0;
2541 uint16_t _height = 0;
2545 assert(NULL != wkb);
2546 assert(0 != wkb_len);
2551 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2556 if (num_bands < 1) {
2587 if (NULL != srs && strlen(srs)) {
2588 arg->
src_sr = OSRNewSpatialReference(NULL);
2589 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2590 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2597 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2598 if (ogrerr != OGRERR_NONE) {
2599 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2608 if (OGR_G_IsEmpty(src_geom)) {
2609 rtinfo(
"Geometry provided is empty. Returning empty raster");
2611 OGR_G_DestroyGeometry(src_geom);
2619 OGR_G_GetEnvelope(src_geom, &src_env);
2622 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2627 (NULL != scale_x) &&
2628 (NULL != scale_y) &&
2633 _scale[0] = fabs(*scale_x);
2634 _scale[1] = fabs(*scale_y);
2637 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2639 _dim[0] = abs(*width);
2640 _dim[1] = abs(*height);
2643 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2648 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2653 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2655 OGR_G_DestroyGeometry(src_geom);
2661 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2665 if (NULL != skew_x) {
2679 if (NULL != skew_y) {
2701 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2703 (wkbtype == wkbPoint) ||
2704 (wkbtype == wkbMultiPoint) ||
2705 (wkbtype == wkbLineString) ||
2706 (wkbtype == wkbMultiLineString)
2712 #if POSTGIS_GDAL_VERSION > 10800
2714 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2715 extent.
MinX -= (_scale[0] / 2.);
2716 extent.
MaxX += (_scale[0] / 2.);
2718 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2719 extent.
MinY -= (_scale[1] / 2.);
2720 extent.
MaxY += (_scale[1] / 2.);
2724 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2725 extent.
MinX -= _scale[0];
2726 extent.
MaxX += _scale[0];
2728 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2729 extent.
MinY -= _scale[1];
2730 extent.
MaxY += _scale[1];
2755 if (skewedrast == NULL) {
2756 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2758 OGR_G_DestroyGeometry(src_geom);
2765 _dim[0] = skewedrast->
width;
2766 _dim[1] = skewedrast->
height;
2776 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2778 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2783 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2785 OGR_G_DestroyGeometry(src_geom);
2798 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2799 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2800 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2810 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2818 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2819 ((NULL == ul_xw) && (NULL != ul_yw))
2821 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2824 OGR_G_DestroyGeometry(src_geom);
2834 (NULL != grid_xw) || (NULL != grid_yw)
2839 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2840 ((NULL == grid_xw) && (NULL != grid_yw))
2842 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2845 OGR_G_DestroyGeometry(src_geom);
2852 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2860 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2875 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2878 OGR_G_DestroyGeometry(src_geom);
2891 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2894 OGR_G_DestroyGeometry(src_geom);
2905 else if (NULL == scale_x) {
2917 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2920 OGR_G_DestroyGeometry(src_geom);
2927 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2933 else if (NULL == scale_y) {
2945 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2948 OGR_G_DestroyGeometry(src_geom);
2955 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2969 _dim[0] =
rast->width;
2970 _dim[1] =
rast->height;
2975 (NULL != scale_x) && (*scale_x < 0.)
2977 (NULL != scale_y) && (*scale_y > 0)
2983 (NULL != scale_x) &&
2994 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2997 OGR_G_DestroyGeometry(src_geom);
3008 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3013 (NULL != scale_y) &&
3024 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3027 OGR_G_DestroyGeometry(src_geom);
3038 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3046 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3047 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3048 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3057 _drv = GDALGetDriverByName(
"MEM");
3059 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3061 OGR_G_DestroyGeometry(src_geom);
3071 GDALDeregisterDriver(_drv);
3074 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3076 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3078 OGR_G_DestroyGeometry(src_geom);
3081 if (unload_drv) GDALDestroyDriver(_drv);
3087 cplerr = GDALSetGeoTransform(_ds, _gt);
3088 if (cplerr != CE_None) {
3089 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3091 OGR_G_DestroyGeometry(src_geom);
3096 if (unload_drv) GDALDestroyDriver(_drv);
3102 if (NULL != arg->
src_sr) {
3104 OSRExportToWkt(arg->
src_sr, &_srs);
3106 cplerr = GDALSetProjection(_ds, _srs);
3108 if (cplerr != CE_None) {
3109 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3111 OGR_G_DestroyGeometry(src_geom);
3116 if (unload_drv) GDALDestroyDriver(_drv);
3123 for (i = 0; i < arg->
numbands; i++) {
3129 if (cplerr != CE_None) {
3130 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3135 _band = GDALGetRasterBand(_ds, i + 1);
3136 if (NULL == _band) {
3137 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3145 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3146 if (cplerr != CE_None) {
3147 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3151 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3155 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3156 if (cplerr != CE_None) {
3157 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3166 OGR_G_DestroyGeometry(src_geom);
3172 if (unload_drv) GDALDestroyDriver(_drv);
3182 cplerr = GDALRasterizeGeometries(
3191 if (cplerr != CE_None) {
3192 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3194 OGR_G_DestroyGeometry(src_geom);
3199 if (unload_drv) GDALDestroyDriver(_drv);
3205 GDALFlushCache(_ds);
3209 OGR_G_DestroyGeometry(src_geom);
3213 if (unload_drv) GDALDestroyDriver(_drv);
3216 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3225 for (i = 0; i < arg->
numbands; i++) {
3226 uint8_t *
data = NULL;
3233 double nodataval = 0;
3238 if (oldband == NULL) {
3239 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3257 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3268 hasnodata, nodataval,
3272 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3283 for (
x = 0;
x < _width;
x++) {
3284 for (
y = 0;
y < _height;
y++) {
3287 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3299 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3310 if (oldband == NULL) {
3311 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3356 double _offset[2][4] = {{0.}};
3357 uint16_t _dim[2][2] = {{0}};
3362 double gt[6] = {0.};
3364 assert(NULL != rast1);
3365 assert(NULL != rast2);
3366 assert(NULL != rtnraster);
3373 rterror(
"rt_raster_from_two_rasters: Could not test for alignment on the two rasters");
3377 rterror(
"rt_raster_from_two_rasters: The two rasters provided do not have the same alignment");
3382 _dim[0][0] = rast1->
width;
3383 _dim[0][1] = rast1->
height;
3384 _dim[1][0] = rast2->
width;
3385 _dim[1][1] = rast2->
height;
3390 _rast[0]->ipX, _rast[0]->ipY,
3391 &(_offset[1][0]), &(_offset[1][1]),
3394 rterror(
"rt_raster_from_two_rasters: Could not compute offsets of the second raster relative to the first raster");
3397 _offset[1][0] = -1 * _offset[1][0];
3398 _offset[1][1] = -1 * _offset[1][1];
3399 _offset[1][2] = _offset[1][0] + _dim[1][0] - 1;
3400 _offset[1][3] = _offset[1][1] + _dim[1][1] - 1;
3403 switch (extenttype) {
3413 _offset[0][0] = -1 * _offset[1][0];
3414 _offset[0][1] = -1 * _offset[1][1];
3419 dim[0] = _dim[i][0];
3420 dim[1] = _dim[i][1];
3426 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3434 double off[4] = {0};
3448 if (_offset[1][0] < 0)
3449 off[0] = _offset[1][0];
3451 if (_offset[1][1] < 0)
3452 off[1] = _offset[1][1];
3455 off[2] = _dim[0][0] - 1;
3456 if ((
int) _offset[1][2] >= _dim[0][0])
3457 off[2] = _offset[1][2];
3458 off[3] = _dim[0][1] - 1;
3459 if ((
int) _offset[1][3] >= _dim[0][1])
3460 off[3] = _offset[1][3];
3469 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3473 dim[0] = off[2] - off[0] + 1;
3474 dim[1] = off[3] - off[1] + 1;
3488 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3506 &(_offset[0][0]), &(_offset[0][1]),
3509 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the FIRST raster relative to the output raster");
3513 _offset[0][0] *= -1;
3514 _offset[0][1] *= -1;
3519 &(_offset[1][0]), &(_offset[1][1]),
3522 rterror(
"rt_raster_from_two_rasters: Could not get offsets of the SECOND raster relative to the output raster");
3526 _offset[1][0] *= -1;
3527 _offset[1][1] *= -1;
3531 double off[4] = {0};
3535 (_offset[1][2] < 0 || _offset[1][0] > (_dim[0][0] - 1)) ||
3536 (_offset[1][3] < 0 || _offset[1][1] > (_dim[0][1] - 1))
3538 RASTER_DEBUG(3,
"The two rasters provided have no intersection. Returning no band raster");
3542 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3549 if (NULL != offset) {
3550 for (i = 0; i < 4; i++)
3551 offset[i] = _offset[i / 2][i % 2];
3558 if (_offset[1][0] > 0)
3559 off[0] = _offset[1][0];
3560 if (_offset[1][1] > 0)
3561 off[1] = _offset[1][1];
3563 off[2] = _dim[0][0] - 1;
3564 if (_offset[1][2] < _dim[0][0])
3565 off[2] = _offset[1][2];
3566 off[3] = _dim[0][1] - 1;
3567 if (_offset[1][3] < _dim[0][1])
3568 off[3] = _offset[1][3];
3570 dim[0] = off[2] - off[0] + 1;
3571 dim[1] = off[3] - off[1] + 1;
3577 rterror(
"rt_raster_from_two_rasters: Could not create output raster");
3590 rterror(
"rt_raster_from_two_rasters: Could not get spatial coordinates of upper-left pixel of output raster");
3601 &(_offset[0][0]), &(_offset[0][1]),
3604 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the FIRST raster relative to the output raster");
3608 _offset[0][0] *= -1;
3609 _offset[0][1] *= -1;
3614 &(_offset[1][0]), &(_offset[1][1]),
3617 rterror(
"rt_raster_from_two_rasters: Could not get pixel coordinates to compute the offsets of the SECOND raster relative to the output raster");
3621 _offset[1][0] *= -1;
3622 _offset[1][1] *= -1;
3626 rterror(
"rt_raster_from_two_rasters: Extent type ET_CUSTOM is not supported by this function");
3631 if (NULL != offset) {
3632 for (i = 0; i < 4; i++)
3633 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...
void rt_band_init_value(rt_band band, double initval)
Fill in the cells of a band with a starting value frequently used to init with nodata 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,...) __attribute__((format(printf
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,...)
void void rtinfo(const char *fmt,...) __attribute__((format(printf
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
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.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
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.
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.
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.
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.
Datum covers(PG_FUNCTION_ARGS)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
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