【问题标题】:Contextily add_basemap inferred zoom level is not valid and changing zoom parameter doesn't fix issue上下文 add_basemap 推断的缩放级别无效,更改缩放参数不能解决问题
【发布时间】:2020-11-30 10:19:41
【问题描述】:

我想在物业地址的标绘点后面绘制墨尔本的背景地图。

我使用了以下代码:

import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
import matplotlib.pyplot as plt
import contextily

MELB_PROPERTY_DATA = "https://data.melbourne.vic.gov.au/resource/imwx-szwr.json"

properties = pd.read_json(MELB_PROPERTY_DATA)
properties['the_geom'] = properties['the_geom'].apply(shape)
properties_geo = gpd.GeoDataFrame(properties).set_geometry('the_geom')

ax = properties_geo.plot(markersize=1)
contextily.add_basemap(ax)
plt.show()

在 contextily.add_basemap(ax) 行,我收到以下 UserWarning。

contextily\tile.py:632: UserWarning: 推断的缩放级别 30 是 对当前磁贴提供者无效(有效缩放:0 - 18)。

我阅读了Contextily docs,但它们并没有解决我的问题。

将行更改为 contextily.add_basemap(ax, zoom=5) 会删除 UserWarning 但仍然没有出现背景地图。 在 SO 上也有人问过类似的问题,但我无法将它们改造成我的问题。

我觉得我也为这个简单的任务导入了很多库,所以如果您有任何微调它的建议,也将不胜感激。

【问题讨论】:

  • 底图和数据必须在同一个投影上,试试contextily.add_basemap(ax, crs=properties_geo.crs.to_string()

标签: python geospatial geopandas contextily


【解决方案1】:

我从 swatchai 的评论中意识到从未定义过坐标参考系统 (CRS),从而解决了这个问题。

最终代码见下文,错误的行被注释掉以显示差异。

import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
import matplotlib.pyplot as plt
import contextily

MELB_PROPERTY_DATA = "https://data.melbourne.vic.gov.au/resource/imwx-szwr.json"

properties = pd.read_json(MELB_PROPERTY_DATA)
properties['the_geom'] = properties['the_geom'].apply(shape)

# properties_geo = gpd.GeoDataFrame(properties).set_geometry('the_geom')
properties_geo = gpd.GeoDataFrame(properties, geometry='the_geom', crs='EPSG:4326')

ax = properties_geo.plot(markersize=1)

# contextily.add_basemap(ax)
contextily.add_basemap(ax, crs=properties_geo.crs.to_string())

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2017-01-30
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多