Skip to content

Commit

Permalink
Methods to load embedded project and layer, and a method to get layer…
Browse files Browse the repository at this point in the history
… opacity

To reduce duplicated code :
* methods to load embeded project and layer has been added to Lizmap\Project\Qgis\Layer\EmbeddedLayer
* method getLayerOpacity added to LizmapProject\Qgis\Layer\*Layer
  • Loading branch information
rldhont committed Dec 3, 2024
1 parent c39b644 commit 654c16c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 20 deletions.
48 changes: 48 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/EmbeddedLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,52 @@ public function toKeyArray()
'project' => $this->project,
);
}

/**
* Get embedded project path relative from parent project path.
*
* @param string $parentProjectPath The parent project path
*
* @return string The embedded project path
*/
public function getEmbeddedProjectFullPath(string $parentProjectPath)
{
return realpath(dirname($parentProjectPath).DIRECTORY_SEPARATOR.$this->project);
}

/**
* Get embedded project from parent project path.
*
* @param string $parentProjectPath The parent project path
*
* @return Qgis\ProjectInfo The embedded project
*/
public function getEmbeddedProject(string $parentProjectPath)
{
$embeddedPath = $this->getEmbeddedProjectFullPath($parentProjectPath);

return Qgis\ProjectInfo::fromQgisPath($embeddedPath);
}

/**
* Get embedded layer from parent project path.
*
* @param string $parentProjectPath The parent project path
*
* @return null|Qgis\Layer\MapLayer|Qgis\Layer\RasterLayer|Qgis\Layer\VectorLayer The embedded layer
*/
public function getEmbeddedLayer(string $parentProjectPath)
{
$embeddedProject = $this->getEmbeddedProject($parentProjectPath);
foreach ($embeddedProject->projectlayers as $embeddedLayer) {
/** @var Qgis\Layer\MapLayer $embeddedLayer */
if ($embeddedLayer->id !== $this->id) {
continue;
}

return $embeddedLayer;
}

return null;
}
}
10 changes: 10 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/MapLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class MapLayer extends Qgis\BaseQgisXmlObject
'layerOpacity' => 1,
);

/**
* Get layer opacity.
*
* @return float
*/
public function getLayerOpacity()
{
return $this->layerOpacity;
}

/**
* Get map layer as key array.
*
Expand Down
10 changes: 10 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/RasterLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class RasterLayer extends Qgis\BaseQgisObject
'pipe',
);

/**
* Get layer opacity.
*
* @return float
*/
public function getLayerOpacity()
{
return $this->pipe->renderer->opacity;
}

/**
* Get map layer as key array.
*
Expand Down
10 changes: 10 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/VectorLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class VectorLayer extends Qgis\BaseQgisObject
'layerOpacity' => 1,
);

/**
* Get layer opacity.
*
* @return float
*/
public function getLayerOpacity()
{
return $this->layerOpacity;
}

/**
* Get preview field.
*
Expand Down
30 changes: 19 additions & 11 deletions lizmap/modules/lizmap/lib/Project/Qgis/ProjectInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ public function getProjAsKeyArray()
$data[$this->projectCrs->authid] = $this->projectCrs->proj4;
foreach ($this->projectlayers as $layer) {
if ($layer->embedded) {
/** @var Project\Qgis\Layer\EmbeddedLayer $layer */
try {
$embeddedLayer = $layer->getEmbeddedLayer($this->getPath());
} catch (\Exception $e) {
continue;
}
$data[$embeddedLayer->srs->authid] = $embeddedLayer->srs->proj4;

continue;
}
$data[$layer->srs->authid] = $layer->srs->proj4;
Expand All @@ -205,7 +213,7 @@ public function getProjAsKeyArray()
*
* @param string $layerId The layer id
*
* @return null|Layer\MapLayer|Layer\VectorLayer
* @return null|Layer\MapLayer|Layer\RasterLayer|Layer\VectorLayer
*/
public function getLayerById($layerId)
{
Expand All @@ -215,14 +223,10 @@ public function getLayerById($layerId)
}
if ($layer->embedded) {
/** @var Project\Qgis\Layer\EmbeddedLayer $layer */
$embeddedPath = realpath(dirname($this->getPath()).DIRECTORY_SEPARATOR.$layer->project);
$embeddedProject = ProjectInfo::fromQgisPath($embeddedPath);
foreach ($embeddedProject->projectlayers as $embeddedLayer) {
if ($embeddedLayer->id !== $layer->id) {
continue;
}

return $embeddedLayer;
try {
return $layer->getEmbeddedLayer($this->getPath());
} catch (\Exception $e) {
return null;
}
}

Expand All @@ -243,8 +247,12 @@ public function getLayersAsKeyArray()
foreach ($this->projectlayers as $layer) {
if ($layer->embedded) {
/** @var Project\Qgis\Layer\EmbeddedLayer $layer */
$embeddedLayer = $this->getLayerById($layer->id);
$embeddedPath = realpath(dirname($this->getPath()).DIRECTORY_SEPARATOR.$layer->project);
try {
$embeddedLayer = $layer->getEmbeddedLayer($this->getPath());
} catch (\Exception $e) {
continue;
}
$embeddedPath = $layer->getEmbeddedProjectFullPath($this->getPath());

$layerKeyArray = $embeddedLayer->toKeyArray();
$layerKeyArray['qgsmtime'] = filemtime($embeddedPath);
Expand Down
26 changes: 17 additions & 9 deletions lizmap/modules/lizmap/lib/Project/QgisProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,27 @@ protected function setLayerOpacity(ProjectConfig $cfg)

$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
$layername = '';
$opacity = 1;

/** @var Qgis\Layer\MapLayer $layer */
if ($layer->embedded) {
continue;
/** @var Qgis\Layer\EmbeddedLayer $layer */
try {
$embeddedLayer = $layer->getEmbeddedLayer($this->path);
} catch (\Exception $e) {
continue;
}
$layername = $embeddedLayer->layername;
$opacity = $embeddedLayer->getLayerOpacity();
} else {
$layername = $layer->layername;
$opacity = $layer->getLayerOpacity();
}

$opacity = 1;
if ($layer->type == 'raster') {
/** @var Qgis\Layer\RasterLayer $layer */
$opacity = $layer->pipe->renderer->opacity;
} elseif (isset($layer->layerOpacity) && $layer->layerOpacity != 1) {
$opacity = $layer->layerOpacity;
if ($layername == '' || $opacity == 1) {
continue;
}
$layerCfg = $cfg->getLayer($layer->layername);
$layerCfg = $cfg->getLayer($layername);
if ($layerCfg) {
$layerCfg->opacity = $opacity;
}
Expand Down

0 comments on commit 654c16c

Please sign in to comment.