Skip to content

Commit fde94ad

Browse files
committed
Reproject and plot map in lonlat coordinates by default
GIven a region in lonlat coordinates, ensure that the output figure is also plotted in lonlat instead of Web Mercator.
1 parent 0a03a47 commit fde94ad

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

pygmt/src/tilemap.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ def tilemap(
4343
:func:`pygmt.datasets.load_tile_map` into a georeferenced form, and plots
4444
the tiles as a basemap or overlay using :meth:`pygmt.Figure.grdimage`.
4545
46+
**Note**: By default, standard web map tiles served in a Web Mercator
47+
(EPSG:3857) Cartesian format will be reprojected to a geographic coordinate
48+
reference system (OGC:WGS84) and plotted with longitude/latitude bounds
49+
when ``lonlat=True``. If reprojection is not desired, please set
50+
``lonlat=False`` and provide Web Mercator (EPSG:3857) coordinates to the
51+
``region`` parameter.
52+
4653
{aliases}
4754
4855
Parameters
@@ -128,6 +135,13 @@ def tilemap(
128135
wait=wait,
129136
max_retries=max_retries,
130137
)
138+
139+
# Reproject raster from Web Mercator (EPSG:3857) to lonlat (OGC:CRS84) if
140+
# bounding box region was provided in lonlat
141+
if lonlat and raster.rio.crs == "EPSG:3857":
142+
raster = raster.rio.reproject(dst_crs="OGC:CRS84")
143+
raster.gmt.gtype = 1 # set to geographic type
144+
131145
with GMTTempFile(suffix=".tif") as tmpfile:
132146
raster.rio.to_raster(raster_path=tmpfile.name)
133147
with Session() as lib:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: cc40d807da1218cd440dd8655f5f57fb
3+
size: 56875
4+
path: test_tilemap_ogc_wgs84.png
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
outs:
22
- md5: ea1fc9d829e8534aed7dac825ea0ddff
33
size: 123201
4-
path: test_tilemap.png
4+
path: test_tilemap_web_mercator.png

pygmt/tests/test_tilemap.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,32 @@
99

1010

1111
@pytest.mark.mpl_image_compare
12-
def test_tilemap():
12+
def test_tilemap_web_mercator():
1313
"""
14-
Create a simple tilemap plot.
14+
Create a tilemap plot in Web Mercator projection (EPSG:3857).
1515
"""
1616
fig = Figure()
17-
fig.tilemap(region=[-180.0, 180.0, -90, 90], zoom=0, frame="afg")
17+
fig.tilemap(
18+
region=[-20000000.0, 20000000.0, -20000000.0, 20000000.0],
19+
zoom=0,
20+
lonlat=False,
21+
frame="afg",
22+
)
23+
return fig
24+
25+
26+
@pytest.mark.mpl_image_compare
27+
def test_tilemap_ogc_wgs84():
28+
"""
29+
Create a tilemap plot using longitude/latitude coordinates (OGC:WGS84),
30+
centred on the international date line.
31+
"""
32+
fig = Figure()
33+
fig.tilemap(
34+
region=[-180.0, 180.0, -90, 90],
35+
zoom=0,
36+
frame="afg",
37+
projection="R180/5c",
38+
verbose=True,
39+
)
1840
return fig

0 commit comments

Comments
 (0)