-
Notifications
You must be signed in to change notification settings - Fork 902
/
plotting_basemap_background.ipynb.txt
267 lines (267 loc) · 6.82 KB
/
plotting_basemap_background.ipynb.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Adding a background map to plots\n",
"\n",
"This example shows how you can add a background basemap to plots created\n",
"with the geopandas ``.plot()`` method. This makes use of the\n",
"[contextily](https://github.com/geopandas/contextily) package to retrieve\n",
"web map tiles from several sources (OpenStreetMap, Stamen). Also have a\n",
"look at contextily's \n",
"[introduction guide](https://contextily.readthedocs.io/en/latest/intro_guide.html#Using-transparent-layers)\n",
"for possible new features not covered here.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import geopandas\n",
"import contextily as cx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use the NYC borough boundary data that is available in geopandas\n",
"datasets. Plotting this gives the following result:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = geopandas.read_file(geopandas.datasets.get_path('nybb'))\n",
"ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matching coordinate systems \n",
"\n",
"\n",
"Before adding web map tiles to this plot, we first need to ensure the\n",
"coordinate reference systems (CRS) of the tiles and the data match.\n",
"Web map tiles are typically provided in\n",
"[Web Mercator](https://en.wikipedia.org/wiki/Web_Mercator>)\n",
"([EPSG 3857](https://epsg.io/3857)), so let us first check what\n",
"CRS our NYC boroughs are in:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.crs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we know the CRS do not match, so we need to choose in which\n",
"CRS we wish to visualize the data: either the CRS of the tiles,\n",
"the one of the data, or even a different one.\n",
"\n",
"The first option to match CRS is to leverage the `to_crs` method\n",
"of GeoDataFrames to convert the CRS of our data, here to Web Mercator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_wm = df.to_crs(epsg=3857)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can then use `add_basemap` function of contextily to easily add a\n",
"background map to our plot:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"nbsphinx-thumbnail"
]
},
"outputs": [],
"source": [
"ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want to convert the CRS of the tiles instead, which might be advisable\n",
"for large datasets, we can use the `crs` keyword argument of `add_basemap`\n",
"as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax, crs=df.crs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This reprojects map tiles to a target CRS which may in some cases cause a\n",
"loss of sharpness. See \n",
"[contextily's guide on warping tiles](https://contextily.readthedocs.io/en/latest/warping_guide.html)\n",
"for more information on the subject."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Controlling the level of detail"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can control the detail of the map tiles using the optional `zoom` keyword\n",
"(be careful to not specify a too high `zoom` level,\n",
"as this can result in a large download).:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax, zoom=12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choosing a different style"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, contextily uses the Stamen Terrain style. We can specify a\n",
"different style using ``cx.providers``:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax, source=cx.providers.Stamen.TonerLite)\n",
"ax.set_axis_off()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Adding labels as an overlay"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes, when you plot data on a basemap, the data will obscure some important map elements, such as labels,\n",
"that you would otherwise want to see unobscured. Some map tile providers offer multiple sets of partially\n",
"transparent tiles to solve this, and `contextily` will do its best to auto-detect these transparent layers\n",
"and put them on top."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax, source=cx.providers.Stamen.TonerLite)\n",
"cx.add_basemap(ax, source=cx.providers.Stamen.TonerLabels)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By splitting the layers like this, you can also independently manipulate the level of zoom on each layer,\n",
"for example to make labels larger while still showing a lot of detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
"cx.add_basemap(ax, source=cx.providers.Stamen.Watercolor, zoom=12)\n",
"cx.add_basemap(ax, source=cx.providers.Stamen.TonerLabels, zoom=10)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}