1

I have a couple of raster layers and for my next Plugin, I want to get the corner coordinates for each layer.

My idea was to use the GDAL-Tool 'Rasterinformation' which gives as an output a lot of information about a raster file including the corner coordinates. However, after running the tool, the output is presented in the GUI.

enter image description here

How do I manage this in PyQGIS? My code so far is the following:

    def selectRasters(self):
    inputDir = self.getInputPath()
    for lyr in glob.glob(os.path.join(inputDir, "*xyz")):
        info = []
        processing.run('gdal:rasterinfo', lyr, False, True, True, info)

My hope is that I have all the information as a list and then I can do something like raster upper left = info[5] and so on.

Long story short I want add the corner coordinates into a variable. Any ideas how to do that?

1 Answer 1

1

If your aim is just to access the corner co-ordinates, then perhaps something like the following would work better for you. This is just an example of using the extent() method of QgsRasterDataProvider to return a raster layer extent as a QgsRectangle object then store each of its corners as co-ordinate tuples in a dictionary. Of course you don't have to use a dictionary- you could store them as a nested list, list of tuples- whatever. I just think a dictionary with each corner as a key makes retrieving them easier and reduces the potential for confusion or mistakes. You can then work with each corner x and y values by indexing the tuple items, so you can easily use them to create a QgsPointXY object, return it as well known text- or whatever you like.

lyr = iface.activeLayer() #Raster layer object
rect = lyr.dataProvider().extent()
corners = dict()
corners['top_left'] = (rect.xMinimum(), rect.yMaximum())
corners['top_right'] = (rect.xMaximum(), rect.yMaximum())
corners['bottom_right'] = (rect.xMaximum(), rect.yMinimum())
corners['bottom_left'] = (rect.xMinimum(), rect.yMinimum())
print(corners)
upper_left_x = corners['top_left'][0]
upper_left_y = corners['top_left'][1]
pnt = QgsPointXY(upper_left_x, upper_left_y)
print(pnt.asWkt())

If you really want to work with gdalinfo, you could use this approach to create a text file as a temporary output, read each line into a list, and retrieve the x and y values from e.g. the top left corner, but it seems unnecessary for your task:

import processing
lyr = iface.activeLayer() #Raster layer object
outfile = 'C:\\Users\\Ben\\AppData\\Local\\Temp\\Result.txt'
processing.run('gdal:gdalinfo', {'INPUT': lyr, 'OUTPUT': outfile})
with open(outfile, "r") as info:
    info_text = []
    for line in info:
        info_text.append(line)

print(info_text[34])
top_left_x = info_text[34][13:25]
top_left_y = info_text[34][26:38]
print(top_left_x, top_left_y)

Run the algorithm first and check the log output to make sure you are getting the right line/ list item.

There are different approaches to reading the text file into a list. You could start here and make up your own mind, but that part of the process is off topic for gis.stackexchange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.