1564 GDALDatasetH hdsSrc;
1565 GDALRasterBandH hbandSrc;
1568 int ntiles[2] = {1, 1};
1569 int _tile_size[2] = {0, 0};
1572 double gt[6] = {0.};
1573 const char* pszProjectionRef = NULL;
1584 hdsSrc = GDALOpenShared(config->
rt_file[idx], GA_ReadOnly);
1585 if (hdsSrc == NULL) {
1586 rterror(
_(
"convert_raster: Could not open raster: %s"), config->
rt_file[idx]);
1590 nband = GDALGetRasterCount(hdsSrc);
1592 rterror(
_(
"convert_raster: No bands found in raster: %s"), config->
rt_file[idx]);
1599 if (config->
nband[i] > nband) {
1600 rterror(
_(
"convert_raster: Band %d not found in raster: %s"), config->
nband[i], config->
rt_file[idx]);
1607 pszProjectionRef = GDALGetProjectionRef(hdsSrc);
1608 if (pszProjectionRef != NULL && pszProjectionRef[0] !=
'\0') {
1609 info->
srs =
rtalloc(
sizeof(
char) * (strlen(pszProjectionRef) + 1));
1610 if (info->
srs == NULL) {
1611 rterror(
_(
"convert_raster: Could not allocate memory for storing SRS"));
1615 strcpy(info->
srs, pszProjectionRef);
1618 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
1619 if (OSRSetFromUserInput(hSRS, pszProjectionRef) == OGRERR_NONE) {
1620 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
1621 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
1623 pszAuthorityName != NULL &&
1624 strcmp(pszAuthorityName,
"EPSG") == 0 &&
1625 pszAuthorityCode != NULL
1627 info->
srid = atoi(pszAuthorityCode);
1630 OSRDestroySpatialReference(hSRS);
1635 rterror(
_(
"convert_raster: could not determine source srid, cannot transform to target srid %d"), config->
out_srid);
1641 if (GDALGetGeoTransform(hdsSrc, info->
gt) != CE_None) {
1642 rtinfo(
_(
"Using default geotransform matrix (0, 1, 0, 0, 0, -1) for raster: %s"), config->
rt_file[idx]);
1650 memcpy(gt, info->
gt,
sizeof(
double) * 6);
1657 if (info->
nband == NULL) {
1658 rterror(
_(
"convert_raster: Could not allocate memory for storing band indices"));
1668 if (info->
nband == NULL) {
1669 rterror(
_(
"convert_raster: Could not allocate memory for storing band indices"));
1674 info->
nband[i] = i + 1;
1680 rterror(
_(
"convert_raster: Could not allocate memory for storing GDAL data type"));
1686 rterror(
_(
"convert_raster: Could not allocate memory for storing pixel type"));
1692 rterror(
_(
"convert_raster: Could not allocate memory for storing hasnodata flag"));
1698 rterror(
_(
"convert_raster: Could not allocate memory for storing nodata value"));
1708 info->
dim[0] = GDALGetRasterXSize(hdsSrc);
1709 info->
dim[1] = GDALGetRasterYSize(hdsSrc);
1717 info->
dim[0], info->
dim[1],
1745 hbandSrc = GDALGetRasterBand(hdsSrc, info->
nband[i]);
1748 info->
gdalbandtype[i] = GDALGetRasterDataType(hbandSrc);
1752 rterror(
_(
"convert_raster: The pixel type of band %d is a complex data type. PostGIS raster does not support complex data types"), i + 1);
1779 rtwarn(
_(
"The size of each output tile may exceed 1 GB. Use -t to specify a reasonable tile size"));
1782 if (config->
outdb) {
1786 for (ytile = 0; ytile < ntiles[1]; ytile++) {
1788 if (!config->
pad_tile && ntiles[1] > 1 && (ytile + 1) == ntiles[1])
1789 _tile_size[1] = info->
dim[1] - (ytile * info->
tile_size[1]);
1793 for (xtile = 0; xtile < ntiles[0]; xtile++) {
1796 if (!config->
pad_tile && ntiles[0] > 1 && (xtile + 1) == ntiles[0])
1797 _tile_size[0] = info->
dim[0] - (xtile * info->
tile_size[0]);
1802 GDALApplyGeoTransform(
1811 rterror(
_(
"convert_raster: Could not create raster"));
1822 _tile_size[0], _tile_size[1],
1829 rterror(
_(
"convert_raster: Could not create offline band"));
1836 rterror(
_(
"convert_raster: Could not add offlineband to raster"));
1852 rterror(
_(
"convert_raster: Could not convert PostGIS raster to hex WKB"));
1860 if (tileset->
length > 10) {
1867 rterror(
_(
"convert_raster: Could not convert raster tiles into INSERT or COPY statements"));
1879 VRTSourcedRasterBandH hbandDst;
1882 for (ytile = 0; ytile < ntiles[1]; ytile++) {
1885 if (!config->
pad_tile && ntiles[1] > 1 && (ytile + 1) == ntiles[1])
1886 _tile_size[1] = info->
dim[1] - (ytile * info->
tile_size[1]);
1890 for (xtile = 0; xtile < ntiles[0]; xtile++) {
1897 if (!config->
pad_tile && ntiles[0] > 1 && (xtile + 1) == ntiles[0])
1898 _tile_size[0] = info->
dim[0] - (xtile * info->
tile_size[0]);
1903 GDALApplyGeoTransform(
1916 hdsDst = VRTCreate(_tile_size[0], _tile_size[1]);
1920 GDALSetProjection(hdsDst, info->
srs);
1921 GDALSetGeoTransform(hdsDst, gt);
1926 hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, i + 1);
1929 GDALSetRasterNoDataValue(hbandDst, info->
nodataval[i]);
1932 hbandDst, GDALGetRasterBand(hdsSrc, info->
nband[i]),
1934 _tile_size[0], _tile_size[1],
1936 _tile_size[0], _tile_size[1],
1937 "near", VRT_NODATA_UNSET
1942 VRTFlushCache(hdsDst);
1947 rterror(
_(
"convert_raster: Could not convert VRT dataset to PostGIS raster"));
1957 for (i = 0; i < numbands; i++) {
1968 rterror(
_(
"convert_raster: Could not convert PostGIS raster to hex WKB"));
1979 if (tileset->
length > 10) {
1986 rterror(
_(
"convert_raster: Could not convert raster tiles into INSERT or COPY statements"));
GDALDataType * gdalbandtype
int rt_raster_get_num_bands(rt_raster raster)
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
static int append_stringbuffer(STRINGBUFFER *buffer, const char *str)
void rt_band_destroy(rt_band band)
Destroy a raster band.
static int insert_records(const char *schema, const char *table, const char *column, const char *filename, const char *file_column_name, int copy_statements, int out_srid, STRINGBUFFER *tileset, STRINGBUFFER *buffer)
void rtwarn(const char *fmt,...)
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
#define SRID_UNKNOWN
Unknown SRID value.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
void rtinfo(const char *fmt,...)
int rt_band_check_is_nodata(rt_band band)
Returns TRUE if the band is only nodata values.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
static void calc_tile_size(int dimX, int dimY, int *tileX, int *tileY)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
static void raster_destroy(rt_raster raster)
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
rt_band rt_band_new_offline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t bandNum, const char *path)
Create an out-db rt_band.