Package upoints :: Module osm :: Class Osm
[hide private]
[frames] | no frames]

Class Osm

object --+        
         |        
      list --+    
             |    
  point.Points --+
                 |
                Osm

Class for representing an OSM region

Since: 0.9.0

Instance Methods [hide private]
new list
__init__(self, osm_file=None)
Initialise a new Osm object
str
__repr__(self)
Self-documenting string representation
Osm
import_locations(self, osm_file)
Import OSM data files
 
export_osm_file(self)
Generate OpenStreetMap element tree from Osm

Inherited from point.Points: bearing, destination, distance, final_bearing, forward, inverse, midpoint, range, sun_events, sunrise, sunset, to_grid_locator

Inherited from list: __add__, __contains__, __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getitem__, __getslice__, __gt__, __hash__, __iadd__, __imul__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reversed__, __rmul__, __setitem__, __setslice__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, osm_file=None)
(Constructor)

 
Initialise a new Osm object
Parameters:
  • points - Point objects to wrap
  • parse - Whether to attempt import of points
  • units - Unit type to be used for distances when parsing string locations
Returns: new list
Overrides: object.__init__

__repr__(self)
(Representation operator)

 
Self-documenting string representation
Returns: str
String to recreate Osm object
Overrides: object.__repr__

import_locations(self, osm_file)

 

Import OSM data files

import_locations() returns a list of Node and Way objects.

It expects data files conforming to the OpenStreetMap 0.5 DTD, which is XML such as:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="upoints/0.9.0">
  <node id="0" lat="52.015749" lon="-0.221765" user="jnrowe" visible="true" timestamp="2008-01-25T12:52:11+00:00" />
  <node id="1" lat="52.015761" lon="-0.221767" visible="true" timestamp="2008-01-25T12:53:00+00:00">
    <tag k="created_by" v="hand" />
    <tag k="highway" v="crossing" />
  </node>
  <node id="2" lat="52.015754" lon="-0.221766" user="jnrowe" visible="true" timestamp="2008-01-25T12:52:30+00:00">
    <tag k="amenity" v="pub" />
  </node>
  <way id="0" visible="true" timestamp="2008-01-25T13:00:00+0000">
    <nd ref="0" />
    <nd ref="1" />
    <nd ref="2" />
    <tag k="ref" v="My Way" />
    <tag k="highway" v="primary" />
  </way>
</osm>

The reader uses Python's ElementTree module, so should be very fast when importing data. The above file processed by import_locations() will return the following Osm object:

Osm([
    Node(0, 52.015749, -0.221765, True, "jnrowe",
         Timestamp(2008, 1, 25, 12, 52, 11), None),
    Node(1, 52.015761, -0.221767, True,
         Timestamp(2008, 1, 25, 12, 53), None,
         {"created_by": "hand", "highway": "crossing"}),
    Node(2, 52.015754, -0.221766, True, "jnrowe",
         Timestamp(2008, 1, 25, 12, 52, 30),
         {"amenity": "pub"}),
    Way(0, [0, 1, 2], True, None,
        Timestamp(2008, 1, 25, 13, 00),
        {"ref": "My Way", "highway": "primary"})],
    generator="upoints/0.9.0")
>>> region = Osm(open("osm"))
>>> for node in sorted(filter(lambda x: isinstance(x, Node), region),
...                    key=lambda x: x.ident):
...     print(node)
Node 0 (52°00'56"N, 000°13'18"W) [visible, user: jnrowe, timestamp: 2008-01-25T12:52:11+00:00]
Node 1 (52°00'56"N, 000°13'18"W) [visible, timestamp: 2008-01-25T12:53:00+00:00, highway: crossing, created_by: hand]
Node 2 (52°00'56"N, 000°13'18"W) [visible, user: jnrowe, timestamp: 2008-01-25T12:52:30+00:00, amenity: pub]
Parameters:
  • osm_file (file, list or str) - OpenStreetMap data to read
Returns: Osm
Nodes and ways from the data
Overrides: point.Points.import_locations