-
Notifications
You must be signed in to change notification settings - Fork 0
/
BML.py
80 lines (55 loc) · 2.09 KB
/
BML.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
import pymysql
from shapely.geometry import Point, MultiLineString, LineString, Polygon
from shapely.wkt import loads
from osgeo import ogr
import re
import csv
def create(vehicle_id, start_date, end_date):
csv.field_size_limit(1000000000)
connection = pymysql.connect(host='000.000.000.00',
user='user',
password='pass',
db='database',
charset='utf8mb4',
cursorclass=pymysql.cursors.SSDictCursor)
cur = connection.cursor()
# execute a select query and return results as a generator
def generator(stmt):
# error handling code removed
cur.execute(stmt)
for row in cur:
yield row
sql = "select latitude, longitude from ngp.hist_trackrecords where device_id='%s' and reportstamp between '%s' and '%s'" % (vehicle_id, start_date, end_date)
pts = set()
for l in generator(sql):
pts.add(str(Point(l['longitude'], l['latitude'])))
connection.close()
point_list = list(pts)
road = open('roads.csv')
read = csv.reader(road)
for j in read:
m = loads(j[0])
line_list = []
c = 0
for r in point_list:
if c != (len(point_list) - 1):
line = LineString([m.interpolate(m.project(loads(r))),
m.interpolate(m.project(loads(point_list[c + 1])))])
line_list.append(line)
c += 1
ml = MultiLineString(line_list)
geom = ogr.CreateGeometryFromWkt(str(ml))
length = int(geom.Length())
bml_in = str(ml.buffer(.0001))
cut = re.split(r'[()]', bml_in)
cut2 = re.split(r'[,]', cut[2])
nodes = []
if cut2 == [''] or len(cut2) < 3:
pass
else:
for coords in cut2:
cut3 = re.split(r'[ ]', coords)
retype = float(cut3[1]), float(cut3[0])
nodes.append(tuple(retype))
bml_out = Polygon(nodes)
return bml_out, length