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.
1 Answer 1
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!
-
Worked perfectly, at least on the command line. I'm actually calling OGR from Python. Thanks a lot!Julien Bourdon– Julien Bourdon2012年09月19日 08:53:19 +00:00Commented 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.om_henners– om_henners2012年09月19日 15:21:22 +00:00Commented Sep 19, 2012 at 15:21