首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改上下文背景地图

更改上下文背景地图
EN

Stack Overflow用户
提问于 2019-06-12 10:04:25
回答 3查看 10K关注 0票数 11

我有这样的代码:

代码语言:javascript
复制
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()

基本上,这会产生一个背景地图,在新加坡。然而,当我运行它时,我会得到以下错误:HTTPError: Tile URL resulted in a 404 error. Double-check your tile url:http://tile.stamen.com/terrain/29/268436843/268435436.png,但是它仍然生成这个映像:

如何更改Tile URL?我仍然希望以新加坡地图为基础。

编辑

还尝试将此参数包含到add_basemap中:

url ='https://www.openstreetmap.org/#map=12/1.3332/103.7987'

导致此错误的原因:

OSError: cannot identify image file <_io.BytesIO object at 0x000001CC3CC4BC50>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-13 13:04:41

首先,确保您的GeoDataframe位于WebMercat投影(epsg=3857)中。一旦Geodataframe正确地进行了地理引用,您就可以通过Geopandas重新投影来实现这一点:

代码语言:javascript
复制
df = df.to_crs(epsg=3857)

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

代码语言:javascript
复制
### 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文档找到一个可复制的工作示例和更多信息。

代码语言:javascript
复制
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()
票数 10
EN

Stack Overflow用户

发布于 2020-05-18 06:30:07

Contextily的默认crs是epsg:3857。但是,您的数据帧在不同的CRS中。使用以下方法,请参阅手册这里

代码语言:javascript
复制
ctx.add_basemap(ax, crs='epsg:4326', source=ctx.providers.Stamen.TonerLite)

请参阅此链接以使用不同的源,如Stamen.TonerStamen.Terrain等(默认使用Stamen.Terrain)。

此外,您还可以使用df.to_crs()将数据帧转换为EPSG:3857。在这种情况下,您应该跳过crs函数中的ctx.add_basemap()参数。

票数 4
EN

Stack Overflow用户

发布于 2020-04-14 15:17:35

我太新了,没有添加评论,但我想指出,在评论中说,他们得到了404错误。检查您的大写等。雄蕊的网址是具体的这一点。例如,并不是所有的大写调用。它只是大写的第一个字母。例如:

ctx.add_basemap(ax=ax,url=ctx.providers.Stamen.Toner, zoom=10)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56559520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档