[gdal-dev] Converting multilayer KML to single layer output

Even Rouault even.rouault at spatialys.com
Wed Nov 30 13:39:35 PST 2016


On mercredi 30 novembre 2016 05:44:54 CET Rutger wrote:
> Hey,
> 
> I am trying to convert a kml-file containing multiple layers to a new output
> (GeoJSON for example), which only supports a single layer. All geometry
> types in the kml are identical, all polygon.
> 
> The only way i can make it work (by using gdal.VectorTranslate) is to first
> write a single layer to a file in 'overwrite' mode. And then run almost the
> identical command for the remaining layers in 'append' mode. Writing all
> layers in append mode sort of works, it creates the file and writes all
> layers, but it doesn't close the file properly. In the case of GeoJSON, it
> doesnt write ']}' at the end of the file.

I can't reproduce your issue with the file not being terminated with ]}

Hum maybe this is due to trying to append several layers with layers[1:] syntax. You should 
probably 

> 
> What i currently do is:
> /
> res = gdal.VectorTranslate(outfile, infile, format='GeoJSON',
> accessMode='overwrite', layers=layers[0], layerName='outlayer')
> res = None

Note that currently layers must be provided with an array, and not without a string 
(otherwise it iterates over each character of the string...) This is a defect I'm going to fix.

Note also that the 'overwrite' mode is without effect here (and will actually not do what you 
want). overwrite is for multi-layer datasets where you want to overwrite an existing layer. In 
the GeoJSON driver, this is not implemented. You should just remove it and in that case the 
whole file will be overwritten.


> 
> res = gdal.VectorTranslate(outfile, infile, format='GeoJSON',
> accessMode='append', layers=layers[1:], layerName='outlayer')
> res = None/
> 
> Where 'layers' is a Python list populated by iterating over the layers and
> calling 'lyr.GetName()'.
> 
> I dont like needing two commands to do this, and also iterating over all
> layers manually, just to get the names, seems a bit redundant. So i'm
> wondering if anyone knows ways to improve upon this.

Actually I've just tested the following successfully (with trunk)

from osgeo import gdal

gdal.VectorTranslate('out.json', 'src', layerName = 'outlayer', format = 'GeoJSON', 
accessMode='append', layers = ['poly', 'poly2', 'poly3'])


Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20161130/1d24ba63/attachment-0001.html>


More information about the gdal-dev mailing list