[Gdal-dev] SHP Files as Layers into a KML File

Mateusz Loskot mateusz at loskot.net
Fri May 11 15:32:19 EDT 2007


Philippe de Franclieu wrote:
> Hello,
>  
> Objective : put several SHP files as different layers - attribute data being
> different - into a KML file.
> Pb : I cannot -append -update the KML file : "Unable to open existing output
> datasource 'out1.kml'".
>  
> shp2kml.bat file is as follows :
> ogr2ogr -s_srs EPSG:27572 -t_srs EPSG:4326 -f "KML" out1.kml in_1.shp -dsco
> NameField=LABEL -nln NewLayerName_1
> ogr2ogr -update -append -s_srs EPSG:27572 -t_srs EPSG:4326 -f "KML" out1.kml
> in_2.shp -dsco NameField=ID -nln NewLayerName_2
> ogr2ogr -update -append -s_srs EPSG:27572 -t_srs EPSG:4326 -f "KML" out1.kml
> in_3.shp -dsco NameField=ID -nln NewLayerName_3
>  
> What is wrong?

Philippe,

As you can read in the KML driver doc
(http://www.gdal.org/ogr/drv_kml.html)

"OGR has limited support for KML writing and no support for reading KML."

This is the reason of the error you're getting.

It may seems strange because you're trying to *write* KML but not to
read, but if you use -update option, this operation involves
datasource open and read steps.

So, it is not possible to generate single KML file in more than one steps.

As workaround, you could merge shapefiles into single shapefile with
ogr2ogr, if all shapes have the same schema, and then build KML from
single shapefile.

> The other option, merge several SHP files into 'out.shp' then convert it to
> 'out2.kml', does not work either.
>  
> My shp2shp2kml.bat file is as follows :
> ogr2ogr -s_srs EPSG:27572 -t_srs EPSG:27572 -f "ESRI Shapefile" out.shp
> in_1.shp -nln NewLayerName_1
> ogr2ogr -update -append -s_srs EPSG:27572 -t_srs EPSG:27572 -f "ESRI
> Shapefile" out.shp in_2.shp -nln NewLayerName_2
> ogr2ogr -update -append -s_srs EPSG:27572 -t_srs EPSG:27572 -f "ESRI
> Shapefile" out.shp in_3.shp -nln NewLayerName_3
> ogr2ogr -s_srs EPSG:27572 -t_srs EPSG:4326 -f "KML" out2.kml out.shp
>  
> ERROR 1: Failed to create file out.shp\NewLayerName_2.shp.
> ERROR 4: Failed to open Shapefile 'out.shp\NewLayerName_2.shp'.
> ERROR 1: Terminating translation prematurely after failed translation of
> layer in_2

OGR tries to resolve output datasource as a directory but not a file.
If you want to get new layer name (shapefile name), use directory
path as output datasource:

ogr2ogr -s_srs EPSG:27572 -t_srs EPSG:27572 -f "ESRI Shapefile" .
in_1.shp -nln NewLayerName_1

Here, in *current* directory (see the dot), you will get
NewLayerName_1.shp file, generated from in_1.shp.

> Also note that the -nln option does not replace the layer name from 'out' to
> 'NewLayerName_%' whith the - f "ESRI Shapefile" option while it does with
> the -f "KML".

Yes, because layer in ESRI Shapefile driver is...name of shapefile,
but if you use shapefile as a datasource, its filename works has two
meanings: datasoruce and layer name. It's not supported to create, edit
and rename the same shapefile in single step.

If you run:

ogrinfo in_1.shp

you will get list of layers including *one* layer named in_1.

If you run:

ogrinfo /path/to/directory/but/not/to/shapefile

you will get list of N layers - names of shapefiles in given directory.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list