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

Mateusz Loskot mateusz at loskot.net
Tue May 15 08:59:29 EDT 2007


Philippe,

I think it should be easy to write Python script that will get a bunch
of KML files, each generated from a shapefile, and to some merging.

Unfortunately, I don't have any easier solution to suggest, for now.

Mat

Philippe de Franclieu wrote:
> Hello Mateusz,
> 
> Thank you for your prompt reply.
> " if all shapes have the same schema". That's the point.
> The structures of the attribute data being different, merging shapes is not
> the solution. I should have imagine that earlier.
> No easy solution to superimpose heterogeneous shapes and data site by site
> I'm afraid. 
> Best regards
> Philippe
>  
> 
>> -----Message d'origine-----
>> De : Mateusz Loskot [mailto:mateusz at loskot.net] 
>> Envoyé : vendredi 11 mai 2007 21:32
>> À : Philippe de Franclieu
>> Cc : gdal-dev at lists.maptools.org
>> Objet : Re: [Gdal-dev] SHP Files as Layers into a KML File
>>
>> 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
>>  
>>
> 
> 


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



More information about the Gdal-dev mailing list