1

I have made a PyQt application where I load UAV images and I display the coordinates (lat, long) according to where the mouse pointer shows. I use this code: Converting a pixel coordinate in GeoTIFF to a latitude and longitude? . When running the code, I get this error. Any ideas?

Traceback (most recent call last):
  File "/home/just_learning/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 224, in process
    func(*args, **kwargs)
  File "/home/just_learning/.local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3005, in mouse_move
    self.set_message(s)
  File "code.py", line 151, in set_message
    gt = tif.GetGeotransform()
  File "/home/just_learning/.local/lib/python3.8/site-packages/osgeo/gdal.py", line 2045, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, Dataset, name)
  File "/home/just_learning/.local/lib/python3.8/site-packages/osgeo/gdal.py", line 74, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
  File "/home/just_learning/.local/lib/python3.8/site-packages/osgeo/gdal.py", line 69, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
AttributeError: type object 'object' has no attribute '__getattr__'

This is the code:

. . . 

import gdal
tif = gdal.Open('UAV.tif')
gt = tif.GetGeotransform()

x_min = gt[0]
x_size = gt[1]
y_min = gt[3]
y_size = gt[5]

mx, my = x, y # I feed x,y from pixel coordinates
px = mx * x_size + x_min #x pixel
py = my * y_size + y_min #y pixel

. . .

The solutions that I have found, such as this: https://stackoverflow.com/questions/28530982/why-is-object-getattr-missing does not guide me to a solution...

Update(27/09/2021): I tried this code, which seems to work:

import rasterio

with rasterio.open('rasters/raster.tif') as map_layer:
    pixels2coords = map_layer.xy(x,y)  #I feed x,y with pixel coordinates
    print(pixels2coords)

(source: https://stackoverflow.com/questions/52443906/pixel-array-position-to-lat-long-gdal-python)

What modifications should I do on the above code in order to feed it with 'WGS 84' and output coordinates in the format: 00.00000, 0.000000 ?? (Obviously not zeros but you understand the format... )

Update(29/09/2021): I need to transform WGS84 to decimal degrees...

0

0

Your Answer

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

Browse other questions tagged or ask your own question.