Commits

sirex committed ff568b6 Draft

Working code with single driver tracks, without grouping.

Comments (0)

Files changed (6)

     package_dir={'': 'src'},
     install_requires=[
         'Jinja2',
+        'simplekml',
     ],
     entry_points = {
         'console_scripts': [
             'trolis=trolis.main.trolis:main',
             'serve=trolis.main.serve:main',
+            'genkml=trolis.main.genkml:main',
         ],
     },
 )
+from simplekml import Kml
+from simplekml import Style
+
+
+def render_points(points):
+    kml = Kml()
+    fol = kml.newfolder(name="A Folder")
+
+    sharedstyle = Style()
+    sharedstyle.labelstyle.color = 'ff0000ff'  # Red
+
+    for lat, lon, val in points:
+        latlon = (lat, lon)
+        pnt = fol.newpoint(name=','.join(latlon), coords=[latlon])
+        pnt.style = sharedstyle
+
+    kml.save('/tmp/lines.kml')

src/trolis/main/genkml.py

+#!/usr/bin/env python
+
+from __future__ import division
+
+import sys
+
+from datetime import datetime
+from math import sqrt
+
+from simplekml import Kml
+from simplekml import ExtendedData
+
+
+cols = (
+    'id',
+    'TimeUnix',
+    'DeviceID',
+    'StopID',
+    'Status',
+    'LineNum',
+    'RunNum',
+    'TrackType',
+    'DriverID',
+    'DelayNearest',
+    'DelayRunNum',
+    'Lat',
+    'Lng',
+    'Bearing',
+    'Speed',
+    'KmTotal',
+    'KmDelta',
+    'DirectionName',
+)
+
+
+def render_points(points):
+    cmap = ['#%X0000' % i for i in xrange(256)]
+    kml = Kml()
+    fol = kml.newfolder(name="A Folder")
+
+    vmin, vmax = sys.maxint, 0
+    for lat, lon, val in points:
+        vmin = min(val, vmin)
+        vmax = max(val, vmax)
+
+    size = vmax - vmin
+    step = 255 / size
+
+    for lat, lon, val in points:
+        color = int((val-vmin) * step)
+        latlon = (lon, lat)
+        pnt = fol.newpoint(name=','.join(map(str, latlon)), coords=[latlon])
+        pnt.extendeddata = ExtendedData()
+        pnt.extendeddata.newdata('color', cmap[color])
+
+    kml.save('output/lines.kml')
+
+
+def main():
+    points = []
+    last = None
+    with open('vehiclegpsdata.csv') as f:
+        for i, row in enumerate(f):
+            row = row.split(',')
+            assert len(row) == 18
+            timestamp, linenum, runnum, driver, lat, lon, speed  = (
+                row[1], row[5], row[6], row[8], row[11], row[12], row[14]
+            )
+            time = datetime.fromtimestamp(int(timestamp))
+            driver = None if driver == r'\N' else int(driver)
+            lat, lon = float(lat), float(lon)
+            speed = None if speed == r'\N' else float(speed)
+            waits = 0
+            if driver == 15:
+
+                if last:
+                    llat, llon, ltime = last
+                    distance = sqrt((lat - llat)**2 + (lon - llon)**2)
+                    if distance < 0.000001:
+                        waits += (time - ltime).seconds
+                    else:
+                        waits = 0
+
+                if waits > 0:
+                    points.append((lat, lon, waits))
+
+                print '%s %s %s   %0.3f %0.3f   -> %d' % (
+                    driver, linenum, time.strftime('%m-%d %H:%M:%S'), lat, lon,
+                    waits
+                )
+                last = lat, lon, time
+            if i > 1000000: break
+
+    render_points(points)

src/trolis/main/trolis.py

 from jinja2 import Environment
 from jinja2 import PackageLoader
 
+#from trolis.kml import render_points
+
 
 def main():
     env = Environment(loader=PackageLoader('trolis', 'templates'))
     template = env.get_template('index.jinja')
     stream = template.stream(name='foo')
     stream.dump('output/index.html')
+
+    #render_points([
+    #    (25.241, 54.696, 1),
+    #    (25.242, 54.696, 1),
+    #    (25.243, 54.696, 1),
+    #    (25.244, 54.696, 1),
+    #    (25.245, 54.696, 1),
+    #])

src/trolis/templates/index.jinja

     <script type="text/javascript">
     $(function () {
         var map, layer;
-        map = new OpenLayers.Map( 'map');
-        layer = new OpenLayers.Layer.OSM("Simple OSM Map");
+        map = new OpenLayers.Map('map');
+        layer = new OpenLayers.Layer.OSM('Trolis');
         map.addLayer(layer);
+
+        map.addLayer(
+          new OpenLayers.Layer.Vector('KML', {
+              strategies: [new OpenLayers.Strategy.Fixed()],
+              protocol: new OpenLayers.Protocol.HTTP({
+                  url: '/tmp/lines.kml',
+                  format: new OpenLayers.Format.KML({
+                      extractStyles: true, 
+                      extractAttributes: true,
+                      maxDepth: 2
+                  })
+              })
+          })
+        );
+
         map.setCenter(
             new OpenLayers.LonLat(25.241, 54.696).transform(
-                new OpenLayers.Projection("EPSG:4326"),
+                new OpenLayers.Projection('EPSG:4326'),
                 map.getProjectionObject()
             ), 13
         );
 # Required by:
 # trolis==0.1
 Jinja2 = 2.7.1
+
+# Added by buildout at 2013-10-26 19:31:17.188171
+
+# Required by:
+# trolis==0.1
+simplekml = 1.2.3