Skip to content

Commit

Permalink
Recherche spatiale - amélioration des performances pour lignes et pol…
Browse files Browse the repository at this point in the history
…ygones
  • Loading branch information
mdouchin committed Dec 19, 2023
1 parent 9b51956 commit 344ff27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cadastre/classes/cadastreDockable.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function onmapDockable($event)
if ($qgisLayer->getProvider() != 'postgres') {
continue;
}
// Only Layers in attribute table to be able to select features
if (!$p->hasAttributeLayersForLayer($qgisLayer->getName())) {
continue;
}
// Check if the database is the same as the Parcelle layer
if ($qgisLayer->getDatasourceProfile(30, false) != $parcelleProfile) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion cadastre/classes/listParcelleSpatialDatasource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function getData($form)
$spatialLayerPk,
$selectedIds,
$spatialLayerField,
$spatialLayerBuffer
$spatialLayerBuffer,
$dbFieldsInfo->geometryColumn,
$dbFieldsInfo->geometryType
);

$result = array();
Expand Down
37 changes: 32 additions & 5 deletions cadastre/daos/parcelle_info.dao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@
<parameter name="spatialLayerSelectedIds" />
<parameter name="spatialLayerField" />
<parameter name="spatialLayerBuffer" />
<parameter name="spatialLayerGeometry" />
<parameter name="spatialLayerGeometryType" />

<body><![CDATA[
$sql = $this->_selectClause;
Expand All @@ -372,17 +374,42 @@
}
$sql .= $this->_fromClause;
$sql .= ' JOIN "' . $spatialLayerSchema . '"."' . $spatialLayerTable . '" AS i';
$sql .= ' ON ST_DWithin(i.geom, "parcelle_info".geom, ' . $this->_prepareValue($spatialLayerBuffer,'string') .')';
// Filtre des identifiants sélectionnés dans la couche de croisement
if (count($spatialLayerSelectedIds) > 0) {
$sql .= ' AND i."' . trim($spatialLayerPk, " '\n\r\t\v\x00") . '" IN (';
$sqlIds .= '"' . trim($spatialLayerPk, " '\n\r\t\v\x00") . '" IN (';
$virg = '';
foreach($spatialLayerSelectedIds as $v) {
$sql .= $virg . $this->_prepareValue(trim($v), 'string');
$sqlIds .= $virg . $this->_prepareValue(trim($v), 'string');
$virg = ', ';
}
$sql .= ' ) ';
$sqlIds .= ' ) ';
} else {
$sqlIds = '"' . trim($spatialLayerPk, " '\n\r\t\v\x00") . '" IN (\'-999999\')' ;
}
// En fonction du type de géométrie, on utilise soit une jointure directe, soit une sous-requête avec ST_Subdivide
if (!in_array(strtoupper($spatialLayerGeometryType), array('LINESTRING', 'MULTILINESTRING', 'POLYGON', 'MULTIPOLYGON'))) {
// Simple jointure en incluant le filtre spatial et le filtre par identifiants sélectionnés
$sql .= ' JOIN "' . $spatialLayerSchema . '"."' . $spatialLayerTable . '" AS i';
$sql .= ' ON ST_DWithin(i."' . $spatialLayerGeometry . '", "parcelle_info".geom, ' . $this->_prepareValue($spatialLayerBuffer,'string') .')';
if (count($spatialLayerSelectedIds) > 0) {
$sql .= ' AND i.' . $sqlIds;
}
} else {
// Utilisation d'une sous-requête dans la jointure pour subdiviser les géométries
$sql .= ' JOIN ( ';
$sql .= ' SELECT a."' . trim($spatialLayerPk, " '\n\r\t\v\x00") . '", ST_Subdivide(a."' . $spatialLayerGeometry . '") AS geom';
if (!empty($spatialLayerField)) {
$sql .= ' , "' . trim($spatialLayerField, " '\n\r\t\v\x00") . '"';
}
$sql .= ' FROM "' . $spatialLayerSchema . '"."' . $spatialLayerTable . '" AS a';
$sql .= ' WHERE True';
if (count($spatialLayerSelectedIds) > 0) {
$sql .= ' AND a.' . $sqlIds;
}
$sql .= ' ) AS i';
$sql .= ' ON ST_DWithin(i.geom, "parcelle_info".geom, ' . $this->_prepareValue($spatialLayerBuffer,'string') .')';
}
$sql .= $this->_whereClause;
Expand Down

0 comments on commit 344ff27

Please sign in to comment.