[Script] WMEOpenData v1.33 (20130522)

Moderators: Unholy, krankyd, dmcconachie, The Fej

Re: OS Open Data User Scripts

Postby Timbones » Tue Jun 26, 2012 9:56 pm

xteejx wrote:Even if a previously opened link is open from the same site, the script will still open another tab. Any way to <test> the opened tabs to see if one is already open and refresh it?

This should be easy to do - just need to set target="OSOpenData" to the href elements.

xteejx wrote:Once one (or several different) tabs are open, would it be possible to continuously poll for changes in lat/long and change it automatically according to WME? i.e. have another window open in a side-by-side Windows 7 click-to-side thing and drag WME in Window 1 and Window 2 moves too?

I wanted to do this before, but never got around to trying to work out how.

Meanwhile, I'm trying to workout how to replace the second script with bookmarklets. The MusicalChairs link is easy, but I'm struggling to persuade OpenLayers to do the transform to EPSG:27700. Failing that, it should be possible to do a version of the script that does the conversions via the onClick event, rather than continuously polling the browser.[/nerd]
Timbones • UK Country Admin • Forum Moderator • Beta Editor and Routing Expert
Scripts: WME Colour Highlights v1.6 « NEW (Feb 2013)Livemap Navigation v0.72 (Jan 2013)
Timbones
Waze Champs
 
Posts: 2854
Joined: Wed Feb 09, 2011 10:33 am
Location: York, UK
Has thanked: 17 times
Been thanked: 191 times

Re: OS Open Data User Scripts

Postby Twister_UK » Wed Jun 27, 2012 12:47 am

Update for WME to OS, incorporating various suggestions made above...

Code: Select all
// ==UserScript==
// @name                WME to OS link
// @namespace           http://greasemonkey.chizzum.com
// @description         Adds link to WME to open up OS Open Data/Musical Chairs sites at same map location
// @include             https://world.waze.com/editor/*
// @version             0.3
// ==/UserScript==


// Modified from the original (c) Chris Veness 2005-2012
//   www.movable-type.co.uk/scripts/gridref.js
//   www.movable-type.co.uk/scripts/latlon-gridref.html
function toOSGrid(lat, lon)
{
   lat = (lat * Math.PI) / 180;
   lon = (lon * Math.PI) / 180;

   // Airy 1830 major & minor semi-axes
   var a = 6377563.396;
   var b = 6356256.910;
   // NatGrid scale factor on central meridian
   var F0 = 0.9996012717;
   // NatGrid true origin is 49N,2W
   var lat0 = 0.85521;
   var lon0 = -0.0349;
   // northing & easting of true origin, metres
   var N0 = -100000;
   var E0 = 400000;
   // eccentricity squared
   var e2 = 1 - (b*b)/(a*a);

   var n = (a-b)/(a+b);
   var n2 = n*n;
   var n3 = n*n*n;
   var cosLat = Math.cos(lat);
   var sinLat = Math.sin(lat);
   // transverse radius of curvature
   var nu = a*F0/Math.sqrt(1-e2*sinLat*sinLat);
   // meridional radius of curvature
   var rho = a*F0*(1-e2)/Math.pow(1-e2*sinLat*sinLat, 1.5);
   var eta2 = nu/rho-1;
   var Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);
   var Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0);
   var Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0));
   var Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0));
   // meridional arc
   var M = b * F0 * (Ma - Mb + Mc - Md);
   var cos3lat = cosLat*cosLat*cosLat;
   var cos5lat = cos3lat*cosLat*cosLat;
   var tan2lat = Math.tan(lat)*Math.tan(lat);
   var tan4lat = tan2lat*tan2lat;
   var I = M + N0;
   var II = (nu/2)*sinLat*cosLat;
   var III = (nu/24)*sinLat*cos3lat*(5-tan2lat+9*eta2);
   var IIIA = (nu/720)*sinLat*cos5lat*(61-58*tan2lat+tan4lat);
   var IV = nu*cosLat;
   var V = (nu/6)*cos3lat*(nu/rho-tan2lat);
   var VI = (nu/120) * cos5lat * (5 - 18*tan2lat + tan4lat + 14*eta2 - 58*tan2lat*eta2);
   var dLon = lon-lon0;
   var dLon2 = dLon*dLon;
   var dLon3 = dLon2*dLon;
   var dLon4 = dLon3*dLon;
   var dLon5 = dLon4*dLon;
   var dLon6 = dLon5*dLon;
   var N = Math.round(I + II*dLon2 + III*dLon4 + IIIA*dLon6);
   var E = Math.round(E0 + IV*dLon + V*dLon3 + VI*dLon5);
   return '?e='+E+'&n='+N;
}


