簡體   English   中英

如何在 Python 中使用 Geopandas 更改 crs 投影?

[英]How do I change the crs projection using Geopandas in Python?

我正在嘗試將 the.crs 從圓柱投影(WGS84(緯度/經度))更改為墨卡托投影。 一些信息可以在這里找到( https://geopandas.org/projections.html )。 但是,對於比利時的這個 shapefile,它似乎對我不起作用。 (世界geopandas網站上的示例運行良好,因此所有庫都已正確安裝)有人知道問題可能是什么嗎? -> My.crs 保持圓柱形,不會更改為比利時的這個 shapefile 的墨卡托投影。 (數據集'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()不會就地重新投影,它會返回一個新的 object。 如果您想像這樣使用您的代碼,您必須將其分配回municipalities

municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection

最重要的是,您的繪圖代碼將不起作用,正確的語法是:

municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)

注意. 而不是,在數字上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM