-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
85 lines (67 loc) · 2.3 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# принимает два csv файла с проекцией 3857
import csv
import requests
from pyproj import Proj, transform
from xml.dom import minidom
from geopy import distance
from shapely.wkt import loads
from bs4 import BeautifulSoup
yourNavigationBaseURL__ = 'http://www.yournavigation.org/api/dev/route.php?flat=%s&distance=v&flon=%s&tlat=%s&tlon=%s&v=motorcar&fast=0&layer=mapnik&instructions=0'
with open('building.csv', mode='r') as csv_file:
building = []
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
building.append(row)
with open('medical.csv', mode='r') as csv_file:
medical = []
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
medical.append(row)
def transform_back(x,y):
outProj = Proj('epsg:3857')
inProj = Proj('epsg:4326')
x,y = transform(inProj,outProj,x,y)
return x,y
def transform_projection(x,y):
inProj = Proj('epsg:3857')
outProj = Proj('epsg:4326')
x,y = transform(inProj,outProj,x,y)
return x,y
def get_distance(x1,y1,x2,y2):
x1 ,y1 = transform_projection(x1,y1)
x2 , y2 = transform_projection(x2,y2)
url = yourNavigationBaseURL__ % (x1,y1,x2,y2)
r = requests.get(url)
return parser(r.text)
def parser(r):
soup = BeautifulSoup(r,"lxml")
distance = soup.find('distance').text
return distance
# responseData = minidom.parseString(r)
# distance = responseData.getElementsByTagName('distance')[0].text
# print (distance)
csv_columns = ['fid','q','x','y','distance_to_point']
def return_output():
csv_file = "new_building.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in building:
writer.writerow(data)
except IOError:
print("I/O error")
for build in building:
#do такую то функцию и возврати расстояние через апи
x1,y1 = build['x'],build['y']
distances = []
for med in medical:
x2,y2 = med['x'],med['y']
dist = get_distance(x1,y1,x2,y2)
distances.append(dist)
build['distance_to_point'] = (min(distances))
print(distances)
return_output()
print (building)