我一直在使用VK API places.getCheckins method从特定街道办理入住手续,然后将其用于情感分析。
要使它工作,我必须指定纬度和经度参数。 我已经以GeoJSON格式下载了一些街道坐标:
{
...
"properties": {
...
"name": "a-street-so-called",
...
},
"geometry": {
"type": "LineString",
"coordinates": [
[ 37.399092526176915, 55.715745258737407 ],
[ 37.398983226159537, 55.715823964808216 ]
]
}
}
您可以下载here(166 mb)。
从这些中我得到一个带脚本的指定街道的坐标:
def get_streets(name):
coordinates = []
for i in data['features']:
try:
if i['properties']['name'] == name:
coordinates.append(i['geometry']['coordinates'])
except:
None
return coordinates
这会产生类似的结果(我认为它是一个(“type”:“LineString”)):
[
[37.625916884336014, 55.67560424062041],
[37.62689513625539, 55.67304407211511],
[37.62689513625539, 55.67304407211511],
[37.627487820628794, 55.671551422797954],
[37.63091308536064, 55.66356606746359],
[37.631465368960754, 55.663102380580035],
...
]
或者,如果GeoJSON文件中有多个街道实例(“type”:“MultiLineString”):
[
[
[37.625916884336014, 55.67560424062041],
[37.62689513625539, 55.67304407211511]
],
[
[37.62689513625539, 55.67304407211511],
[37.627487820628794, 55.671551422797954]
],
[
[37.63091308536064, 55.66356606746359],
[37.631465368960754, 55.663102380580035],
...
],
...
]
但街道直线部分的坐标之间存在间隙:
我正在填写:
def calc_points(lat_0, lon_0, lat_1, lon_1):
"""
The function takes in two coordinates and returns a
list of new coordinates, which lie on the line between
the first two.
"""
new_points = []
y_displacement = lat_0 - lat_1
x_displacement = lon_0 - lon_1
# Using the formula for a line: y = m * x + b.
m = y_displacement / x_displacement
b = lat_0 - m * lon_0
x = lon_0
if lon_1 > lon_0:
while x < lon_1:
x += 0.00001
lat_new = round(m * x + b, 6)
new_points.append((x, lat_new))
elif lon_0 > lon_1:
while x > lon_1:
x -= 0.00001
lat_new = round(m * x + b, 6)
new_points.append((x, lat_new))
return new_points
然后我试图自动完成所有这些:
def calc_streets(coordinates):
j = 0
# check if the coordinates list is nested
if coordinates[0][0] != None:
for i in coordinates:
threshold = len(i) - 1
while j < threshold:
new = calc_points(i[j][1],
i[j][0],
i[j+1][1],
i[j+1][0])
coordinates.append(new)
j += 1
else:
threshold = len(coordinates) - 1
while j < threshold:
new = calc_points(coordinates[j][1],
coordinates[j][0],
coordinates[j+1][1],
coordinates[j+1][0])
coordinates.append(new)
j += 1
这让我想知道是否有更好的办法来办理登机手续。 感谢。