【问题标题】:Attempting to generate a regular geographic grid using python - lat/lng exceeds limits尝试使用 python 生成常规地理网格 - lat/lng 超出限制
【发布时间】:2018-05-23 19:06:21
【问题描述】:

我想检索美国常规网格的所有纬度/经度坐标对。我从之前堆积的问题中获取了以下代码:

import shapely.geometry
import pyproj

# https://stackoverflow.com/questions/40342355/how-can-i-generate-a-regular-geographic-grid-using-python
# Set up projections
p_ll = pyproj.Proj(init='epsg:4326') #4326
p_mt = pyproj.Proj(init='epsg:3857') # espg:3857 metric; same as EPSG:900913

# Create corners of rectangle to be transformed to a grid
nw = shapely.geometry.Point((24.0,-89))  #actually SW
print(nw)
se = shapely.geometry.Point((25.0,-65)) #actually NE

# 24.0,-89
# 50.0,-65

stepsize = 50000 # 5 km grid step size

# Project corners to target projection
s = pyproj.transform(p_ll, p_mt, nw.x, nw.y) # Transform NW point to 3857
e = pyproj.transform(p_ll, p_mt, se.x, se.y) # .. same for SE
print(s)
print(e)
# Iterate over 2D area
gridpoints = []
x = s[0]
while x < e[0]:
    y = s[1]
    while y < e[1]:
        p = shapely.geometry.Point(pyproj.transform(p_mt, p_ll, x, y))
        gridpoints.append(p)
        y += stepsize
    x += stepsize

# with open('testout.csv', 'wb') as of:
#     of.write('lon;lat\n')
for p in gridpoints:
    print(str(p.x)+"," +str(p.y))
    # of.write('{:f};{:f}\n'.format(p.x, p.y))

来源:How can I generate a regular geographic grid using python?

当我的经度超过 -89.9999(-90 到 -180)时,代码不起作用,这会导致以下错误:

MacBook-Pro:Desktop user$ python updated_calculate_points.py 
Traceback (most recent call last):
  File "updated_calculate_points.py", line 19, in <module>
    s = pyproj.transform(p_ll, p_mt, nw.x, nw.y) # Transform NW point to 3857
  File "/Users/user/anaconda3/lib/python3.6/site-packages/pyproj/__init__.py", line 504, in transform
    _proj._transform(p1,p2,inx,iny,inz,radians)
  File "_proj.pyx", line 361, in _proj._transform (_proj.c:3762)
RuntimeError: b'latitude or longitude exceeded limits'

我不完全理解这些预测是如何工作的。有人可以帮忙吗?

【问题讨论】:

  • 您为什么需要超过 +/- 90 度的纬度? (北极以北或南极以南)大多数投影都不允许这样做。
  • @geocodezip - 你是对的,错误与 -90 到 -180 之间的 longitude 有关。

标签: python google-maps shapely geopy epsg


【解决方案1】:

h3 library 与定义的地理位置(即从 Openstreetmap 中提取)结合使用是一种优雅的解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多