OSM Data Structures#
This module provides classes for working with OpenStreetMap (OSM) data elements.
Base Class#
osmdiff.osm.OSMObject(tags={}, attribs={}, bounds=None)
#
Base class for all OpenStreetMap elements (nodes, ways, relations).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags
|
Dict[str, str]
|
Key-value tag dictionary |
{}
|
attribs
|
Dict[str, str]
|
XML attributes dictionary |
{}
|
bounds
|
List[float]
|
Optional bounding box coordinates [minlon, minlat, maxlon, maxlat] |
None
|
Note
This is an abstract base class - use Node, Way or Relation for concrete elements.
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(tags={}, attribs={}, bounds=None)
#
Bases: OSMObject
OpenStreetMap node (geographic point feature).
Implements geo_interface for GeoJSON compatibility as a Point feature. Coordinates must be valid (-180<=lon<=180, -90<=lat<=90).
Source code in src/osmdiff/osm/osm.py
lat
property
#
Get latitude value.
lon
property
#
Get longitude value.
Way#
osmdiff.osm.Way(tags=None, attribs=None, bounds=None, nodes=None)
#
Bases: OSMObject
Represents an OSM way (linear feature).
Implements geo_interface for GeoJSON compatibility as either: - LineString for open ways - Polygon for closed ways
Initialize a Way object.
Source code in src/osmdiff/osm/osm.py
Relation#
osmdiff.osm.Relation(tags=None, attribs=None, bounds=None)
#
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"
Initialize a Relation object.