Monday, February 25, 2019

Traffic Mapping Fun

Traffic Mapping Fun

I am working on a little, crude, traffic mapping system for Nassau. (Anywhere really, but I am doing it for Nassau.)

It started out this way...

I developed a little android app that used its location, speed, time, etc. to build a fake URL that contains this information and then requests this fake URL from a web server that I have access to the logs for. Since this is a fake URL, it never gets back what it asks for but this is intended. The information I need is now in the logs.

I can then scan web server logs and look for lines that the app created. I have a script that puts these lines into a format that I want to parse for the web map.

I am making the web map using openlayers and openstreetmap stuff. Oh, javascript, jquery, and bootstrap too.

Not that I really know how to use this stuff fit a kick. My ignorance is vast.

So. This is the html page: wblogger.html
<pre  style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> &lt;!doctype html&gt;  
 &lt;html lang="en"&gt;  
   &lt;link rel="stylesheet" href="" type="text/css"&gt;  
    .map {  
     height: 400px;  
     width: 100%;  
    #map {  
      position: relative;  
    #popup {  
      padding-bottom: 45px;  
   &lt;script src=""&gt;&lt;/script&gt;  
   &lt;script type = "text/javascript" src = ""&gt;&lt;/script&gt;  
   &lt;script src="//"&gt;&lt;/script&gt;  
   &lt;link href="//" rel="stylesheet"&gt;  
   &lt;script type="text/javascript"&gt;  
      $( document ).ready(function() {  
        console.log( "ready!" );  
   &lt;title&gt;OpenLayers example&lt;/title&gt;  
   &lt;h2&gt;zotzBrothers Traffic Map&lt;/h2&gt;  
   &lt;div id="map" style="width:1200px;height:800px" class="map"&gt;&lt;/div&gt;  
        &lt;div id="popup"&gt;&lt;/div&gt;  
   &lt;script type="text/javascript"&gt;  
   window.carLocs = new Array();  
        $.get('systemlines.txt', function(data){  
       window.carLocs = data.split('\n');  
 function mymap(){  
   var vectorSource = new ol.source.Vector({  
    //create empty vector  
   //create the style  
   var iconStyle = new{  
    image: new** @type {} */ ({  
     anchor: [0, 0],  
     anchorXUnits: 'fraction',  
     anchorYUnits: 'pixels',  
     opacity: 0.75,  
      color: [113, 140, 0],  
     src: 'ol_icon.png'  
   //create a bunch of icons and add to source vector  
   for (var i=0;i&lt;(carLocs.length-1);i++){  
      var myrecs = carLocs[i].split('/');  
           //console.log("lat ",mypos[0]," long ",mypos[1], " speed ", mypos[2]);  
       var mypos = myrecs[4].split('||');  
           //console.log("lat ",mypos[0]," long ",mypos[1], " speed ", mypos[2]);  
       var myspeed = myrecs[5].split(':');   
           //console.log("Only speed: ",myspeed[1]);  
       var dauser = myrecs[2];  
      //console.log("user ",dauser);       
      var dalat = Number(mypos[1]);  
      //console.log("lat ",dalat);  
      var dalong = Number(mypos[0]);  
      //console.log("long ",dalong);  
      //console.log("speed bit: ", myrecs[5]);  
      var daspeed = Math.round( Number(myspeed[1]) * 10 ) / 10;  
      //console.log("speed ",daspeed);  
     var iconFeature = new ol.Feature({  
      geometry: new   
       ol.geom.Point(ol.proj.transform([dalat, dalong], 'EPSG:4326',  'EPSG:3857')),  
     name: 'daInstance_' + i + " Speed: " + daspeed,  
     speed: daspeed  
      if (daspeed &lt; 10) {  
                image: new** @type {} */ ({  
                     anchor: [0, 0],  
                         anchorXUnits: 'fraction',  
                         anchorYUnits: 'pixels',  
                         opacity: 0.75,  
                          color: '#DC143C',  
                         src: 'ol_icon.png',  
                          scale: 0.5  
   //add the feature vector to the layer vector, and apply a style to whole layer  
   var vectorLayer = new ol.layer.Vector({  
    source: vectorSource,  
    style: iconStyle  
   var map = new ol.Map({  
    layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer],  
    target: document.getElementById('map'),  
    view: new ol.View({  
     center: ol.proj.fromLonLat([-77.353011, 25.0785398]),  
     zoom: 12  
   var element = document.getElementById('popup');  
   var popup = new ol.Overlay({  
      element: element,  
      positioning: 'bottom-center',  
      stopEvent: false  
 // display popup on click  
 map.on('click', function(evt) {  
  var feature = map.forEachFeatureAtPixel(evt.pixel,  
   function(feature, layer) {  
    return feature;  
  if (feature) {  
   var geometry = feature.getGeometry();  
   var coord = geometry.getCoordinates();  
    'placement': 'top',  
    'html': true  
   $(element).data("bs.popover").options.content= feature.get('name');  
   //document.getElementsByClassName("popover-content")[0].innerHTML = feature.get('name');   
  } else {  
 // change mouse cursor when over marker  
 map.on('pointermove', function(e) {  
  if (e.dragging) {  
  var pixel = map.getEventPixel(e.originalEvent);  
  var hit = map.hasFeatureAtPixel(pixel);  
  map.getTarget().style.cursor = hit ? 'pointer' : '';  

And this is the datafile: systemlines.txt

So this was my first take. What this shows...

  1. The ability to place "cars" on the map based on external data in a file.
  2. The ability to change the colour of the cars based on the car's speed.
  3. The ability to change the size of the cars. (Again based on the car's speed but this in not the final purpose for setting the size.)
  4. The ability to popup information about the car by clicking on its icon.
Desired improvements...
  1. Automatically put app data into a database. (mysql/mariadb with geo features.)
  2. Ability of web page to pull in data from the database instead of a text file.
  3. Ability to change the size of the car icons  in an intelligent way so that they scale with the size of the roads they are on as the map zoom changes.
  4. Fix the problem with the car icons being "offset" from the roads. Unless this turns out to be an issue with the data being inaccurate...
  5. (I don't think the problem mentioned in 4 can be the whole issue) Fix the fact that the car icon's position with respect to the map roads changes on zooming.
  6. It would be nice if the choice  of when to consider a car slow could be based on the speed limit of the road but that is not 100% necessary at this point.
  7. It would be nice to change the orientation of the car to match the road so that it does not sit sideways on north/south roads. (Perhaps it would be better to represent the car as a point if this is not possible?)
  8. perhaps add a few more colour coded speed "bands" rather than just red for slow and green for normal.
I have made progress on some of the above and will post improvements when I can.

I am looking for people who would like to discuss this and people who have a bit more expertise with openlayers/javascript/etc.