Writing OSMChange Files (changewriter)

The changewriter module provides an interface for writing OSMChange files. This interface is defined as OSMChangeWriter.

It supports addition, removal, and modification of Nodes, Ways, and Relations.

The general workflow to use this class is the following:

Note

The opening <osmChange> tag in the output file will contain a generator attribute which is set as follows: osmchangewriter (Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro})

class changegen.changewriter.Tag(key, value)

Bases: tuple

OSM Tag element.

key

Alias for field number 0

value

Alias for field number 1

class changegen.changewriter.Node(id, version, lat, lon, tags)

Bases: tuple

OSM Node element.

id

Alias for field number 0

lat

Alias for field number 2

lon

Alias for field number 3

tags

Alias for field number 4

version

Alias for field number 1

class changegen.changewriter.Way(id, version, nds, tags)

Bases: tuple

OSM Way element. All fields are required.

id

Alias for field number 0

nds

Nds is a list of OSM IDs representing the Nodes that comprise the Way.

tags

Alias for field number 3

version

Alias for field number 1

class changegen.changewriter.Relation(id, version, members, tags)

Bases: tuple

OSM Relation element. Members must be RelationMember objects.

id

Alias for field number 0

members

List of RelationMember objects.

tags

Alias for field number 3

version

Alias for field number 1

class changegen.changewriter.RelationMember(ref, type, role)

Bases: tuple

OSM RelationMember element.

ref

Alias for field number 0

role

Alias for field number 2

type

Alias for field number 1

class changegen.changewriter.OSMChangeWriter(filename=None, compress=False)

Bases: object

Write changesets as OSMChange format to a file.

Provides support for modify, create, add delete tags currently, with support for Node (Node), Way (Way), and Relation (Relation) OSM elements.

close() must be called to ensure compliance with XML schema.

Parameters
  • filename (str) – A path specifying the location of the output changefile.

  • compress (bool) – A boolean indicating wither to use GZip compression when writing the changefile.

Raises

warnings.ResourceWarning – if close() isn’t called before object is garbage-collected the resulting file will be missing a closing XML tag and will be invalid.

writer = OSMChangeWriter('test.osc', compress=True)
_tag = changewriter.Tag("attribute", "value")
_node = changewriter.Node(id="-111", version="99", lat=90, lon=180, tags=[_tag])
writer.add_create([_node])
writer.close()
close()

Add the </osmChange> closing tag and close the file.

add_modify(elementlist: List[Union[changegen.changewriter.Node, changegen.changewriter.Relation, changegen.changewriter.Way]])

Creates <modify> tag containing all elements in elementlist.

add_create(elementlist: List[Union[changegen.changewriter.Node, changegen.changewriter.Relation, changegen.changewriter.Way]])

Creates <create> element containing all elements in elementlist.

NOTE: does not ensure that <node> elements are added for every <node> in any Ways in the elementlist.

add_delete(elementlist: List[Union[changegen.changewriter.Node, changegen.changewriter.Relation, changegen.changewriter.Way]])

Creates a <delete> element containing all elements in elementlist

changegen.changewriter.write_osm_object(osm: Union[changegen.changewriter.Node, changegen.changewriter.Way, changegen.changewriter.Relation], writer: changegen.changewriter.OSMChangeWriter)

Helper function that writes an OSM object (Node, Way, Relation) as an XML object using an OSMChangeWriter. Uses the type of the namedtuple representing each OSM object to determine the type of XML element to write.

Creates child elements for each Tag or nd (node ref) contained within the parent object.