问题描述
我有此代码:
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
但是,它仍然产生此图像:
Basically, this generates a background map that is in Singapore. However, when I run it, I get the following error: HTTPError: Tile URL resulted in a 404 error. Double-check your tile url:http://tile.stamen.com/terrain/29/268436843/268435436.png
However, it still produces this image:
如何更改图块URL?我仍然希望将新加坡地图作为基础图层.
How can I change the Tile URL? I would still like to have the map of Singapore as the base layer.
编辑:
还尝试将此参数包含在add_basemap中:url ='https://www.openstreetmap.org/#map=12/1.3332/103.7987'
产生此错误的原因:OSError: cannot identify image file <_io.BytesIO object at 0x000001CC3CC4BC50>
EDIT:
Also tried including this argument to add_basemap:url ='https://www.openstreetmap.org/#map=12/1.3332/103.7987'
Which produced this error:OSError: cannot identify image file <_io.BytesIO object at 0x000001CC3CC4BC50>
推荐答案
首先请确保您的GeoDataframe在Web墨卡托投影(epsg=3857
)中.对Geodataframe正确地进行地理参考后,您可以通过Geopandas重新投影来实现:
First make sure that your GeoDataframe is in Web Mercator projection (epsg=3857
). Once your Geodataframe is correctly georeferenced, you can achieve this by Geopandas reprojection:
df = df.to_crs(epsg=3857)
完成此操作后,您可以轻松选择任何受支持的地图样式.在撰写本文时,可以在contextily.sources
模块中找到完整列表:
Once you have this done, you easily choose any of the supported map styles. A full list can be found in contextily.sources
module, at the time of writing:
### 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磁贴编号(就像在"EDIT"示例中所做的那样). ctx将处理所有这一切.
Keep in mind that you should not be adding actual x,y,z tile numbers in your tile URL (like you did in your "EDIT" example). ctx will take care of all this.
您可以在 GeoPandas文档.
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()
这篇关于根据上下文更改背景图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!