Find Lat/long Coordinates From Pixel Point In .geotiff Using Python And Gdal
I have points defined by pixel coordinates in a .geotiff imagesat. I am trying to convert these pixel coordinates into longitude and latitude. At first a tried to adapt the given
Solution 1:
You have flipped the order in which you convert between pixels, projected coordinates and lat longs. You are doing the order pixels -> lat lngs -> projected
it should be pixels -> projected -> lat lngs
.
The following should work
from osgeo import osr, ogr, gdal
def pixel_to_world(geo_matrix, x, y):
ul_x = geo_matrix[0]
ul_y = geo_matrix[3]
x_dist = geo_matrix[1]
y_dist = geo_matrix[5]
_x = x * x_dist + ul_x
_y = y * y_dist + ul_y
return _x, _y
def build_transform_inverse(dataset, EPSG):
source = osr.SpatialReference(wkt=dataset.GetProjection())
target = osr.SpatialReference()
target.ImportFromEPSG(EPSG)
return osr.CoordinateTransformation(source, target)
def find_spatial_coordinate_from_pixel(dataset, transform, x, y):
world_x, world_y = pixel_to_world(dataset.GetGeoTransform(), x, y)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(world_x, world_y)
point.Transform(transform)
return point.GetX(), point.GetY()
ds = gdal.Open(source_directory_path + filename)
_t = build_transform_inverse(ds, 4326)
coordinates = find_spatial_coordinate_from_pixel(ds, _t, point[0], point[1])
print(coordinates)
In find_spatial_coordinate_from_pixel
i have flipped the order, such that pixel_to_world
is now called first, before the coordinate transformation.
Post a Comment for "Find Lat/long Coordinates From Pixel Point In .geotiff Using Python And Gdal"