Skip to content
Permalink
Browse files

Automatically load and convert ESRI .shp.xml metadata when loading

a shapefile with this sidecar file present
  • Loading branch information
nyalldawson committed May 4, 2021
1 parent 71bfc7d commit e66e305a40e948806ba40dd933a5df35759e3189
@@ -1502,6 +1502,34 @@ void QgsOgrProvider::loadMetadata()
}
}
}
else if ( ( mGDALDriverName == QLatin1String( "ESRI Shapefile" ) ) )
{
// look for .shp.xml sidecar file
const QString sidecarPath = mFilePath + ".xml";
if ( QFileInfo::exists( sidecarPath ) )
{
QFile file( sidecarPath );
if ( file.open( QFile::ReadOnly ) )
{
QDomDocument doc;
int line, column;
QString errorMessage;
if ( doc.setContent( &file, &errorMessage, &line, &column ) )
{
mLayerMetadata = QgsMetadataUtils::convertFromEsri( doc );
}
else
{
QgsDebugMsg( QStringLiteral( "Error reading %1: %2 at line %3 column %4" ).arg( sidecarPath, errorMessage ).arg( line ).arg( column ) );
}
file.close();
}
}
else
{
QgsDebugMsg( QStringLiteral( "Error reading %1 - could not open file for read" ).arg( sidecarPath ) );
}
}
}
mLayerMetadata.setType( QStringLiteral( "dataset" ) );
}
@@ -1067,6 +1067,18 @@ def testGdbLayerMetadata(self):
self.assertEqual(vl.metadata().extent().spatialExtents()[0].bounds.yMinimum(), 3)
self.assertEqual(vl.metadata().extent().spatialExtents()[0].bounds.yMaximum(), 4)

def testShpLayerMetadata(self):
"""
Test that we translate .shp.xml metadata to QGIS layer metadata on loading a shp file (if present)
"""
datasource = os.path.join(unitTestDataPath(), 'france_parts.shp')
vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(vl.metadata().identifier(), 'QLD_STRUCTURAL_FRAMEWORK_OUTLINE')
self.assertEqual(vl.metadata().title(), 'QLD_STRUCTURAL_FRAMEWORK_OUTLINE')
self.assertEqual(vl.metadata().type(), 'dataset')
self.assertEqual(vl.metadata().language(), 'EN')

def testOpenOptions(self):

filename = os.path.join(tempfile.gettempdir(), "testOpenOptions.gpkg")

0 comments on commit e66e305

Please sign in to comment.