-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_coordinates.py
69 lines (56 loc) · 2.34 KB
/
read_coordinates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from osgeo import gdal
import sys
from pyproj import Transformer
# function to get the coordinates of the corners of a raster
def get_raster_corners(raster_path):
"""
Retrieves the coordinates of the corners and center of a raster image.
Args:
raster_path (str): The path to the raster image.
Returns:
dict: A dictionary containing the coordinates of the corners and center of the raster image.
The keys of the dictionary are:
- "Upper Left": The coordinates of the upper left corner.
- "Lower Left": The coordinates of the lower left corner.
- "Upper Right": The coordinates of the upper right corner.
- "Lower Right": The coordinates of the lower right corner.
- "Center": The coordinates of the center of the raster image.
"""
dataset = gdal.Open(raster_path)
width = dataset.RasterXSize
height = dataset.RasterYSize
gt = dataset.GetGeoTransform()
def get_coords(x, y):
x_coord = gt[0] + x * gt[1] + y * gt[2]
y_coord = gt[3] + x * gt[4] + y * gt[5]
return (x_coord, y_coord)
corners = {
"Upper Left": get_coords(0, 0),
"Lower Left": get_coords(0, height),
"Upper Right": get_coords(width, 0),
"Lower Right": get_coords(width, height),
"Center": get_coords(width // 2, height // 2)
}
return corners
# function to convert coordinates from EPSG:3857 to EPSG:4326
from pyproj import Transformer
def convert_coordinates(coords):
"""
Converts coordinates from EPSG:3857 to EPSG:4326.
Args:
coords (tuple): A tuple containing the x and y coordinates.
Returns:
tuple: A tuple containing the converted longitude and latitude coordinates.
"""
transformer = Transformer.from_crs("epsg:3857", "epsg:4326", always_xy=True)
return transformer.transform(coords[0], coords[1])
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python read_coordinates.py <raster_path>")
sys.exit(1)
raster_path = sys.argv[1]
corners = get_raster_corners(raster_path)
print("corner: (longitude, latitude)")
for corner, coords in corners.items():
long_lat = convert_coordinates(coords)
print(f"{corner}: {long_lat}")