Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Include layer opacity info for embedded layers #4964

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lizmap/modules/lizmap/lib/Project/QgisProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,31 @@ protected function setShortNames(ProjectConfig $cfg)
*/
protected function setLayerOpacity(ProjectConfig $cfg)
{
$layerWithOpacities = $this->xpathQuery('//maplayer/layerOpacity[.!=1]/parent::* | //maplayer/pipe/rasterrenderer/@opacity[.!=1]/ancestor::maplayer');
if ($layerWithOpacities) {
foreach ($layerWithOpacities as $layerWithOpacity) {
$name = (string) $layerWithOpacity->layername;
$layers = $this->layers;
// loop through project layers to get embedded ones as well
foreach ($layers as $layer) {
$xpath = "//maplayer[id='{$layer['id']}']/layerOpacity[.!=1]/parent::* | //maplayer[id='{$layer['id']}']/pipe/rasterrenderer/@opacity[.!=1]/ancestor::maplayer";
$layerWithOpacity = null;

// check if layer is embedded or not
$qgisProject = $this->getEmbeddedQgisProject($layer['id']);

if ($qgisProject) {
$layerWithOpacity = $qgisProject->xpathQuery($xpath);
} else {
$layerWithOpacity = $this->xpathQuery($xpath);
}

// layerWithOpacity should have length == 1, if any
if ($layerWithOpacity && count($layerWithOpacity) == 1) {
$name = (string) $layerWithOpacity[0]->layername;
$layerCfg = $cfg->getLayer($name);
$opacity = 1;
if ($layerCfg) {
if (isset($layerWithOpacity->layerOpacity)) {
$opacity = (float) $layerWithOpacity->layerOpacity;
} elseif (isset($layerWithOpacity->pipe->rasterrenderer['opacity'])) {
$opacity = (float) $layerWithOpacity->pipe->rasterrenderer['opacity'];
if (isset($layerWithOpacity[0]->layerOpacity)) {
$opacity = (float) $layerWithOpacity[0]->layerOpacity;
} elseif (isset($layerWithOpacity[0]->pipe->rasterrenderer['opacity'])) {
$opacity = (float) $layerWithOpacity[0]->pipe->rasterrenderer['opacity'];
}
$layerCfg->opacity = $opacity;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/units/classes/Project/QgisProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,51 @@ public function testSetLayerOpacity()
$cfg = new Project\ProjectConfig((object) array('layers' => $json->layers));
$testProj = new qgisProjectForTests();
$testProj->setXmlForTest(simplexml_load_file(__DIR__.'/Ressources/opacity.qgs'));
$layers = array(
array (
'id' => 'events_4c3b47b8_3939_4c8c_8e91_55bdb13a2101',
'name' => 'montpellier_events',
),
array (
'id' => 'raster_78572dfa_41b3_42da_a9c6_933ead8bad8f',
'name' => 'local_raster_layer',
),
);
$testProj->setLayers($layers);
$testProj->setLayerOpacityForTest($cfg);
$this->assertEquals($expectedLayer, $cfg->getLayers());

// embedded layers
$file = __DIR__.'/Ressources/opacity_embed.qgs';
$data = array(
'WMSInformation' => array(),
'layers' => array(),
);
$file = __DIR__.'/Ressources/opacity_embed.qgs';
$testProjE = new ProjectForTests();

$testQgis = new QgisProjectForTests($data);
$rep = new Project\Repository('key', array(), null, null, null);
$testQgis->setPath($file);
$testQgis->readXMLProjectTest($file);

$cfg = json_decode(file_get_contents($file.'.cfg'));
$config = new Project\ProjectConfig($cfg);

$testProjE->setCfg($config);
$testProjE->setQgis($testQgis);
$testProjE->setRepo($rep);
$testProjE->setKey('test');

$testQgis->setLayerOpacityForTest($config);

$emLayer = $testQgis->getLayer('_a5a62408_edf3_4c07_a266_0e8ae6642517', $testProjE);
$this->assertNotNull($emLayer);
$this->assertEquals($emLayer->getName(), 'Fabbricati');

$eLayerName = $emLayer->getName();
$this->assertNotNull($config->getLayer($eLayerName));
$this->assertEquals(0.4,$config->getLayer($eLayerName)->opacity);
}

public static function getLayerData()
Expand Down
1 change: 1 addition & 0 deletions tests/units/classes/Project/Ressources/opacity.qgs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<customproperties/>
<blendMode>0</blendMode>
<layername>montpellier_events</layername>
<id>events_4c3b47b8_3939_4c8c_8e91_55bdb13a2101</id>
<featureBlendMode>0</featureBlendMode>
<layerOpacity>0.85</layerOpacity>
<geometryOptions geometryPrecision="0" removeDuplicateNodes="0">
Expand Down
Loading
Loading