Skip to content

Commit 4b17d2f

Browse files
committed
more documentation on the different writers
1 parent 9887c96 commit 4b17d2f

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

docs/user_manual/06-Writing-Data.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ pyosmium will refuse to overwrite any existing files. Either make sure to
1818
delete the files before instantiating a writer or use the parameter
1919
`overwrite=true`.
2020

21+
All writers are [context managers](https://docs.python.org/3/reference/datamodel.html#context-managers) and to ensure that the file is properly closed in the
22+
end, the recommended way to use them is in a with statement:
23+
24+
!!! example
25+
```python
26+
with osmium.SimpleWriter('my_extra_data.osm.pbf') as writer:
27+
# do stuff here
28+
```
29+
30+
When not used inside a with block, then don't forget to call the `close()`
31+
function explicitly to close the writer.
32+
2133
Once a writer is instantiated, one of the `add*` functions can be used to
2234
add an OSM object to the file. You can either use one of the
2335
`add_node/way/relation` functions to force writing a specific type of
@@ -27,9 +39,6 @@ they are given to the writer object. It is your responsibility as a user to
2739
make sure that the order is correct with respect to the
2840
[conventions for object order][order-in-osm-files].
2941

30-
After writing all data the writer needs to be closed using the `close()`
31-
function. It is usually easier to use a writer as a context manager.
32-
3342
Here is a complete example for a script that converts a file from OPL format
3443
to PBF format:
3544

@@ -129,3 +138,48 @@ pyosmium implements three different writer classes: the basic
129138
the two reference-completing writers
130139
[ForwardReferenceWriter][osmium.ForwardReferenceWriter] and
131140
[BackReferenceWriter][osmium.BackReferenceWriter].
141+
142+
### Writing specific objects only
143+
144+
The [SimpleWriter][osmium.SimpleWriter] creates an OSM data file by directly
145+
writing out any OSM object that it receives in the chosen format.
146+
147+
148+
### Writing reference-complete files
149+
150+
The [BackReferenceWriter][osmium.BackReferenceWriter] will make sure that the
151+
file that is written out is reference-complete, meaning all objects that are
152+
directly referenced by the object written are added to the output file as well.
153+
This is needed when you want to make sure that geometries can be recreated
154+
from the object in the file.
155+
156+
Creating a file with backward references is a two-stage process: while the
157+
writer is open, it will write all objects received through one of the `add_*()`
158+
functions into a temporary file and keeps a record of which objects are needed
159+
to make the file reference-complete. Once the writer is closed, it collects the
160+
missing object from a given reference file, merges them with the data from
161+
the temporary file and writes out the final result.
162+
163+
### Writing files with forward references
164+
165+
The [ForwardReferenceWriter][osmium.ForwardReferenceWriter] completes the
166+
written objects with forward references. This is particularly useful when
167+
creating geographic extracts of any kind: one selects the node of interest
168+
in a particular area and then lets the ForwardReferenceWriter complete the
169+
ways and relations referring to the nodes.
170+
171+
Files written by the ForwardReferenceWriter are not necessarily
172+
reference-complete. That is easy to see when considering the example of the
173+
geographic extract: there may be ways in the area that cross the boundary
174+
of the area chosen but only the nodes within the area are written out. This
175+
might be useful in many situations as the way would be simply seem to be cut
176+
on the area of interest. However, it has the disadvantage that some objects
177+
will get invalid geometries, especially when they represent areas.
178+
179+
The other thing to consider during forward completion are indirect references.
180+
When completing relations indirectly referenced through ways or other relations,
181+
then the resulting file can become big very quickly. For example, a seemingly
182+
small extract of the city of Strasbourg can suddenly contain not only the
183+
relations for France and Germany but also electoral boundaries and entire
184+
timezones. For that reason, when forward-completing relations, it is not
185+
recommended to use backward completion.

0 commit comments

Comments
 (0)