Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance constraint add on frontend #2084

Merged
merged 17 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
98471f9
feat(package): turf/distance package add for distance calculation bet…
NSUWAL123 Jan 13, 2025
ecb6779
fix(types): ProjectData type update
NSUWAL123 Jan 13, 2025
d2cfabd
fix(geolocation): reset userLocation coordinate if location disabled
NSUWAL123 Jan 13, 2025
c053890
feat(dialog-entities-actions): function add to check validity of dist…
NSUWAL123 Jan 13, 2025
3d81fa2
fix(dialog-entities): fix typo
NSUWAL123 Jan 13, 2025
4cdb795
feat(package): turf/distance package add for distance calculation bet…
NSUWAL123 Jan 13, 2025
5f63d14
fix(types): ProjectData type update
NSUWAL123 Jan 13, 2025
f66cef4
fix(geolocation): reset userLocation coordinate if location disabled
NSUWAL123 Jan 13, 2025
31e9caf
feat(dialog-entities-actions): function add to check validity of dist…
NSUWAL123 Jan 13, 2025
3c9f4c5
fix(dialog-entities): fix typo
NSUWAL123 Jan 13, 2025
b9f4c1a
refactor: update new feature draw warning/error logic
spwoodcock Jan 13, 2025
3b24cf6
docs: update comment describing the new feat dist constraint
spwoodcock Jan 13, 2025
7b44d00
fix(merge): merge conflict solve
NSUWAL123 Jan 15, 2025
9e7c5c3
Merge branch 'development' of github.com:hotosm/fmtm into feat/distan…
NSUWAL123 Jan 16, 2025
947725a
fix(dialog-entities-actions): update distance constraing logic
NSUWAL123 Jan 16, 2025
fe34e78
feat(dialog-entities-actions): show warning dialog on feature map if …
NSUWAL123 Jan 20, 2025
1fbcaba
Merge branch 'development' of github.com:hotosm/fmtm into feat/distan…
NSUWAL123 Jan 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@turf/bbox": "^7.1.0",
"@turf/buffer": "^7.1.0",
"@turf/centroid": "^7.1.0",
"@turf/distance": "^7.2.0",
"@turf/helpers": "^7.1.0",
"@watergis/maplibre-gl-terradraw": "^0.8.1",
"drizzle-orm": "^0.35.3",
Expand Down
30 changes: 30 additions & 0 deletions src/mapper/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 118 additions & 2 deletions src/mapper/src/lib/components/dialog-entities-actions.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<script lang="ts">
import { distance } from '@turf/distance';
import type { Coord } from '@turf/helpers';
import { TaskStatusEnum, type ProjectData } from '$lib/types';
import { getEntitiesStatusStore } from '$store/entities.svelte.ts';
import { getAlertStore } from '$store/common.svelte.ts';
import { getTaskStore } from '$store/tasks.svelte.ts';
import { mapTask } from '$lib/db/events';
import type { SlDialog } from '@shoelace-style/shoelace';

type Props = {
isTaskActionModalOpen: boolean;
Expand All @@ -14,6 +17,9 @@

let { isTaskActionModalOpen, toggleTaskActionModal, selectedTab, projectData }: Props = $props();

let dialogRef: SlDialog | null = $state(null);
let toggleDistanceWarningDialog = $state(false);

const entitiesStore = getEntitiesStatusStore();
const alertStore = getAlertStore();
const taskStore = getTaskStore();
Expand Down Expand Up @@ -53,6 +59,54 @@
}
};

const handleMapFeature = () => {
/**
Logic to handle mapping feature in different scenarios:
1. No geolocation, no force geo constraint: allow mapping, ignore / do not show warning
2. No geolocation, force geo constraint: block mapping, show prompt to enable geolocation
3. Geolocation, no force geo constraint: show warning dialog if feature is far away
4. Geolocation, force geo constraint: block mapping if out of range else allow
**/
const coordTo = entitiesStore.selectedEntityCoordinate?.coordinate;
const coordFrom = entitiesStore.userLocationCoord;

// Run only if geo_restrict_force_error is set to true
if (projectData?.geo_restrict_force_error) {
// Geolocation not enabled, warn user
if (!coordFrom) {
alertStore.setAlert({
message:
'This project has distance constraint enabled. Please enable device geolocation for optimal functionality',
variant: 'warning',
});
return;
}

const entityDistance = distance(coordFrom as Coord, coordTo as Coord, { units: 'kilometers' }) * 1000;
if (entityDistance && entityDistance > projectData?.geo_restrict_distance_meters) {
// Feature is far away from user, warn user
alertStore.setAlert({
message: `The feature must be within ${projectData?.geo_restrict_distance_meters} meters of your location`,
variant: 'warning',
});
return;
}
}

// Show warning dialog if geo_restrict_force_error is set to false, user location enabled and feature is far away
if (
!projectData?.geo_restrict_force_error &&
coordFrom &&
distance(coordFrom as Coord, coordTo as Coord, { units: 'kilometers' }) * 1000 >
projectData?.geo_restrict_distance_meters
) {
toggleDistanceWarningDialog = true;
return;
}

mapFeature();
};

const navigateToEntity = () => {
if (!entitiesStore.toggleGeolocation) {
alertStore.setAlert({ message: 'Please enable geolocation to navigate to the entity.', variant: 'warning' });
Expand Down Expand Up @@ -138,11 +192,11 @@
size="small"
class="primary flex-grow"
onclick={() => {
mapFeature();
handleMapFeature();
}}
onkeydown={(e: KeyboardEvent) => {
if (e.key === 'Enter') {
mapFeature();
handleMapFeature();
}
}}
role="button"
Expand All @@ -158,3 +212,65 @@
</div>
</div>
{/if}

{#if entitiesStore.selectedEntityCoordinate?.coordinate && entitiesStore.userLocationCoord}
<hot-dialog
bind:this={dialogRef}
class="dialog-overview z-50 font-barlow font-regular"
open={toggleDistanceWarningDialog}
onsl-hide={() => {
toggleDistanceWarningDialog = false;
}}
noHeader
>
<div class="flex items-start flex-col">
<p class="text-base mb-5 text-gray-700">
Your are <b
>{(
distance(
entitiesStore.selectedEntityCoordinate?.coordinate as Coord,
entitiesStore.userLocationCoord as Coord,
{ units: 'kilometers' },
) * 1000
).toFixed(2)}m</b
> away from the feature. Are you sure you want to map this feature?
</p>
<div class="flex gap-2 ml-auto">
<sl-button
variant="default"
size="small"
class="secondary flex-grow"
onclick={() => (toggleDistanceWarningDialog = false)}
onkeydown={(e: KeyboardEvent) => {
if (e.key === 'Enter') {
toggleDistanceWarningDialog = false;
}
}}
role="button"
tabindex="0"
>
<span class="font-barlow font-medium text-sm">NO</span>
</sl-button>
<sl-button
variant="default"
size="small"
class="primary flex-grow"
onclick={() => {
mapFeature();
toggleDistanceWarningDialog = false;
}}
onkeydown={(e: KeyboardEvent) => {
if (e.key === 'Enter') {
mapFeature();
toggleDistanceWarningDialog = false;
}
}}
role="button"
tabindex="0"
>
<span class="font-barlow font-medium text-sm">YES</span>
</sl-button>
</div>
</div>
</hot-dialog>
{/if}
1 change: 1 addition & 0 deletions src/mapper/src/lib/components/map/geolocation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
on:click={() => {
entitiesStore.setToggleGeolocation(!entitiesStore.toggleGeolocation);
if (!entitiesStore.toggleGeolocation) {
entitiesStore.setUserLocationCoordinate(undefined);
exitNavigationMode();
}
}}
Expand Down
2 changes: 2 additions & 0 deletions src/mapper/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface ProjectData {
hashtags: string[];
tasks: ProjectTask[];
new_geom_type: NewGeomTypes;
geo_restrict_distance_meters: number;
geo_restrict_force_error: boolean;
}

export interface ZoomToTaskEventDetail {
Expand Down
Loading