Skip to content

Commit

Permalink
LIBKML: make edition of existing file work (fixes https://issues.qgis…
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Sep 21, 2018
1 parent 1c9185d commit b5d04c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 38 additions & 0 deletions autotest/ogr/ogr_libkml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,43 @@ def ogr_libkml_read_kml_with_space_content_in_coordinates():
return 'success'

###############################################################################


def ogr_libkml_update_existing_kml():

if not ogrtest.have_read_libkml:
return 'skip'

filename = '/vsimem/ogr_libkml_update_existing_kml.kml'
gdal.FileFromMemBuffer(filename, """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>An instance of A</name>
</Placemark>
<Placemark>
<name>An instance of B</name>
</Placemark>
</Document>
</kml>
""")
ds = ogr.Open(filename, update=1)
lyr = ds.GetLayer(0)
fc_before = lyr.GetFeatureCount()
f = ogr.Feature(lyr.GetLayerDefn())
lyr.CreateFeature(f)
ds = None

ds = ogr.Open(filename)
lyr = ds.GetLayer(0)
fc_after = lyr.GetFeatureCount()
if fc_after != fc_before + 1:
return 'fail'

gdal.Unlink(filename)
return 'success'

###############################################################################
# Cleanup


Expand Down Expand Up @@ -2313,6 +2350,7 @@ def ogr_libkml_cleanup():
ogr_libkml_read_placemark_in_root_and_subfolder,
ogr_libkml_read_tab_separated_coord_triplet,
ogr_libkml_read_kml_with_space_content_in_coordinates,
ogr_libkml_update_existing_kml,
ogr_libkml_cleanup]

if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions gdal/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,9 @@ int OGRLIBKMLDataSource::OpenKml( const char *pszFilename, int bUpdateIn )
/***** parse the kml into the DOM *****/
std::string oKmlErrors;

ElementPtr poKmlRoot = OGRLIBKMLParse( oKmlKml, &oKmlErrors );
m_poKmlDSKml = AsKml(OGRLIBKMLParse( oKmlKml, &oKmlErrors ));

if( !poKmlRoot )
if( !m_poKmlDSKml )
{
CPLError( CE_Failure, CPLE_OpenFailed,
"ERROR parsing kml %s :%s",
Expand All @@ -1059,7 +1059,7 @@ int OGRLIBKMLDataSource::OpenKml( const char *pszFilename, int bUpdateIn )

/***** get the container from root *****/
if( !( m_poKmlDSContainer = GetContainerFromRoot( m_poKmlFactory,
poKmlRoot ) ) )
m_poKmlDSKml ) ) )
{
CPLError( CE_Failure, CPLE_OpenFailed,
"ERROR parsing kml %s :%s %s",
Expand Down Expand Up @@ -1090,7 +1090,7 @@ int OGRLIBKMLDataSource::OpenKml( const char *pszFilename, int bUpdateIn )

AddLayer( layername_default.c_str(),
wkbUnknown,
this, poKmlRoot, m_poKmlDSContainer, pszFilename, FALSE,
this, m_poKmlDSKml, m_poKmlDSContainer, pszFilename, FALSE,
bUpdateIn, 1 );
}

Expand Down

0 comments on commit b5d04c2

Please sign in to comment.