Skip to content

Commit

Permalink
Merge pull request #4937 from 3liz/backport-4754-to-release_3_9
Browse files Browse the repository at this point in the history
[Backport release_3_9] Action: allow geometry drawn by user
  • Loading branch information
nboisteault authored Nov 5, 2024
2 parents 3ab9aa5 + 20fa921 commit f20925a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
16 changes: 16 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.getElementById('action-digitizing')?.remove();

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

Expand All @@ -93,6 +96,18 @@ export default class ActionSelector extends HTMLElement {
if ('description' in action && action.description) {
description = action.description;
}
if (action?.geometry) {
const actionDigitizing = `<div id="action-digitizing"><lizmap-digitizing context="action" selected-tool="${action.geometry}" available-tools="${action.geometry}"></lizmap-digitizing><div id="action-message-html"></div></div>`;
document.querySelector('.action-selector-container').insertAdjacentHTML('afterend', actionDigitizing);
mainLizmap.digitizing.context = "action";
mainLizmap.digitizing.toggleVisibility(true);
} else {
mainLizmap.digitizing.toolSelected = 'deactivate'
mainLizmap.digitizing.toggleVisibility(false);
}
} else {
mainLizmap.digitizing.toolSelected = 'deactivate'
mainLizmap.digitizing.toggleVisibility(false);
}

descriptionSpan.textContent = description;
Expand All @@ -117,6 +132,7 @@ export default class ActionSelector extends HTMLElement {

onActionDeactivateClick(event) {
// Deactivate the current active action
mainLizmap.digitizing.eraseAll();
mainLizmap.action.resetLizmapAction();
}

Expand Down
30 changes: 24 additions & 6 deletions 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 @@ -376,11 +382,22 @@ export default class Action {
return false;
}

const WKTformat = new WKT();
const projOptions = {
featureProjection: mainLizmap.projection,
dataProjection: 'EPSG:4326'
};

// Reset the other actions
// We allow only one active action at a time
// We do not remove the active status of the button (btn-primary)
this.resetLizmapAction(true, true, true, false);

// Take drawn geometry if any and if none exists as a parameter
if (!wkt && mainLizmap.digitizing.context === "action" && mainLizmap.digitizing.featureDrawn) {
wkt = WKTformat.writeFeatures(mainLizmap.digitizing.featureDrawn, projOptions);
}

// Set the request parameters
let options = {
"layerId": layerId,
Expand All @@ -391,11 +408,6 @@ export default class Action {

// We add the map extent and center
// as WKT geometries
const WKTformat = new WKT();
const projOptions = {
featureProjection: mainLizmap.projection,
dataProjection: 'EPSG:4326'
};
options['mapExtent'] = WKTformat.writeGeometry(fromExtent(mainLizmap.extent), projOptions);
options['mapCenter'] = WKTformat.writeGeometry(new Point(mainLizmap.center), projOptions);

Expand Down Expand Up @@ -444,6 +456,12 @@ export default class Action {
if (message) {
mainLizmap.lizmap3.addMessage(message, 'info', true).attr('id', 'lizmap-action-message');
}

// Display the HTML message if given
const message_html = featureProperties?.message_html?.trim();
if (message_html) {
document.getElementById('action-message-html').innerHTML = message_html;
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions lizmap/www/assets/css/action.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
padding-bottom: 10px;
}
div.action-selector-container {
padding: 5px;
width: 100%;
margin-bottom: 5px;
}

select.action-select {
Expand All @@ -35,6 +34,8 @@ select.action-select {
font-style: italic;
background: lightgray;
margin-right: 5%;
border: 1px solid var(--color-contrasted-elements-light);
border-radius: 5px;
}

div.action-buttons {
Expand All @@ -54,3 +55,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
20 changes: 19 additions & 1 deletion tests/qgis-projects/tests/set_tests_module_action.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 +114,24 @@ 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(wkt) != '' 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,
'<p>The displayed geometry represents the buffer <strong>%3$s m</strong> of the point drawn by the user</p>' AS message_html
$$,
lizmap_project,
wkt,
parameters->>'buffer_size'
);
END IF;

-- actions for the layer scope
Expand Down

0 comments on commit f20925a

Please sign in to comment.