gpt4 book ai didi

Python 和 Shapefile : Very large coordinates after importing shapefile

转载 作者:行者123 更新时间:2023-11-30 23:08:08 29 4
gpt4 key购买 nike

我下载了波士顿的 shapefile,并想使用下面的代码将其绘制出来。然而它给了我一个错误 ValueError: lat_0 必须在 -90.000000 和 90.000000 度之间

原来coords的值为(33869.92130000144, 777617.2998000011, 330800.31099999696, 959741.1853)为什么这么大?

Boston shapefile is obtained here

代码

# 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

最佳答案

您需要从东距和北距的 native 投影重新投影到另一个坐标引用系。如果您想要纬度和经度,通常是 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))

native box: POLYGON ((330800.310999997 777617.2998000011, 330800.310999997 959741.1853, 33869.92130000144 959741.1853, 33869.92130000144 777617.2998000011, 330800.310999997 777617.2998000011))

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

否则, basemap 所需的大部分参数都在 shp.crs 中(看一下)。

关于Python 和 Shapefile : Very large coordinates after importing shapefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900600/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com