Skip to content

Commit 5ddde77

Browse files
Add GeometryType enum
1 parent 4902649 commit 5ddde77

6 files changed

Lines changed: 58 additions & 43 deletions

File tree

src/client/javascripts/geospatial-map.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getCentroidGridRef,
88
getCoordinateGridRef
99
} from '~/src/client/javascripts/map.js'
10+
import { GeometryType } from '~/src/server/plugins/engine/types.js'
1011

1112
const helpPanelConfig = {
1213
showLabel: true,
@@ -45,7 +46,7 @@ const polygonFeatureProperties = {
4546
}
4647

4748
/**
48-
* @type {Record<'Point' | 'LineString' | 'Polygon', string>}
49+
* @type {Record<GeometryType.Point | GeometryType.LineString | GeometryType.Polygon, string>}
4950
*/
5051
const typeDescriptions = {
5152
Point: 'Point',
@@ -139,13 +140,13 @@ export function processGeospatial(config, geospatial, index) {
139140
*/
140141
export function addFeatureToMap(feature, drawPlugin, map) {
141142
switch (feature.geometry.type) {
142-
case 'Polygon':
143+
case GeometryType.Polygon:
143144
drawPlugin.addFeature({ ...feature, ...polygonFeatureProperties })
144145
break
145-
case 'LineString':
146+
case GeometryType.LineString:
146147
drawPlugin.addFeature({ ...feature, ...lineFeatureProperties })
147148
break
148-
case 'Point':
149+
case GeometryType.Point:
149150
map.addMarker(feature.id, feature.geometry.coordinates)
150151
break
151152
default:
@@ -319,9 +320,9 @@ function prepareGeometry(feature) {
319320
coordinates[1] = +coordinates[1].toFixed(maxPrecision)
320321
}
321322

322-
if (geometry.type === 'Point') {
323+
if (geometry.type === GeometryType.Point) {
323324
formatPrecision(geometry.coordinates)
324-
} else if (geometry.type === 'LineString') {
325+
} else if (geometry.type === GeometryType.LineString) {
325326
geometry.coordinates.forEach(formatPrecision)
326327
} else {
327328
geometry.coordinates.flat().forEach(formatPrecision)
@@ -713,7 +714,7 @@ function onInteractMarkerChangedFactory(context) {
713714
if (activeFeatureId) {
714715
// Editing an existing point
715716
const feature = updateFeature(activeFeatureId, {
716-
type: 'Point',
717+
type: GeometryType.Point,
717718
coordinates: e.coords
718719
})
719720

@@ -734,7 +735,7 @@ function onInteractMarkerChangedFactory(context) {
734735
description: ''
735736
},
736737
geometry: {
737-
type: 'Point',
738+
type: GeometryType.Point,
738739
coordinates: e.coords
739740
},
740741
id
@@ -775,10 +776,10 @@ function onListElClickFactory(context) {
775776
/**
776777
* Delete a feature
777778
* @param {string} id - the feature id
778-
* @param {string} type - the feature type
779+
* @param {GeometryType} type - the feature type
779780
*/
780781
function deleteFeature(id, type) {
781-
if (type === 'Point') {
782+
if (type === GeometryType.Point) {
782783
map.removeMarker(id)
783784
removeFeature(id)
784785
} else {
@@ -792,13 +793,13 @@ function onListElClickFactory(context) {
792793
/**
793794
* Start editing feature
794795
* @param {string} id - the feature id
795-
* @param {string} type - the feature type
796+
* @param {GeometryType} type - the feature type
796797
*/
797798
function editFeature(id, type) {
798799
setActiveFeature(id)
799800

800801
// "Change" feature link was clicked
801-
if (type === 'Point') {
802+
if (type === GeometryType.Point) {
802803
interactPlugin.selectFeature({ featureId: id })
803804
interactPlugin.enable()
804805
} else {
@@ -840,14 +841,14 @@ function onListElClickFactory(context) {
840841

841842
if (action === 'edit') {
842843
// "Update" feature link was clicked
843-
editFeature(id, type)
844+
editFeature(id, /** @type {GeometryType} */ (type))
844845
} else {
845846
e.preventDefault()
846847
e.stopPropagation()
847848

848849
if (action === 'delete') {
849850
// "Remove" feature link was clicked
850-
deleteFeature(id, type)
851+
deleteFeature(id, /** @type {GeometryType} */ (type))
851852
}
852853
}
853854
}

src/client/javascripts/map.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import OsGridRef, { LatLon } from 'geodesy/osgridref.js'
44

55
import { processGeospatial } from '~/src/client/javascripts/geospatial-map.js'
66
import { processLocation } from '~/src/client/javascripts/location-map.js'
7+
import { GeometryType } from '~/src/server/plugins/engine/types.js'
78

89
// Center of UK
910
const DEFAULT_LAT = 53.825564
@@ -73,12 +74,12 @@ export function osGridRefToLatLong(osGridRef) {
7374
* @param {Feature} feature
7475
*/
7576
export function getCoordinateGridRef(feature) {
76-
if (feature.geometry.type === 'Point') {
77+
if (feature.geometry.type === GeometryType.Point) {
7778
const [long, lat] = feature.geometry.coordinates
7879
const point = new LatLon(lat, long)
7980

8081
return point.toOsGrid().toString()
81-
} else if (feature.geometry.type === 'LineString') {
82+
} else if (feature.geometry.type === GeometryType.LineString) {
8283
const [long, lat] = feature.geometry.coordinates[0]
8384
const point = new LatLon(lat, long)
8485

@@ -96,7 +97,7 @@ export function getCoordinateGridRef(feature) {
9697
* @param {Feature} feature
9798
*/
9899
export function getCentroidGridRef(feature) {
99-
if (feature.geometry.type === 'Point') {
100+
if (feature.geometry.type === GeometryType.Point) {
100101
const [long, lat] = feature.geometry.coordinates
101102
const point = new LatLon(lat, long)
102103

src/server/plugins/engine/components/helpers/__stubs__/geospatial.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { type GeospatialState } from '~/src/server/plugins/engine/types.js'
1+
import {
2+
GeometryType,
3+
type GeospatialState
4+
} from '~/src/server/plugins/engine/types.js'
25

36
export const validState: GeospatialState = [
47
{
@@ -10,7 +13,7 @@ export const validState: GeospatialState = [
1013
},
1114
geometry: {
1215
coordinates: [-2.5723699109417737, 53.2380485215034],
13-
type: 'Point'
16+
type: GeometryType.Point
1417
},
1518
id: 'a'
1619
},
@@ -26,7 +29,7 @@ export const validState: GeospatialState = [
2629
[-2.570496516462896, 53.239162468888566],
2730
[-2.5722447488110447, 53.238174174285746]
2831
],
29-
type: 'LineString'
32+
type: GeometryType.LineString
3033
},
3134
id: 'b'
3235
},
@@ -49,7 +52,7 @@ export const validState: GeospatialState = [
4952
[-2.573552894955583, 53.238229751360706]
5053
]
5154
],
52-
type: 'Polygon'
55+
type: GeometryType.Polygon
5356
},
5457
id: 'c'
5558
},
@@ -62,7 +65,7 @@ export const validState: GeospatialState = [
6265
},
6366
geometry: {
6467
coordinates: [-2.5724, 53.239],
65-
type: 'Point'
68+
type: GeometryType.Point
6669
},
6770
id: 'd'
6871
}
@@ -78,7 +81,7 @@ export const validSingleState: GeospatialState = [
7881
},
7982
geometry: {
8083
coordinates: [-2.5723699109417737, 53.2380485215034],
81-
type: 'Point'
84+
type: GeometryType.Point
8285
},
8386
id: 'a'
8487
}

src/server/plugins/engine/components/helpers/geospatial.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Bourne from '@hapi/bourne'
22
import JoiBase from 'joi'
33

44
import {
5+
GeometryType,
56
type Coordinates,
67
type Feature,
78
type FeatureProperties,
@@ -58,17 +59,19 @@ const featurePropertiesSchema = Joi.object<FeatureProperties>()
5859
.required()
5960

6061
const featureGeometrySchema = Joi.object<Geometry>().keys({
61-
type: Joi.string().valid('Point', 'LineString', 'Polygon').required(),
62+
type: Joi.string()
63+
.valid(GeometryType.Point, GeometryType.LineString, GeometryType.Polygon)
64+
.required(),
6265
coordinates: Joi.array()
6366
.when('type', {
6467
switch: [
65-
{ is: 'Point', then: coordinatesSchema },
68+
{ is: GeometryType.Point, then: coordinatesSchema },
6669
{
67-
is: 'LineString',
70+
is: GeometryType.LineString,
6871
then: Joi.array().items(coordinatesSchema).min(2)
6972
},
7073
{
71-
is: 'Polygon',
74+
is: GeometryType.Polygon,
7275
then: Joi.array().items(Joi.array().items(coordinatesSchema).min(3))
7376
}
7477
]

src/server/plugins/engine/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ export interface RepeatItemState extends FormPayload {
288288

289289
export type RepeatListState = RepeatItemState[]
290290

291+
export enum GeometryType {
292+
Point = 'Point',
293+
LineString = 'LineString',
294+
Polygon = 'Polygon'
295+
}
296+
291297
/**
292298
* A longitude/latitude coordinate pair in WGS84 format
293299
* Format: [longitude, latitude]
@@ -298,23 +304,23 @@ export type Coordinates = [longitude: number, latitude: number]
298304
* GeoJSON Point geometry
299305
*/
300306
export interface PointGeometry {
301-
type: 'Point'
307+
type: GeometryType.Point
302308
coordinates: Coordinates
303309
}
304310

305311
/**
306312
* GeoJSON LineString geometry
307313
*/
308314
export interface LineStringGeometry {
309-
type: 'LineString'
315+
type: GeometryType.LineString
310316
coordinates: Coordinates[]
311317
}
312318

313319
/**
314320
* GeoJSON Polygon geometry
315321
*/
316322
export interface PolygonGeometry {
317-
type: 'Polygon'
323+
type: GeometryType.Polygon
318324
coordinates: Coordinates[][]
319325
}
320326

test/client/javascripts/map.test.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
initMaps,
66
makeTileRequestTransformer
77
} from '~/src/client/javascripts/map.js'
8+
import { GeometryType } from '~/src/server/plugins/engine/types.js'
89

910
describe('Maps Client JS', () => {
1011
/** @type {jest.Mock} */
@@ -605,7 +606,7 @@ describe('Maps Client JS', () => {
605606
centroidGridReference: 'TQ 29031 79662'
606607
},
607608
geometry: {
608-
type: 'Point',
609+
type: GeometryType.Point,
609610
coordinates: [-0.14242385752663722, 51.50118200993498]
610611
},
611612
id: '6d67810c-7228-4f71-b6ec-0d16b132fcd7'
@@ -634,7 +635,7 @@ describe('Maps Client JS', () => {
634635
[-0.13995851581486818, 51.50204381014825]
635636
]
636637
],
637-
type: 'Polygon'
638+
type: GeometryType.Polygon
638639
}
639640
},
640641
{
@@ -650,7 +651,7 @@ describe('Maps Client JS', () => {
650651
[-0.14969813781837615, 51.502534952613814],
651652
[-0.1404050934734471, 51.50217984872572]
652653
],
653-
type: 'LineString'
654+
type: GeometryType.LineString
654655
}
655656
}
656657
]
@@ -869,7 +870,7 @@ describe('Maps Client JS', () => {
869870
id: 'guid',
870871
type: 'Feature',
871872
geometry: {
872-
type: 'Polygon',
873+
type: GeometryType.Polygon,
873874
coordinates: [
874875
[
875876
[-2.0868919921875886, 53.896834237148596],
@@ -907,7 +908,7 @@ describe('Maps Client JS', () => {
907908
id: 'guid1',
908909
type: 'Feature',
909910
geometry: {
910-
type: 'LineString',
911+
type: GeometryType.LineString,
911912
coordinates: [
912913
[-1.3068626953125317, 54.019651598965936],
913914
[-2.0868919921875886, 53.896834237148596]
@@ -968,7 +969,7 @@ describe('Maps Client JS', () => {
968969
const updatedPolygonFeature = {
969970
...features[1],
970971
geometry: {
971-
type: 'Polygon',
972+
type: GeometryType.Polygon,
972973
coordinates: [
973974
[
974975
[0, 0],
@@ -1054,7 +1055,7 @@ describe('Maps Client JS', () => {
10541055
...features,
10551056
{
10561057
geometry: {
1057-
type: 'Point',
1058+
type: GeometryType.Point,
10581059
coordinates: [-2.086892, 53.8968342]
10591060
},
10601061
properties: {
@@ -1357,7 +1358,7 @@ describe('Maps Client JS', () => {
13571358
id: 'id',
13581359
type: 'Feature',
13591360
geometry: {
1360-
type: 'Point',
1361+
type: GeometryType.Point,
13611362
coordinates: [-0.1356213, 51.5121161]
13621363
},
13631364
properties: {
@@ -1373,7 +1374,7 @@ describe('Maps Client JS', () => {
13731374
id: 'id',
13741375
type: 'Feature',
13751376
geometry: {
1376-
type: 'LineString',
1377+
type: GeometryType.LineString,
13771378
coordinates: [
13781379
[-0.1356213, 51.5121161],
13791380
[-0.0578579, 51.5182996]
@@ -1392,7 +1393,7 @@ describe('Maps Client JS', () => {
13921393
id: 'id',
13931394
type: 'Feature',
13941395
geometry: {
1395-
type: 'Polygon',
1396+
type: GeometryType.Polygon,
13961397
coordinates: [
13971398
[
13981399
[-0.1356213, 51.5121161],
@@ -1415,7 +1416,7 @@ describe('Maps Client JS', () => {
14151416
id: 'id',
14161417
type: 'Feature',
14171418
geometry: {
1418-
type: 'Point',
1419+
type: GeometryType.Point,
14191420
coordinates: [-0.1356213, 51.5121161]
14201421
},
14211422
properties: {
@@ -1431,7 +1432,7 @@ describe('Maps Client JS', () => {
14311432
id: 'id',
14321433
type: 'Feature',
14331434
geometry: {
1434-
type: 'LineString',
1435+
type: GeometryType.LineString,
14351436
coordinates: [
14361437
[-0.1356213, 51.5121161],
14371438
[-0.0578579, 51.5182996]
@@ -1450,7 +1451,7 @@ describe('Maps Client JS', () => {
14501451
id: 'id',
14511452
type: 'Feature',
14521453
geometry: {
1453-
type: 'Polygon',
1454+
type: GeometryType.Polygon,
14541455
coordinates: [
14551456
[
14561457
[-0.1356213, 51.5121161],

0 commit comments

Comments
 (0)