osm

osm - Imports OpenStreetMap data files..

class upoints.osm.Node(ident, latitude, longitude, visible=False, user=None, timestamp=None, tags=None)[source]

Bases: upoints.point.Point

Class for representing a node element from OSM data files.

New in version 0.9.0.

Initialise a new Node object.

Parameters:
  • ident (int) – Unique identifier for the node
  • latitude (float) – Nodes’s latitude
  • longitude (float) – Node’s longitude
  • visible (bool) – Whether the node is visible
  • user (str) – User who logged the node
  • timestamp (str) – The date and time a node was logged
  • tags (dict) – Tags associated with the node
fetch_area_osm(distance)[source]

Fetch, and import, an OSM region.

Parameters:distance (int) – Boundary distance in kilometres
Returns:All the data OSM has on a region imported for use
Return type:Osm
get_area_url(distance)[source]

Generate URL for downloading OSM data within a region.

Parameters:distance (int) – Boundary distance in kilometres
Returns:
URL that can be used to fetch the OSM data within distance
of location
Return type:str
static parse_elem(element)[source]

Parse a OSM node XML element.

Parameters:element (etree.Element) – XML Element to parse
Returns:Object representing parsed element
Return type:Node
toosm()[source]

Generate a OSM node element subtree.

Returns:OSM node element
Return type:etree.Element
class upoints.osm.Osm(osm_file=None)[source]

Bases: upoints.point.Points

Class for representing an OSM region.

New in version 0.9.0.

Initialise a new Osm object.

export_osm_file()[source]

Generate OpenStreetMap element tree from Osm.

import_locations(osm_file)[source]

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 the 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",
         utils.Timestamp(2008, 1, 25, 12, 52, 11), None),
    Node(1, 52.015761, -0.221767, True,
         utils.Timestamp(2008, 1, 25, 12, 53), None,
         {"created_by": "hand", "highway": "crossing"}),
    Node(2, 52.015754, -0.221766, True, "jnrowe",
         utils.Timestamp(2008, 1, 25, 12, 52, 30),
         {"amenity": "pub"}),
    Way(0, [0, 1, 2], True, None,
        utils.Timestamp(2008, 1, 25, 13, 00),
        {"ref": "My Way", "highway": "primary"})],
    generator="upoints/0.9.0")
Parameters:osm_file (iter) – OpenStreetMap data to read
Returns:Nodes and ways from the data
Return type:Osm
class upoints.osm.Way(ident, nodes, visible=False, user=None, timestamp=None, tags=None)[source]

Bases: upoints.point.Points

Class for representing a way element from OSM data files.

New in version 0.9.0.

Initialise a new Way object.

Parameters:
  • ident (int) – Unique identifier for the way
  • nodes (list of str) – Identifiers of the nodes that form this way
  • visible (bool) – Whether the way is visible
  • user (str) – User who logged the way
  • timestamp (str) – The date and time a way was logged
  • tags (dict) – Tags associated with the way
static parse_elem(element)[source]

Parse a OSM way XML element.

Parameters:element (etree.Element) – XML Element to parse
Returns:Way object representing parsed element
Return type:Way
toosm()[source]

Generate a OSM way element subtree.

Returns:OSM way element
Return type:etree.Element
upoints.osm.get_area_url(location, distance)[source]

Generate URL for downloading OSM data within a region.

This function defines a boundary box where the edges touch a circle of distance kilometres in radius. It is important to note that the box is neither a square, nor bounded within the circle.

The bounding box is strictly a trapezoid whose north and south edges are different lengths, which is longer is dependant on whether the box is calculated for a location in the Northern or Southern hemisphere. You will get a shorter north edge in the Northern hemisphere, and vice versa. This is simply because we are applying a flat transformation to a spherical object, however for all general cases the difference will be negligible.

Parameters:
  • location (Point) – Centre of the region
  • distance (int) – Boundary distance in kilometres
Returns:

URL that can be used to fetch the OSM data within distance of

location

Return type:

str