11import express from 'express' ;
2- import { Resource } from '@models' ;
2+ import { GeometryType , Resource } from '@models' ;
33import { buildQuery } from '@utils/query/queryBuilder' ;
44import config from 'config' ;
55import 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 */
7375const 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 */
130135router . 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 ) ;
0 commit comments