summaryrefslogtreecommitdiffstats
path: root/osm_parser.py
blob: b3eed86df82fcc6cacf442348374d2ff29c16010 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Henrik 'catears' Adolfsson, henad221@student.liu.se
# Date: 2014-11-09
# Copyright (c) 2014 Henrik Adolfsson
# Licensed under MIT License (see bottom of file)

"""
In this file you will find a class that can parse OpenStreetMap Node-data and OpenStreetMap Way-data.
There is a helper function for retrieving a default parser called get_default_parser(fname)
It provides you with a basic parser that can iterate over nodes and iterate over ways

If you want to tweak the behaviour of the default parser you can change the
tags that will be returned with the tuples DEFAULT_NODE/WAY_TAGS under the Module Config-section

The default parser will return edges with tags if they exist in "DEFAULT_WAY_TAGS".
You can tweak the behaviour of the default parser so that it will return all tags it encounters.
You do this by calling 'get_default_parser(fname, allow_all=True)'

The only dependency of the module is the xml.etree.ElementTree which 
is a part of the python library for both py2 and py3
"""

import xml.etree.ElementTree as ET
FILEEXT = ".osm"


##########################################
# Module config                          #
##########################################
DEFAULT_NODE_TAGS = ('id', 'lat', 'lon')
DEFAULT_WAY_TAGS = ('highway', 'name', 'oneway')


##########################################
# OSM Specific Keys                      #
##########################################
OSM_NODE = "node"
OSM_WAY = "way"
OSM_WAYNODE = "nd"
OSM_NODE_REFERENCE = "ref"
OSM_TAG = "tag"
OSM_WAY_ID = "id"
OSM_TAG_KEY = "k"
OSM_TAG_VALUE = "v"


##########################################
# Ouput keys for returned dictionary     #
##########################################
OUT_EDGE_ID = "id"
OUT_EDGES_KEY = "road"
OUT_TAGS_KEY = "tags"


##########################################
# Parser implementation                  #
##########################################

class OSMParser:
    """
    A Parser that parses OSM data and enables iterating over
    Nodes and Ways as well as selecting which tags to show from the data.

    One of the focuses of the parser has been to create something simple
    that abstracts away as much object oriented features as possible from a normal user

    Essentially the parser will parse the XML file and convert each element present
    in the XML file into a python dictionary. (since this key-value stuff is basically what xml is)
    """

    def __init__(self, fname, all_way_tags):
        """
        Initialize. if all_way_tags is True then the parser will return ways
        with all tags, otherwise it will only choose specific tags
        """
        if not fname.endswith(FILEEXT):
            fname = fname + FILEEXT

        self.fname = fname
        self.node_tags = set()
        self.way_tags = set()
        self.allow_all = all_way_tags

    def add_node_tag(self, tag):
        """ Adds a tag to be searched for when looking at nodes """
        self.node_tags.add(tag)

    def add_way_tag(self, tag):
        """ Adds a tag to be searched for when looking at ways (edges) """
        self.way_tags.add(tag)

    def iter_nodes(self):
        """
        Iterator-object for all nodes in osm-tree.
        Returns a dictionary with the (manually) added tags
        """
        for ev, el in ET.iterparse(self.fname, events=("start", "end")):
            if ev == "start" and el.tag == "osm":
                root = el
            elif ev == "end" and el.tag == "node":
                yield {k: v for k, v in el.items()}
            if ev == "end":
                if el in root:
                    root.remove(el)
                el.clear()

    def iter_ways(self):
        """
        Iterator-object for all ways (edges) in osm-tree.
        Returns a dictionary with the ways
        """
        reading_way = False
        for ev, el in ET.iterparse(self.fname, events=("start", "end")):
            if ev == "start" and el.tag == "osm":
                root = el
            elif ev == "start" and el.tag == "way":
                reading_way = True
            elif ev == "end" and el.tag == "way":
                reading_way = False
                # Take out roads and tags
                road = tuple(node.get(OSM_NODE_REFERENCE)
                             for node in el.iter(OSM_WAYNODE))
                tags = {tag.get(OSM_TAG_KEY): tag.get(OSM_TAG_VALUE)
                        for tag in el.iter(OSM_TAG)
                        if self.allow_all
                        or tag.get(OSM_TAG_KEY) in self.way_tags}

                # Yield the edge id, the road and the tags
                yield {
                    OUT_EDGE_ID: el.get(OSM_WAY_ID),
                    OUT_EDGES_KEY: road,
                    OUT_TAGS_KEY: tags,
                }

            if ev == "end" and not reading_way:
                if el in root:
                    root.remove(el)
                el.clear()


##########################################
# Example Usage and Default Getter       #
##########################################

def get_default_parser(fname, allow_all=False, verbose=True):
    """ 
    Returns a default parser 
    The config used can be tweaked inside the Module Config section

    allow_all sets if the parser should take all tags or if it should just
    take the tags specified, in this case all tags inside DEFAULT_WAY_TAGS
    """
    if verbose:
        print("Parsing OSM data from: {}".format(fname))

    parser = OSMParser(fname, allow_all)

    # Add default node and way tags
    for tag in DEFAULT_NODE_TAGS:
        parser.add_node_tag(tag)

    for tag in DEFAULT_WAY_TAGS:
        parser.add_way_tag(tag)
    
    return parser
        

def print_osm_data(fname):
    """
    Prints all the nodes and all the ways in the map
    Good example of how to use the code =D
    """
    # Create parser
    parser = get_default_parser(fname)
    
    # Print all nodes
    for node in parser.iter_nodes():
        print (node)

    # Print all ways that are tagged with highway
    for way in parser.iter_ways():
        if 'highway' in way['tags']:
            print (way)

    
"""
The MIT License

Copyright (c) 2014 Henrik Adolfsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""