Merge pull request #61731 from elpaso/bugfix-gh61181-vector-extent-not-stored-2

[bug] lazy vector extent not stored in the project
This commit is contained in:
Alexander Bruy 2025-05-07 17:07:33 +01:00 committed by GitHub
commit e64c04582e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 2 deletions

View File

@ -755,8 +755,15 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() ) if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) ); layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
else if ( !mExtent2D.isNull() ) else
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent2D, document ) ); {
// Extent might be null because lazily set
const QgsRectangle extent2D { mExtent2D.isNull() ? extent() : mExtent2D };
if ( !extent2D.isNull() )
{
layerElement.appendChild( QgsXmlUtils::writeRectangle( extent2D, document ) );
}
}
if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() ) if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
{ {

View File

@ -17,6 +17,7 @@ from io import BytesIO
from shutil import copyfile from shutil import copyfile
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from zipfile import ZipFile from zipfile import ZipFile
from lxml import etree as et
from osgeo import ogr from osgeo import ogr
from qgis.PyQt import sip from qgis.PyQt import sip
@ -2119,6 +2120,43 @@ class TestQgsProject(QgisTestCase):
del project del project
def testVectorExtentIsStored(self):
"""
Test that vector layer extent is stored in the project
Test for GH #61181
"""
tmpDir = QTemporaryDir()
tmpFile = f"{tmpDir.path()}/project.qgs"
for ext in ["shp", "shx", "dbf"]:
copyfile(
os.path.join(TEST_DATA_DIR, "points." + ext),
os.path.join(tmpDir.path(), "points." + ext),
)
project = QgsProject()
l0 = QgsVectorLayer(os.path.join(tmpDir.path(), "points.shp"), "points", "ogr")
#l0.extent()
self.assertTrue(l0.isValid())
self.assertTrue(project.addMapLayers([l0]))
self.assertTrue(project.write(tmpFile))
del project
# Read the project.qgs as XML using etree and check that the maplayer extent is in the XML file
with open(tmpFile, "r") as f:
xml = f.read()
root = et.XML(xml)
layerXML = root.findall('.//projectlayers/maplayer')[0]
extentXML = layerXML.findall('.//extent')[0]
self.assertNotEqual(len(extentXML.getchildren()), 0)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()