Greasemonkey script for easy river drawing

The place to get information and ask questions about everything to do with properly and successfully editing the Waze Map.

Use this forum for all general editing questions, and the sub-forums for specific types of Waze Map Editor features.

Moderators: support, Unholy, krankyd, The Fej

Re: Greasemonkey script for easy river drawing

Postby fvwazing » Fri Jan 06, 2012 9:13 pm

Nice work, aeytom! Created a small stream in my hood that was so curvy that I didn't have the courage to create it until now. Your script makes it very easy.

However... to make that river look really good I had to make a little bit of variation in the width, so that curves are a bit wider that the straights.

Geul_Landmark.jpg
Geul - a small stream that "swings"
Geul_Landmark.jpg (90.65 KiB) Viewed 1167 times


To create this effect automatically, I took the liberty of adding a bit of code to your script. Now it takes an extra parameter, "variancy", expressed as an extra parameter in the name of the river. As in "20m 30v Spree". Shorter segments are a bit wider, longer segments are a bit more narrow now. I would love to have created the effect depending on the angle between consecutive segments but I am afraid I did not completely grasp how you do the math (in getEquation), and the effect is good enough as it is now, I think.

In order NOT to pollute this thread with endless copies of code I send you my version as PM, maybe you can have a look to see if it is any good.
Waze jezelf!
600K+ edits
AM for some 25.000 km² around Maastricht, the Netherlands
iPhone 3Gs / iOS 6.0.1
fvwazing
Waze Champs
 
Posts: 2870
Joined: Sat Nov 14, 2009 2:48 pm
Has thanked: 100 times
Been thanked: 67 times

Re: Greasemonkey script for easy river drawing

Postby gerben » Sun Jan 29, 2012 11:48 pm

This tool has stopped working because the url for the editor has canged.

Working code:

Code: Select all
// ==UserScript==
// @name Papyrus: Street to river
// @description This script create a new river landmark in waze editor papyrus. It transforms the the geometry of a new unsaved street to a polygon.
// @namespace http://www.tay-tec.de/waze-street-to-river
// @match https://world.waze.com/editor/*
// @match https://www.waze.com/editor/*
// ==/UserScript==


// Mini howto:
// 1) install this script as greasemonkey script or chrome extension
// 2) draw a new street but do not save the street
// 3) add and apply a street name to define the rivers name and the the width of the river
//    Example: "20m Spree" creates a 20 meters width river named "Spree"
// 4) Select the helper street
// 5) Click the "Street to river" button
// 4) Delete the helper street
// 5) Edit the new landmark as you like


if ('undefined' == typeof __RTLM_PAGE_SCOPE_RUN__) {
  (function page_scope_runner() {
    // If we're _not_ already running in the page, grab the full source
    // of this script.
    var my_src = "(" + page_scope_runner.caller.toString() + ")();";

    // Create a script node holding this script, plus a marker that lets us
    // know we are running in the page scope (not the Greasemonkey sandbox).
    // Note that we are intentionally *not* scope-wrapping here.
    var script = document.createElement('script');
    script.setAttribute("type", "text/javascript");
    script.textContent = "var __RTLM_PAGE_SCOPE_RUN__ = true;\n" + my_src;

    // Insert the script node into the page, so it will run, and immediately
    // remove it to clean up.  Use setTimeout to force execution "outside" of
    // the user script scope completely.
    setTimeout(function() {
          document.body.appendChild(script);
          document.body.removeChild(script);
        }, 0);
  })();

  // Stop running, because we know Greasemonkey actually runs us in
  // an anonymous wrapper.
  return;
}


function streetToRiver() {
 
 var defaultWidth = 20;

  insertButton();

  function insertButton() {
    var btn = $('<button title="create a new street, select and click this button">Street to river</button>');
    btn.click(start);
    var cnt = $('<div style="padding:0.5em 15px; background:#333; color:#fff"/>');
    cnt.append(btn);
    $("div.contents","#editPanel").after(cnt);
    console_log("Street to river initialized");
  }

  function start(ev) {
    var foundSelectedSegment = false;
    for (var s=selectionManager.selectedItems.length-1; s>=0; s--) {
      var sel = selectionManager.selectedItems[s];
      if (sel.type == "segment" && sel.state == "Insert") {
   // found segment
   foundSelectedSegment = true;
   if (convertToLandmark(sel))
   {
     alert("Successfully created new river landmark");
   }
      }
    }
    if (! foundSelectedSegment) {
   alert("No unsaved and selected new street found!");
      }
  }

  function convertToLandmark(sel) {
   
    var leftPa, rightPa, leftPb, rightPb;
    var prevLeftEq, prevRightEq;
    var street = getStreet(sel);
   
    var displacement = getDisplacement(street);
   
    var polyPoints = null;
    var vertices = sel.geometry.getVertices();
    for (var i=vertices.length-1; i>0; i--)
    {
      var pa = vertices[i];
      var pb = vertices[i-1];
      var scale = (pa.distanceTo(pb) + displacement) / pa.distanceTo(pb);
     
      leftPa = pa.clone();
      leftPa.resize(scale, pb, 1);
      rightPa = leftPa.clone();
      leftPa.rotate(90,pa);
      rightPa.rotate(-90,pa);
     
      leftPb = pb.clone();
      leftPb.resize(scale, pa, 1);
      rightPb = leftPb.clone();
      leftPb.rotate(-90,pb);
      rightPb.rotate(90,pb);
     
      var leftEq = getEquation({ 'x1': leftPa.x, 'y1': leftPa.y, 'x2': leftPb.x, 'y2': leftPb.y });
      var rightEq = getEquation({ 'x1': rightPa.x, 'y1': rightPa.y, 'x2': rightPb.x, 'y2': rightPb.y });
      if (polyPoints == null) {
   polyPoints = [ leftPa, rightPa ];
      }
      else {
   var li = intersectX(leftEq, prevLeftEq);
   var ri = intersectX(rightEq, prevRightEq);
   if (li && ri) {
     polyPoints.unshift(li);   
     polyPoints.push(ri);
   }
   else {
     polyPoints.unshift(leftPb.clone());
     polyPoints.push(rightPb.clone());
   }
      }

      prevLeftEq = leftEq;
      prevRightEq = rightEq;
    }
   
    polyPoints.push(rightPb);
    polyPoints.push(leftPb);
   
    var polygon = new OpenLayers.Geometry.Polygon(new OpenLayers.Geometry.LinearRing(polyPoints));
   
    var landmark = new Waze.Feature.Vector.Landmark(polygon);
    landmark.attributes.mtfcc = "H3010";
    if (street) {
      landmark.attributes.name = street.name.replace(/^\d+(m|ft)\s*/, '');
    }
    var what = wazeModel.actionManager.add(new Waze.Action.AddLandmark(landmark));
   
    return true;
  }

 
  function getEquation(segment) {
    if (segment.x2 == segment.x1)
      return { 'x': segment.x1 };
   
    var slope =  (segment.y2 - segment.y1) / (segment.x2 - segment.x1);
    var offset = segment.y1 - (slope  * segment.x1)
    return { 'slope': slope, 'offset': offset };
  }

  //
  // line A: y = ax + b
  // line B: y = cx + b
  //
  // x = (d - b) / (a - c)
  function intersectX(eqa,eqb,defaultPoint) {
    if ("number" == typeof eqa.slope && "number" == typeof eqb.slope) {
      if (eqa.slope == eqb.slope)
   return null;
     
      var ix = (eqb.offset - eqa.offset) / (eqa.slope - eqb.slope);
      var iy = eqa.slope * ix + eqa.offset;
      return new OpenLayers.Geometry.Point(ix, iy);
    }
    else if ("number" == typeof eqa.x) {
      return new OpenLayers.Geometry.Point(eqa.x, eqb.slope * eqa.x + eqb.offset);
    }
    else if ("number" == typeof eqb.y) {
      return new OpenLayers.Geometry.Point(eqb.x, eqa.slope * eqb.x + eqa.offset);
    }
    return null;
  }
 

  function getStreet(segment) {
    if (! segment.attributes.primaryStreetID)
      return null;
    var street = segment.model.streets.get(segment.attributes.primaryStreetID)
    return street;
  }
 
  function getDisplacement(street) {
    if (! street)
      return defaultWidth;
    if (street.name.match(/^(\d+)m\b/))
      return parseInt(RegExp.$1);
    if (street.name.match(/^(\d+)ft\b/))
      return parseInt(RegExp.$1) * 0.3048;
    return defaultWidth;
  }
 
  function console_log(msg) {
    if (console.log)
      console.log(msg);
  }
}

   


streetToRiver();
Normally a HTC Desire S - Stock ROM (Android 2.3.5), but temporarily back to my G1 (HTC Heart) - Froyo by Laszlo ROM - Waze 3.6
Waze Champ, Mega Mapper/Driver
Countrymanager The Netherlands
gerben
Waze Champs
 
Posts: 4253
Joined: Sun Dec 13, 2009 10:42 pm
Location: Almelo
Has thanked: 6 times
Been thanked: 44 times

Re: Greasemonkey script for easy river drawing

Postby Dave2084 » Mon Jan 30, 2012 5:35 am

