8

I am converting GeoJSON files to other formats (including KML) using ogr2ogr.

As you may know, there is no field in a GeoJSON file to specify the name of the layer, causing every conversion to have the name OGRGeoJSON. For example the following GeoJSON file:

{ "type": "FeatureCollection",
 "features": [
 { "type": "Feature",
 "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
 "properties": {"prop0": "value0"}
 }
 ]
 }

is converted as follows in KML:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>OGRGeoJSON</name>
<Schema name="OGRGeoJSON" id="OGRGeoJSON">
 <SimpleField name="Name" type="string"></SimpleField>
 <SimpleField name="Description" type="string"></SimpleField>
 <SimpleField name="prop0" type="string"></SimpleField>
</Schema>
 <Placemark>
 <ExtendedData><SchemaData schemaUrl="#OGRGeoJSON">
 <SimpleData name="prop0">value0</SimpleData>
 </SchemaData></ExtendedData>
 <Point><coordinates>102.0,0.5</coordinates></Point>
 </Placemark>
</Folder></Document></kml>

I've read this blog post from 2007 explaining why this occurs and I know that GeoJSON does not have the concept of layer name in its specifications.

I would like to know if there is a simple way to rename the layer on the fly using OGR, or if I should generate the output with ogr2ogr and programmatically change the output layer name, depending on the output file format with another program since, as far as I know, the OGR API does not have a function to rename the layer.

asked Sep 19, 2012 at 3:07

1 Answer 1

10

In the current ogr2ogr documentation it outlines the -nln property, which allows for specifying an alternate layer name. I don't know how new this is so you may need to download a new version, but it does work.

ogr2ogr -f KML some_output.kml example.geojson -nln SOMENAME

Using the above command on your example geojson produced the following results:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>SOMENAME</name>
<Schema name="SOMENAME" id="SOMENAME">
 <SimpleField name="Name" type="string"></SimpleField>
 <SimpleField name="Description" type="string"></SimpleField>
 <SimpleField name="prop0" type="string"></SimpleField>
</Schema>
 <Placemark>
 <ExtendedData><SchemaData schemaUrl="#SOMENAME">
 <SimpleData name="prop0">value0</SimpleData>
 </SchemaData></ExtendedData>
 <Point><coordinates>102.0,0.5</coordinates></Point>
 </Placemark>
</Folder></Document></kml>

Good luck!

answered Sep 19, 2012 at 4:19
2
  • Worked perfectly, at least on the command line. I'm actually calling OGR from Python. Thanks a lot! Commented Sep 19, 2012 at 8:53
  • @JulienBourdon Glad to hear it. In case you didn't know about it you can use os.system to call command line utilities in Pythoon, or the subprocess module for more complex data interaction. Commented Sep 19, 2012 at 15:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.