function processPermalink()
{
   // extract current lat/lon & zoom level from the permalink URL
   var plsrc = document.getElementById("permalink-container").innerHTML;
   var zoompos = plsrc.indexOf("?zoom=");
   var latpos = plsrc.indexOf("&amp;lat=");
   var lonpos = plsrc.indexOf("&amp;lon=");
   var layerpos = plsrc.indexOf("&amp;layers=");

   // does the URL contain all three parameters?
   if((zoompos != -1)&&(latpos != -1)&&(lonpos != -1)&&(layerpos != -1))
   {
      // yes, so extract them...
      var zoom = parseInt(plsrc.substr(zoompos+6,latpos-(zoompos+6)));
      var lat = plsrc.substr(latpos+9,lonpos-(latpos+9));
      var lon = plsrc.substr(lonpos+9,layerpos-(lonpos+9));

      // compare the freshly extracted parameters against the persistent copies, and update the
      // links to OSMC & OSOD only if there's a change required - the newly-inserted <a> element
      // can't be clicked on until the insertion process is complete, and if we were to re-insert
      // it every 250ms then it'd spend a lot of its time giving the appearance of being clickable
      // but without actually doing anything...
      if((zoom != sessionStorage.zoom)||(lat != sessionStorage.lat)||(lon != sessionStorage.lon))
      {
         // update the persistent vars with the new position
         sessionStorage.zoom = zoom;
         sessionStorage.lat = lat;
         sessionStorage.lon = lon;

         // translate the zoom level between WME and Musical Chairs - this gives a pretty close match
         var mczoom = zoom + 12;
         if(mczoom > 18) mczoom = 18;
         // generate the Musical Chairs URL
         var osmc_url = 'http://ris.dev.openstreetmap.org/oslmusicalchairs/map?zoom='+mczoom+'&lat='+lat+'&lon='+lon+'&layers=B0TT&view_mode=pseudorandom';

         // translate the zoom level between WME and OpenData - the match here isn't quite so good...
         var odzoom = zoom + 5;
         if(odzoom < 6) odzoom = 6;
         if(odzoom > 10) odzoom = 10;
         // generate the OpenData URL - requires the support of os_opendata_fullheight.user.js
         var osod_url = 'http://www.ordnancesurvey.co.uk/oswebsite/opendata/viewer/'+toOSGrid(lat,lon)+'&z='+odzoom;

         // translate the zoom level between WME and live map.  The only correlation is (WME)=[Live] (0 or 1)=[7], (2 or 3)=[8], (4 or more)=[9]
         var livemap_zoom = Math.floor(zoom/2)+7;
         if (livemap_zoom > 9 ) livemap_zoom = 9;
         var livemap_url = 'https://world.waze.com/livemap/?zoom='+livemap_zoom+'&lat='+lat+'&lon='+lon+'&layers=BTTTT';
         // Modify existing livemap link to reference current position in WME
         document.getElementById("livemap").href = livemap_url;
         document.getElementById("livemap").target = '_blank';

         // "borrow" the Bing attribution div to insert the new clicky-links, remembering to maintain the attribution after we're done messing with it!
         var pl_colour = document.defaultView.getComputedStyle(document.getElementById("map-footer"),"").getPropertyValue("color");
         document.getElementById("bing-attribution").innerHTML = ' <a href="'+osod_url+'" target=_osopendata style="color:' + pl_colour +'">OS OpenData</a> | <a href="'+osmc_url+'" target=_osmusicalchairs style="color:' + pl_colour +'">OS Musical Chairs</a> | Imagery by Bing';

         // uncomment these lines if you want the OpenData and/or MusicalChairs tabs to auto-follow the WME location...
         // window.open(osod_url,'_osopendata');
         // window.open(osmc_url,'_osmusicalchairs');
      }
   }
}

