-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mapper): add new point geom in maplibre terradraw, inject to ODK…
… collect (#1966)
- Loading branch information
1 parent
aacabbc
commit 6fdbf96
Showing
9 changed files
with
195 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Geometry as GeoJSONGeometry } from 'geojson'; | ||
|
||
import { getAlertStore } from '$store/common.svelte.ts'; | ||
import { geojsonGeomToJavarosa } from '$lib/odk/javarosa'; | ||
|
||
const alertStore = getAlertStore(); | ||
|
||
export function openOdkCollectNewFeature(xFormId: string, geom: GeoJSONGeometry) { | ||
if (!xFormId || !geom) { | ||
return; | ||
} | ||
|
||
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); | ||
|
||
const javarosaGeom = geojsonGeomToJavarosa(geom); | ||
|
||
if (isMobile) { | ||
// TODO we need to update the form to support task_id=${}& | ||
document.location.href = `odkcollect://form/${xFormId}?new_feature=${javarosaGeom}`; | ||
} else { | ||
alertStore.setAlert({ | ||
variant: 'warning', | ||
message: 'Requires a mobile phone with ODK Collect.', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Geometry as GeoJSONGeometry } from 'geojson'; | ||
|
||
export function geojsonGeomToJavarosa(geometry: GeoJSONGeometry) { | ||
if (geometry.type === 'GeometryCollection') { | ||
console.error('Unsupported GeoJSON type: GeometryCollection'); | ||
return; | ||
} | ||
|
||
if (!geometry || !geometry.type || !geometry.coordinates) { | ||
throw new Error('Invalid GeoJSON feature: Missing geometry or coordinates.'); | ||
} | ||
|
||
// Normalize geometries into a common structure for processing | ||
let coordinates: any[] = []; | ||
switch (geometry.type) { | ||
case 'Point': | ||
coordinates = [[geometry.coordinates]]; // [[x, y]] | ||
break; | ||
case 'LineString': | ||
case 'MultiPoint': | ||
coordinates = [geometry.coordinates]; // [[x, y], [x, y]] | ||
break; | ||
case 'Polygon': | ||
case 'MultiLineString': | ||
coordinates = geometry.coordinates; // [[[x, y], [x, y]]] | ||
break; | ||
case 'MultiPolygon': | ||
coordinates = geometry.coordinates.flat(); // Flatten [[[...]], [[...]]] | ||
break; | ||
default: | ||
throw new Error(`Unsupported GeoJSON geometry type: ${geometry}`); | ||
} | ||
|
||
// Convert to JavaRosa format | ||
const javarosaGeometry = coordinates | ||
.flatMap((polygonOrLine) => | ||
polygonOrLine.map(([longitude, latitude]: [number, number]) => `${latitude} ${longitude} 0.0 0.0`), | ||
) | ||
.join(';'); | ||
|
||
// Must append a final ; to finish the geom | ||
return `${javarosaGeometry};`; | ||
} |
File renamed without changes.
Oops, something went wrong.