Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when reprojecting coordinates #374

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions mapreader/load/geo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ def reproject_geo_info(image_path, target_crs="EPSG:4326", calc_size_in_m=False)
tiff_shape, tiff_proj, tiff_coord = extractGeoInfo(image_path)

# Coordinate transformation: proj1 ---> proj2
transformer = Transformer.from_crs(tiff_proj, target_crs)
ymin, xmin = transformer.transform(tiff_coord[0], tiff_coord[1])
ymax, xmax = transformer.transform(tiff_coord[2], tiff_coord[3])
coord = (xmin, ymin, xmax, ymax)

transformer = Transformer.from_crs(tiff_proj, target_crs, always_xy=True)
coord = transformer.transform_bounds(*tiff_coord)
print(f"[INFO] New CRS: {target_crs}")
print("[INFO] Reprojected coordinates: {:.4f} {:.4f} {:.4f} {:.4f}".format(*coord))

height, width, _ = tiff_shape

# Calculate the size of image in meters
xmin, ymin, xmax, ymax = coord
if calc_size_in_m:
if calc_size_in_m in ["geodesic", "gd"]:
bottom = geodesic((ymin, xmin), (ymin, xmax)).meters
Expand Down
11 changes: 2 additions & 9 deletions mapreader/load/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,15 +2108,8 @@ def _add_geo_info_id(
tiff_proj = tiff_src.crs.to_string()
# Coordinate transformation: proj1 ---> proj2
# tiff is "lat, lon" instead of "x, y"
transformer = Transformer.from_crs(tiff_proj, target_crs)
ymin, xmin = transformer.transform(
tiff_src.bounds.left, tiff_src.bounds.bottom
)
ymax, xmax = transformer.transform(
tiff_src.bounds.right, tiff_src.bounds.top
)
# New projected coordinates
coords = (xmin, ymin, xmax, ymax)
transformer = Transformer.from_crs(tiff_proj, target_crs, always_xy=True)
coords = transformer.transform_bounds(*tiff_src.bounds)
self.parents[image_id]["coordinates"] = coords
self.parents[image_id]["crs"] = target_crs

Expand Down
2 changes: 1 addition & 1 deletion tests/test_load/test_geo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_reproject(sample_dir):
assert new_crs == "EPSG:4326"
assert reprojected_coord == approx((-0.061, 51.6142, -0.0610, 51.614), rel=1e-2)
print(size_in_m)
assert size_in_m == approx((0.5904, 0.6209, 0.594, 0.6209), rel=1e-2)
assert size_in_m == approx((0.62, 0.62, 0.62, 0.62), rel=1e-2)


def test_versus_loader(sample_dir):
Expand Down
Loading