2
2
Test Figure.tilemap.
3
3
"""
4
4
5
+ import importlib
6
+ from unittest .mock import patch
7
+
5
8
import pytest
6
9
from pygmt import Figure
7
10
8
- contextily = pytest .importorskip ("contextily" )
9
- rioxarray = pytest .importorskip ("rioxarray" )
11
+ try :
12
+ import contextily
13
+
14
+ _HAS_CONTEXTILY = True
15
+ except ImportError :
16
+ _HAS_CONTEXTILY = False
17
+
18
+ _HAS_RIOXARRAY = bool (importlib .util .find_spec ("rioxarray" ))
10
19
11
20
12
21
@pytest .mark .mpl_image_compare
22
+ @pytest .mark .skipif (not _HAS_CONTEXTILY , reason = "contextily is not installed" )
13
23
def test_tilemap_web_mercator ():
14
24
"""
15
25
Create a tilemap plot in Spherical Mercator projection (EPSG:3857).
@@ -27,6 +37,10 @@ def test_tilemap_web_mercator():
27
37
28
38
@pytest .mark .benchmark
29
39
@pytest .mark .mpl_image_compare
40
+ @pytest .mark .skipif (
41
+ not (_HAS_CONTEXTILY and _HAS_RIOXARRAY ),
42
+ reason = "contextily and rioxarray are not installed" ,
43
+ )
30
44
def test_tilemap_ogc_wgs84 ():
31
45
"""
32
46
Create a tilemap plot using longitude/latitude coordinates (OGC:WGS84), centred on
@@ -45,6 +59,10 @@ def test_tilemap_ogc_wgs84():
45
59
46
60
@pytest .mark .mpl_image_compare
47
61
@pytest .mark .parametrize ("no_clip" , [False , True ])
62
+ @pytest .mark .skipif (
63
+ not (_HAS_CONTEXTILY and _HAS_RIOXARRAY ),
64
+ reason = "contextily and rioxarray are not installed" ,
65
+ )
48
66
def test_tilemap_no_clip (no_clip ):
49
67
"""
50
68
Create a tilemap plot clipped to the Southern Hemisphere when no_clip is False, but
@@ -60,3 +78,34 @@ def test_tilemap_no_clip(no_clip):
60
78
no_clip = no_clip ,
61
79
)
62
80
return fig
81
+
82
+
83
+ @pytest .mark .skipif (_HAS_CONTEXTILY , reason = "contextily is installed." )
84
+ def test_tilemap_no_contextily ():
85
+ """
86
+ Raise an ImportError when contextily is not installed.
87
+ """
88
+ fig = Figure ()
89
+ with pytest .raises (ImportError , match = "Package `contextily` is required" ):
90
+ fig .tilemap (
91
+ region = [- 20000000.0 , 20000000.0 , - 20000000.0 , 20000000.0 ],
92
+ zoom = 0 ,
93
+ lonlat = False ,
94
+ frame = "afg" ,
95
+ )
96
+
97
+
98
+ @pytest .mark .skipif (_HAS_RIOXARRAY , reason = "rioxarray is installed." )
99
+ def test_tilemap_no_rioxarray ():
100
+ """
101
+ Raise an ImportError when rioxarray is not installed and contextily is installed.
102
+ """
103
+ fig = Figure ()
104
+ # In our CI, contextily and rioxarray are installed together, so we will see the
105
+ # error about contextily, not rioxarray. Here we mock contextily as installed, to
106
+ # make sure that we see the rioxarray error message when rioxarray is not installed.
107
+ with patch ("pygmt.datasets.tile_map._HAS_CONTEXTILY" , True ):
108
+ with pytest .raises (ImportError , match = "Package `rioxarray` is required" ):
109
+ fig .tilemap (
110
+ region = [- 180.0 , 180.0 , - 90 , 90 ], zoom = 0 , lonlat = True , frame = "afg"
111
+ )
0 commit comments