From e0e2d7598aea8558b88649153b8ef25f0c94769a Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Thu, 29 Feb 2024 20:11:47 -0500 Subject: [PATCH] use [lon, lat] for pointIsWithinBounds I thought coordinates would be given with latitude first. But the geojson spec is to have longitude first https://www.rfc-editor.org/rfc/rfc7946#section-3.1.1 Flipped the logic for this function and added a bit of JSDoc just so it's clear --- www/js/survey/enketo/conditionalSurveys.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/www/js/survey/enketo/conditionalSurveys.ts b/www/js/survey/enketo/conditionalSurveys.ts index 255b5821c..63f9a9b83 100644 --- a/www/js/survey/enketo/conditionalSurveys.ts +++ b/www/js/survey/enketo/conditionalSurveys.ts @@ -1,14 +1,21 @@ import { displayError } from '../../plugin/logger'; import { SurveyButtonConfig } from '../../types/appConfigTypes'; import { TimelineEntry } from '../../types/diaryTypes'; +import { Position } from 'geojson'; const conditionalSurveyFunctions = { - pointIsWithinBounds: (pt: [number, number], bounds: [[number, number], [number, number]]) => { - // pt is [lat, lon] and bounds is NW and SE corners as [[lat, lon], [lat, lon]] - // pt's lat must be south of, or less than, NW's lat; and north of, or greater than, SE's lat + /** + @description Returns true if the given point is within the given bounds. + Coordinates are in [longitude, latitude] order, since that is the GeoJSON spec. + @param pt point to check as [lon, lat] + @param bounds NW and SE corners as [[lon, lat], [lon, lat]] + @returns true if pt is within bounds + */ + pointIsWithinBounds: (pt: Position, bounds: Position[]) => { // pt's lon must be east of, or greater than, NW's lon; and west of, or less than, SE's lon - const latInRange = pt[0] < bounds[0][0] && pt[0] > bounds[1][0]; - const lonInRange = pt[1] > bounds[0][1] && pt[1] < bounds[1][1]; + const lonInRange = pt[0] > bounds[0][0] && pt[0] < bounds[1][0]; + // pt's lat must be south of, or less than, NW's lat; and north of, or greater than, SE's lat + const latInRange = pt[1] < bounds[0][1] && pt[1] > bounds[1][1]; return latInRange && lonInRange; }, };