// initialise persistent vars
sessionStorage.zoom = 0;
sessionStorage.lat = '';
sessionStorage.lon = '';

// check for new map co-ords every 250ms
setInterval(processPermalink,250);


Changes:
* Top navbar link back to the livemap uses WME location (Dave2084)
* OSMC/OSOD links copy style from permalink (Dave2084)
* OpenData and MusicalChairs links reuse any existing tabs/windows (Timbones)

Not enabled by default, uncomment the last two lines in the processPermalink() function:
* OpenData and MusicalChairs tabs/windows automatically track WME position (xteejx)

I didn't enable this last change by default since I'm not entirely sure I like it - if the extents of the OSOD map viewport exactly matched that of the WME viewport for all zoom levels, it might work well, but there are often times when the mismatch between the two viewports means that I only need to drag the WME map a little to expose a road which is still visible on the OSOD map (don't really use OSMC so I'm not sure if that is any better in this respect), and I wouldn't then want OSOD to reload. Comments are welcomed from anyone who does try it out!

HTC One X - Android 4.1.1 - Waze 3.6.0.0 - International Server
AM for NW London and the M4-A404-M40 loop - Editing Expert
WME enhancement scripts (WMEOD:URO:LMUR:SRS) @ userscripts.org
Twister_UK
 
Posts: 485
Joined: Sat Jan 07, 2012 3:56 pm
Location: NW London
Has thanked: 19 times
Been thanked: 90 times

Re: OS Open Data User Scripts

Postby Dave2084 » Wed Jun 27, 2012 5:30 pm

Fantastic!

Two little extra's:

1. Add an include for "https://descartesw.waze.com/beta/*"
2. Add a Cartouche Link at it is still a useful tool for fixing roundabouts:
Code: Select all
var cartouche_url = 'http://world.waze.com/cartouche_old/?zoom='+zoom+'&lon='+lon+'&lat='+lat;


:-)
iPhone 4S 32GB (Jailed) • iOS 6.1.2 • Waze 3.6.99.15 Beta
Lincolnshire Area Manager • UK Country Administrator • iOS Beta Tester • iPhone Expert
UK WikiUK ForumWaze UK on FacebookBecome a UK Area ManagerWaze Status
Dave2084
Waze Champs
 
Posts: 1547
Joined: Tue Feb 09, 2010 12:58 am
Location: Lincoln, UK
Has thanked: 22 times
Been thanked: 32 times

Re: Re: OS Open Data User Scripts

Postby dknight212 » Wed Jun 27, 2012 6:41 pm

Dave2084 wrote:Fantastic!

Two little extra's:

1. Add an include for "https://descartesw.waze.com/beta/*"
2. Add a Cartouche Link at it is still a useful tool for fixing roundabouts:
Code: Select all
var cartouche_url = 'http://world.waze.com/cartouche_old/?zoom='+zoom+'&lon='+lon+'&lat='+lat;


:-)


So that's where the beta is!

David using Tapatalk
Samsung Galaxy Note 2 (stock) • Android 4.1.2 • Waze 3.5.1.4
Area Manager for LondonUK Country Admin
UK WikiUK ForumWaze UK on FacebookBecome a UK Area ManagerWaze Status
dknight212
Waze Champs
 
Posts: 2689
Joined: Sun Apr 24, 2011 9:21 am
Location: London, UK
Has thanked: 15 times
Been thanked: 38 times

Re: OS Open Data User Scripts

Postby xteejx » Wed Jun 27, 2012 7:19 pm

You need a login, your normal one won't work. It's no different though.

Sent from my GT-I9100P using Tapatalk 2
Area Manager for Kent. Localisation expert and translator for UK English.
Image
UK specific editing: http://www.waze.com/wiki/index.php/United_Kingdom
xteejx
 
