# [Merkaartor] "Align Nodes" fix [quasi-patch]

Totor Osm totor_osm at yahoo.com
Mon Sep 21 15:35:28 BST 2009

```Hi everybody,

I'm an impatient guy, so I tried to patch my version of Merkaartor as per Chris Baird's suggestion.
It seems to work, but aligning longer sections sometimes makes Merkaartor hang.(in my case over 100m)

After I changed the length function to :

double length() const
{
return sqrt((double)((double)Lat*(double)Lat+(double)Lon*(double)Lon));
}

I could go up to 100km without problems. It might need more investigation though...

Further on, I modified alignNodes to align the nodes on the line passing by the first and last node. This seemed more logical to me.

[...]
//we do the alignment
Coord pos(0,0);
const Coord p1(Nodes[0]->position());
//Original direction was given by 2nd point
//const Coord p2(Nodes[1]->position()-p1);
//Direction given by last point
const Coord p2(Nodes[Nodes.size()-1]->position()-p1);
const double slope = angle(p2);
//original for loop : no need to adjust node[1]
//for (int i=2; i<Nodes.size(); ++i) {
//we need to adjust node[1] but no need to adjust last node
for (int i=1; i<(Nodes.size()-1); ++i) {
pos=Nodes[i]->position()-p1;
rotate(pos,-slope);
pos.setLat(0);
rotate(pos,slope);
pos=pos+p1;
theList->add(new MoveTrackPointCommand( Nodes[i], pos,  theDocument->getDirtyOrOriginLayer(Nodes[i]->layer()) ));
}
[...]

It would probably even be better to "slide" the nodes following crossing highways, instead of using an orthogonal projection. This would avoid to move crossing highways in most cases (at least one side). But this is beyond my understanding of the program for the moment...

Cheers,

Totor

original message below
--------------------------------------------------------------

It will... when I'm back from holidays :-)

- Chris -

2009/8/21 Eugene Alvin Villar <seav80 at gmail.com>

> Hi,
>
> I hope this fix gets integrated. I was just about to ask what the Align
> command was supposed to do since it didn't do anything sane at all. :-)
>
> Eugene
>
>
>
> On Fri, Aug 21, 2009 at 11:55 PM, Chris Baird <cjb at brushtail.apana.org.au>wrote:
>
>> (I attempted to file a bug ticket at first, however Trac is spitting
>> out Python errors.. Trying to post to this list wasn't the easiest,
>> either, with Mailman not understanding RFC5233 +tagging..)
>>
>> The foremost issue I found in the code of the buggy 'Align Nodes'
>> feature was the multiplications overflowing the 32-bit integer
>> datatype in Coord::length (src/Maps/Coord.h, line 83 as of 20090818
>> checkout). This bug was causing src/Maps/Coord.cpp:angle() to return a
>> bogus value.
>>
>> Problem code in length():
>>    return sqrt((double)Lat*Lat+Lon*Lon));
>>
>> A fix by someone from a time when you had to be paranoid about C
>> compiler behavour:
>>
>>    return sqrt((double)((long)Lat*(long)Lat+(long)Lon*(long)Lon));
>>
>>
>> In the process of locating the above bug, I made a few code rewrites
>> 'for clarity', that I should probably include below, just in case I've
>> forgotten a subtlety.
>>
>> src/Maps/Coord.cpp:
>>
>>  double angle(Coord p1)
>>    {
>>        if (p1.length() == 0)
>>          return 0;
>>        double adjacent = (double)p1.lon() / p1.length();
>>        if (p1.lat() > 0)
>>    }
>>
>> The original code mucked-about with constant coordinate conversions
>> (intToRad), which is unnecessary as (Trigonometric) ratios were
>> involved.
>>
>> src/Maps/FeatureManipulations.cpp:alignNodes():
>>
>>  void alignNodes(MapDocument* theDocument, CommandList* theList,
>> PropertiesDock* theDock)
>>  {
>> [..]
>>        //we do the alignment
>>        Coord pos(0,0);
>>        const Coord p1(Nodes[0]->position());
>>        const Coord p2(Nodes[1]->position()-p1);
>>        const double slope = angle(p2);
>>        for (int i=2; i<Nodes.size(); ++i) {
>>                pos=Nodes[i]->position()-p1;
>>                rotate(pos,-slope);
>>                pos.setLat(0);
>>                rotate(pos,slope);
>>                pos=pos+p1;
>> theDocument->getDirtyOrOriginLayer(Nodes[i]->layer()) ));
>>        }
>>  }
>>
>> (I had an "Aha!" moment when I figured out how this code worked. Nice
>> idea. :)
>>
>> --
>> Chris Baird,, <cjb+merkaator at brushtail.apana.org.au<cjb%2Bmerkaator at brushtail.apana.org.au>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

```