[英]How do I change the crs projection using Geopandas in Python?
I'm trying to change the.crs from a cilindrical projection (WGS84 (lat/lon)) to a Mercator-projection.我正在尝试将 the.crs 从圆柱投影(WGS84(纬度/经度))更改为墨卡托投影。 Some information can be found here ( https://geopandas.org/projections.html ).
一些信息可以在这里找到( https://geopandas.org/projections.html )。 However it doesn't seem to work for me for this shapefile of Belgium.
但是,对于比利时的这个 shapefile,它似乎对我不起作用。 (the example on the geopandas-website for the world worked well so all libraries are installed correctly) Someone an idea what the problem might be?
(世界geopandas网站上的示例运行良好,因此所有库都已正确安装)有人知道问题可能是什么吗? -> My.crs stays cilindrical and does not change to Mercator-projection for this shapefile of Belgium.
-> My.crs 保持圆柱形,不会更改为比利时的这个 shapefile 的墨卡托投影。 (dataset 'BELGIUM__Municipalities.shp' -> https://hub.arcgis.com/datasets/esribeluxdata::belgium-municipalities-1 )
(数据集'BELGIUM__Municipalities.shp'-> https://hub.arcgis.com/datasets/esribeluxdata::belgium-municipalities-1 )
Example-code:示例代码:
import geopandas
import fiona
import matplotlib.pyplot as plt
import pandas as pd
def records(filename, list):
list = sorted(list)
with fiona.open(filename) as source:
for i, feature in enumerate(sourceô:max(list)+1):
if i in list:
yield feature
a = list(range(588))
municipalities = geopandas.GeoDataFrame.from_features(records("BELGIUM__Municipalities.shp",a))
municipalities.crs = "epsg:4326" #WGS84(lat/lon)-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
municipalities.to_crs("epsg:3395") #Mercator-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
plt.show()
EDIT:编辑:
import geopandas
import fiona
import matplotlib.pyplot as plt
import pandas as pd
def records(filename, list):
list = sorted(list)
with fiona.open(filename) as source:
for i, feature in enumerate(sourceô:max(list)+1):
if i in list:
yield feature
a = list(range(588))
municipalities = geopandas.GeoDataFrame.from_features(records("BELGIUM__Municipalities.shp",a))
municipalities.crs = "epsg:4326" #WGS84(lat/lon)-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
plt.show()
GeoDataFrame.to_crs()
does not re-project inplace, it returns a new object. GeoDataFrame.to_crs()
不会就地重新投影,它会返回一个新的 object。 You have to assign it back to municipalities
if you want to use your code like this.如果您想像这样使用您的代码,您必须将其分配回
municipalities
。
municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection
On top of that, your plotting code will not work, the correct syntax is this:最重要的是,您的绘图代码将不起作用,正确的语法是:
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
Note the .
注意
.
instead of ,
in numbers.而不是
,
在数字上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.