Skip to content

Commit

Permalink
QgisProject remove code in setPropertiesAfterRead methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Oct 4, 2024
1 parent 4d72c4e commit 6d91035
Show file tree
Hide file tree
Showing 6 changed files with 1,067 additions and 167 deletions.
189 changes: 42 additions & 147 deletions lizmap/modules/lizmap/lib/Project/QgisProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,35 +376,18 @@ public function setPropertiesAfterRead(ProjectConfig $cfg)
*/
protected function setShortNames(ProjectConfig $cfg)
{
if ($this->path) {
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
if (is_null($layer->shortname)) {
continue;
}
$layerCfg = $cfg->getLayer($layer->layername);
if ($layerCfg) {
$layerCfg->shortname = $layer->shortname;
}
}

if (!$this->path) {
return;
}

$shortNames = $this->xpathQuery('//maplayer/shortname');
if ($shortNames) {
foreach ($shortNames as $sname) {
$sname = (string) $sname;
$xmlLayer = $this->xpathQuery("//maplayer[shortname='{$sname}']");
if (!$xmlLayer) {
continue;
}
$xmlLayer = $xmlLayer[0];
$name = (string) $xmlLayer->layername;
$layerCfg = $cfg->getLayer($name);
if ($layerCfg) {
$layerCfg->shortname = $sname;
}
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
if (is_null($layer->shortname)) {
continue;
}
$layerCfg = $cfg->getLayer($layer->layername);
if ($layerCfg) {
$layerCfg->shortname = $layer->shortname;
}
}
}
Expand All @@ -414,29 +397,18 @@ protected function setShortNames(ProjectConfig $cfg)
*/
protected function setLayerOpacity(ProjectConfig $cfg)
{
if ($this->path) {
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
if ($layer->layerOpacity == 1) {
continue;
}
$layerCfg = $cfg->getLayer($layer->layername);
if ($layerCfg) {
$layerCfg->opacity = $layer->layerOpacity;
}
}

if (!$this->path) {
return;
}
$layerWithOpacities = $this->xpathQuery('//maplayer/layerOpacity[.!=1]/parent::*');
if ($layerWithOpacities) {
foreach ($layerWithOpacities as $layerWithOpacity) {
$name = (string) $layerWithOpacity->layername;
$layerCfg = $cfg->getLayer($name);
if ($layerCfg) {
$opacity = (float) $layerWithOpacity->layerOpacity;
$layerCfg->opacity = $opacity;
}

$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
if ($layer->layerOpacity == 1) {
continue;
}
$layerCfg = $cfg->getLayer($layer->layername);
if ($layerCfg) {
$layerCfg->opacity = $layer->layerOpacity;
}
}
}
Expand All @@ -446,82 +418,26 @@ protected function setLayerOpacity(ProjectConfig $cfg)
*/
protected function setLayerGroupData(ProjectConfig $cfg)
{
if ($this->path) {
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
$groupShortNames = $project->layerTreeRoot->getGroupShortNames();
foreach ($groupShortNames as $name => $shortName) {
$layerCfg = $cfg->getLayer($name);
if (!$layerCfg) {
continue;
}
$layerCfg->shortname = $shortName;
}
$groupsMutuallyExclusive = $project->layerTreeRoot->getGroupsMutuallyExclusive();
foreach ($groupsMutuallyExclusive as $group) {
$layerCfg = $cfg->getLayer($group);
if (!$layerCfg) {
continue;
}
$layerCfg->mutuallyExclusive = 'True';
}

if (!$this->path) {
return;
}
$groupsWithShortName = $this->xpathQuery("//layer-tree-group/customproperties/property[@key='wmsShortName']/parent::*/parent::*");
if ($groupsWithShortName) {
foreach ($groupsWithShortName as $group) {
$name = (string) $group['name'];
$shortNameProperty = $group->xpath("customproperties/property[@key='wmsShortName']");
if (!$shortNameProperty) {
continue;
}

$shortNameProperty = $shortNameProperty[0];
$sname = (string) $shortNameProperty['value'];
if (!$sname) {
continue;
}

$layerCfg = $cfg->getLayer($name);
if (!$layerCfg) {
continue;
}
$layerCfg->shortname = $sname;
}
} else {
$groupsWithShortName = $this->xpathQuery("//layer-tree-group/customproperties/Option[@type='Map']/Option[@name='wmsShortName']/parent::*/parent::*/parent::*");
if ($groupsWithShortName) {
foreach ($groupsWithShortName as $group) {
$name = (string) $group['name'];
$shortNameProperty = $group->xpath("customproperties/Option[@type='Map']/Option[@name='wmsShortName']");
if (!$shortNameProperty) {
continue;
}

$shortNameProperty = $shortNameProperty[0];
$sname = (string) $shortNameProperty['value'];
if (!$sname) {
continue;
}

$layerCfg = $cfg->getLayer($name);
if (!$layerCfg) {
continue;
}
$layerCfg->shortname = $sname;
}
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
$groupShortNames = $project->layerTreeRoot->getGroupShortNames();
foreach ($groupShortNames as $name => $shortName) {
$layerCfg = $cfg->getLayer($name);
if (!$layerCfg) {
continue;
}
$layerCfg->shortname = $shortName;
}

$groupsMutuallyExclusive = $this->xpathQuery("//layer-tree-group[@mutually-exclusive='1']");
if ($groupsMutuallyExclusive) {
foreach ($groupsMutuallyExclusive as $group) {
$name = (string) $group['name'];
$layerCfg = $cfg->getLayer($name);
if ($layerCfg) {
$layerCfg->mutuallyExclusive = 'True';
}
$groupsMutuallyExclusive = $project->layerTreeRoot->getGroupsMutuallyExclusive();
foreach ($groupsMutuallyExclusive as $group) {
$layerCfg = $cfg->getLayer($group);
if (!$layerCfg) {
continue;
}
$layerCfg->mutuallyExclusive = 'True';
}
}

Expand All @@ -530,31 +446,18 @@ protected function setLayerGroupData(ProjectConfig $cfg)
*/
protected function setLayerShowFeatureCount(ProjectConfig $cfg)
{
if ($this->path) {
$project = Qgis\ProjectInfo::fromQgisPath($this->path);
$layersShowFeatureCount = $project->layerTreeRoot->getLayersShowFeatureCount();
foreach ($layersShowFeatureCount as $layer) {
$layerCfg = $cfg->getLayer($layer);
if (!$layerCfg) {
continue;
}
$layerCfg->showFeatureCount = 'True';
}

if (!$this->path) {
return;
}
$layersWithShowFeatureCount = $this->xpathQuery("//layer-tree-layer/customproperties/property[@key='showFeatureCount'][@value='1']/parent::*/parent::*");
if (!$layersWithShowFeatureCount) {
$layersWithShowFeatureCount = $this->xpathQuery("//layer-tree-layer/customproperties/Option[@type='Map']/Option[@name='showFeatureCount'][@value='1']/parent::*/parent::*/parent::*");
}
if ($layersWithShowFeatureCount) {
foreach ($layersWithShowFeatureCount as $layer) {
$name = (string) $layer['name'];
$layerCfg = $cfg->getLayer($name);
if ($layerCfg) {
$layerCfg->showFeatureCount = 'True';
}

$project = Qgis\ProjectInfo::fromQgisPath($this->path);
$layersShowFeatureCount = $project->layerTreeRoot->getLayersShowFeatureCount();
foreach ($layersShowFeatureCount as $layer) {
$layerCfg = $cfg->getLayer($layer);
if (!$layerCfg) {
continue;
}
$layerCfg->showFeatureCount = 'True';
}
}

Expand All @@ -571,14 +474,6 @@ protected function unsetPropAfterRead(ProjectConfig $cfg)
$cfg->removeLayer($layer->layername);
}
}
} else {
$pluginLayers = $this->xpathQuery('//maplayer[type="plugin"]');
if ($pluginLayers) {
foreach ($pluginLayers as $layer) {
$name = (string) $layer->layername;
$cfg->removeLayer($name);
}
}
}

// unset cache for editionLayers
Expand Down
33 changes: 30 additions & 3 deletions tests/units/classes/Project/QgisProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,42 @@ public function testSetLayerOpacity()
{
$file = __DIR__.'/Ressources/simpleLayer.qgs.cfg';
$json = json_decode(file_get_contents($file));
$expectedLayer = clone $json->layers;
$expectedLayer = json_decode(json_encode($json->layers));
$expectedLayer->montpellier_events->opacity = (float) 0.85;
$cfg = new Project\ProjectConfig((object) array('layers' => $json->layers));
$testProj = new qgisProjectForTests();
$testProj->setXmlForTest(simplexml_load_file(__DIR__.'/Ressources/opacity.qgs'));
$testProj->setPath(__DIR__.'/Ressources/opacity.qgs');
$testProj->setLayerOpacityForTest($cfg);
$this->assertEquals($expectedLayer, $cfg->getLayers());
}

public function testSetLayerGroupData()
{
$file = __DIR__.'/Ressources/hiddengrouplayer.qgs.cfg';
$json = json_decode(file_get_contents($file));
$expectedLayer = json_decode(json_encode($json->layers));
$expectedLayer->Hidden->shortname = 'Hidden';
$expectedLayer->Hidden->mutuallyExclusive = 'True';
$cfg = new Project\ProjectConfig((object) array('layers' => $json->layers));
$testProj = new qgisProjectForTests();
$testProj->setPath(__DIR__.'/Ressources/opacity.qgs');
$testProj->setLayerGroupDataForTest($cfg);
$this->assertEquals($expectedLayer->Hidden, $cfg->getLayers()->Hidden);
}

public function testSetLayerShowFeatureCount()
{
$file = __DIR__.'/Ressources/simpleLayer.qgs.cfg';
$json = json_decode(file_get_contents($file));
$expectedLayer = json_decode(json_encode($json->layers));
$expectedLayer->montpellier_events->showFeatureCount = 'True';
$cfg = new Project\ProjectConfig((object) array('layers' => $json->layers));
$testProj = new qgisProjectForTests();
$testProj->setPath(__DIR__.'/Ressources/opacity.qgs');
$testProj->setLayerShowFeatureCountForTest($cfg);
$this->assertEquals($expectedLayer, $cfg->getLayers());
}

public static function getLayerData()
{
$layers = array(
Expand Down Expand Up @@ -447,7 +474,7 @@ public function testSetShortNames($file, $lname, $sname)
),
);
$testProj = new qgisProjectForTests();
$testProj->setXmlForTest(simplexml_load_file($file));
$testProj->setPath($file);
$cfg = new Project\ProjectConfig((object) array('layers' => (object) $layers));
$testProj->setShortNamesForTest($cfg);
$layer = $cfg->getLayers();
Expand Down
28 changes: 28 additions & 0 deletions tests/units/classes/Project/Ressources/hiddengrouplayer.qgs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"layers": {
"Hidden": {
"id": "Hidden",
"name": "Hidden",
"type": "group",
"title": "Hidden",
"abstract": "",
"link": "",
"minScale": 1,
"maxScale": 1000000000000,
"toggled": "True",
"popup": "False",
"popupSource": "lizmap",
"popupTemplate": "",
"popupMaxFeatures": 10,
"popupDisplayChildren": "False",
"noLegendImage": "False",
"groupAsLayer": "False",
"baseLayer": "False",
"displayInLegend": "True",
"singleTile": "False",
"imageFormat": "image/png",
"cached": "False",
"clientCacheExpiration": 300
}
}
}
Loading

0 comments on commit 6d91035

Please sign in to comment.