[GIS] Convert KML to GeoJSON with ogr2ogr and remove KML special attributes

gdalogrogr2ogrpythonqgis

I'm trying to convert KML to GeoJSON (or some other formats) using ogr2ogr (look at this question). It's all working fine, except of one problem:

I'm always receiving these metadata attributes with any operation (using org2ogr or ogrinfo or Python script)

Name, description, timestamp, begin, end, altitudeMode, tessellate,
extrude, visibility, drawnOrder,icon

But my Schema doesn't have these fields:

<Schema name="distritos_mad" id="distritos_mad">
    <SimpleField name="ID" type="float"></SimpleField>
    <SimpleField name="CD_GEOCODS" type="string"></SimpleField>
    <SimpleField name="NM_SUBDIST" type="string"></SimpleField>
    <SimpleField name="Nomes" type="string"></SimpleField>
    <SimpleField name="AREA (HA)" type="float"></SimpleField>
    <SimpleField name="Pop (2010)" type="float"></SimpleField>
    <SimpleField name="Ddem p/km" type="float"></SimpleField>
</Schema>

Looking at LIBKML Drive documentation I realize that this fields are "special fields" and for an unknown reason this fields are included on feature properties.

How can I prevent OGR from writing these feature attributes?

I'm runing this command:

ogr2ogr -f "GeoJSON" C:\out.json C:\in.kml -t_srs EPSG:4326 -dim 2 --config OGR_FORCE_ASCII NO

GDAL 1.11.3, released 2015/09/16

Best Answer

You should be able to just use the sql argument in ogr2ogr. For instance, with the following polygon Shapefile with two features and two attributes:

$>ogrinfo -so -al polygon.shp
INFO: Open of `polygon.shp'
  using driver `ESRI Shapefile' successful.

 Layer name: polygon
 Geometry: Polygon
 Feature Count: 2
 Extent: (-1.206294, -0.828671) - (0.727273, 0.566434)
 Layer SRS WKT:
 GEOGCS["GCS_WGS_1984",
    DATUM["WGS_1984",
       SPHEROID["WGS_84",6378137,298.257223563]],
   PRIMEM["Greenwich",0],
   UNIT["Degree",0.017453292519943295]] 
id: Integer (10.0)
area: Real (6.1)

Assuming the above Shapefile has been converted to KML, you could do the following:

ogr2ogr -f GeoJSON polygon.json polygon.kml -sql 'SELECT id, area FROM polygon'

Which will give the following GeoJSON output:

{"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "id": 1, "area": 0.300000 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -0.870629370629371, 0.566433566433566 ], [ -0.534965034965035, 0.43006993006993 ], [ -0.534965034965035, 0.43006993006993 ], [ -0.517482517482518, 0.195804195804196 ], [ -0.765734265734266, -0.129370629370629 ], [ -1.206293706293706, 0.048951048951049 ], [ -0.870629370629371, 0.566433566433566 ] ] ] } },
{ "type": "Feature", "properties": { "id": 2, "area": 0.500000 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.108391608391608, -0.136363636363636 ], [ 0.199300699300699, 0.454545454545455 ], [ 0.727272727272727, -0.185314685314685 ], [ 0.433566433566433, -0.828671328671329 ], [ -0.153846153846154, -0.342657342657343 ], [ 0.108391608391608, -0.136363636363636 ] ] ] } }
]}

If you don't pass the -sql option you will get all of the additional fields from the KML file.