Skip to content

Commit

Permalink
use [lon, lat] for pointIsWithinBounds
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JGreenlee committed Mar 1, 2024
1 parent 82df443 commit e0e2d75
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions www/js/survey/enketo/conditionalSurveys.ts
Original file line number Diff line number Diff line change
@@ -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;
},
};
Expand Down

0 comments on commit e0e2d75

Please sign in to comment.