#!/usr/bin/env python # coding: utf-8 # In[1]: import ogr import gdal import os gdal.VersionInfo() # In[2]: get_ipython().run_line_magic('cd', 'D:\\\\') # In[3]: def getnames(infile): layers = [] ds = ogr.Open(infile) for i in range(ds.GetLayerCount()): lyr = ds.GetLayer(i) layers.append(lyr.GetName()) ds = None return layers # In[4]: infile = 'doc.kml' layers = getnames(infile) print(len(layers)) # In[5]: outfile1 = 'doc1.geojson' outfile2 = 'doc2.geojson' os.path.exists(outfile1), os.path.exists(outfile2) # ## Doesnt work: # In[6]: res = gdal.VectorTranslate(outfile1, infile, format='GeoJSON', accessMode='append', layers=layers, layerName='outlayer') res # In[7]: res = None # In[8]: print(ogr.Open(outfile1)) # file is also 'in use' in Explorer, res = None doesnt release handle? Console shows: "ERROR 1: GeoJSON parsing error: unexpected end of data (at offset 937626) ERROR 4: Failed to read GeoJSON data" Which is the missing ']}' at the end of the file, all features/geometries are in the file. # ## Works: # In[9]: res = gdal.VectorTranslate(outfile2, infile, format='GeoJSON', accessMode='append', layers=layers, layerName='outlayer') res = None # In[10]: print(ogr.Open(outfile2)) # In[ ]: