From b6ed3bbce607cd19dd8d8d45e73fb94c1ea316d9 Mon Sep 17 00:00:00 2001 From: Riccardo Beltrami Date: Wed, 24 Jan 2024 15:59:37 +0100 Subject: [PATCH] speed-up loading of embedded layers --- .../lizmap/lib/Project/QgisProject.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lizmap/modules/lizmap/lib/Project/QgisProject.php b/lizmap/modules/lizmap/lib/Project/QgisProject.php index e453be317d..71c9048b01 100644 --- a/lizmap/modules/lizmap/lib/Project/QgisProject.php +++ b/lizmap/modules/lizmap/lib/Project/QgisProject.php @@ -1486,18 +1486,20 @@ protected function readLayers($xml) if (!$xmlLayers) { return $layers; } + // Associative array that stores the embedded projects path as key and the list of embedded layers attributes as value. + // The the embedded layers definition are retreived outside the main foreach loop to avoid loading the same embedded qgis project multiple times + // for each embedded layer + $embeddedProjects = array(); foreach ($xmlLayers as $xmlLayer) { $attributes = $xmlLayer->attributes(); if (isset($attributes['embedded']) && (string) $attributes->embedded == '1') { $xmlFile = realpath(dirname($this->path).DIRECTORY_SEPARATOR.(string) $attributes->project); - $qgsProj = new QgisProject($xmlFile, $this->services, $this->appContext); - $layer = $qgsProj->getLayerDefinition((string) $attributes->id); - $layer['qgsmtime'] = filemtime($xmlFile); - $layer['file'] = $xmlFile; - $layer['embedded'] = 1; - $layer['projectPath'] = (string) $attributes->project; - $layers[] = $layer; + if (!array_key_exists($xmlFile, $embeddedProjects)) { + $embeddedProjects[$xmlFile] = array(); + } + // populate array of embedded layers + $embeddedProjects[$xmlFile][] = $attributes; } else { $layer = array( 'type' => (string) $attributes->type, @@ -1659,6 +1661,20 @@ protected function readLayers($xml) $layers[] = $layer; } } + // loop through the embedded projects if any, to get the embedded layers definition + foreach ($embeddedProjects as $projectPath => $layersAttributes) { + if (is_array($layersAttributes)) { + $embeddedProject = new QgisProject($projectPath, $this->services, $this->appContext); + foreach ($layersAttributes as $attributes) { + $layer = $embeddedProject->getLayerDefinition((string) $attributes->id); + $layer['qgsmtime'] = filemtime($projectPath); + $layer['file'] = $projectPath; + $layer['embedded'] = 1; + $layer['projectPath'] = (string) $attributes->project; + $layers[] = $layer; + } + } + } return $layers; }