python - 更改上下文的背景图

标签 python python-3.x geopandas contextily

我有这个代码:

import pandas as pd
import numpy as np
from geopandas import GeoDataFrame
import geopandas
from shapely.geometry import LineString, Point
import matplotlib.pyplot as plt
import contextily

''' Do Something'''

df = start_stop_df.drop('track', axis=1)
crs = {'init': 'epsg:4326'}
gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

ax = gdf.plot()
contextily.add_basemap(ax)
ax.set_axis_off()
plt.show()

基本上,这会生成一个位于新加坡的背景 map 。但是,当我运行它时,出现以下错误:HTTPError: Tile URL resulted in a 404 error. Double-check your tile url:http://tile.stamen.com/terrain/29/268436843/268435436.png 但是,它仍然会生成此图像: Code output

如何更改磁贴 URL?我仍然希望将新加坡 map 作为基础层。

编辑:
还尝试将此参数包含在 add_basemap 中:
url ='https://www.openstreetmap.org/#map=12/1.3332/103.7987'
产生此错误的原因:
OSError: cannot identify image file <_io.BytesIO object at 0x000001CC3CC4BC50>

最佳答案

首先确保您的 GeoDataframe 使用 Web 墨卡托投影 (epsg=3857)。一旦您的 Geodataframe 被正确地地理引用,您就可以通过 Geopandas 重投影实现这一点:

df = df.to_crs(epsg=3857)

完成此操作后,您可以轻松选择任何受支持的 map 样式。在撰写本文时,可以在 contextily.sources 模块中找到完整列表:

### Tile provider sources ###

ST_TONER = 'http://tile.stamen.com/toner/tileZ/tileX/tileY.png'
ST_TONER_HYBRID = 'http://tile.stamen.com/toner-hybrid/tileZ/tileX/tileY.png'
ST_TONER_LABELS = 'http://tile.stamen.com/toner-labels/tileZ/tileX/tileY.png'
ST_TONER_LINES = 'http://tile.stamen.com/toner-lines/tileZ/tileX/tileY.png'
ST_TONER_BACKGROUND = 'http://tile.stamen.com/toner-background/tileZ/tileX/tileY.png'
ST_TONER_LITE = 'http://tile.stamen.com/toner-lite/tileZ/tileX/tileY.png'

ST_TERRAIN = 'http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'
ST_TERRAIN_LABELS = 'http://tile.stamen.com/terrain-labels/tileZ/tileX/tileY.png'
ST_TERRAIN_LINES = 'http://tile.stamen.com/terrain-lines/tileZ/tileX/tileY.png'
ST_TERRAIN_BACKGROUND = 'http://tile.stamen.com/terrain-background/tileZ/tileX/tileY.png'

ST_WATERCOLOR = 'http://tile.stamen.com/watercolor/tileZ/tileX/tileY.png'

# OpenStreetMap as an alternative
OSM_A = 'http://a.tile.openstreetmap.org/tileZ/tileX/tileY.png'
OSM_B = 'http://b.tile.openstreetmap.org/tileZ/tileX/tileY.png'
OSM_C = 'http://c.tile.openstreetmap.org/tileZ/tileX/tileY.png'

请记住,您不应在磁贴 URL 中添加实际的 x、y、z 磁贴编号(就像您在“编辑”示例中所做的那样)。 ctx 将处理所有这些。

您可以在 GeoPandas docs 找到一个可复制粘贴的工作示例和更多信息。 .

import contextily as ctx

# Dataframe you want to plot
gdf = GeoDataFrame(df, crs= {"init": "epsg:4326"}) # Create a georeferenced dataframe  
gdf = gdf.to_crs(epsg=3857) # reproject it in Web mercator
ax = gdf.plot()

# choose any of the supported maps from ctx.sources
ctx.add_basemap(ax, url=ctx.sources.ST_TERRAIN)
ax.set_axis_off()
plt.show()

关于python - 更改上下文的背景图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56559520/

相关文章:

python - Django 表单错误 : Select a valid choice. 该选项不是可用选项之一

pandas - 用 Folium 和 Geopandas 绘制多边形不起作用

python - 如何将 web.py 绑定(bind)到本地主机?

python - Python 3.6.0 中类内的方法是非本地的

python - 了解查找字典中最长键的函数

Python 高效排序字典中的并行列表

python-3.x - 如何使用全年的每小时数据计算每天的总降水量?

python - NetworkX 访问具有多个节点属性的节点

python - 如何有效地浏览和比较非常大的字典的值与列表的元素?

python-2.7 - 更改 geopandas 中的单个补丁颜色