• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

docs/H03-May-2022-2,0311,527

fiona/H31-May-2021-159,996116,237

tests/H31-May-2021-9,2507,455

PKG-INFOH A D31-May-202157.7 KiB1,3541,049

README.rstH A D01-Apr-202112.2 KiB343251

benchmark.pyH A D22-Oct-20181.1 KiB5244

setup.pyH A D28-May-202112.6 KiB362280

README.rst

1=====
2Fiona
3=====
4
5Fiona is GDAL_'s neat and nimble vector API for Python programmers.
6
7.. image:: https://github.com/Toblerity/Fiona/workflows/Linux%20CI/badge.svg?branch=maint-1.8
8   :target: https://github.com/Toblerity/Fiona/actions?query=branch%3Amaint-1.8
9
10.. image:: https://ci.appveyor.com/api/projects/status/github/Toblerity/Fiona?svg=true
11   :target: https://ci.appveyor.com/project/sgillies/fiona/branch/master
12
13.. image:: https://coveralls.io/repos/Toblerity/Fiona/badge.svg
14   :target: https://coveralls.io/r/Toblerity/Fiona
15
16Fiona is designed to be simple and dependable. It focuses on reading and
17writing data in standard Python IO style and relies upon familiar Python types
18and protocols such as files, dictionaries, mappings, and iterators instead of
19classes specific to OGR. Fiona can read and write real-world data using
20multi-layered GIS formats and zipped virtual file systems and integrates
21readily with other Python GIS packages such as pyproj_, Rtree_, and Shapely_.
22Fiona is supported only on CPython versions 2.7 and 3.4+.
23
24For more details, see:
25
26* Fiona `home page <https://github.com/Toblerity/Fiona>`__
27* Fiona `docs and manual <https://fiona.readthedocs.io/en/stable/>`__
28* Fiona `examples <https://github.com/Toblerity/Fiona/tree/master/examples>`__
29
30Usage
31=====
32
33Collections
34-----------
35
36Records are read from and written to ``file``-like `Collection` objects
37returned from the ``fiona.open()`` function.  Records are mappings modeled on
38the GeoJSON format. They don't have any spatial methods of their own, so if you
39want to do anything fancy with them you will probably need Shapely or something
40like it. Here is an example of using Fiona to read some records from one data
41file, change their geometry attributes, and write them to a new data file.
42
43.. code-block:: python
44
45    import fiona
46
47    # Open a file for reading. We'll call this the "source."
48
49    with fiona.open('tests/data/coutwildrnp.shp') as src:
50
51        # The file we'll write to, the "destination", must be initialized
52        # with a coordinate system, a format driver name, and
53        # a record schema.  We can get initial values from the open
54        # collection's ``meta`` property and then modify them as
55        # desired.
56
57        meta = src.meta
58        meta['schema']['geometry'] = 'Point'
59
60        # Open an output file, using the same format driver and
61        # coordinate reference system as the source. The ``meta``
62        # mapping fills in the keyword parameters of fiona.open().
63
64        with fiona.open('test_write.shp', 'w', **meta) as dst:
65
66            # Process only the records intersecting a box.
67            for f in src.filter(bbox=(-107.0, 37.0, -105.0, 39.0)):
68
69                # Get a point on the boundary of the record's
70                # geometry.
71
72                f['geometry'] = {
73                    'type': 'Point',
74                    'coordinates': f['geometry']['coordinates'][0][0]}
75
76                # Write the record out.
77
78                dst.write(f)
79
80    # The destination's contents are flushed to disk and the file is
81    # closed when its ``with`` block ends. This effectively
82    # executes ``dst.flush(); dst.close()``.
83
84Reading Multilayer data
85-----------------------
86
87Collections can also be made from single layers within multilayer files or
88directories of data. The target layer is specified by name or by its integer
89index within the file or directory. The ``fiona.listlayers()`` function
90provides an index ordered list of layer names.
91
92.. code-block:: python
93
94    for layername in fiona.listlayers('tests/data'):
95        with fiona.open('tests/data', layer=layername) as src:
96            print(layername, len(src))
97
98    # Output:
99    # (u'coutwildrnp', 67)
100
101Layer can also be specified by index. In this case, ``layer=0`` and
102``layer='test_uk'`` specify the same layer in the data file or directory.
103
104.. code-block:: python
105
106    for i, layername in enumerate(fiona.listlayers('tests/data')):
107        with fiona.open('tests/data', layer=i) as src:
108            print(i, layername, len(src))
109
110    # Output:
111    # (0, u'coutwildrnp', 67)
112
113Writing Multilayer data
114-----------------------
115
116Multilayer data can be written as well. Layers must be specified by name when
117writing.
118
119.. code-block:: python
120
121    with open('tests/data/cowildrnp.shp') as src:
122        meta = src.meta
123        f = next(src)
124
125    with fiona.open('/tmp/foo', 'w', layer='bar', **meta) as dst:
126        dst.write(f)
127
128    print(fiona.listlayers('/tmp/foo'))
129
130    with fiona.open('/tmp/foo', layer='bar') as src:
131        print(len(src))
132        f = next(src)
133        print(f['geometry']['type'])
134        print(f['properties'])
135
136        # Output:
137        # [u'bar']
138        # 1
139        # Polygon
140        # OrderedDict([(u'PERIMETER', 1.22107), (u'FEATURE2', None), (u'NAME', u'Mount Naomi Wilderness'), (u'FEATURE1', u'Wilderness'), (u'URL', u'http://www.wilderness.net/index.cfm?fuse=NWPS&sec=wildView&wname=Mount%20Naomi'), (u'AGBUR', u'FS'), (u'AREA', 0.0179264), (u'STATE_FIPS', u'49'), (u'WILDRNP020', 332), (u'STATE', u'UT')])
141
142A view of the /tmp/foo directory will confirm the creation of the new files.
143
144.. code-block:: console
145
146    $ ls /tmp/foo
147    bar.cpg bar.dbf bar.prj bar.shp bar.shx
148
149Collections from archives and virtual file systems
150--------------------------------------------------
151
152Zip and Tar archives can be treated as virtual filesystems and Collections can
153be made from paths and layers within them. In other words, Fiona lets you read
154and write zipped Shapefiles.
155
156.. code-block:: python
157
158    for i, layername in enumerate(fiona.listlayers('zip://tests/data/coutwildrnp.zip')):
159        with fiona.open('zip://tests/data/coutwildrnp.zip', layer=i) as src:
160            print(i, layername, len(src))
161
162    # Output:
163    # (0, u'coutwildrnp', 67)
164
165Fiona can also read from more exotic file systems. For instance, a
166zipped shape file in S3 can be accessed like so:
167
168.. code-block:: python
169
170   with fiona.open('zip+s3://mapbox/rasterio/coutwildrnp.zip') as src:
171       print(len(src))
172
173   # Output:
174   # 67
175
176
177Fiona CLI
178=========
179
180Fiona's command line interface, named "fio", is documented at `docs/cli.rst
181<https://github.com/Toblerity/Fiona/blob/master/docs/cli.rst>`__. Its ``fio
182info`` pretty prints information about a data file.
183
184.. code-block:: console
185
186    $ fio info --indent 2 tests/data/coutwildrnp.shp
187    {
188      "count": 67,
189      "crs": "EPSG:4326",
190      "driver": "ESRI Shapefile",
191      "bounds": [
192        -113.56424713134766,
193        37.0689811706543,
194        -104.97087097167969,
195        41.99627685546875
196      ],
197      "schema": {
198        "geometry": "Polygon",
199        "properties": {
200          "PERIMETER": "float:24.15",
201          "FEATURE2": "str:80",
202          "NAME": "str:80",
203          "FEATURE1": "str:80",
204          "URL": "str:101",
205          "AGBUR": "str:80",
206          "AREA": "float:24.15",
207          "STATE_FIPS": "str:80",
208          "WILDRNP020": "int:10",
209          "STATE": "str:80"
210        }
211      }
212    }
213
214Installation
215============
216
217Fiona requires Python 2.7 or 3.4+ and GDAL/OGR 1.8+. To build from
218a source distribution you will need a C compiler and GDAL and Python
219development headers and libraries (libgdal1-dev for Debian/Ubuntu, gdal-dev for
220CentOS/Fedora).
221
222To build from a repository copy, you will also need Cython to build C sources
223from the project's .pyx files. See the project's requirements-dev.txt file for
224guidance.
225
226The `Kyngchaos GDAL frameworks
227<http://www.kyngchaos.com/software/frameworks#gdal_complete>`__ will satisfy
228the GDAL/OGR dependency for OS X, as will Homebrew's GDAL Formula (``brew install
229gdal``).
230
231Python Requirements
232-------------------
233
234Fiona depends on the modules ``enum34``, ``six``, ``cligj``,  ``munch``, ``argparse``, and
235``ordereddict`` (the two latter modules are standard in Python 2.7+). Pip will
236fetch these requirements for you, but users installing Fiona from a Windows
237installer must get them separately.
238
239Unix-like systems
240-----------------
241
242Assuming you're using a virtualenv (if not, skip to the 4th command) and
243GDAL/OGR libraries, headers, and `gdal-config`_ program are installed to well
244known locations on your system via your system's package manager (``brew
245install gdal`` using Homebrew on OS X), installation is this simple.
246
247.. code-block:: console
248
249  $ mkdir fiona_env
250  $ virtualenv fiona_env
251  $ source fiona_env/bin/activate
252  (fiona_env)$ pip install fiona
253
254If gdal-config is not available or if GDAL/OGR headers and libs aren't
255installed to a well known location, you must set include dirs, library dirs,
256and libraries options via the setup.cfg file or setup command line as shown
257below (using ``git``). You must also specify the version of the GDAL API on the
258command line using the ``--gdalversion`` argument (see example below) or with
259the ``GDAL_VERSION`` environment variable (e.g. ``export GDAL_VERSION=2.1``).
260
261.. code-block:: console
262
263  (fiona_env)$ git clone git://github.com/Toblerity/Fiona.git
264  (fiona_env)$ cd Fiona
265  (fiona_env)$ python setup.py build_ext -I/path/to/gdal/include -L/path/to/gdal/lib -lgdal install --gdalversion 2.1
266
267Or specify that build options and GDAL API version should be provided by a
268particular gdal-config program.
269
270.. code-block:: console
271
272  (fiona_env)$ GDAL_CONFIG=/path/to/gdal-config pip install fiona
273
274Windows
275-------
276
277Binary installers are available at
278https://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona and coming eventually to PyPI.
279
280You can download a binary distribution of GDAL from `here
281<http://www.gisinternals.com/release.php>`_.  You will also need to download
282the compiled libraries and headers (include files).
283
284When building from source on Windows, it is important to know that setup.py
285cannot rely on gdal-config, which is only present on UNIX systems, to discover
286the locations of header files and libraries that Fiona needs to compile its
287C extensions. On Windows, these paths need to be provided by the user.
288You will need to find the include files and the library files for gdal and
289use setup.py as follows. You must also specify the version of the GDAL API on the
290command line using the ``--gdalversion`` argument (see example below) or with
291the ``GDAL_VERSION`` environment variable (e.g. ``set GDAL_VERSION=2.1``).
292
293.. code-block:: console
294
295    $ python setup.py build_ext -I<path to gdal include files> -lgdal_i -L<path to gdal library> install --gdalversion 2.1
296
297Note: The following environment variables needs to be set so that Fiona works correctly:
298
299* The directory containing the GDAL DLL (``gdal304.dll`` or similar) needs to be in your
300  Windows ``PATH`` (e.g. ``C:\gdal\bin``).
301* The gdal-data directory needs to be in your Windows ``PATH`` or the environment variable
302  ``GDAL_DATA`` must be set (e.g. ``C:\gdal\bin\gdal-data``).
303* The environment variable ``PROJ_LIB`` must be set to the proj library directory (e.g.
304  ``C:\gdal\bin\proj6\share``)
305
306The  `Appveyor CI build <https://ci.appveyor.com/project/sgillies/fiona/history/>`_
307uses the GISInternals GDAL binaries to build Fiona. This produces a binary wheel
308for successful builds, which includes GDAL and other dependencies, for users
309wanting to try an unstable development version.
310The `Appveyor configuration file <appveyor.yml>`_ may be a useful example for
311users building from source on Windows.
312
313Development and testing
314=======================
315
316Building from the source requires Cython. Tests require `pytest <http://pytest.org>`_. If the GDAL/OGR
317libraries, headers, and `gdal-config`_ program are installed to well known
318locations on your system (via your system's package manager), you can do this::
319
320  (fiona_env)$ git clone git://github.com/Toblerity/Fiona.git
321  (fiona_env)$ cd Fiona
322  (fiona_env)$ pip install cython
323  (fiona_env)$ pip install -e .[test]
324  (fiona_env)$ py.test
325
326Or you can use the ``pep-518-install`` script::
327
328  (fiona_env)$ git clone git://github.com/Toblerity/Fiona.git
329  (fiona_env)$ cd Fiona
330  (fiona_env)$ ./pep-518-install
331
332If you have a non-standard environment, you'll need to specify the include and
333lib dirs and GDAL library on the command line::
334
335  (fiona_env)$ python setup.py build_ext -I/path/to/gdal/include -L/path/to/gdal/lib -lgdal --gdalversion 2 develop
336  (fiona_env)$ py.test
337
338.. _GDAL: http://www.gdal.org
339.. _pyproj: http://pypi.python.org/pypi/pyproj/
340.. _Rtree: http://pypi.python.org/pypi/Rtree/
341.. _Shapely: http://pypi.python.org/pypi/Shapely/
342.. _gdal-config: http://www.gdal.org/gdal-config.html
343