# 寻找一种Python方式来计算WKT线串的长度

13

srid="WGS84"
line="LINESTRING(3.0 4.0, 3.1 4.1)"
print length(line, srid)

tomkralidis，这是一个GIS网站。您的答案忽略了这是地理空间坐标之间的距离（查找SRID）。形状本身无法计算地理空间距离，因为它不了解地图投影。

18

from geopy import distance

line_wkt="LINESTRING(3.0 4.0, 3.1 4.1)"

# a number of other elipsoids are supported
distance.VincentyDistance.ELLIPSOID = 'WGS-84'
d = distance.distance

# convert the coordinates to xy array elements, compute the distance
dist = d(line.xy[0], line.xy[1])

print dist.meters

1
+1，如果可以的话，则为+10。节省了我团队的编程时间。

LarsVegas

1
@LarsVegas是的，Shapely仅处理平面坐标-因此它将精确地测量投影空间中的距离，而不是地理区域（例如WGS-1984）。
scw 2013年

4

from shapely.wkt import loads

print l.length

Mike T

2

def line_length(line):
"""Calculate length of a line in meters, given in geographic coordinates.
Args:
line: a shapely LineString object with WGS 84 coordinates
Returns:
Length of line in meters
"""
# Swap shapely (lonlat) to geopy (latlon) points
latlon = lambda lonlat: (lonlat[1], lonlat[0])
total_length = sum(distance(latlon(a), latlon(b)).meters
for (a, b) in pairs(line.coords))
return round(total_length, 0)

def pairs(lst):
"""Iterate over a list in overlapping pairs without wrap-around.

Args:
lst: an iterable/list

Returns:
Yields a pair of consecutive elements (lst[k], lst[k+1]) of lst. Last
call yields the last two elements.

Example:
lst = [4, 7, 11, 2]
pairs(lst) yields (4, 7), (7, 11), (11, 2)

Source:
/programming/1257413/1257446#1257446
"""
i = iter(lst)
prev = i.next()
for item in i:
yield prev, item
prev = item

1

Martin Burch

@MartinBurch：哎呀，你是对的。令人讨厌的东西甚至都不是[, z]，但必须将参数交换(y, x)(x, y)。感谢您发现它。您能否确定此修改是否看起来少了一些错误？
ojdo '19