python - Unable to translate to Mercator projection using geopandas
问题描述
I am trying to plot a map of Spain using Geopandas and Matplotlib. I am using the GeoJSON file you see below in which the geometry is in geographical coordinates (EPSG 4326)
I would like to plot the map in Mercator projection but when I try to convert the units using
df.to_crs({'init': 'epsg:3395'})
I receive an error saying:
RuntimeError: b'no arguments in initialization list'
I have followed the instructions in http://geopandas.org/projections.html. There it says that if the geopandas dataframe does not count with the information of the CRS, you must define it. But this is not the problem here.
This is the code I have so far
import geopandas as gpd
%matplotlib inline
import matplotlib.pyplot as plt
geojson_url = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/spain-provinces.geojson'
df = gpd.read_file(geojson_url)
If I run the following command:
df.crs
I receive the following response (which means that the geopandas DataFrame have the information about CRS)
{'init': 'epsg:4326'}
Then
df = df.to_crs({'init': 'epsg:3395'})
Returns an error
What am I missing? Thanks in advance.
解决方案
看来您使用的是旧版本的 geopandas 和 pyproj。我建议升级到适用的较新版本:
>>> import geopandas
>>> geopandas.__version__
'0.5.0'
>>> import pyproj
>>> pyproj.__version__
'2.2.0'
>>> geojson_url = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/spain-provinces.geojson'
>>> df = geopandas.read_file(geojson_url)
>>> df.crs
{'init': 'epsg:4326'}
>>> df2 = df.to_crs({'init': 'epsg:3395'})
>>> df2.crs
{'init': 'epsg:3395'}
推荐阅读
- node.js - 在 mongoose 和 nodejs 中提取多种语言的数据
- java - 在 Java 中发送多部分 HttpServletResponse
- kdb - 为什么 select 和 exec 为聚合函数列给出不同的结果
- python - AWS Boto3 EC2 实例所有状态代码的位置
- php - API类实现的PHP/Laravel接口
- java - 为什么 XLDeploy-maven-plugin 告诉我必须在存在时设置“file”或“fileUri”属性
- c++ - boost::mp11::mp_list 无法基于 boost::msm 为 FSM 定义正确的转换表,缺少什么?
- spring-cloud-stream - 延迟启动消费者,因为它取决于尚未处于运行状态的 MaterializedView
- tensorflow - 奇怪的张量流形状行为
- c++ - 为什么我的反向整数函数特别不适用于 2 位数的正整数?C++