Commit 7277a368 authored by Sven Greiner's avatar Sven Greiner
Browse files

Fix most issues

- Douglas-Peucker
    + reuse point objects
    + reflection for dynamic field access
    + use single recursive function
    + use lists instead of indices

- print and plot tracks

- select correct attributes for shape file

- much more
parent 01b2aaf0
......@@ -58,7 +58,6 @@ def recalculateTrack(points):
point = points[i]
if i>0:
point.segmentlength = point.distanceTo(points[i-1])
point.distance = points[i-1].distance + point.segmentlength
point.speed = Point.speed(points[i-1], point)
point.vspeed = Point.vspeed(points[i-1], point)
point.slope = Point.slope(points[i-1], point)
......@@ -90,62 +89,90 @@ def handleTrackpoint(tp):
#-------------------------------------------------------------------------------
def dpeuker(points,eps):
result = []
result.append(Point(points[0].latitude, points[0].longitude, points[0].time, points[0].elevation))
dp(points,eps,result,0,len(points)-1)
result.append(Point(points[-1].latitude, points[-1].longitude, points[-1].time, points[-1].elevation))
return result
def dp(points,eps,result,iu,io):
if iu+1 < io:
maxi=0
maxd=0.0
for i in range(iu+1,io):
dx = points[io].latitude - points[iu].latitude
dy = points[io].longitude - points[iu].longitude
d = fabs(points[i].longitude - points[iu].longitude - dy/dx * (points[i].latitude-points[iu].latitude))
if d>maxd:
maxi=i
maxd=d
if maxd>eps:
dp(points,eps,result,iu,maxi)
result.append(Point(points[maxi].latitude, points[maxi].longitude, points[maxi].time, points[maxi].elevation))
dp(points,eps,result,maxi,io)
def dp(points, xname, yname, eps):
dx = getattr(points[-1], xname) - getattr(points[0], xname)
dy = getattr(points[-1], yname) - getattr(points[0], yname)
m = dy/dx
dmax = 0
index = 0
for i in xrange(1, len(points) - 1):
d = fabs(getattr(points[i], yname) - getattr(points[0], yname) - m * (getattr(points[i], xname) - getattr(points[0], xname)))
if d > dmax:
dmax = d
index = i
if dmax >= eps:
return dp(points[:index+1], xname, yname, eps)[:-1] + dp(points[index:], xname, yname, eps)
else:
return [points[0], points[-1]]
#-------------------------------------------------------------------------------
def writeToShape(track, simplified, shapename):
w = shapefile.Writer(shapefile.POLYLINE)
w.autoBalance = 1
w.field('ID', 'N', '16')
w.field('Time', 'N', '16')
w.field('Distance', 'N', '16')
w.field('Elevation', 'N', '16')
w.field('StartTime', 'N', '16')
w.field('StartDist', 'N', '16')
w.field('Speed', 'N', '16')
w.field('Slope', 'N', '32')
w.field('VerticalSpeed', 'N', '32')
k = 0
for i in xrange(len(simplified) - 1):
parts = []
startPoint = simplified[i]
endPoint = simplified[i+1]
endPoint = simplified[i+1]
parts = []
for j in range(k, len(track)):
point = track[j]
parts.append([point.latitude, point.longitude])
if(point.latitude == endPoint.latitude and point.longitude == endPoint.longitude):
k = j
break
break
w.line([parts])
w.record(ID=i,Time=endPoint.time-startPoint.time,Distance=endPoint.distance - startPoint.distance,Elevation=endPoint.elevation,Speed=endPoint.speed,Slope=endPoint.slope,VerticalSpeed=endPoint.vspeed)
w.record(
ID=i,
StartTime=startPoint.time,
StartDist=startPoint.distance,
Speed=endPoint.speed,
Slope=endPoint.slope,
VerticalSpeed=endPoint.vspeed
)
w.save(shapename)
#-------------------------------------------------------------------------------
def printTrack(track):
print " lat | lon | time | distance | seglen | speed | slope | vspeed | elevation "
print "------------+------------+------------+------------+------------+------------+------------+------------+------------"
for p in track:
print " %10.6f | %10.6f | %10d | %10.2f | %10.2f | %10.2f | %10.2f | %10.2f | %10.2f" % (p.latitude, p.longitude, p.time, p.distance, p.segmentlength, p.speed, p.slope, p.vspeed, p.elevation)
#-------------------------------------------------------------------------------
def plotTrack(tracks, xname, yname):
for track in tracks:
plot([getattr(p, xname) for p in track[0]], [getattr(p, yname) for p in track[0]], track[1])
show()
#-------------------------------------------------------------------------------
if __name__ == "__main__":
gpxname = "samples/ol_egestorf.gpx"
shapename = "result.shp"
track = parseTrack(xml.dom.minidom.parse(gpxname))
simplified = dpeuker(track, 0.001)
simplified = dp(track, "distance", "elevation", 5)
recalculateTrack(simplified)
printTrack(simplified)
plotTrack([(track, 'b.'), (simplified, 'r-')], "distance", "elevation")
writeToShape(track, simplified, shapename)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment