Skip to content

Commit fc33c06

Browse files
feat: query of polygon features on map
1 parent 026cd69 commit fc33c06

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

src/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
"feature": {
294294
"errors": {
295295
"invalidFields": "Please provide either geoField or latitudeField and longitudeField to the request parameters.",
296+
"missingPolygonGeoField": "Please provide a geoField when querying polygons.",
296297
"unexpected": "System failed to query data."
297298
}
298299
}

src/i18n/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
"feature": {
294294
"errors": {
295295
"invalidFields": "******",
296+
"missingPolygonGeoField": "******",
296297
"unexpected": "******"
297298
}
298299
}

src/models/layer.model.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ export interface LayerDefinition {
7777
drawingInfo?: DrawingInfo;
7878
}
7979

80+
/** Allowed geometry types */
81+
// eslint-disable-next-line @typescript-eslint/naming-convention
82+
export enum GeometryType {
83+
POINT = 'Point',
84+
POLYGON = 'Polygon',
85+
}
86+
8087
/**
8188
* Layer Datasource interface
8289
*/
@@ -88,6 +95,7 @@ export interface LayerDatasource {
8895
geoField?: string;
8996
latitudeField?: string;
9097
longitudeField?: string;
98+
type: GeometryType;
9199
}
92100

93101
/** Layer documents interface declaration */
@@ -149,6 +157,10 @@ const layerSchema = new Schema(
149157
geoField: String,
150158
latitudeField: String,
151159
longitudeField: String,
160+
type: {
161+
type: String,
162+
enum: Object.values(GeometryType),
163+
},
152164
},
153165
},
154166
{

src/routes/gis/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from 'express';
2-
import { Resource } from '@models';
2+
import { GeometryType, Resource } from '@models';
33
import { buildQuery } from '@utils/query/queryBuilder';
44
import config from 'config';
55
import i18next from 'i18next';
@@ -20,6 +20,7 @@ interface IFeatureQuery {
2020
maxLat?: number;
2121
minLng?: number;
2222
maxLng?: number;
23+
type: GeometryType;
2324
}
2425

2526
/**
@@ -63,6 +64,7 @@ const getFilterPolygon = (query: IFeatureQuery) => {
6364
* Get feature from item and add it to collection
6465
*
6566
* @param features collection of features
67+
* @param layerType layer type
6668
* @param item item to get feature from
6769
* @param mapping fields mapping, to build geoJson from
6870
* @param mapping.geoField geo field to extract geojson
@@ -72,6 +74,7 @@ const getFilterPolygon = (query: IFeatureQuery) => {
7274
*/
7375
const getFeatureFromItem = (
7476
features: any[],
77+
layerType: GeometryType,
7578
item: any,
7679
mapping: {
7780
geoField?: string;
@@ -91,7 +94,9 @@ const getFeatureFromItem = (
9194
...geo,
9295
properties: { ...item },
9396
};
94-
features.push(feature);
97+
// Only push if feature is of the same type as layer
98+
if (geo.type === 'Feature' && geo.geometry?.type === layerType)
99+
features.push(feature);
95100
} else {
96101
}
97102
}
@@ -125,7 +130,7 @@ const getFeatureFromItem = (
125130
*
126131
* @param req current http request
127132
* @param res http response
128-
* @returns GeoJSON feature collectionmutations
133+
* @returns GeoJSON feature collection mutations
129134
*/
130135
router.get('/feature', async (req, res) => {
131136
const featureCollection = {
@@ -135,6 +140,7 @@ router.get('/feature', async (req, res) => {
135140
const latitudeField = get(req, 'query.latitudeField');
136141
const longitudeField = get(req, 'query.longitudeField');
137142
const geoField = get(req, 'query.geoField');
143+
const layerType = get(req, 'query.type', GeometryType.POINT);
138144
// const tolerance = get(req, 'query.tolerance', 1);
139145
// const highQuality = get(req, 'query.highquality', true);
140146
// turf.simplify(geoJsonData, {
@@ -146,6 +152,14 @@ router.get('/feature', async (req, res) => {
146152
.status(400)
147153
.send(i18next.t('routes.gis.feature.errors.invalidFields'));
148154
}
155+
156+
// Polygons are only supported for geoField
157+
if (layerType === GeometryType.POLYGON && !geoField) {
158+
return res
159+
.status(400)
160+
.send(i18next.t('routes.gis.feature.errors.missingPolygonGeoField'));
161+
}
162+
149163
const mapping = {
150164
geoField,
151165
longitudeField,
@@ -231,14 +245,20 @@ router.get('/feature', async (req, res) => {
231245
}
232246
for (const field in data.data) {
233247
if (Object.prototype.hasOwnProperty.call(data.data, field)) {
234-
if (data.data[field].items && data.data[field].items.length > 0) {
248+
if (data.data[field].items?.length > 0) {
235249
data.data[field].items.map(async function (result) {
236-
getFeatureFromItem(featureCollection.features, result, mapping);
250+
getFeatureFromItem(
251+
featureCollection.features,
252+
layerType,
253+
result,
254+
mapping
255+
);
237256
});
238257
} else {
239258
data.data[field].edges.map(async function (result) {
240259
getFeatureFromItem(
241260
featureCollection.features,
261+
layerType,
242262
result.node,
243263
mapping
244264
);

src/schema/inputs/layer.input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const LayerDataSourceInputType = new GraphQLInputObjectType({
118118
geoField: { type: GraphQLString },
119119
latitudeField: { type: GraphQLString },
120120
longitudeField: { type: GraphQLString },
121+
type: { type: GraphQLString },
121122
}),
122123
});
123124

src/schema/types/layer.type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { Connection } from './pagination.type';
1313
import { LayerTypeEnum } from '@const/enumTypes';
1414
import GraphQLJSON from 'graphql-type-json';
15+
import { GeometryType } from '@models';
1516

1617
/**
1718
* GraphQL datasourceType type.
@@ -26,6 +27,10 @@ const LayerDatasource = new GraphQLObjectType({
2627
geoField: { type: GraphQLString },
2728
latitudeField: { type: GraphQLString },
2829
longitudeField: { type: GraphQLString },
30+
type: {
31+
type: GraphQLNonNull(GraphQLString),
32+
resolve: (parent) => parent.type ?? GeometryType.POINT,
33+
},
2934
}),
3035
});
3136

0 commit comments

Comments
 (0)