Debian Codegraph @

examples/open.py

raw
1 import fiona
2
3 # This module contains examples of opening files to get feature collections in
4 # different ways.
5 #
6 # It is meant to be run from the distribution root, the directory containing
7 # setup.py.
8 #
9 # A ``path`` is always the ``open()`` function's first argument. It can be
10 # absolute or relative to the working directory. It is the only positional
11 # argument, though it is conventional to use the mode as a 2nd positional
12 # argument.
13
14 # 1. Opening a file with a single data layer (shapefiles, etc).
15 #
16 # args: path, mode
17 # kwds: none
18 #
19 # The relative path to a file on the filesystem is given and its single layer
20 # is selected implicitly (a shapefile has a single layer). The file is opened
21 # for reading (mode 'r'), but since this is the default, we'll omit it in
22 # following examples.
23
24 with fiona.open('docs/data/test_uk.shp', 'r') as c:
25 assert len(c) == 48
26
27 # 2. Opening a file with explicit layer selection (FileGDB, etc).
28 #
29 # args: path
30 # kwds: layer
31 #
32 # Same as above but layer specified explicitly by name..
33
34 with fiona.open('docs/data/test_uk.shp', layer='test_uk') as c:
35 assert len(c) == 48
36
37 # 3. Opening a directory for access to a single file.
38 #
39 # args: path
40 # kwds: layer
41 #
42 # Same as above but using the path to the directory containing the shapefile,
43 # specified explicitly by name.
44
45 with fiona.open('docs/data', layer='test_uk') as c:
46 assert len(c) == 48
47
48 # 4. Opening a single file within a zip archive.
49 #
50 # args: path
51 # kwds: vfs
52 #
53 # Open a file given its absolute path within a virtual filesystem. The VFS
54 # is given an Apache Commons VFS identifier. It may contain either an absolute
55 # path or a path relative to the working directory.
56 #
57 # Example archive:
58 #
59 # $ unzip -l docs/data/test_uk.zip
60 # Archive: docs/data/test_uk.zip
61 # Length Date Time Name
62 # -------- ---- ---- ----
63 # 10129 04-08-13 20:49 test_uk.dbf
64 # 143 04-08-13 20:49 test_uk.prj
65 # 65156 04-08-13 20:49 test_uk.shp
66 # 484 04-08-13 20:49 test_uk.shx
67 # -------- -------
68 # 75912 4 files
69
70 with fiona.open('/test_uk.shp', vfs='zip://docs/data/test_uk.zip') as c:
71 assert len(c) == 48
72
73 # 5. Opening a directory within a zip archive to select a layer.
74 #
75 # args: path
76 # kwds: layer, vfs
77 #
78 # The most complicated case. As above, but specifying the root directory within
79 # the virtual filesystem as the path and the layer by name (combination of
80 # 4 and 3). It ought to be possible to open a file geodatabase within a zip
81 # file like this.
82
83 with fiona.open('/', layer='test_uk', vfs='zip://docs/data/test_uk.zip') as c:
84 assert len(c) == 48
85