Skip to content

Commit

Permalink
Merge pull request #277 from Living-with-machines/fix_geo_utils
Browse files Browse the repository at this point in the history
Fix reproject geo info
  • Loading branch information
rwood-97 authored Dec 7, 2023
2 parents 255c0c1 + 019c13f commit e385370
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions mapreader/load/geo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,37 @@ def reproject_geo_info(image_path, target_crs="EPSG:4326", calc_size_in_m=False)
height, width, _ = tiff_shape

# Calculate the size of image in meters
if calc_size_in_m in ["geodesic", "gd"]:
bottom = geodesic((ymin, xmin), (ymin, xmax)).meters
right = geodesic((ymin, xmax), (ymax, xmax)).meters
top = geodesic((ymax, xmax), (ymax, xmin)).meters
left = geodesic((ymax, xmin), (ymin, xmin)).meters

elif calc_size_in_m in ["gc", "great-circle", "great_circle"]:
bottom = great_circle((ymin, xmin), (ymin, xmax)).meters
right = great_circle((ymin, xmax), (ymax, xmax)).meters
top = great_circle((ymax, xmax), (ymax, xmin)).meters
left = great_circle((ymax, xmin), (ymin, xmin)).meters

elif not calc_size_in_m:
size_in_m = False

else:
raise NotImplementedError(
f'[ERROR] ``calc_size_in_m`` must be one of "great-circle", "great_circle", "gc", "geodesic" or "gd", not: {calc_size_in_m}'
if calc_size_in_m:
if calc_size_in_m in ["geodesic", "gd"]:
bottom = geodesic((ymin, xmin), (ymin, xmax)).meters
right = geodesic((ymin, xmax), (ymax, xmax)).meters
top = geodesic((ymax, xmax), (ymax, xmin)).meters
left = geodesic((ymax, xmin), (ymin, xmin)).meters

elif calc_size_in_m in ["gc", "great-circle", "great_circle"]:
bottom = great_circle((ymin, xmin), (ymin, xmax)).meters
right = great_circle((ymin, xmax), (ymax, xmax)).meters
top = great_circle((ymax, xmax), (ymax, xmin)).meters
left = great_circle((ymax, xmin), (ymin, xmin)).meters

else:
raise NotImplementedError(
f'[ERROR] ``calc_size_in_m`` must be one of "great-circle", "great_circle", "gc", "geodesic" or "gd", not: {calc_size_in_m}'
)

size_in_m = (left, bottom, right, top) # anticlockwise order

mean_pixel_height = np.mean([right / height, left / height])
mean_pixel_width = np.mean([bottom / width, top / width])

print(
f"[INFO] Size in meters of left/bottom/right/top: {left:.2f}/{bottom:.2f}/{right:.2f}/{top:.2f}"
)
print(
f"Each pixel is ~{mean_pixel_height:.3f} X {mean_pixel_width:.3f} meters (height x width)."
) # noqa

size_in_m = (left, bottom, right, top) # anticlockwise order

mean_pixel_height = np.mean([right / height, left / height])
mean_pixel_width = np.mean([bottom / width, top / width])

print(
f"[INFO] Size in meters of left/bottom/right/top: {left:.2f}/{bottom:.2f}/{right:.2f}/{top:.2f}"
)
print(
f"Each pixel is ~{mean_pixel_height:.3f} X {mean_pixel_width:.3f} meters (height x width)."
) # noqa
else:
size_in_m = None

return tiff_shape, tiff_proj, target_crs, coord, size_in_m

0 comments on commit e385370

Please sign in to comment.