|
16 | 16 | # See the License for the specific language governing permissions and |
17 | 17 | # limitations under the License. |
18 | 18 |
|
19 | | - |
| 19 | +import affine |
20 | 20 | import logging |
21 | 21 | import os |
22 | 22 | import subprocess |
23 | 23 | import lxml.etree as ET |
| 24 | + |
| 25 | +from osgeo import osr |
| 26 | +import mgrs |
| 27 | + |
24 | 28 | from core import S2L_config |
25 | | -from grids.mgrs_framing import pixel_center |
| 29 | +from core.image_file import S2L_ImageFile |
| 30 | + |
26 | 31 |
|
27 | 32 | logger = logging.getLogger("Sen2Like") |
28 | 33 |
|
29 | 34 |
|
| 35 | +def get_mgrs_center(tilecode: str, utm=False) -> tuple: |
| 36 | + """Get MGRS tile center coordinates in native tile UTM or WGS84 |
| 37 | +
|
| 38 | + Args: |
| 39 | + tilecode (str): tile to get center coords |
| 40 | + utm (bool, optional): Flag for output SRS . Defaults to False. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + tuple: (lat,long) coords if utm=False, else (utm, N/S, easting, northing) |
| 44 | + """ |
| 45 | + if tilecode.startswith('T'): |
| 46 | + tilecode = tilecode[1:] |
| 47 | + centercode = tilecode + '5490045100' |
| 48 | + m = mgrs.MGRS() |
| 49 | + if utm: |
| 50 | + return m.MGRSToUTM(centercode) |
| 51 | + return m.toLatLon(centercode) |
| 52 | + |
| 53 | + |
30 | 54 | class Sen2corClient: |
31 | 55 |
|
32 | 56 | gipp_template_file = os.path.join( |
@@ -117,26 +141,63 @@ def _write_gipp(self, product): |
117 | 141 | # ref_band = None is considered as S2 product format (S2A, S2B, S2P prisma) |
118 | 142 | ref_band = self.roi_ref_band.get(product.mtl.mission, None) |
119 | 143 |
|
120 | | - if ref_band is None: |
121 | | - logger.debug("For sentinel, sen2cor don't use ROI") |
122 | | - ET.ElementTree(root).write(gipp_path, encoding='utf-8', xml_declaration=True) |
123 | | - return gipp_path |
| 144 | + if ref_band: |
| 145 | + # Compute ROI center for landsat and fill template with result |
| 146 | + ref_band_file = product.get_band_file(ref_band) |
124 | 147 |
|
125 | | - ref_band_file = product.get_band_file(ref_band) |
| 148 | + y, x = self._pixel_center(ref_band_file) |
126 | 149 |
|
127 | | - y, x = pixel_center(ref_band_file, self.out_mgrs) |
128 | | - logger.debug('Pixel center : (%s, %s)', y, x) |
| 150 | + logger.debug('Pixel center : (%s, %s)', y, x) |
129 | 151 |
|
130 | | - row0 = root.find('Common_Section/Region_Of_Interest/row0') |
131 | | - row0.text = str(y) |
132 | | - col0 = root.find('Common_Section/Region_Of_Interest/col0') |
133 | | - col0.text = str(x) |
| 152 | + row0 = root.find('Common_Section/Region_Of_Interest/row0') |
| 153 | + row0.text = str(y) |
| 154 | + col0 = root.find('Common_Section/Region_Of_Interest/col0') |
| 155 | + col0.text = str(x) |
134 | 156 |
|
135 | | - ET.ElementTree(root).write(gipp_path, encoding='utf-8', xml_declaration=True) |
| 157 | + ET.ElementTree(root).write(gipp_path, encoding='utf-8', xml_declaration=True) |
| 158 | + |
| 159 | + else: |
| 160 | + logger.debug("For sentinel, sen2cor don't use ROI") |
136 | 161 |
|
137 | 162 | logger.info('GIPP L2A : %s', gipp_path) |
| 163 | + |
| 164 | + ET.ElementTree(root).write(gipp_path, encoding='utf-8', xml_declaration=True) |
| 165 | + |
138 | 166 | return gipp_path |
139 | 167 |
|
| 168 | + def _pixel_center(self, image: S2L_ImageFile): |
| 169 | + """Get mgrs tile center coordinates from tile code in image coordinates |
| 170 | +
|
| 171 | + Args: |
| 172 | + image (S2L_ImageFile): image to get srs from |
| 173 | +
|
| 174 | + Returns: |
| 175 | + tuple: (y,x) image coordinates |
| 176 | + """ |
| 177 | + |
| 178 | + lat, lon = get_mgrs_center(self.out_mgrs) # pylint: disable=W0632 |
| 179 | + |
| 180 | + # Transform src SRS |
| 181 | + wgs84_srs = osr.SpatialReference() |
| 182 | + wgs84_srs.ImportFromEPSG(4326) |
| 183 | + |
| 184 | + # Transform dst SRS |
| 185 | + image_srs = osr.SpatialReference(wkt=image.projection) |
| 186 | + |
| 187 | + # convert MGRS center coordinates from lat lon to image EPSG coordinates (UTM) |
| 188 | + transformation = osr.CoordinateTransformation(wgs84_srs, image_srs) |
| 189 | + easting, northing, _ = transformation.TransformPoint(lat, lon) |
| 190 | + |
| 191 | + # northing = y = latitude, easting = x = longitude |
| 192 | + tr = affine.Affine( |
| 193 | + image.yRes, 0, image.yMax, |
| 194 | + 0, image.xRes, image.xMin |
| 195 | + ) |
| 196 | + |
| 197 | + # compute y,x in image coordinates |
| 198 | + y, x = (northing, easting) * (~ tr) |
| 199 | + return int(y), int(x) |
| 200 | + |
140 | 201 |
|
141 | 202 | class Sen2corError(Exception): |
142 | 203 | pass |
0 commit comments