假设:
我需要构建一个稀疏矩阵,它对网格上的每个点都有一个虚拟单元格(因此,它的形状为(len(longitudes), len(latitudes))
);对于磁盘外的所有网格点,它应为零,对于磁盘内的所有网格点,它应为常量非零值。网格很大 - 覆盖整个地球表面,从赤道-60到+85,赤道大约10公里。
就我而言:
import pyproj
from shapely.geometry import Point, MultiPoint
from shapely.ops import transform as sh_transform
WGS84_globe = pyproj.Proj(proj='latlong', ellps='WGS84')
from functools import partial
def Disk(x, y, radius):
return Point(x, y).buffer(radius)
def disk_to_matrix(latitudes, longitudes, ref_lat, ref_lon, max_dist):
# half of the equatorial circumference of the Earth, in meters
# it is impossible for the target to be farther away than this
max_dist = min(max_dist, 20037508)
# To find all points on the Earth within a certain distance of
# a reference latitude and longitude, back-project onto the
# Earth from an azimuthal-equidistant map with its zero point
# at the reference latitude and longitude.
projection = pyproj.Proj(
proj='aeqd', ellps='WGS84', lat_0=ref_lat, lon_0=ref_lon)
to_globe = partial(
sh_transform,
partial(pyproj.transform, projection, WGS84_globe))
# pyproj (and therefore shapely) does everything in lon/lat order.
grid = MultiPoint(list(
(lon, lat)
for lon in longitudes
for lat in latitudes
))
outer_ring = to_globe(Disk(0, 0, max_dist))
ringpoints = grid.intersection(outer_ring)
# scipy.sparse juggling goes here
我在这一点上陷入困境,因为只是尝试创建MultiPoint实例使得解释器在我点击 ^ C 之前旋转了十五分钟。这需要超级,超快,因为它只是算法最里面循环的一个步骤,它将迭代这些磁盘的千。我可以预先创建它,但这会让脚本花费超过十五分钟才能启动,我有这种可怕的感觉,交叉操作和矩阵结构也会慢得令人无法接受。建议?
(当磁盘内每个点的值应该是距离零点的距离的非平凡函数时,算法的加值点。)