Transforms an image computed in (longitude,latitude) coordinates into the a Mercator projection image. Parameters ---------- data: numpy array or equivalent list-like object. Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) lat_bounds : length 2 tuple Minim
(
data: Any,
lat_bounds: Tuple[float, float],
origin: str = "upper",
height_out: Optional[int] = None,
)
| 214 | |
| 215 | |
| 216 | def mercator_transform( |
| 217 | data: Any, |
| 218 | lat_bounds: Tuple[float, float], |
| 219 | origin: str = "upper", |
| 220 | height_out: Optional[int] = None, |
| 221 | ) -> np.ndarray: |
| 222 | """ |
| 223 | Transforms an image computed in (longitude,latitude) coordinates into |
| 224 | the a Mercator projection image. |
| 225 | |
| 226 | Parameters |
| 227 | ---------- |
| 228 | |
| 229 | data: numpy array or equivalent list-like object. |
| 230 | Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) |
| 231 | |
| 232 | lat_bounds : length 2 tuple |
| 233 | Minimal and maximal value of the latitude of the image. |
| 234 | Bounds must be between -85.051128779806589 and 85.051128779806589 |
| 235 | otherwise they will be clipped to that values. |
| 236 | |
| 237 | origin : ['upper' | 'lower'], optional, default 'upper' |
| 238 | Place the [0,0] index of the array in the upper left or lower left |
| 239 | corner of the axes. |
| 240 | |
| 241 | height_out : int, default None |
| 242 | The expected height of the output. |
| 243 | If None, the height of the input is used. |
| 244 | |
| 245 | See https://en.wikipedia.org/wiki/Web_Mercator for more details. |
| 246 | |
| 247 | """ |
| 248 | |
| 249 | def mercator(x): |
| 250 | return np.arcsinh(np.tan(x * np.pi / 180.0)) * 180.0 / np.pi |
| 251 | |
| 252 | array = np.atleast_3d(data).copy() |
| 253 | height, width, nblayers = array.shape |
| 254 | |
| 255 | lat_min = max(lat_bounds[0], -85.051128779806589) |
| 256 | lat_max = min(lat_bounds[1], 85.051128779806589) |
| 257 | if height_out is None: |
| 258 | height_out = height |
| 259 | |
| 260 | # Eventually flip the image |
| 261 | if origin == "upper": |
| 262 | array = array[::-1, :, :] |
| 263 | |
| 264 | lats = lat_min + np.linspace(0.5 / height, 1.0 - 0.5 / height, height) * ( |
| 265 | lat_max - lat_min |
| 266 | ) |
| 267 | latslats = mercator(lat_min) + np.linspace( |
| 268 | 0.5 / height_out, 1.0 - 0.5 / height_out, height_out |
| 269 | ) * (mercator(lat_max) - mercator(lat_min)) |
| 270 | |
| 271 | out: np.ndarray = np.zeros((height_out, width, nblayers)) |
| 272 | for i in range(width): |
| 273 | for j in range(nblayers): |