Skip to content

Commit

Permalink
Action: allow geometry drawn by user
Browse files Browse the repository at this point in the history
  • Loading branch information
nboisteault committed Sep 17, 2024
1 parent a981780 commit 0a2334d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
8 changes: 8 additions & 0 deletions assets/src/components/ActionSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export default class ActionSelector extends HTMLElement {
}

onActionSelectChange(event) {
// Remove previous digitizing tool if any
document.querySelector('#project-action-selector-container lizmap-digitizing')?.remove();

// Get the host component
let host = event.target.closest("lizmap-action-selector");

Expand All @@ -93,6 +96,11 @@ export default class ActionSelector extends HTMLElement {
if ('description' in action && action.description) {
description = action.description;
}
if (action?.geometry) {
this._digitizingElement = `<lizmap-digitizing context="action" selected-tool="${action.geometry}" available-tools="${action.geometry}"></lizmap-digitizing>`;
document.querySelector('.action-selector-container').insertAdjacentHTML('afterend', this._digitizingElement);
mainLizmap.digitizing.context = "action";
}
}

descriptionSpan.textContent = description;
Expand Down
13 changes: 12 additions & 1 deletion assets/src/modules/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ export default class Action {
}
});
}

mainLizmap.lizmap3.events.on({
minidockclosed: (event) => {
if (event.id === 'action'){
mainLizmap.digitizing.toolSelected = 'deactivate';
}
}
});
}

/**
Expand Down Expand Up @@ -399,6 +405,11 @@ export default class Action {
options['mapExtent'] = WKTformat.writeGeometry(fromExtent(mainLizmap.extent), projOptions);
options['mapCenter'] = WKTformat.writeGeometry(new Point(mainLizmap.center), projOptions);

// Optional geometry
if (mainLizmap.digitizing.context === "action" && mainLizmap.digitizing.featureDrawn) {
options['actionGeometry'] = WKTformat.writeFeatures(mainLizmap.digitizing.featureDrawn, projOptions);
}

// Request action and get data
let url = actionConfigData.url;
try {
Expand Down
5 changes: 3 additions & 2 deletions lizmap/modules/action/controllers/service.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public function index()

// Check the given WKT (optional parameter, but must be valid)
// Check also the map center and extent (must be valid WKT)
$wktParameters = array('wkt', 'mapCenter', 'mapExtent');
$wkt = $mapCenter = $mapExtent = '';
$wktParameters = array('wkt', 'mapCenter', 'mapExtent', 'actionGeometry');
$wkt = $mapCenter = $mapExtent = $actionGeometry = '';
foreach($wktParameters as $paramName) {
$value = trim($this->param($paramName, ''));
${$paramName} = $value;
Expand Down Expand Up @@ -173,6 +173,7 @@ public function index()
'feature_id' => null,
'map_center' => $mapCenter,
'map_extent' => $mapExtent,
'action_geometry' => $actionGeometry,
'wkt' => $wkt,
);

Expand Down
5 changes: 5 additions & 0 deletions lizmap/www/assets/css/action.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ div.action-buttons button:hover:not([disabled]) {
color: white;
text-shadow: none;
}

#project-action-selector-container lizmap-digitizing {
margin-bottom: 10px;
display: block;
}
17 changes: 17 additions & 0 deletions tests/qgis-projects/tests/feature_toolbar.qgs.action
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@
},
"callbacks": []
},
{
"name": "project_map_drawn_point_buffer",
"title": "Get the buffer of the point drawn by the user",
"description": "This is an example action which returns a buffer around a point drawn by the user",
"scope": "project",
"icon": "icon-star",
"geometry": "point",
"options": {
"buffer_size": 2000
},
"style": {
"fill-color": "rgba(255,165,0,0.3)",
"stroke-width": 4,
"stroke-color": "rgba(255,0,0,0.8)"
},
"callbacks": []
},
{
"name": "layer_spatial_extent",
"title": "Get the contour of all the layer features",
Expand Down
21 changes: 20 additions & 1 deletion tests/qgis-projects/tests/set_tests_module_action.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DECLARE
wkt text;
map_center text;
map_extent text;
action_geometry text;
sqltext text;
datasource text;
ajson json;
Expand All @@ -67,6 +68,7 @@ BEGIN
layer_table:= parameters->>'layer_table';
map_center:= parameters->>'map_center';
map_extent:= parameters->>'map_extent';
action_geometry:= parameters->>'action_geometry';
layer_srid:= 0;
feature_id:= (parameters->>'feature_id')::integer;
wkt:= parameters->>'wkt';
Expand Down Expand Up @@ -97,7 +99,7 @@ BEGIN
-- selects an action in the list, then click on the button
IF action_scope = 'project' THEN

-- Return the buffer 500m of map center point
-- Return the buffer 2000m of map center point
IF action_name = 'project_map_center_buffer' AND trim(map_center) != '' THEN
datasource:= format(
$$
Expand All @@ -114,6 +116,23 @@ BEGIN
map_center,
parameters->>'buffer_size'
);
-- Return the buffer 2000m of point drawn by user
ELSEIF action_name = 'project_map_drawn_point_buffer' AND trim(action_geometry) != '' THEN
datasource:= format(
$$
SELECT
1 AS id,
'%1$s' AS project,
ST_Buffer(
ST_GeomFromText('%2$s', 4326)::geography,
%3$s
)::geometry(POLYGON, 4326) AS geom,
'The displayed geometry represents the buffer %3$s m of the point drawn by the user' AS message
$$,
lizmap_project,
action_geometry,
parameters->>'buffer_size'
);
END IF;

-- actions for the layer scope
Expand Down

0 comments on commit 0a2334d

Please sign in to comment.