Thanks!
iPhone 4S 32GB (Jailed) • iOS 6.1.2 • Waze 3.6.99.9 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: 1546
Joined: Tue Feb 09, 2010 12:58 am
Location: Lincoln, UK
Has thanked: 21 times
Been thanked: 32 times

Re: Greasemonkey script for easy river drawing

Postby aeytom » Mon Jan 30, 2012 8:00 am

Hi,

gerben wrote:This tool has stopped working because the url for the editor has canged.


I have updated the script on userscripts.org.

tom
AM: Berlin South/East, Fläming; HTC Desire, Cyanogenmod 7.2
Image
aeytom
 
Posts: 279
Joined: Sat Nov 27, 2010 8:14 am
Location: Berlin/Germany
Has thanked: 0 time
Been thanked: 1 time

Re: Greasemonkey script for easy river drawing

Postby Laakkus » Wed Feb 08, 2012 10:56 am

aeytom wrote:Hi,

gerben wrote:This tool has stopped working because the url for the editor has canged.


I have updated the script on userscripts.org.

tom

Hello. Just run into your script and wanted to use it. However, it doesn't show the street to river -button in chrome :(
Hope you get it fixed soon! :)
Image
Area manager: Some cities
Country manager: Finland
Suomenkielinen Wiki | Asemien tarkistus | Editointiohje
i9300 | CM10.1 Nightly | Waze 3.6
Laakkus
 
Posts: 361
Joined: Thu Jul 28, 2011 11:48 am
Has thanked: 1 time
Been thanked: 4 times

Re: Greasemonkey script for easy river drawing

Postby gerben » Wed Feb 08, 2012 11:23 am

It works fine for me in Chrome. Did you restart the browser or at least refresh the page after installation?
Normally a HTC Desire S - Stock ROM (Android 2.3.5), but temporarily back to my G1 (HTC Heart) - Froyo by Laszlo ROM - Waze 3.6
Waze Champ, Mega Mapper/Driver
Countrymanager The Netherlands
gerben
Waze Champs
 
Posts: 4253
Joined: Sun Dec 13, 2009 10:42 pm
Location: Almelo
Has thanked: 6 times
Been thanked: 44 times

Re: Greasemonkey script for easy river drawing

Postby Laakkus » Wed Feb 08, 2012 11:42 am

gerben wrote:It works fine for me in Chrome. Did you restart the browser or at least refresh the page after installation?


OK, I found what was wrong. I had Tampermonkey on (to get script update notifications). Only had to disable it and install street to river again. Maybe put "a warning" about using Tampermonkey to tool's wiki?

Works now, thanks!
Image
Area manager: Some cities
Country manager: Finland
Suomenkielinen Wiki | Asemien tarkistus | Editointiohje
i9300 | CM10.1 Nightly | Waze 3.6
Laakkus
 
Posts: 361
Joined: Thu Jul 28, 2011 11:48 am
Has thanked: 1 time
Been thanked: 4 times

Re: Greasemonkey script for easy river drawing

Postby aeytom » Tue Mar 13, 2012 3:54 pm

Hello Wazers,

I have uploaded a new version to userscripts.org. This new version integrates some changes from Timbones. Now you can create "railroad" landmarks with one click.

tom
AM: Berlin South/East, Fläming; HTC Desire, Cyanogenmod 7.2
Image
aeytom
 
Posts: 279
Joined: Sat Nov 27, 2010 8:14 am
Location: Berlin/Germany
Has thanked: 0 time
Been thanked: 1 time

Re: Greasemonkey script for easy river drawing

Postby Timbones » Tue Mar 13, 2012 4:14 pm

aeytom wrote:I have uploaded a new version to userscripts.org. This new version integrates some changes from Timbones. Now you can create "railroad" landmarks with one click.

Thanks Tom!

Here's a summary of the changes:
  • separate button to do 'Street to railway'
  • default width of 8m (if not set in road name)
  • creates landmark of type 'Other'
  • will work on existing railroad segments
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: 2853
Joined: Wed Feb 09, 2011 10:33 am
Location: York, UK
Has thanked: 17 times
Been thanked: 188 times

Re: Greasemonkey script for easy river drawing

Postby AlanOfTheBerg » Tue Mar 13, 2012 4:18 pm

Thanks for keeping the wiki page updated!
Oregon-based US Country Manager | iPhone5 - VZ - iOS 6.1.2 | Waze v3.6
Image
Wiki Resources: Map Editing Manual | Oregon Project/To-Do List
AlanOfTheBerg
Waze Champs
 
Posts: 13773
Joined: Sat Aug 28, 2010 8:48 pm
Location: Oregon, USA
Has thanked: 120 times
Been thanked: 428 times

PreviousNext

Return to Waze Map Editor

Who is online

Users browsing this forum: mwburn, OyyoDams, wakkoedu