Skip to content

Instantly share code, notes, and snippets.

@eMerzh
Last active December 18, 2015 09:19
Show Gist options
  • Save eMerzh/5760869 to your computer and use it in GitHub Desktop.
Save eMerzh/5760869 to your computer and use it in GitHub Desktop.
#!/bin/jython
'''
This code is released under the WTFPL
It tries to fix Urbis osm files (missing addresses in relation )
'''
from javax.swing import JOptionPane
from org.openstreetmap.josm import Main
import org.openstreetmap.josm.command as Command
import org.openstreetmap.josm.data.osm.Node as Node
import org.openstreetmap.josm.data.osm.Way as Way
import org.openstreetmap.josm.data.osm.Relation as Relation
import org.openstreetmap.josm.data.osm.TagCollection as TagCollection
import org.openstreetmap.josm.data.osm.DataSet as DataSet
import org.openstreetmap.josm.data.osm.RelationMember as RelationMember
import re, time
import codecs
import org.openstreetmap.josm.actions.search.SearchAction as SearchAction
import org.openstreetmap.josm.actions.search.SearchAction.SearchMode as SearchMode
def getMapView():
if Main.main and Main.main.map:
return Main.main.map.mapView
else:
return None
def isMember (relation, item):
for member in relation.getMembers():
if member.getUniqueId() == item.getUniqueId():
return True
return False
commandsList = []
print "hello"
mv = getMapView()
if mv and mv.editLayer and mv.editLayer.data:
#SearchAction.search('-role:house "addr:housenumber"', SearchMode.fromCode('R'))
for way in mv.getActiveLayer().data.getWays():
if way.get('addr:housenumber'):
#Get All building with address, find the matching relation
for relation in mv.getActiveLayer().data.getRelations():
if relation.get('type') == 'associatedStreet':
newRel = Relation(relation)
if way.get('addr:street') == relation.get('name'):
if not isMember(relation, way):
newMember = RelationMember("house", way)
relation.addMember(newMember)
print "ok for w: " + relation.get('name') + " n " + way.get('addr:housenumber')
newRel.addMember(newMember)
commandsList.append(Command.ChangeCommand(relation, newRel))
Main.main.undoRedo.add(Command.SequenceCommand("Adding way to relation", commandsList))
for node in mv.getActiveLayer().data.getNodes():
if node.get('addr:housenumber'):
#Get All building with address, find the matching relation
SearchAction.search('"ref:UrbIS"=' + node.get('ref:UrbIS') + '', SearchMode.fromCode('R'))
for relation in mv.getActiveLayer().data.getRelations():
#for member in restriction.getMembers():
if relation.get('type') == 'associatedStreet':
#newRel = Relation(relation)
if node.get('addr:street') == relation.get('name'):
if not isMember(relation, node):
newMember = RelationMember("house", node)
newRel.addMember(newMember)
print "ok for n: " + relation.get('name') + " n " + node.get('addr:housenumber')
commandsList.append(Command.ChangeCommand(relation, newRel))
Main.main.undoRedo.add(Command.SequenceCommand("Adding node to relation", commandsList))
print "bye\n"
#!/bin/jython
'''
This code is released under the GNU General
Public License v2 or later.
The GPL v3 is accessible here:
http://www.gnu.org/licenses/gpl.html
The GPL v2 is accessible here:
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
It comes with no warranty whatsoever.
This code illustrates how to use Jython (Python in the scripting plugin of JOSM) to:
* loop over all members of a route relation
* find out whether the member is a node, a way or a relation
* add members to a relation
* How to set an element selected
'''
from javax.swing import JOptionPane
from org.openstreetmap.josm import Main
import org.openstreetmap.josm.command as Command
import org.openstreetmap.josm.data.osm.Node as Node
import org.openstreetmap.josm.data.osm.Way as Way
import org.openstreetmap.josm.data.osm.Relation as Relation
import org.openstreetmap.josm.data.osm.TagCollection as TagCollection
import org.openstreetmap.josm.data.osm.DataSet as DataSet
import org.openstreetmap.josm.data.osm.RelationMember as RelationMember
import re, time
import codecs
def getMapView():
if Main.main and Main.main.map:
return Main.main.map.mapView
else:
return None
dummy_way = Way()
dummy_relation = Relation()
mv = getMapView()
if mv and mv.editLayer and mv.editLayer.data:
selectedRelations = mv.editLayer.data.getSelectedRelations()
selectedWays = mv.editLayer.data.getSelectedWays()
selectedNodes = mv.editLayer.data.getSelectedNodes()
if not(selectedRelations or selectedWays or selectedNodes):
JOptionPane.showMessageDialog(Main.parent, "Please select an address node, a building or an associatedStreet relation")
else:
asrelations = []
for node in selectedNodes:
for parent in node.getReferrers():
if parent.getType() == dummy_relation.getType() and parent.get('type') == 'associatedStreet':
asrelations.append(parent)
for way in selectedWays:
for parent in way.getReferrers():
if parent.getType() == dummy_relation.getType() and parent.get('type') == 'associatedStreet':
asrelations.append(parent)
for relation in selectedRelations:
if parentrelation.get('type') == 'associatedStreet':
asrelations.append(parent)
buildingsWithoutAddresses = {}
for way in mv.getActiveLayer().data.getWays():
if way.get('building') and not way.get('addr:housenumber'):
buildingsWithoutAddresses[way.get('ref:UrbIS')] = way
for relation in asrelations:
Main.main.getCurrentDataSet().addSelected(relation)
newAssStrRel = Relation(relation); modified = False; commandsList = []; lowPos = 1
for member in relation.getMembers():
member.getMember().get('addr:housenumber')
if member.isNode():
node = member.getNode()
ref = node.get('ref:UrbIS')
if ref in buildingsWithoutAddresses:
newMember = RelationMember("house",buildingsWithoutAddresses[ref])
del buildingsWithoutAddresses[ref]
newAssStrRel.addMember(lowPos, newMember)
modified = True
lowPos += 1
if modified:
commandsList.append(Command.ChangeCommand(relation, newAssStrRel))
Main.main.undoRedo.add(Command.SequenceCommand("Adding buildings without address information", commandsList))
commandsList = []
Main.main.getCurrentDataSet().addSelected(relation.getMembers())
#!/bin/jython
'''
This code is released under the WTFPL
It try to fix Urbis osm files (missing addresses in relation )
'''
from javax.swing import JOptionPane
from org.openstreetmap.josm import Main
import org.openstreetmap.josm.data.osm.Node as Node
import org.openstreetmap.josm.data.osm.Way as Way
import org.openstreetmap.josm.data.osm.Relation as Relation
import org.openstreetmap.josm.data.osm.RelationMember as RelationMember
import org.openstreetmap.josm.actions.search.SearchAction as SearchAction
import org.openstreetmap.josm.actions.search.SearchAction.SearchMode as SearchMode
import re, time
import codecs
def getMapView():
if Main.main and Main.main.map:
return Main.main.map.mapView
else:
return None
def addRelFromObj(item, old_rel):
rel = Relation(old_rel, True)
rel.put('name',item.get('addr:street'))
newMember = RelationMember("house", item)
for i in range(old_rel.getMembersCount()-1,-1 ,-1):
rel.removeMember(i)
rel.addMember(newMember)
print "Add Relation " +item.get('addr:street')
rel.save()
return rel
dummy_way = Way()
dummy_relation = Relation()
first_rel = None
print "hello"
mv = getMapView()
if mv and mv.editLayer and mv.editLayer.data:
SearchAction.search('-role:house "addr:housenumber"=*', SearchMode.fromCode('R'))
selectedRelations = mv.editLayer.data.getSelectedRelations()
selectedWays = mv.editLayer.data.getSelectedWays()
selectedNodes = mv.editLayer.data.getSelectedNodes()
# IIIIIK /o\
for r in mv.getActiveLayer().data.getRelations():
if r.getType() == dummy_relation.getType() and r.get('type') == 'associatedStreet':
first_rel =r
break
nbr = 0
for item in selectedWays:
old_nbr = nbr
for rel in mv.getActiveLayer().data.getRelations():
if rel.get('type') == 'associatedStreet' and item.get('addr:street') == rel.get('name'):
newMember = RelationMember("house", item)
rel.addMember(newMember)
nbr = nbr +1
if old_nbr == nbr:
mv.editLayer.data.addPrimitive(addRelFromObj (item, first_rel))
for item in selectedNodes:
old_nbr = nbr
for rel in mv.getActiveLayer().data.getRelations():
if rel.get('type') == 'associatedStreet' and item.get('addr:street') == rel.get('name'):
newMember = RelationMember("house", item)
rel.addMember(newMember)
nbr = nbr +1
if old_nbr == nbr:
mv.editLayer.data.addPrimitive(addRelFromObj (item, first_rel))
for item in selectedRelations:
old_nbr = nbr
for rel in mv.getActiveLayer().data.getRelations():
if rel.get('type') == 'associatedStreet' and item.get('addr:street') == rel.get('name'):
newMember = RelationMember("house", item)
rel.addMember(newMember)
nbr = nbr +1
if old_nbr == nbr:
mv.editLayer.data.addPrimitive(addRelFromObj (item, first_rel) )
print 'Added '+ str(nbr)+' items in relations'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment