Skip to content

Commit

Permalink
Fix some issues
Browse files Browse the repository at this point in the history
*  read group profiles instead of groups
* construct WMS URL via projected bbox
  • Loading branch information
ridoo committed Feb 27, 2025
1 parent aac4bc3 commit e0076eb
Showing 1 changed file with 74 additions and 64 deletions.
138 changes: 74 additions & 64 deletions geonode_mapstore_client/client/js/plugins/LitterAssessment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import jp from "jsonpath";

import axios from "@mapstore/framework/libs/ajax";
import { createPlugin } from "@mapstore/framework/utils/PluginsUtils";
import { mapSelector } from "@mapstore/framework/selectors/map";
import { getExtentFromViewport} from "@mapstore/framework/utils/CoordinatesUtils";
import { setControlProperty } from "@mapstore/framework/actions/controls";
import { addAuthenticationParameter } from "@mapstore/framework/utils/SecurityUtils";
import Message from "@mapstore/framework/components/I18N/Message";
import controls from "@mapstore/framework/reducers/controls";
import { default as notifications } from "@mapstore/framework/components/notifications/NotificationContainer";
import {
info as infoNotification,
error as errorNotification,
success as successNotification,
success as successNotification
} from "@mapstore/framework/actions/notifications";

import { parseDevHostname, getGeoNodeLocalConfig } from "@js/utils/APIUtils";
Expand All @@ -32,32 +33,34 @@ import Form from "@rjsf/core";
const log = (type) => console.log.bind(console, type);

async function createGroupSelectorWidget(setInferenceGroup) {
const url = parseDevHostname("/api/v2/groups?exclude[]=*&include[]=title&include[]=pk");
const group_response = await fetch(url, {"Accept": "application/json"});
const json = await group_response.json()
const groups = json.group_profiles.map(group => {
return {
title: group.title,
pk: group.pk
};
});

if (groups.length > 0) {
// set state to first group
setInferenceGroup(`${groups[0].pk}`);
}
return props => (
<select
required={props.required}
onChange={(event) => setInferenceGroup(`${event.target.value.pk}`)}
>
{groups.map((group, index) => (
<option key={index} value={group.pk}>
{group.title}
</option>
))}
</select>
)
const url = parseDevHostname(
"/api/v2/groups?exclude[]=*&include[]=title&include[]=group"
);
const group_response = await fetch(url, { Accept: "application/json" });
const json = await group_response.json();
const groupProfiles = json.group_profiles.map((profile) => {
return {
title: profile.group.name,
pk: profile.group.pk
};
});

if (groupProfiles.length > 0) {
// set state to first group
setInferenceGroup(`${groupProfiles[0].pk}`);
}
return (props) => (
<select
required={props.required}
onChange={(event) => setInferenceGroup(`${event.target.value.pk}`)}
>
{groupProfiles.map((group, index) => (
<option key={index} value={group.pk}>
{group.title}
</option>
))}
</select>
);
}

async function getUiSchemas() {
Expand All @@ -82,7 +85,7 @@ async function getModels() {
function _triggerAiInference(selectedModel, { formData }) {
const headers = {
"Content-type": "application/json",
Accept: "application/json",
Accept: "application/json"
};

const path = `/litterassessment/models/${selectedModel}/`;
Expand All @@ -92,7 +95,7 @@ function _triggerAiInference(selectedModel, { formData }) {
dispatch(
infoNotification({
title: "Litter Assessment",
message: "Triggering assessment ...",
message: "Triggering assessment ..."
})
);
return axios.post(url, formData, headers);
Expand All @@ -102,7 +105,7 @@ function _triggerAiInference(selectedModel, { formData }) {
dispatch(
errorNotification({
title: "Litter Assessment",
message: "Could not connect to assessment service!",
message: "Could not connect to assessment service!"
})
);
})
Expand All @@ -116,24 +119,28 @@ function _triggerAiInference(selectedModel, { formData }) {
dispatch(
errorNotification({
title: "Litter Assessment",
message: `Assessment could not be started! ${content.msg}`,
message: `Assessment could not be started! ${content.msg}`
})
);
} else {
dispatch(
successNotification({
title: "Litter Assessment",
message: "Assessment started and will be uploaded once ready.",
message: "Assessment started and will be uploaded once ready."
})
);
}
});
};
}

function toWmsUrl(wmsLayerOptions, securityToken) {
function toWmsUrl(wmsLayerOptions, map, securityToken) {
const bounds = wmsLayerOptions.bbox.bounds;
const bbox = [bounds.minx, bounds.miny, bounds.maxx, bounds.maxy];
const bboxDeg = [bounds.minx, bounds.miny, bounds.maxx, bounds.maxy];
const projection = map.projection;
const bboxMeters = getExtentFromViewport(wmsLayerOptions.bbox, projection);
const width = (bboxMeters[2] - bboxMeters[0]) * 1000;
const height = (bboxMeters[3] - bboxMeters[1]) * 1000;
const queryParameters = assign(
{},
{
Expand All @@ -143,11 +150,11 @@ function toWmsUrl(wmsLayerOptions, securityToken) {
TRANSPARENT: true,
SERVICE: "WMS",
REQUEST: "GetMap",
WIDTH: "1000",
HEIGHT: "600",
BBOX: bbox,
WIDTH: Math.floor(width),
HEIGHT: Math.floor(height),
BBOX: bboxDeg,
TILED: false,
VERSION: "1.3.0",
VERSION: "1.3.0"
}
);

Expand All @@ -160,13 +167,14 @@ function toWmsUrl(wmsLayerOptions, securityToken) {
function LitterAssessment({
enabled,
resource,
map,
wmsLayers = [],
securityToken,
triggerAiInference,
onClose,
onClose
}) {
const pk = resource?.pk
const title = resource?.title
const pk = resource?.pk;
const title = resource?.title;

const [models, setModels] = useState({});
const [uischemas, setSchemas] = useState({});
Expand All @@ -175,17 +183,21 @@ function LitterAssessment({
const [inferenceGroup, setInferenceGroup] = useState(null);
const [widgets, setWidgets] = useState({});

const wmsUrl = wmsLayers?.length && map
? toWmsUrl(wmsLayers[0], map, securityToken)
: "";

const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;

createGroupSelectorWidget(setInferenceGroup)
.then(GroupSelectorWidget => {
.then((GroupSelectorWidget) => {
setWidgets({
groupSelectorWidget: GroupSelectorWidget
});
})
.catch(err => {
.catch((err) => {
console.error("could not create group select!", err);
});

Expand Down Expand Up @@ -227,12 +239,12 @@ function LitterAssessment({
});

getUiSchemas()
.then(async response => {
.then(async (response) => {
const schemas = response.data.schemas;
Object.values(schemas).forEach(schema => {
Object.values(schemas).forEach((schema) => {
schema.inferenceGroup = {
"ui:widget": "groupSelectorWidget"
}
};
});
setSchemas(schemas);
})
Expand All @@ -258,10 +270,6 @@ function LitterAssessment({
};
}, []);

const wmsLayer = wmsLayers?.length
? toWmsUrl(wmsLayers[0], securityToken)
: "";

return (
<OverlayContainer enabled={enabled} className="gn-overlay-wrapper">
<section className="gn-litterassessment-panel">
Expand Down Expand Up @@ -306,7 +314,7 @@ function LitterAssessment({
widgets={widgets}
schema={jsonSchemas[selectedModel] || {}}
uiSchema={uischemas[selectedModel] || {}}
formData={{ imageUrl: wmsLayer, pk, title, inferenceGroup }}
formData={{ imageUrl: wmsUrl, pk, title, inferenceGroup }}
onSubmit={(e) => triggerAiInference(selectedModel, e)}
onError={log("errors")}
>
Expand All @@ -324,35 +332,37 @@ function LitterAssessment({

LitterAssessment.propTypes = {
enabled: PropTypes.bool,
onClose: PropTypes.func,
onClose: PropTypes.func
};

LitterAssessment.defaultProps = {
enabled: false,
onClose: () => {},
onClose: () => {}
};

const LitterAssessmentPlugin = connect(
createSelector(
[
(state) => state?.controls?.rightOverlay?.enabled === "LitterAssessment",
(state) => state?.gnresource?.data || null,
(state) => mapSelector(state),
(state) => state.layers,
(state) => state.security,
(state) => state.security
],
(enabled, resource, layers, security) => ({
(enabled, resource, map, layers, security) => ({
enabled,
resource,
map,
wmsLayers:
layers?.flat?.filter(
(l) => l.type === "wms" && (!l.group || l.group !== "background")
) || [],
securityToken: security.token,
securityToken: security.token
})
),
(dispatch) => ({
onClose: setControlProperty.bind(null, "rightOverlay", "enabled", false),
triggerAiInference: bindActionCreators(_triggerAiInference, dispatch),
triggerAiInference: bindActionCreators(_triggerAiInference, dispatch)
})
)(LitterAssessment);

Expand All @@ -370,15 +380,15 @@ function LitterAssessmentButton({ enabled, variant, onClick, size }) {

const ConnectedLitterAssessmentButton = connect(
createSelector(getResourceId, (resourceId) => ({
enabled: resourceId !== undefined,
enabled: resourceId !== undefined
})),
{
onClick: setControlProperty.bind(
null,
"rightOverlay",
"enabled",
"LitterAssessment"
),
)
}
)(LitterAssessmentButton);

Expand All @@ -387,11 +397,11 @@ export default createPlugin("LitterAssessment", {
containers: {
ActionNavbar: {
name: "LitterAssessment",
Component: ConnectedLitterAssessmentButton,
},
Component: ConnectedLitterAssessmentButton
}
},
epics: {},
reducers: {
controls,
},
controls
}
});

0 comments on commit e0076eb

Please sign in to comment.