Posts: 2340
Joined: Wed Jan 19, 2011 3:17 pm
Location: Sheerness, Kent, UK
Has thanked: 11 times
Been thanked: 15 times

Re: OS Open Data User Scripts

Postby Twister_UK » Thu Jun 28, 2012 8:54 pm

Code: Select all
// ==UserScript==
// @name                WME to OS link
// @namespace           http://greasemonkey.chizzum.com
// @description         Adds link to WME to open up various mapping sites at the same map location
// @include             https://world.waze.com/editor/*
// @include             https://descartesw.waze.com/beta/*
// @version             0.4
// ==/UserScript==


// Modified from the original (c) Chris Veness 2005-2012
//   www.movable-type.co.uk/scripts/gridref.js
//   www.movable-type.co.uk/scripts/latlon-gridref.html
function toOSGrid(lat, lon)
{
   lat = (lat * Math.PI) / 180;
   lon = (lon * Math.PI) / 180;

   // Airy 1830 major & minor semi-axes
   var a = 6377563.396;
   var b = 6356256.910;
   // NatGrid scale factor on central meridian
   var F0 = 0.9996012717;
   // NatGrid true origin is 49N,2W
   var lat0 = 0.85521;
   var lon0 = -0.0349;
   // northing & easting of true origin, metres
   var N0 = -100000;
   var E0 = 400000;
   // eccentricity squared
   var e2 = 1 - (b*b)/(a*a);

   var n = (a-b)/(a+b);
   var n2 = n*n;
   var n3 = n*n*n;
   var cosLat = Math.cos(lat);
   var sinLat = Math.sin(lat);
   // transverse radius of curvature
   var nu = a*F0/Math.sqrt(1-e2*sinLat*sinLat);
   // meridional radius of curvature
   var rho = a*F0*(1-e2)/Math.pow(1-e2*sinLat*sinLat, 1.5);
   var eta2 = nu/rho-1;
   var Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);
   var Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0);
   var Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0));
   var Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0));
   // meridional arc
   var M = b * F0 * (Ma - Mb + Mc - Md);
   var cos3lat = cosLat*cosLat*cosLat;
   var cos5lat = cos3lat*cosLat*cosLat;
   var tan2lat = Math.tan(lat)*Math.tan(lat);
   var tan4lat = tan2lat*tan2lat;
   var I = M + N0;
   var II = (nu/2)*sinLat*cosLat;
   var III = (nu/24)*sinLat*cos3lat*(5-tan2lat+9*eta2);
   var IIIA = (nu/720)*sinLat*cos5lat*(61-58*tan2lat+tan4lat);
   var IV = nu*cosLat;
   var V = (nu/6)*cos3lat*(nu/rho-tan2lat);
   var VI = (nu/120) * cos5lat * (5 - 18*tan2lat + tan4lat + 14*eta2 - 58*tan2lat*eta2);
   var dLon = lon-lon0;
   var dLon2 = dLon*dLon;
   var dLon3 = dLon2*dLon;
   var dLon4 = dLon3*dLon;
   var dLon5 = dLon4*dLon;
   var dLon6 = dLon5*dLon;
   var N = Math.round(I + II*dLon2 + III*dLon4 + IIIA*dLon6);
   var E = Math.round(E0 + IV*dLon + V*dLon3 + VI*dLon5);
   return '?e='+E+'&n='+N;
}


