OSM Data Structures#
This module provides classes for working with OpenStreetMap (OSM) data elements.
Base Class#
osmdiff.osm.OSMObject
#
Base class for OpenStreetMap objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags
|
dict
|
OSM tags (key-value pairs) |
{}
|
attribs
|
dict
|
XML attributes |
{}
|
bounds
|
list
|
Bounding box [minlon, minlat, maxlon, maxlat] |
None
|
Attributes:
Name | Type | Description |
---|---|---|
tags |
dict
|
OSM tags (key-value pairs) |
attribs |
dict
|
XML attributes |
bounds |
list
|
Bounding box [minlon, minlat, maxlon, maxlat] |
Methods:
Name | Description |
---|---|
from_xml |
Create object from XML element |
_parse_tags |
Parse tags from XML |
_parse_bounds |
Parse bounds from XML |
Raises:
Type | Description |
---|---|
ValueError
|
If XML element is invalid |
TypeError
|
If element type is unknown |
Example:
node = Node()
node.attribs = {"lon": "0.0", "lat": "51.5"}
Source code in src/osmdiff/osm/osm.py
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
__init__(tags={}, attribs={}, bounds=None)
#
Initialize an empty OSM object.
Source code in src/osmdiff/osm/osm.py
__repr__()
#
String representation of the OSM object.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Object type and ID, with additional info for ways/relations |
Source code in src/osmdiff/osm/osm.py
from_file(filename)
classmethod
#
Create object from XML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Path to XML file |
required |
Returns:
Name | Type | Description |
---|---|---|
OSMObject |
OSMObject
|
Parsed object |
Source code in src/osmdiff/osm/osm.py
from_xml(elem)
classmethod
#
Create OSM object from XML element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
elem
|
Element
|
XML element representing an OSM object |
required |
Returns:
Name | Type | Description |
---|---|---|
OSMObject |
OSMObject
|
Appropriate subclass instance |
Raises:
Type | Description |
---|---|
ValueError
|
If XML element is invalid |
TypeError
|
If element type is unknown |
Source code in src/osmdiff/osm/osm.py
to_dict()
#
Convert object to dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary representation |
Source code in src/osmdiff/osm/osm.py
to_json()
#
OSM Elements#
Node#
osmdiff.osm.Node
#
Bases: OSMObject
Represents an OSM node (point feature).
Attributes#
lon (float): Longitude
lat (float): Latitude
__geo_interface__ (dict): GeoJSON-compatible interface, see https://gist.github.com/sgillies/2217756 for more details.
Example#
node = Node()
node.attribs = {"lon": "0.0", "lat": "51.5"}
print(node.lon, node.lat) # 0.0, 51.5
Source code in src/osmdiff/osm/osm.py
lat
property
#
Get latitude value.
lon
property
#
Get longitude value.
Way#
osmdiff.osm.Way
#
Bases: OSMObject
Represents an OSM way (linear feature).
Attributes#
nodes (list): List of Node objects
__geo_interface__ (dict): GeoJSON-compatible interface, see https://gist.github.com/sgillies/2217756 for more details.
Example#
way = Way()
way.nodes = [Node(), Node()] # Add nodes
print(way.__geo_interface__["type"]) # "LineString" or "Polygon"
Source code in src/osmdiff/osm/osm.py
Relation#
osmdiff.osm.Relation
#
Bases: OSMObject
Represents an OSM relation (collection of features).
Attributes#
members (list): List of member objects
__geo_interface__ (dict): GeoJSON-compatible interface, see https://gist.github.com/sgillies/2217756 for more details.
Example#
relation = Relation()
relation.members = [Way(), Node()] # Add members
print(relation.__geo_interface__["type"]) # "FeatureCollection"