簡體   English   中英

Python 和 Shapefile:導入 shapefile 后坐標非常大

[英]Python and Shapefile: Very large coordinates after importing shapefile

我下載了波士頓的 shapefile 並想使用下面的代碼將其繪制出來。 但是它給了我一個錯誤ValueError: lat_0 must be between -90.000000 and 90.000000 degrees

結果coords有值(33869.92130000144, 777617.2998000011, 330800.31099999696, 959741.1853)為什么這么大?

波士頓 shapefile 在這里獲得

代碼

# Import Boston shapefile
shapefilename = 'ZIPCODES_NT_POLY'
shp = fiona.open(shapefilename + '.shp')
coords = shp.bounds
shp.close()

w, h = coords[2] - coords[0], coords[3] - coords[1]
extra = 0.01

m = Basemap(
    projection='tmerc', ellps='WGS84',
    lon_0 = np.mean([coords[0], coords[2]]), 
    lat_0 = np.mean([coords[1], coords[3]]), 
    llcrnrlon = coords[0] - extra * w,
    llcrnrlat = coords[1] - extra * h,
    urcrnrlon = coords[2] + extra * w,
    urcrnrlat = coords[3] + extra * h,
    resolution = 'i', suppress_ticks = True)

錯誤

ValueError: lat_0 must be between -90.000000 and 90.000000 degrees

您需要從東距和北距的本地投影重新投影到另一個坐標參考系統。 如果您想要緯度和經度的度數,通常是WGS84EPSG:4326 以下是重新投影它的方法:

import fiona
import pyproj
from functools import partial
from shapely.geometry import box
from shapely.ops import transform

shp = fiona.open('ZIPCODES_NT_POLY.shp', 'r')
p_in = pyproj.Proj(shp.crs)
bound_box = box(*shp.bounds)
shp.close()

p_out = pyproj.Proj({'init': 'EPSG:4326'})  # aka WGS84
project = partial(pyproj.transform, p_in, p_out)

bound_box_wgs84 = transform(project, bound_box)

print('native box: ' + str(bound_box))
print('WGS84 box: ' + str(bound_box_wgs84))

本機框:POLYGON((330800.310999997 777617.2998000011,330800.310999997 959741.1853,33869.92130000144 959741.1853,33869.92130000144 777617.2998000011,330800.310999997 777617.2998000011))

WGS84盒:POLYGON((-69.93980848410942 41.23787282321487,-69.899038698261 42.87724537285449,-73.53324195423785 42.8704709990465,-73.48147096070339 41.2312695091688,-69.93980848410942 41.23787282321487))

否則,Basemap 需要的大部分參數都在shp.crs (看看)。

您必須更改坐標參考系。 我推薦geopandas

import geopandas as gpd
import matplotlib.pyplot as plt

# load zips with the source projection
shapefilename = 'ZIPCODES_NT_POLY'
zips = gpd.read_file(shapefilename + '.shp')
# convert projection to familiar lat/lon
zips = zips.to_crs('epsg:4326')

zips.plot()
plt.show()

暫無
暫無

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

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