Pythonist
Pythonist

Reputation: 2679

How to specify input files with the geographical metadata when reading PNG files with rasterio

I'm working with the rasterio library of Python to read in PNG files, manipulate them, and finally write GeoTiff files. These images are georeferenced, but the metadata with the geographical information is stored in two additional files I have obtained with other software: file.pgw and file.png.aux.xml (the name is important here, the name of the PNG is file.png).

Well, the issue is that the only way I have found to make rasterio "understanding" the image with the metadata is to use exactly the same filename and to have these three files in the same directory. This is inherited by the way GDAL work. By following this convention, the call to rasterio.open creates an object that not only contains the image, but also the metadata and then you can work with it, reproject etc.

But this approach inconvenient because I have several PNG files that share the same coordinates, so it's cumbersome and inefficient having to clone the .pgw and .png.aux.xml files every time I read a new PNG file. Is there a way to communicate to rasterio that the metadata is stored in a file other than file.pgw and file.png.aux.xml? Something like rasterio.read('file.png', pgwfile = 'meta_pgw.pgw', xmlfile = 'meta_xml.xml')? I haven't found anything like this in the documentation.

Upvotes: 1

Views: 675

Answers (1)

Pythonist
Pythonist

Reputation: 2679

For the sake of completeness, I have figured out a workaround this, in case someone gets the same issue. It is not possible to do what I original wanted, i.e. to specify different names for the .pgw and .png.aux.xml files. What I finally did was to use a VRT files. I use a general template (I obtained with GDAL's gdalbuildvrt) and then I create VRT files for each raster file using the template on the fly with Python, basically changing the name of the underlying raster file. These VRT files are XML, and therefere very lightweight. This approach, if not yet perfect, is cleaner than having to copy (or link) dummy files, so it's sufficient for my purposes.

Upvotes: 0

Related Questions