-
Notifications
You must be signed in to change notification settings - Fork 1
/
segment_macth.py
175 lines (133 loc) · 4.92 KB
/
segment_macth.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time : 16/8/26 下午2:55
# Author : youth
# Site : www.imxingzhe.com
# File : segment_macth.py
# Desc :
import os
import ogr
import osr
import time
import math
import json
import gpxpy
import random
import gpxpy.gpx
from pyproj import Proj, transform
from shapely.geometry import Point, Polygon
"""
gdal is needed
pip install shapely gpxpy
"""
# distance to buffer polyline
BUFFER_DIS = 50
def get_gpx_data():
alts = []
with open('test.gpx', 'r') as gpx_file:
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
alts.append(ll_to_proj((point.longitude + random.choice(xrange(1, 10)) / 1000,
point.latitude + random.choice(xrange(1, 10)) / 1000)))
return alts
def ll_to_proj(ll):
p4326 = Proj(init='epsg:4326')
p3857 = Proj(init='epsg:3857')
return transform(p4326, p3857, ll[0], ll[1])
def proj_to_ll(proj):
p4326 = Proj(init='epsg:4326')
p3857 = Proj(init='epsg:3857')
return transform(p3857, p4326, proj[0], proj[1])
def get_segment_from_gpx(filename):
seg_line1 = 'LINESTRING ('
seg_line2 = 'LINESTRING ('
data = []
with open(filename, 'r') as gpx_file:
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
for segment in track.segments:
for idx, point in enumerate(segment.points):
print point.longitude, point.latitude
data.append((
float(round(point.longitude, 6)) + random.choice(xrange(10, 100))/10000,
float(round(point.latitude, 6)) + random.choice(xrange(10, 100))/10000
))
p = ll_to_proj((point.longitude, point.latitude))
if idx == 0:
seg_line1 += '%s %s' % (str(round(p[0], 3) + random.choice(xrange(10, 100))),
str(round(p[1], 3) + random.choice(xrange(10, 100))))
seg_line2 += '%s %s' % (str(round(p[0], 3)),
str(round(p[1], 3)))
else:
seg_line1 += ',%s %s' % (str(round(p[0], 3) + random.choice(xrange(10, 100))),
str(round(p[1], 3) + random.choice(xrange(10, 100))))
seg_line2 += ',%s %s' % (str(round(p[0], 3)),
str(round(p[1], 3)))
return seg_line1 + ')', seg_line2 + ')', data
def export_geojson(geom, filename):
outdriver = ogr.GetDriverByName('GeoJSON')
# Create the output GeoJSON
outdataSource = outdriver.CreateDataSource(filename)
outlayer = outdataSource.CreateLayer(filename, geom_type=ogr.wkbPolygon)
featureDefn = outlayer.GetLayerDefn()
# create a new feature
outfeature = ogr.Feature(featureDefn)
# Set new geometry
outfeature.SetGeometry(geom)
outlayer.CreateFeature(outfeature)
# destroy the feature
outfeature.Destroy
# Close DataSources
outdataSource.Destroy()
def buffer_segment(segment):
print 'create geometry'
polyline = ogr.CreateGeometryFromWkt(segment[1])
polyline1 = ogr.CreateGeometryFromWkt(segment[0])
data = segment[2]
print 'buffer'
polyline_buffer = polyline.Buffer(BUFFER_DIS)
print 'project'
source_geom = osr.SpatialReference()
source_geom.ImportFromEPSG(3857)
target_geom = osr.SpatialReference()
target_geom.ImportFromEPSG(4326)
transform_geom = osr.CoordinateTransformation(source_geom, target_geom)
polyline.Transform(transform_geom)
polyline1.Transform(transform_geom)
polyline_buffer.Transform(transform_geom)
print 'remove js'
try:
os.remove('../geogo/polyline.geojson'),
os.remove('../geogo/polyline1.geojson')
os.remove('../geogo/polygon.geojson'),
except:
pass
print 'export js'
export_geojson(polyline, '../geogo/polyline.geojson')
export_geojson(polyline1, '../geogo/polyline1.geojson')
export_geojson(polyline_buffer, '../geogo/polygon.geojson')
print 'judge within'
start = time.time()
# for i in range(0, polyline1.GetPointCount()):
# ll = polyline1.GetPoint(i)
# print ll
for ll in data:
p = ogr.CreateGeometryFromWkt('POINT (%s %s)' % (str(ll[0]), str(ll[1])))
p.Within(polyline_buffer)
end = time.time()
print (end - start)
# print len(get_gpx_data())
# p = ogr.CreateGeometryFromWkt('POINT (%s %s)' % (str(ll[0]), str(ll[1])))
#
# print p.Within(polyline_buffer)
# polyline.ExportToJson()
def run():
line = get_segment_from_gpx('test.gpx')
buffer_segment(line)
if __name__ == '__main__':
import cProfile
#
# cProfile.run("run()")
run()