如何在 Python 中使用 Geopandas 更改 crs 投影?
How do I change the crs projection using Geopandas in Python?
我正在尝试将 .crs 从圆柱投影 (WGS84 (lat/lon)) 更改为墨卡托投影。
可以在此处找到一些信息 (https://geopandas.org/projections.html)。但是,对于比利时的这个 shapefile,它似乎对我不起作用。 (geopandas-website for the world 上的示例运行良好,因此所有库都已正确安装)
有人知道问题可能是什么? -> 对于比利时的这个 shapefile,我的 .crs 保持圆柱形并且不会更改为墨卡托投影。 (数据集 'BELGIUM__Municipalities.shp' -> https://hub.arcgis.com/datasets/esribeluxdata::belgium-municipalities-1)
示例代码:
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()
编辑:
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()
不会就地重新投影,它 returns 一个新对象。如果你想像这样使用你的代码,你必须将它分配回 municipalities
。
municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection
最重要的是,您的绘图代码将不起作用,正确的语法是这样的:
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
请注意数字中的 .
而不是 ,
。
我正在尝试将 .crs 从圆柱投影 (WGS84 (lat/lon)) 更改为墨卡托投影。 可以在此处找到一些信息 (https://geopandas.org/projections.html)。但是,对于比利时的这个 shapefile,它似乎对我不起作用。 (geopandas-website for the world 上的示例运行良好,因此所有库都已正确安装) 有人知道问题可能是什么? -> 对于比利时的这个 shapefile,我的 .crs 保持圆柱形并且不会更改为墨卡托投影。 (数据集 'BELGIUM__Municipalities.shp' -> https://hub.arcgis.com/datasets/esribeluxdata::belgium-municipalities-1)
示例代码:
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()
编辑:
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()
不会就地重新投影,它 returns 一个新对象。如果你想像这样使用你的代码,你必须将它分配回 municipalities
。
municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection
最重要的是,您的绘图代码将不起作用,正确的语法是这样的:
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)
请注意数字中的 .
而不是 ,
。