You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user_manual/06-Writing-Data.md
+57-3Lines changed: 57 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,18 @@ pyosmium will refuse to overwrite any existing files. Either make sure to
18
18
delete the files before instantiating a writer or use the parameter
19
19
`overwrite=true`.
20
20
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
+
21
33
Once a writer is instantiated, one of the `add*` functions can be used to
22
34
add an OSM object to the file. You can either use one of the
23
35
`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
27
39
make sure that the order is correct with respect to the
28
40
[conventions for object order][order-in-osm-files].
29
41
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
-
33
42
Here is a complete example for a script that converts a file from OPL format
34
43
to PBF format:
35
44
@@ -129,3 +138,48 @@ pyosmium implements three different writer classes: the basic
129
138
the two reference-completing writers
130
139
[ForwardReferenceWriter][osmium.ForwardReferenceWriter] and
0 commit comments