3

I'd like to know if it's possible to read/write GML files (or even KML files) using Fiona.

Fiona documents don't specify what drivers we can use. I read some answers about the drivers that are avalaible but I still haven't figured out the right answer.

These two different sentences print a different number of drivers. The first one doesn't include GML o KML (in fact there are very few formats supported).

print(fiona.supported_drivers)

vs

print('\n'.join(sorted(fiona.drivers().drivers())))

I know how to do it using GDAL/OGR but I want to do the same using Fiona if it's possible.

Daniel
  • 87
  • 1
  • 7

2 Answers2

1

In order to read both KML and GML file formats the OGR binaries distributed with the Fiona implementation you're using need to be compiled against libexpat or Xerces (XML parsers). If these libraries exist in the same installation where the ogr.dll (Windows), ogr.so (Linux) used by Fiona is, then read support is most probably available. If no, then only write support will be available.

LuisTavares
  • 2,146
  • 2
  • 12
  • 18
  • Does it mean that we should use GDAL/OGR to work with most of the vectorial formats and only use Fiona to work with the very few formats avalaible according to (fiona.supported_drivers? This way Fiona is very limited, isn't it? is there any way I can make Fiona work with other formats like GML o KML? What profesionals usually do? Thanks – Daniel Nov 15 '18 at 15:06
  • I guess Fiona's main purpose is to provide data to Shapely. As Shapely is not able to read many formats, Fiona doesn't need to write in many formats either. If we want to transform data between different vectorial formats it's better to use OGC. However, we can use OGR to provide data to Shapely too so most of the time it's worth it to work with OGC instead of working with Fiona. I would appreciate some opinions about that. Thanks – Daniel Nov 15 '18 at 17:04
  • ...and Shapely is a wrapper around GEOS C++. Both projects exist because original SWIG generated OGR python bindings are not very "pythonic" (there's not even a documented API, the only documentations available is OGR C++ API) – LuisTavares Nov 15 '18 at 22:11
  • Anyway, it doesn't hurt you trying to read/write a GML/KML and see what happens. Just don't go thinking that because drivers are not listed, functionality isn't there... as you said different lines are printing different drivers. Besides, drivers() lists only pertains to Read support. – LuisTavares Nov 15 '18 at 23:36
  • Oh I didn't know drivers() lists only pertains to Read support. Thanks again. – Daniel Nov 16 '18 at 13:06
0

I've just found out that the newest version of Fiona added support to read and write GML files.

Daniel
  • 87
  • 1
  • 7
  • 1
    Can you please add some explanation how to read and write them in newest version? – sanyassh May 27 '19 at 14:30
  • 1
    You need to install Fiona 1.8.4 (Fiona 1.7.10 didn't offer any driver to read or write GML files). Then, you read a GML file the same way you would read any other format (shapefile, geojson etc.). Fiona always work the same way regardless of the file format it's reading, because it always creates a Collections object (geojson object, like a Python dictionary). There is no difference in the way Fiona read files between Fiona 1.7.10 and Fiona 1.8.4, the difference is the number of formats that Fiona can read in each version. – Daniel May 28 '19 at 17:57
  • Just take in consideration that a GML file is a multilayer dataset, so if your GML stores more than a layer, you'll first have to choose which one to work with. Look at Fiona's documentation because it tells you how to do it very easily. If it only has a layer, it would be like reading any single layer file (geojson, shapefile etc.). You can know the name of the layers but writing fiona.listlayers(path). – Daniel May 28 '19 at 17:57
  • 1
    I have an example on how to read a KML file with Fiona [here](https://stackoverflow.com/a/52851541/7657658) – MCMZL Nov 08 '19 at 09:05