function processPermalink()
{
   // extract current lat/lon & zoom level from the permalink URL
   var plsrc = document.getElementById("permalink-container").innerHTML;
   var zoompos = plsrc.indexOf("?zoom=");
   var latpos = plsrc.indexOf("&amp;lat=");
   var lonpos = plsrc.indexOf("&amp;lon=");
   var layerpos = plsrc.indexOf("&amp;layers=");

   // does the URL contain all three parameters?
   if((zoompos != -1)&&(latpos != -1)&&(lonpos != -1)&&(layerpos != -1))
   {
      // yes, so extract them...
      var zoom = parseInt(plsrc.substr(zoompos+6,latpos-(zoompos+6)));
      var lat = plsrc.substr(latpos+9,lonpos-(latpos+9));
      var lon = plsrc.substr(lonpos+9,layerpos-(lonpos+9));

      // compare the freshly extracted parameters against the persistent copies, and update the
      // links to OSMC & OSOD only if there's a change required - the newly-inserted <a> element
      // can't be clicked on until the insertion process is complete, and if we were to re-insert
      // it every 250ms then it'd spend a lot of its time giving the appearance of being clickable
      // but without actually doing anything...
      if((zoom != sessionStorage.zoom)||(lat != sessionStorage.lat)||(lon != sessionStorage.lon))
      {
         // update the persistent vars with the new position
         sessionStorage.zoom = zoom;
         sessionStorage.lat = lat;
         sessionStorage.lon = lon;

         // translate the zoom level between WME and Musical Chairs - this gives a pretty close match
         var mczoom = zoom + 12;
         if(mczoom > 18) mczoom = 18;
         // generate the Musical Chairs URL
         var osmc_url = 'http://ris.dev.openstreetmap.org/oslmusicalchairs/map?zoom='+mczoom+'&lat='+lat+'&lon='+lon+'&layers=B0TT&view_mode=pseudorandom';

         // translate the zoom level between WME and OpenData - the match here isn't quite so good...
         var odzoom = zoom + 5;
         if(odzoom < 6) odzoom = 6;
         if(odzoom > 10) odzoom = 10;
         // generate the OpenData URL - requires the support of os_opendata_fullheight.user.js
         var osod_url = 'http://www.ordnancesurvey.co.uk/oswebsite/opendata/viewer/'+toOSGrid(lat,lon)+'&z='+odzoom;

         // generate the Cartouche URL
         var cartouche_url = 'http://world.waze.com/cartouche_old/?zoom='+zoom+'&lon='+lon+'&lat='+lat;

         // translate the zoom level between WME and live map.  The only correlation is (WME)=[Live] (0 or 1)=[7], (2 or 3)=[8], (4 or more)=[9]
         var livemap_zoom = Math.floor(zoom/2)+7;
         if (livemap_zoom > 9 ) livemap_zoom = 9;
         var livemap_url = 'https://world.waze.com/livemap/?zoom='+livemap_zoom+'&lat='+lat+'&lon='+lon+'&layers=BTTTT';
         // Modify existing livemap link to reference current position in WME
         document.getElementById("livemap").href = livemap_url;
         document.getElementById("livemap").target = '_blank';

         // update the link URLs
         document.getElementById("_linkOSOD").href = osod_url;
         document.getElementById("_linkOSMC").href = osmc_url;
         document.getElementById("_linkCartouche").href = cartouche_url;

         // refresh any of the site tabs/windows we've checked for auto-tracking
         if(document.getElementById('_cbAutoTrackOSOD').checked == 1) window.open(osod_url,'_osopendata');
         if(document.getElementById('_cbAutoTrackOSMC').checked == 1) window.open(osmc_url,'_osmusicalchairs');
         if(document.getElementById('_cbAutoTrackCartouche').checked == 1) window.open(cartouche_url,'_cartouche');
      }
   }
}

// initialise persistent vars
sessionStorage.zoom = 0;
sessionStorage.lat = '';
sessionStorage.lon = '';

// get the colour attribute for the permalink, so that our new links are visually consistent
var pl_colour = document.defaultView.getComputedStyle(document.getElementById("map-footer"),"").getPropertyValue("color");
// create a new child div in the existing map footer div, so we don't have to borrow the bing attrinution one any more...
var mlcDiv = document.createElement('div');
mlcDiv.setAttribute('id','MapLinkControls');
// add the anchors and auto-track checkboxes for OS OpenData, Musical Chairs and Cartouche.  Note that the urls are blank at this stage,
// they'll be filled in as soon as we've done our first processPermalink() call
mlcDiv.innerHTML = '<a href="" id="_linkOSOD" target=_osopendata style="color:' + pl_colour +'">OS OpenData</a> <input type="checkbox" id="_cbAutoTrackOSOD"></input> | ';
mlcDiv.innerHTML += '<a href="" id="_linkOSMC" target=_osmusicalchairs style="color:' + pl_colour +'">OS Musical Chairs</a> <input type="checkbox" id="_cbAutoTrackOSMC"></input> | ';
mlcDiv.innerHTML += '<a href="" id="_linkCartouche" target=_cartouche style="color:' + pl_colour +'">Cartouche</a> <input type="checkbox" id="_cbAutoTrackCartouche"></input>';
mlcDiv.innerHTML += '<br>(Checkboxes enable auto-tracking)';
document.getElementById('map-footer').appendChild(mlcDiv);

