OSMExpress icon indicating copy to clipboard operation
OSMExpress copied to clipboard

Python api enhancements

Open bdon opened this issue 5 years ago • 2 comments

@CloudNiner

Some small Python API breaking changes (I should bump major version on PyPI)

osmx.tags_dict(way.tags) becomes osmx.tags_dict(way)

ways.get(way_id) becomes ways[way_id]

Two new API functions:

Check for the existence of a OSM ID in a table: if way_id not in ways

Iterate through all OSM entities in a table: for relation_id, relation in relations

Example code using new API methods:

import sys
import osmx

env = osmx.Environment(sys.argv[1])
with osmx.Transaction(env) as txn:
    locations = osmx.Locations(txn)
    ways = osmx.Ways(txn)
    relations = osmx.Relations(txn)

    for relation_id, relation in relations:
        tags = osmx.tag_dict(relation)
        if 'type' in tags and tags['type'] == 'boundary':
            for member in relation.members:
                if member.type == 'way' and member.ref not in ways:
                    print(f"Relation {relation_id} missing way {member.ref}")

bdon avatar Oct 17 '20 19:10 bdon

perhaps we should keep the old get method and leave it as returning None when the key is missing, but the new [] method raises an Exception. That would echo the behavior of python dictionaries.

similarly, perhaps for relation in relations should yield relation IDs, and for relation_id, relation in relations.items() should yield the pair (relation_id, relation) - again acting like python dictionaries

bdon avatar Oct 18 '20 04:10 bdon

Nice changes to make this a bit more pythonic -- thanks for the heads up! Would be worth documenting whichever subset of dictionary ops are supported as a newer Python programmer may not understand at a glance that, for example def __contains__ powers the <foo> in <bar> syntax.

perhaps we should keep the old get method and leave it as returning None when the key is missing, but the new [] method raises an Exception.

💯 to keeping get with default None. Both versions are super useful in a variety of cases.

CloudNiner avatar Oct 19 '20 15:10 CloudNiner