Skip to content

Instantly share code, notes, and snippets.

@RafalR RafalR/adresy.js
Last active Feb 3, 2017

Embed
What would you like to do?
var JOptionPane = javax.swing.JOptionPane;
var Main = org.openstreetmap.josm.Main;
var DeleteCommand = org.openstreetmap.josm.command.DeleteCommand;
var ChangePropertyCommand = org.openstreetmap.josm.command.ChangePropertyCommand;
var SequenceCommand = org.openstreetmap.josm.command.SequenceCommand;
function getLayerManager() {
if (Main.main == null) return null;
if (Main.map == null) return null;
return Main.layerManager;
}
function calcAlpha(n1,n2){
var p1 = n1.getCoor(),
p2 = n2.getCoor(),
dA = Math.atan((p2.getY() - p1.getY()) / (p2.getX() - p1.getX())) * 180 / Math.PI + (p1.getX() > p2.getX() ? 180 : 0);
return checkAlpha(dA);
}
function checkAlpha(dA){
if (dA > 180) {
return dA - 360;
}
if (dA <= -180) {
return dA + 360;
}
return dA;
}
var lm = getLayerManager();
var cmda = new Array()
if(lm && lm.editLayer && lm.editLayer.data ){
var selectedWays = lm.editLayer.data.getSelectedWays().toArray()
for (i in selectedWays){
var way = selectedWays[i];
if(way.area && way.hasKey('building')){
var bb = way.getBBox();
var nn = lm.editLayer.data.searchNodes(bb).toArray();
var an = new Array();
nLoop:
for (j in nn){
var n = nn[j];
if(n.deleted){continue;}
var k = n.getNumKeys();
if(k>=4){
keys = n.getKeys().keySet().toArray()
for(l in keys){
var s = new String(keys[l])
if(s.indexOf("addr") < 0){continue nLoop;}
}
an.push(n);
}
}
if(an.length>0){
_an = new Array();
var ln = way.getNodes();
for(var f=0;f<an.length;f++){
var dA;
var dAO = calcAlpha(an[f], ln.get(ln.size()-1));
var dSA = 0;
for (var g=0;g<ln.size();g++){
dA = calcAlpha(an[f], ln.get(g));
dSA += checkAlpha( dA - dAO );
dAO = dA;
}
dSA = Math.abs(dSA);
if(dSA > 359 && dSA < 361){
_an.push(an[f])
}
}
an = _an;
}
if(an.length == 1){
var cmd = new Array();
var addr = an[0].getKeys();
var akeys = addr.keySet().toArray()
for(l in akeys){
cmd.push(new ChangePropertyCommand(way, akeys[l], addr.get(akeys[l])));
}
if(an[0].deleted){
JOptionPane.showMessageDialog(Main.parent, an[0]);
}else{
cmd.push(new DeleteCommand(an[0]));
}
cmda.push(new SequenceCommand("Przenoszenie adresu", cmd))
}
}
//JOptionPane.showMessageDialog(Main.parent, way.getBBox);
}
Main.main.undoRedo.add(new SequenceCommand("Przeniesiono adresy: "+cmda.length, cmda))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.