// set up a check for new map co-ords every 250ms
setInterval(processPermalink,250);


On the "what's new" menu for today...

  • Enabled for descartes - I can't fully test it as I don't have a login, but I at least can see the new links are being inserted as expected, so it looks as if it'll work ok...
  • Added link to Cartouche
  • Moved links over to left of screen in their own div
  • Added checkboxes to enable auto-tracking without requiring any code edits - if the relevant box is ticked, that site will be reloaded each time the script detects a change in the current map position/zoom level

HTC One X - Android 4.1.1 - Waze 3.6.0.0 - International Server
AM for NW London and the M4-A404-M40 loop - Editing Expert
WME enhancement scripts (WMEOD:URO:LMUR:SRS) @ userscripts.org
Twister_UK
 
Posts: 485
Joined: Sat Jan 07, 2012 3:56 pm
Location: NW London
Has thanked: 19 times
Been thanked: 90 times

Re: OS Open Data User Scripts

Postby Twister_UK » Thu Jun 28, 2012 9:25 pm

Now uploaded both scripts to userscripts.org

HTC One X - Android 4.1.1 - Waze 3.6.0.0 - International Server
AM for NW London and the M4-A404-M40 loop - Editing Expert
WME enhancement scripts (WMEOD:URO:LMUR:SRS) @ userscripts.org
Twister_UK
 
Posts: 485
Joined: Sat Jan 07, 2012 3:56 pm
Location: NW London
Has thanked: 19 times
Been thanked: 90 times

Re: OS Open Data User Scripts

Postby Dave2084 » Fri Jun 29, 2012 4:25 pm

Cool!

I did find another link to the editor which would need adding to the include last night when I clicked on the link in this post which I didn't realise still worked
https://world.waze.com/map-editor/*


Also I think that the link separators "|" are a bit superfluous with the check boxes there now (they are a great idea though).

Anyway, well done, this is an awesome addition to the map editors arsenal :-)


All we need now is "WME musical chairs" to check Waze road-names against the OS Locator DB :D
iPhone 4S 32GB (Jailed) • iOS 6.1.2 • Waze 3.6.99.15 Beta
Lincolnshire Area Manager • UK Country Administrator • iOS Beta Tester • iPhone Expert
UK WikiUK ForumWaze UK on FacebookBecome a UK Area ManagerWaze Status
Dave2084
Waze Champs
 
Posts: 1547
Joined: Tue Feb 09, 2010 12:58 am
Location: Lincoln, UK
Has thanked: 22 times
Been thanked: 32 times

Re: OS Open Data User Scripts

Postby timbucks » Fri Jun 29, 2012 5:37 pm

Will this work in Chrome?
timbucks
 
Posts: 227
Joined: Sat Jan 07, 2012 8:30 pm
Has thanked: 4 times
Been thanked: 2 times

Re: OS Open Data User Scripts

Postby Dave2084 » Fri Jun 29, 2012 5:44 pm

timbucks wrote:Will this work in Chrome?


Just done a quick test (after installing it first) and it seem to work fine.
iPhone 4S 32GB (Jailed) • iOS 6.1.2 • Waze 3.6.99.15 Beta
Lincolnshire Area Manager • UK Country Administrator • iOS Beta Tester • iPhone Expert
UK WikiUK ForumWaze UK on FacebookBecome a UK Area ManagerWaze Status
Dave2084
Waze Champs
 
Posts: 1547
Joined: Tue Feb 09, 2010 12:58 am
Location: Lincoln, UK
Has thanked: 22 times
Been thanked: 32 times

PreviousNext

Return to United Kingdom

Who is online

Users browsing this forum: No registered users