[OSM-dev] Adding support for relations to change_tags.py and other stuff

Ævar Arnfjörð Bjarmason avarab at gmail.com
Wed Apr 1 10:32:01 BST 2009


(CC-ing to OSM-dev in case anyone else finds this interesting)

Background: I've been looking for some sort of XML-SAX-like engine to
do massive changes to OSM data so that I can do mass-edits without
writing all the tedious parsing/upload/diff logic myself.

change_tags.py looks perfect for this with a few caveats, some of
which have patches & suggestions for:

Firstly it doesn't handle relations, in the attached patch I've
implemented support for this which seems to work, except:

* The Nodes/Ways/Relations count doesn't seem to work.

This appears to be due to some race condition in the XML parser I
can't find, if I change the counting code around line 300 to this:

        if name in ['node', 'way', 'relation', 'tag']:
            if name == 'relation':
                print "Read relation"
            self.read[name] += 1

And run this command:

PYTHONPATH=. python
~/src/osm/applications/utils/change_tags/change_tags.py --dry-run
--verbose --check-api --file Iceland.osm  --module iceland --function
test
Nodes: 259621/0, Ways: 5288/0, Relations: 0/0, 0 complete
Read relation
Read relation
Read relation
[...]

You can see the relations are being read /after/ the count has been
made, but I don't know why.

* I haven't

I just hacked it up, I tried writing a test scripts which changes a
relation and it seems to work (attached as iceland.py), the output
makes sense, here I'm changing name="Vatnsnesvegur" to name="Fleh":

$ PYTHONPATH=. python
~/src/osm/applications/utils/change_tags/change_tags.py --dry-run
--verbose --check-api --file Iceland.osm  --module iceland --function
test
Nodes: 259621/0, Ways: 5276/0, Relations: 0/0, 0 complete
URL:  http://api.openstreetmap.org/api/0.5/relation/106216
XML:
<osm version="0.5">
  <relation id="106216">
    <member ref="23699605" role="" type="way" />
    <member ref="24215960" role="" type="way" />
    <tag k="created_by" v="Potlatch 0.10f" />
    <tag k="name" v="Fleh" />
    <tag k="network" v="T" />
    <tag k="ref" v="711" />
    <tag k="route" v="road" />
    <tag k="type" v="route" />
  </relation>
</osm>
Total Read: 259621 nodes, 13344 ways, 259 relations, 41054 tags
Total Changed: 0 nodes, 0 ways, 1 relations
Previously Changed: 0 nodes, 0 ways, 0 relations

But I haven't uploaded any data with the altered tool.

* The interface is too limited

The script does all the work of figuring out the nodes in a given way
(and with my patch, the members) as well as various other stuff being
stored as member variables in the ChangeTags class, but the user
doesn't get access to this. In my case I wanted to get the members of
relations, I can do this by changing this:

    run = self.converter(self.current['tags'], self.current['type'])

to this:

    run = self.converter(self.current['tags'], self.current['type'],
self.current)

or even this:

    run = self.converter(self.current)

I can then do this in my script:

def test(current)
    if current['type'] == 'relation':
        current['tags']['name'] = 'Fleh'
        current['members'] = [{ 'type': 'way', 'ref': '1234', 'role':'wohoo'}
        return True
    return False

And get this as a result:

URL:  http://api.openstreetmap.org/api/0.5/relation/106216
XML:
<osm version="0.5">
  <relation id="106216">
    <member ref="1234" role="wohoo" type="way" />
    <tag k="created_by" v="Potlatch 0.10f" />
    <tag k="name" v="Fleh" />
    <tag k="network" v="T" />
    <tag k="ref" v="711" />
    <tag k="route" v="road" />
    <tag k="type" v="route" />
  </relation>

I don't care what the interface looks like or what it's called, I just
want to be able to access that data, how do *you* think it should look
like? :)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: change_tags-relation-support.patch
Type: text/x-diff
Size: 7250 bytes
Desc: not available
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20090401/f471b898/attachment.patch>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: iceland.py
Type: text/x-python
Size: 654 bytes
Desc: not available
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20090401/f471b898/attachment.py>


More information about the dev mailing list