Skip to content

KML: the KML driver doesn't read attribute columns (simpledata elements) #14698

@theroggy

Description

@theroggy

What is the bug?

When writing a .kml with the "KML" driver, attribute colums are correctly written to "simpledata" elements in the .kml output file.

When reading the file again though, these columns are ignored.

Ideally they would be read correctly, but at least it would be useful if this behaviour was documented in the driver doc.

Steps to reproduce the issue

import tempfile
from pathlib import Path

import geopandas as gpd
from osgeo import gdal, ogr

gdal.UseExceptions()

tmp_dir = Path(tempfile.gettempdir())

# Create a KML via the gdal python API
kml_gdal_path = tmp_dir / "polygon-parcel-gdal-kml.kml"

ds = ogr.GetDriverByName("KML").CreateDataSource(kml_gdal_path)
lyr = ds.CreateLayer("src_lyr", geom_type=ogr.wkbNone)

field = ogr.FieldDefn("text_1", ogr.OFTString)
lyr.CreateField(field)
field = ogr.FieldDefn("text_2", ogr.OFTString)
lyr.CreateField(field)
field = ogr.FieldDefn("text_3", ogr.OFTString)
lyr.CreateField(field)

f = ogr.Feature(lyr.GetLayerDefn())
f.SetField("text_1", "row_1")
f.SetField("text_2", "row_1")
f.SetField("text_3", "row_1")
lyr.CreateFeature(f)
ds = None

# Print the KML file content
print(f"KML file content:\n{kml_gdal_path.read_text()}")

# Read the KML via the gdal python API
kml_gdal_ds = ogr.GetDriverByName("KML").Open(str(kml_gdal_path))
kml_gdal_lyr = kml_gdal_ds.GetLayer()

# List the columns in the KML layer
print("gdal columns:", [field.name for field in kml_gdal_lyr.schema])

Output:

KML file content:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="src_lyr" id="src_lyr">
        <SimpleField name="text_1" type="string"></SimpleField>
        <SimpleField name="text_2" type="string"></SimpleField>
        <SimpleField name="text_3" type="string"></SimpleField>
</Schema>
<Folder><name>src_lyr</name>
  <Placemark id="src_lyr.1">
        <ExtendedData><SchemaData schemaUrl="#src_lyr">
                <SimpleData name="text_1">row_1</SimpleData>
                <SimpleData name="text_2">row_1</SimpleData>
                <SimpleData name="text_3">row_1</SimpleData>
        </SchemaData></ExtendedData>
  </Placemark>
</Folder>
</Document></kml>

gdal columns: ['Name', 'Description']

Versions and provenance

Tested on windows, with gdal 3.12.2 installed via conda

Additional context

Noticed while fixing a bug in pyogrio geopandas/pyogrio#650

Metadata

Metadata

Assignees

Labels

not for AI loversSee https://gdal.org/en/stable/community/ai_tool_policy.html

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions