How to specify the name of the input files with the geographical metadata?


juanjo.gomeznavarro@...
 

Good morning,

I'm working with PNG files, and the metadata is stored in two separate files: .pgw and .png.aux.xml. Currently I'm forced to use the same name for the three files, i.e. the image and the two auxiliary files with the geographical information. This is cumbersome and inconvenient, because I have a set of images that share the same georeferentiation and currently I'm forced to clone the .pgw and .png.aux.xml files in a somewhat silly way for introducing each image. Is there a way to tell rasterio.open that the two files with the metadata are named different from the PNG image?

Thanks a lot!


Sean Gillies
 

Hi,

On Tue, Feb 23, 2021 at 9:28 AM <juanjo.gomeznavarro@...> wrote:
Good morning,

I'm working with PNG files, and the metadata is stored in two separate files: .pgw and .png.aux.xml. Currently I'm forced to use the same name for the three files, i.e. the image and the two auxiliary files with the geographical information. This is cumbersome and inconvenient, because I have a set of images that share the same georeferentiation and currently I'm forced to clone the .pgw and .png.aux.xml files in a somewhat silly way for introducing each image. Is there a way to tell rasterio.open that the two files with the metadata are named different from the PNG image?

Thanks a lot!

The naming of the auxiliary files is a GDAL convention and cannot, to my knowledge, be changed. Neither does rasterio allow a user to open an image in read only mode and add or replace the spatial referencing and other metadata. I agree that would be handy in your case. 

Workarounds include:

Opening your PNGs in "r+" mode and patching them, like

with rasterio.open("example.png", "r+") as dataset:
    dataset.crs = rasterio.crs.CRS.from_epsg(3857)
    dataset.transform = rasterio.transform.Affine.translation(ulx, uly) * rasterio.transform.Affine.scale(dx, -dy)

Note that this would produce .pgw and .png.aux.xml files on your system.

Or you could make a generic VRT XML wrapper template and substitute PNG paths into it. See https://gdal.org/drivers/raster/vrt.html. The XML string can then be opened by rasterio.

with rasterio.open("<VRTDataset>...</VRTDataset>") as dataset:
    ...

That's not a well known feature of GDAL/rasterio but I use it quite a bit.

--
Sean Gillies


juanjo.gomeznavarro@...
 

Thanks. It's a bit strange that this is not a native feature of GDAL, as it seems pretty natural to work on bunches of files that share the same geographical information. But anyway, I have finally opted for using programmatically generated VRT files.