Skip to content

Commit

Permalink
[Bugfix] AtlasPrint Listener not enough specific
Browse files Browse the repository at this point in the history
The cadastre atlas print listener was not enough specific.

To fix it, the cadastreConfig has been enhanced and the cadastre atlas print listener checks the config and uses the cadastre profile based on Parcelle layer.

Funded by Haute Saone Numérique
  • Loading branch information
rldhont committed Nov 19, 2024
1 parent 9434879 commit 2cc0611
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 18 deletions.
54 changes: 51 additions & 3 deletions cadastre/classes/atlasPrint.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ class atlasPrintListener extends jEventListener
*/
public function onBeforePdfCreation($event)
{
$cadastreConfig = array();
$profile = 'cadastre';
$services = lizmap::getServices();
if (version_compare($services->qgisServerVersion, '3.0', '<')) {
if (!preg_match('#^cadastre#i', $event->project)) {
return null;
}
$cadastreConfig = array(
'layer' => 'Parcelles',
'pk' => 'geo_parcelle',
);
} else {
$config = cadastreConfig::get($event->repository, $event->project);
if ($config == null) {
return null;
}
$cadastreConfig = array(
'layer' => $config->parcelle->name,
'pk' => $config->parcelle->unique_field,
);
if ($config->parcelle->shortName && strlen($config->parcelle->shortName)) {
$cadastreConfig['layer'] = $config->parcelle->shortName;
}
$profile = cadastreProfile::getWithLayerId(
$event->repository,
$event->project,
$config->parcelle->id
);
}

try {
// try to get the specific search profile to do not rebuild it
jProfiles::get('jdb', $profile, true);
} catch (Exception $e) {
return null;
}

$status = 'error';
$file = null;

Expand All @@ -35,7 +72,7 @@ public function onBeforePdfCreation($event)

$layer = $params['layer'];
$exp_filter = $params['exp_filter'];
if (!$layer or $layer != 'Parcelles' or !$exp_filter) {
if (!$layer or $layer != $cadastreConfig['layer'] or !$exp_filter) {
$event->add(
array('status' => $status, 'file' => $file)
);
Expand All @@ -49,9 +86,18 @@ public function onBeforePdfCreation($event)
$get_fid = preg_match('(\d+)', $exp_filter, $match);
$fid = intval($match[0]);

if ($fid > 0) {
if ($fid < 1) {
$event->add(
array('status' => $status, 'file' => $file)
);

return null;
}

// Try to get data from geo_commune
try {
$sql = 'SELECT geo_parcelle FROM parcelle_info WHERE ogc_fid = ' . $fid;
$cnx = jDb::getConnection('cadastre');
$cnx = jDb::getConnection($profile);
$result = $cnx->query($sql);
$geo_parcelle = -1;
foreach ($result as $line) {
Expand Down Expand Up @@ -103,6 +149,8 @@ public function onBeforePdfCreation($event)
// \jLog::log($path);
$file = $path;
$status = 'success';
} catch (Exception $e) {
jLog::log("Cadastre :: " . $e->getMessage());
}

$event->add(
Expand Down
72 changes: 57 additions & 15 deletions cadastre/classes/cadastreConfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,67 @@ class cadastreConfig
public static function get($repository, $project)
{
$p = lizmap::getProject($repository . '~' . $project);
if ($p) {
$request = new lizmapCadastreRequest(
$p,
array(
'service' => 'CADASTRE',
'version' => '1.0.0',
'request' => 'GetCapabilities',
)
);
$result = $request->process();
if ($result->code === 200 && $result->mime !== 'text/xml') {
$data = json_decode($result->data);
if ($data->status == 'success') {
return $data->data;
if (!$p) {
return null;
}
$customVariables = $p->getCustomProjectVariables();
if (!$customVariables) {
return null;
}
if (!array_key_exists('cadastre_parcelle_layer_id', $customVariables)
|| !array_key_exists('cadastre_parcelle_unique_field', $customVariables)) {
return null;
}

$parcelleLayerId = $customVariables['cadastre_parcelle_layer_id'];
$parcelleLayerUniqueField = $customVariables['cadastre_parcelle_unique_field'];

$parcelleLayer = $p->getLayer($parcelleLayerId);
if (!$parcelleLayer) {
return null;
}

$capabilities = array(
'parcelle' => array(
'id' => $parcelleLayerId,
'name' => $parcelleLayer->getName(),
'title' => $parcelleLayer->getTitle(),
'shortName' => $parcelleLayer->getShortName(),
'unique_field' => $customVariables['cadastre_parcelle_unique_field'],
),
);

if (array_key_exists('cadastre_section_layer_id', $customVariables)) {
$layer = $p->getLayer($customVariables['cadastre_section_layer_id']);
if ($layer) {
$capabilities['section'] = array(
'id' => $layer->getId(),
'name' => $layer->getName(),
'title' => $layer->getTitle(),
'shortName' => $layer->getShortName(),
);
if (array_key_exists('cadastre_section_unique_field', $customVariables)) {
$capabilities['section']['unique_field'] = $customVariables['cadastre_section_unique_field'];
}
}
}

return null;
if (array_key_exists('cadastre_commune_layer_id', $customVariables)) {
$layer = $p->getLayer($customVariables['cadastre_commune_layer_id']);
if ($layer) {
$capabilities['commune'] = array(
'id' => $layer->getId(),
'name' => $layer->getName(),
'title' => $layer->getTitle(),
'shortName' => $layer->getShortName(),
);
if (array_key_exists('cadastre_commune_unique_field', $customVariables)) {
$capabilities['commune']['unique_field'] = $customVariables['cadastre_commune_unique_field'];
}
}
}

return (object) $capabilities;
}

public static function getLayerSql($repository, $project, $layerId)
Expand Down

0 comments on commit 2cc0611

Please sign in to comment.