geopandas / pyogrio

Vectorized vector I/O using OGR
https://pyogrio.readthedocs.io
MIT License
257 stars 21 forks source link

Appending/over-writing KML file results in introduction of Z-dimension #424

Open nicholas-ys-tan opened 2 weeks ago

nicholas-ys-tan commented 2 weeks ago

This issue was encountered in writing the tests for PR #421.

When over-writing a KML file, a Z-dimension was undesirably added to the co-ordinates. This arose when testing appending to the KML file as it involves writing into an existing file. It appears that when writing to an existing file, the driver uses LIBKML even if driver='KML' is specified resulting in the addition of the Z-dimension The addition of a Z-dimension is an artifact of the LIBKML driver.

Investigation of the .kml file does suggest any appending or over-writing an existing KML file produces a file format consistent with the LIBKML driver.

points = [Point(10, 20), Point(30, 40), Point(50, 60)]
gdf = gpd.GeoDataFrame(geometry=points, crs="EPSG:4326")
output_path = r'/home/nicholas/dev/data/ogr_test/temporary_kml_file2.kml'
write_dataframe(
    gdf, output_path, layer="tmp_layer", driver="KML", use_arrow=True
)

write_dataframe(
    gdf, output_path, layer="tmp_layer", driver="KML", use_arrow=True
)

print(gdf_read.geometry)
0    POINT Z (10 20 0)
1    POINT Z (30 40 0)
2    POINT Z (50 60 0)
Name: geometry, dtype: geometry

It is possible that the KML driver does not allow the opening of a file in update mode (called via ogr_open executing GDALOpenEx with the GDAL_OF_UPDATE flag) which then silently falls back on the LIBKML driver.

Additionally, if the LIBKML driver is not available - as it is not in the ubuntu-small CIs, we are unable to append to the file and raises a misleading error message: "It might help to specify the correct driver explicitly by prefixing the file path with ':', e.g. 'CSV:path'" despite explicitly specifying the KML driver.