Opened 5 years ago

# Align nodes to rectangle - don't change when calling twice

Reported by: Owned by: jekader team minor Core latest

Currently, the tool works only for a closed way of four nodes.

I think, it would be useful to align any four nodes like that.

So, the Idea is - I select any 4 nodes, press Q, and voila - they are aligned in a rectangle

### comment:1 Changed 5 years ago by anonymous

Problem is, the Rectangle is constructed from the sidelines, not the nodes...

### comment:2 Changed 5 years ago by jekader

I don't know the internals of the algorythm. Just made a suggestion, that would help me to map :)

Moreover, my knowledge of geometry tells me, 4 nodes would be sufficient for the job :)
As well as, say, a "make lines parallel" tool - I'd consider that useful, too.

So' if it's difficult to implement, leave it alone. JOSM is a great tool, and I'll use it anyway. Thanks for the great job, and the german quality!

### comment:3 Changed 5 years ago by Henry Loenwind

I wouldn't say it's impossible to change the tool. However, I don't really understand how it works. ;)

If someone had an algorithm for aligning 4 randomly positioned points into a rectangle, I'd be happy to implement it.

### comment:4 Changed 5 years ago by anonymous

1. calculate the center of the polygon descripted from the 4 nodes
1. calculate the radius of a circle as the distance from the center of the farest node
1. place every other node on the circunference using sin() and cos() based on the angle

### comment:5 Changed 5 years ago by jekader

yeah, now I understood where the difficulty lies :)
I didn't notice it before - that one of the nodes can be inside a triangle of others... So if the nodes are not connected by ways - how should the program "unite" them to see the polygon?

Need an algorythm for that! The previous comment seems to have one :)

### comment:6 Changed 5 years ago by hkucharek

I currently work on a more general approach: You have a way describing an object and all corners should be right angles. You select the way, then the general orientation of edges is computed. Then the edges are aligned to the general orientation and new corners are computed as intersections of re-orientated edges.
I've added some buildings to OSM which are not simple rectangles, but L-, U- or other shapes. With my approach, I'll get them nice. Maybe I'll allow several ways to be processed at once. This would help to orient a group of buildings nicely.

### comment:7 Changed 5 years ago by jekader

yep! that's what I wanted to achieve with my proposal. But your idea is surely better! It's much easier to align the whole building at once, than aligning it's parts step by step.

### comment:8 Changed 5 years ago by hkucharek

I'm now in testing my implementation.
Current results can be seen at
http://img381.imageshack.us/my.php?image=orthomn5.gif[[BR]]
Upper right are three arbitrary shapes to be orthogonalized.
Lower left is outcome of current methode which has to be applied sequentially.
Upper left is outcome of applying my new method sequentially.
Lower right is outcome of applying new method to all three shapes at once.
I modified computing the general orientation by weighting headings by the length of the segment. Thus, longer segments, which are usually better aligned to the real orientation, have more importance in the computations. After establishing the general orientation, each segment is rotated around its center to align with the new orientation. Then the newly oriented segments are intersected with each other. The original nodes are the moved to the intersection points.

### Changed 5 years ago by hkucharek

Outcome of new orthogonalization method

### Changed 5 years ago by hkucharek

Patch for orthonormalization action

### comment:9 follow-up: ↓ 10 Changed 5 years ago by hkucharek

I attached a patch and icon file to add the described orthonormalization action to JOSM. Patch is based on version 1054 of JOSM. Feedback welcome.

### comment:10 in reply to: ↑ 9 ; follow-up: ↓ 11 Changed 5 years ago by hkucharek

I attached a patch and icon file to add the described orthonormalization action to JOSM. Patch is based on version 1054 of JOSM. Feedback welcome.

Ooops. Don't use. Seems, every segment is turned by 180 degrees, so nodes change position more than necessary.

### comment:11 in reply to: ↑ 10 Changed 5 years ago by hkucharek

I attached a patch and icon file to add the described orthonormalization action to JOSM. Patch is based on version 1054 of JOSM. Feedback welcome.

Ooops. Don't use. Seems, every segment is turned by 180 degrees, so nodes change position more than necessary.

Was even more stupid. Had it correct in the comment, but wrong in the code.
Replace the line

```Node n = myWay.nodes.get(i1);
```

with

```Node n = myWay.nodes.get(i2);
```

### comment:12 Changed 5 years ago by jekader

I'm not a developer, so I can't test your work right now - but let's hope it gets into JOSM core soon - very useful for drawing buildings!

### Changed 5 years ago by hkucharek

Alignment action with orientation given by two selected nodes

### comment:13 Changed 5 years ago by hkucharek

I added the possibility to select besides the ways also two single nodes. The new orientation is then established by the line connecting the two nodes and not computed from the ways. This way, you can align the ways to something else, maybe a road or such things. If you already applied the patch, you can just replace the file with the attached one. If you don't select nodes, the behaviour is as before. Checks are done, that if, only two nodes will be accepted.

### comment:14 follow-up: ↓ 15 Changed 5 years ago by framm

• Resolution set to fixed
• Status changed from new to closed

I have applied this patch for r1076 with minor changes: The new action now replaces the old "align in rectangle" method, re-using the key code. There is also a "don't show again"able message warning you if you have EPSG:4326 set. There's one thing I do not like yet, and that is that multiple changes are generated if you call the function repeatedly (it doesn't recognize that the shape is already rectangular, makes minuscle adjustments). I tried to build in a treshold under which there would be no change recorded but it didn't work out well.

### comment:15 in reply to: ↑ 14 Changed 5 years ago by hkucharek

• Resolution fixed deleted
• Status changed from closed to reopened
• Summary changed from Aline nodes to rectangle proposal to Align nodes to rectangle proposal

I have applied this patch for r1076 with minor changes: The new action now replaces the old "align in rectangle" method, re-using the key code. There is also a "don't show again"able message warning you if you have EPSG:4326 set. There's one thing I do not like yet, and that is that multiple changes are generated if you call the function repeatedly (it doesn't recognize that the shape is already rectangular, makes minuscle adjustments). I tried to build in a treshold under which there would be no change recorded but it didn't work out well.

First, thanks for putting it into JOSM. Good idea with the warning about the coordinate system.

Second, I already recognized that and also don't like it. I work on it.

Third, the short description says "Move nodes so all angles are 0/90/180/270deg" which better should read "Move nodes so all angles are 90 or 270deg"

### comment:17 Changed 4 years ago by anonymous

• Cc tdljmgjeQkxKiUvSX removed
• Keywords sdqhYAkZucFvPHw removed

### comment:18 Changed 4 years ago by stoecker

• Status changed from reopened to new
• Summary changed from Align nodes to rectangle proposal to Align nodes to rectangle - don't change when calling twice

### comment:19 Changed 14 months ago by skyper

• Description modified (diff)
• Owner changed from framm to team

### Modify Ticket

Change Properties