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

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.

37 changes: 37 additions & 0 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 { trusted } from 'svelte/legacy';

type Props = {
isTaskActionModalOpen: boolean;
Expand All @@ -25,7 +28,41 @@
const selectedEntityCoordinate = $derived(entitiesStore.selectedEntityCoordinate);
const entityToNavigate = $derived(entitiesStore.entityToNavigate);

// Check if drawn geometry is within set distance constraints from user
const isDistanceConstaintValid = (): boolean => {
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 a distance constraint set. Please enable device geolocation for optimal functionality',
variant: 'warning',
});
return false;
}

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 false;
}
}

// Valid coord
return true;
};

const mapFeature = () => {
if (!isDistanceConstaintValid()) return;

const xformId = projectData?.odk_form_id;
const entityUuid = selectedEntity?.entity_id;

Expand Down
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