From 6d2544ee555b096766dadb9fa2c893a41054bcbc Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Sat, 13 Apr 2024 13:12:11 +0800 Subject: [PATCH] `utils0.latlon2utm()`: ensure a single output UTM zone + utils.utils0.latlon2utm(): add `meta` in as required argument to invoke `force_zone_number` while calling `utm.from_latlon()`, to ensure all coordiantes are converted into the same single UTM zone, as designated in the UTM_ZONE metadata, even if they cross a UTM boundary. + adjust the `latlon2utm()` usage in all scripts: - objects.coord.py - utils.plot.py --- src/mintpy/objects/coord.py | 4 ++-- src/mintpy/utils/plot.py | 2 +- src/mintpy/utils/utils0.py | 12 +++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/mintpy/objects/coord.py b/src/mintpy/objects/coord.py index f500511a8..2b8a23195 100644 --- a/src/mintpy/objects/coord.py +++ b/src/mintpy/objects/coord.py @@ -132,7 +132,7 @@ def lalo2yx(self, lat_in, lon_in): and 'UTM_ZONE' in self.src_metadata and np.max(np.abs(lat_in)) <= 90 and np.max(np.abs(lon_in)) <= 360): - lat_in, lon_in = ut0.latlon2utm(np.array(lat_in), np.array(lon_in)) + lat_in, lon_in = ut0.latlon2utm(self.src_metadata, np.array(lat_in), np.array(lon_in)) # convert coordinates y_out = [] @@ -292,7 +292,7 @@ def geo2radar(self, lat, lon, print_msg=True, debug_mode=False): if ('UTM_ZONE' in self.src_metadata and np.max(np.abs(lat)) <= 90 and np.max(np.abs(lon)) <= 360): - lat, lon = ut0.latlon2utm(np.array(lat), np.array(lon)) + lat, lon = ut0.latlon2utm(self.src_metadata, np.array(lat), np.array(lon)) self.open() if self.geocoded: diff --git a/src/mintpy/utils/plot.py b/src/mintpy/utils/plot.py index 008a4a33c..190adc9d2 100644 --- a/src/mintpy/utils/plot.py +++ b/src/mintpy/utils/plot.py @@ -1144,7 +1144,7 @@ def plot_gps(ax, SNWE, inps, metadata=dict(), print_msg=True): # post-query: convert lat/lon to UTM for plotting if 'UTM_ZONE' in metadata.keys(): - site_lats, site_lons = ut0.latlon2utm(site_lats, site_lons) + site_lats, site_lons = ut0.latlon2utm(metadata, site_lats, site_lons) # mask out stations not coincident with InSAR data if inps.mask_gps and inps.msk is not None: diff --git a/src/mintpy/utils/utils0.py b/src/mintpy/utils/utils0.py index 1e96c38c1..fd603e8c7 100644 --- a/src/mintpy/utils/utils0.py +++ b/src/mintpy/utils/utils0.py @@ -368,16 +368,22 @@ def utm2latlon(meta, easting, northing): return lat, lon -def latlon2utm(lat, lon): +def latlon2utm(meta, lat, lon): """Convert latitude/longitude in degrees to UTM easting/northing in meters. - Parameters: lat - scalar/list/tuple/1-2D np.ndarray, WGS 84 coordinates in y direction + Parameters: meta - dict, mintpy attributes that includes: + UTM_ZONE + lat - scalar/list/tuple/1-2D np.ndarray, WGS 84 coordinates in y direction lon - scalar/list/tuple/1-2D np.ndarray, WGS 84 coordinates in x direction Returns: easting - scalar/list/tuple/1-2D np.ndarray, UTM coordinates in x direction northing - scalar/list/tuple/1-2D np.ndarray, UTM coordinates in y direction """ import utm - easting, northing = utm.from_latlon(np.array(lat), np.array(lon))[:2] + + # invoke zone_num to ensure all coordinates are converted into the same single UTM zone, + # even if they cross a UTM boundary. + zone_num = int(meta['UTM_ZONE'][:-1]) + easting, northing = utm.from_latlon(np.array(lat), np.array(lon), force_zone_number=zone_num)[:2] # output format if any(isinstance(x, (list, tuple)) for x in [lat, lon]):