根据上下文,add_basemap推断的缩放级别无效,并且更改缩放参数无法解决问题 [英] Contextily add_basemap inferred zoom level is not valid and changing zoom parameter doesn't fix issue

查看:91
本文介绍了根据上下文,add_basemap推断的缩放级别无效,并且更改缩放参数无法解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在属性地址的绘制点后面绘制墨尔本的背景图.

我使用了以下代码:

 将pandas导入为pd以gpd格式导入geopandas从shapely.geometry导入形状导入matplotlib.pyplot作为plt根据上下文导入MELB_PROPERTY_DATA ="https://data.melbourne.vic.gov.au/resource/imwx-szwr.json"属性= 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.plot(markersize = 1)contextily.add_basemap(ax)plt.show() 

在contextily.add_basemap(ax)行中,我获得了以下UserWarning.

contextily \ tile.py:632:用户警告:推断的缩放级别30为对于当前的图块提供程序无效(有效缩放:0-18).

我阅读了

解决方案

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

请参阅下面的最终代码,并注释掉错误的行以显示差异.

 将pandas导入为pd以gpd格式导入geopandas从shapely.geometry导入形状导入matplotlib.pyplot作为plt根据上下文导入MELB_PROPERTY_DATA ="https://data.melbourne.vic.gov.au/resource/imwx-szwr.json"属性= 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')斧= properties_geo.plot(markersize = 1)#contextily.add_basemap(ax)contextily.add_basemap(ax,crs = properties_geo.crs.to_string())plt.show() 

I want to plot the background map of Melbourne behind the plotted points of property addresses.

I used the following code:

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()

At the contextily.add_basemap(ax) line I get the following UserWarning.

contextily\tile.py:632: UserWarning: The inferred zoom level of 30 is not valid for the current tile provider (valid zooms: 0 - 18).

I read the Contextily docs but they don't fix my problem.

Changing the line to contextily.add_basemap(ax, zoom=5) removes the UserWarning but still no background map appears. Similar questions have been asked on SO, but I can't retrofit them to my problem.

I feel like I'm importing lots of libraries for this simple task as well, so if you have any suggestions to fine-tune it that would also be appreciated.

解决方案

I solved this by realising from swatchai's comment that a Coordinate Reference System (CRS) was never defined.

See below for final code, with erroneous lines commented out to show the difference.

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()

这篇关于根据上下文,add_basemap推断的缩放级别无效,并且更改缩放参数无法解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