Field names mixed up writing via "KML" driver
See original GitHub issueExpected behavior and actual behavior.
Attempting to save a GeoPandas dataframe using:
with fiona.Env():
gdf.to_file(kml_path, driver='KML', NameField='name')
produces a KML file in which the value for <name>
is taken from another field, and likewise <description>
. If the file is written the hard way (using ogr
) it is correct. Perhaps there are some kwargs that need to be passed along to correct this? In the example above NameField='name'
was an unsuccessful attempt.
Correct KML using ogr
:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="walk" id="walk">
<SimpleField name="id" type="string"></SimpleField>
<SimpleField name="date" type="string"></SimpleField>
<SimpleField name="distance" type="string"></SimpleField>
<SimpleField name="speed" type="string"></SimpleField>
<SimpleField name="time" type="string"></SimpleField>
<SimpleField name="activity_name" type="string"></SimpleField>
</Schema>
<Folder><name>walk</name>
<Placemark>
<name>Walked 3.97 km on 22/1/17</name>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#walk">
<SimpleData name="id">1965703340</SimpleData>
<SimpleData name="date">01/22/2017</SimpleData>
<SimpleData name="distance">3.96978493824</SimpleData>
<SimpleData name="speed">1.397811597971831</SimpleData>
<SimpleData name="time">2840</SimpleData>
<SimpleData name="activity_name">Walk</SimpleData>
</SchemaData></ExtendedData>
<LineString><coordinates>151.165393442,-33.7688339688 ... 151.16551338,-33.7687453898</coordinates></LineString>
</Placemark> ...
Incorrect KML using Fiona
(see Placemark name and description):
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="walk" id="walk">
<SimpleField name="id" type="string"></SimpleField>
<SimpleField name="date" type="string"></SimpleField>
<SimpleField name="distance" type="float"></SimpleField>
<SimpleField name="speed" type="float"></SimpleField>
<SimpleField name="time" type="string"></SimpleField>
<SimpleField name="activity_name" type="string"></SimpleField>
</Schema>
<Folder><name>walk</name>
<Placemark>
<name>2840</name>
<description>01/22/2017</description>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#walk">
<SimpleData name="id">3.96978493824</SimpleData>
<SimpleData name="date">Walked 3.97 km on 22/1/17</SimpleData>
<SimpleData name="distance">1.39781159797183</SimpleData>
<SimpleData name="speed">0</SimpleData>
</SchemaData></ExtendedData>
<LineString><coordinates>151.165393442,-33.7688339688 ... 151.16551338,-33.7687453898</coordinates></LineString>
</Placemark> ...
Steps to reproduce the problem.
I don’t think there was anything special about the GeoDataFrame, but can provide a script and sample data if the answer is not trivial.
Operating system
Mac OS X 10.15.4
Fiona and GDAL version and provenance
Fiona 1.8.13.post1 loaded by PyCharm GDAL 2.4.0 loaded by PyCharm
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
KML - Keyhole Markup Language — GDAL documentation
KML Writing The KML Driver will rename some layers, or source KML folder names, into new names it considers valid, for example...
Read more >Adding Custom Data | Keyhole Markup Language
KML offers three ways to add custom data to a KML Feature. Which approach you choose depends on the kind of data you're...
Read more >How to load all fields/ExtendedData (not just 'name' and ...
The underlying problem is the missing library LibKML for windows. My solution is extracting the data directly from the KML via a function....
Read more >ogr2ogr export to KML from SQLite, when using SELECT for ...
db. Now, I'd like to ommit the creation of the ExtendedData by the KML driver and tried so by putting the columns 'name'...
Read more >KML - Keyhole Markup Language - MapServer
First you should make sure that your GDAL/OGR build contains the «KML» driver, by using the ‹–formats› command: >ogrinfo --formats Loaded OGR Format...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So https://github.com/Toblerity/Fiona/blob/master/fiona/ogrext.pyx#L1119-L1122 explains how the fields get mixed up. If I simulate the behaviour of the
dict(zip())
it produces a mapping that agrees with the result:[note that this process remaps
time
toname
and then later on the ogr driver remapsname
toName
]I understand that you are not necessarily keen to support KML, but if this issue also affects some other drivers would it be possible to classify it as a bug?
I imagine the fix would be to check that the
collection.schema['properties'].keys()
andogr_schema['properties'].keys()
were the same length prior to zipping them. If all of the offending ogr drivers add their special fields at the beginning of the schema, this might work as a fix:This assumes that the driver will look after its own
Name
andDescription
fields.I apologise again that I am not set up to be able to test it.
The real issue is, that the KML driver automatically adds fields: https://github.com/OSGeo/gdal/blob/master/gdal/ogr/ogrsf_frmts/kml/ogrkmllayer.cpp#L116-L120
https://github.com/Toblerity/Fiona/blob/master/fiona/ogrext.pyx#L1119-L1122 assumes that only the fields in the schema provided to Fiona exist. The same issue is also present for the DXF, GPX, GPSTrackMacker and DGN driver.
Tested by adding: