Skip to content

Commit

Permalink
new Lizmap\Project\Qgis\Layer\RasterLayer* classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Nov 15, 2024
1 parent e51ed9a commit 90de9b9
Show file tree
Hide file tree
Showing 10 changed files with 1,019 additions and 26 deletions.
9 changes: 8 additions & 1 deletion lizmap/modules/lizmap/lib/Project/Qgis/Layer/MapLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected static function getAttributes($oXmlReader)
*
* @param array $data the instance data
*
* @return EmbeddedLayer|MapLayer|VectorLayer the instance
* @return EmbeddedLayer|MapLayer|RasterLayer|VectorLayer the instance
*/
protected static function buildInstance($data)
{
Expand All @@ -156,6 +156,10 @@ protected static function buildInstance($data)
&& $data['type'] === 'vector') {
return new VectorLayer($data);
}
if (array_key_exists('type', $data)
&& $data['type'] === 'raster') {
return new RasterLayer($data);
}

return new MapLayer($data);
}
Expand Down Expand Up @@ -406,3 +410,6 @@ protected static function buildInstance($data)
MapLayer::registerChildParser('renderer-v2', function ($oXmlReader) {
return RendererV2::fromXmlReader($oXmlReader);
});
MapLayer::registerChildParser('pipe', function ($oXmlReader) {
return RasterLayerPipe::fromXmlReader($oXmlReader);
});
89 changes: 89 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/RasterLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* QGIS Raster layer.
*
* @author 3liz
* @copyright 2023 3liz
*
* @see http://3liz.com
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

namespace Lizmap\Project\Qgis\Layer;

use Lizmap\Project\Qgis;

/**
* QGIS Raster layer.
*
* @property string $id
* @property bool $embedded
* @property string $type
* @property string $layername
* @property Qgis\SpatialRefSys $srs
* @property string $datasource
* @property string $provider
* @property MapLayerStyleManager $styleManager
* @property null|string $shortname
* @property null|string $title
* @property null|string $abstract
* @property null|array<string> $keywordList
* @property RasterLayerPipe $pipe
* @property MapLayerStyleManager $styleManager
*/
class RasterLayer extends Qgis\BaseQgisObject
{
/** @var array<string> The instance properties */
protected $properties = array(
'id',
'embedded',
'type',
'layername',
'srs',
'datasource',
'provider',
'styleManager',
'shortname',
'title',
'abstract',
'keywordList',
'pipe',
);

/** @var array<string> The not null properties */
protected $mandatoryProperties = array(
'id',
'embedded',
'type',
'layername',
'srs',
'datasource',
'provider',
'styleManager',
'pipe',
);

/**
* Get map layer as key array.
*
* @return array
*/
public function toKeyArray()
{
return array(
'type' => $this->type,
'id' => $this->id,
'name' => $this->layername,
'shortname' => $this->shortname !== null ? $this->shortname : '',
'title' => $this->title !== null ? $this->title : $this->layername,
'abstract' => $this->abstract !== null ? $this->abstract : '',
'proj4' => $this->srs->proj4,
'srid' => $this->srs->srid,
'authid' => $this->srs->authid,
'datasource' => $this->datasource,
'provider' => $this->provider,
'keywords' => $this->keywordList !== null ? $this->keywordList : array(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* QGIS Raster layer hue saturation.
*
* @author 3liz
* @copyright 2023 3liz
*
* @see http://3liz.com
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

namespace Lizmap\Project\Qgis\Layer;

use Lizmap\Project;

/**
* QGIS Raster layer hue saturation.
*
* @property int $saturation
* @property int $grayscaleMode
* @property bool $invertColors
* @property bool $colorizeOn
* @property int $colorizeRed
* @property int $colorizeGreen
* @property int $colorizeBlue
* @property int $colorizeStrength
*/
class RasterLayerHueSaturation extends Project\Qgis\BaseQgisXmlObject
{
/** @var array<string> The instance properties */
protected $properties = array(
'saturation',
'grayscaleMode',
'invertColors',
'colorizeOn',
'colorizeRed',
'colorizeGreen',
'colorizeBlue',
'colorizeStrength',
);

/** @var array<string> The not null properties */
protected $mandatoryProperties = array(
'saturation',
'grayscaleMode',
'invertColors',
'colorizeOn',
'colorizeRed',
'colorizeGreen',
'colorizeBlue',
'colorizeStrength',
);

/** @var string The XML element local name */
protected static $qgisLocalName = 'huesaturation';

protected static function getAttributes($oXmlReader)
{
return array(
'saturation' => (int) $oXmlReader->getAttribute('saturation'),
'grayscaleMode' => (int) $oXmlReader->getAttribute('grayscaleMode'),
'invertColors' => filter_var($oXmlReader->getAttribute('invertColors'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
'colorizeOn' => filter_var($oXmlReader->getAttribute('colorizeOn'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
'colorizeRed' => (int) $oXmlReader->getAttribute('colorizeRed'),
'colorizeGreen' => (int) $oXmlReader->getAttribute('colorizeGreen'),
'colorizeBlue' => (int) $oXmlReader->getAttribute('colorizeBlue'),
'colorizeStrength' => (int) $oXmlReader->getAttribute('colorizeStrength'),
);
}
}
68 changes: 68 additions & 0 deletions lizmap/modules/lizmap/lib/Project/Qgis/Layer/RasterLayerPipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* QGIS Raster layer pipe.
*
* @author 3liz
* @copyright 2023 3liz
*
* @see http://3liz.com
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

namespace Lizmap\Project\Qgis\Layer;

use Lizmap\Project;

/**
* QGIS Raster layer pipe.
*
* @property RasterLayerRasterRenderer $renderer
* @property RasterLayerHueSaturation $hueSaturation
*/
class RasterLayerPipe extends Project\Qgis\BaseQgisXmlObject
{
/** @var array<string> The instance properties */
protected $properties = array(
'renderer',
'hueSaturation',
);

/** @var array<string> The not null properties */
protected $mandatoryProperties = array(
'renderer',
'hueSaturation',
);

/** @var string The XML element local name */
protected static $qgisLocalName = 'pipe';

/** @var array<string> The XML element parsed children */
protected static $children = array(
'rasterrenderer',
'huesaturation',
);

protected static $childParsers = array();

protected static function buildInstance($data)
{
if (array_key_exists('rasterrenderer', $data)) {
$data['renderer'] = $data['rasterrenderer'];
unset($data['rasterrenderer']);
}

if (array_key_exists('huesaturation', $data)) {
$data['hueSaturation'] = $data['huesaturation'];
unset($data['huesaturation']);
}

return new RasterLayerPipe($data);
}
}
RasterLayerPipe::registerChildParser('rasterrenderer', function ($oXmlReader) {
return RasterLayerRasterRenderer::fromXmlReader($oXmlReader);
});
RasterLayerPipe::registerChildParser('huesaturation', function ($oXmlReader) {
return RasterLayerHueSaturation::fromXmlReader($oXmlReader);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* QGIS Raster layer raster renderer.
*
* @author 3liz
* @copyright 2023 3liz
*
* @see http://3liz.com
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

namespace Lizmap\Project\Qgis\Layer;

use Lizmap\Project;

/**
* QGIS Raster layer raster renderer.
*
* @property string $type
* @property float $opacity
*/
class RasterLayerRasterRenderer extends Project\Qgis\BaseQgisXmlObject
{
/** @var array<string> The instance properties */
protected $properties = array(
'type',
'opacity',
);

/** @var array<string> The not null properties */
protected $mandatoryProperties = array(
'type',
'opacity',
);

/** @var string The XML element local name */
protected static $qgisLocalName = 'rasterrenderer';

protected static function getAttributes($oXmlReader)
{
return array(
'type' => $oXmlReader->getAttribute('type'),
'opacity' => (float) $oXmlReader->getAttribute('opacity'),
);
}
}
13 changes: 11 additions & 2 deletions lizmap/modules/lizmap/lib/Project/QgisProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,21 @@ protected function setLayerOpacity(ProjectConfig $cfg)

$project = Qgis\ProjectInfo::fromQgisPath($this->path);
foreach ($project->projectlayers as $layer) {
if (!isset($layer->layerOpacity) || $layer->layerOpacity == 1) {
/** @var Qgis\Layer\MapLayer $layer */
if ($layer->embedded) {
continue;
}

$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;
}
$layerCfg = $cfg->getLayer($layer->layername);
if ($layerCfg) {
$layerCfg->opacity = $layer->layerOpacity;
$layerCfg->opacity = $opacity;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/units/classes/Project/Qgis/MapLayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testFromXmlReader()
$oXml = App\XmlTools::xmlReaderFromString($xmlStr);
$layer = Qgis\Layer\MapLayer::fromXmlReader($oXml);
$this->assertFalse($layer->embedded);
$this->assertInstanceOf(Qgis\Layer\MapLayer::class, $layer);
$this->assertInstanceOf(Qgis\Layer\RasterLayer::class, $layer);

$data = array(
'id' => 'osm_mapnik20180315181738526',
Expand Down
Loading

0 comments on commit 90de9b9

Please sign in to comment.