25
25
* Modified by: Jason Fox - FIWARE Foundation
26
26
*/
27
27
28
+ /* eslint-disable consistent-return */
29
+
28
30
const request = require ( 'request' ) ;
29
31
const statsService = require ( './../stats/statsRegistry' ) ;
30
32
const async = require ( 'async' ) ;
@@ -35,11 +37,59 @@ const utils = require('../northBound/restUtils');
35
37
const config = require ( '../../commonConfig' ) ;
36
38
const constants = require ( '../../constants' ) ;
37
39
const moment = require ( 'moment-timezone' ) ;
40
+ const NGSIUtils = require ( './ngsiUtils' ) ;
38
41
const logger = require ( 'logops' ) ;
39
42
const context = {
40
43
op : 'IoTAgentNGSI.Entities-v2'
41
44
} ;
42
- const NGSIUtils = require ( './ngsiUtils' ) ;
45
+
46
+ /**
47
+ * Amends an NGSIv2 Geoattribute from String to GeoJSON format
48
+ *
49
+ * @param {Object } attr Attribute to be analyzed
50
+ * @return {Object } GeoJSON version of the attribute
51
+ */
52
+ function formatGeoAttrs ( attr ) {
53
+ const obj = attr ;
54
+ if ( attr . type ) {
55
+ switch ( attr . type . toLowerCase ( ) ) {
56
+ // GeoProperties
57
+ case 'geo:json' :
58
+ case 'geoproperty' :
59
+ case 'point' :
60
+ case 'geo:point' :
61
+ obj . type = 'geo:json' ;
62
+ obj . value = NGSIUtils . getLngLats ( 'Point' , attr . value ) ;
63
+ break ;
64
+ case 'linestring' :
65
+ case 'geo:linestring' :
66
+ obj . type = 'geo:json' ;
67
+ obj . value = NGSIUtils . getLngLats ( 'LineString' , attr . value ) ;
68
+ break ;
69
+ case 'polygon' :
70
+ case 'geo:polygon' :
71
+ obj . type = 'geo:json' ;
72
+ obj . value = NGSIUtils . getLngLats ( 'Polygon' , attr . value ) ;
73
+ break ;
74
+ case 'multipoint' :
75
+ case 'geo:multipoint' :
76
+ obj . type = 'geo:json' ;
77
+ obj . value = NGSIUtils . getLngLats ( 'MultiPoint' , attr . value ) ;
78
+ break ;
79
+ case 'multilinestring' :
80
+ case 'geo:multilinestring' :
81
+ obj . type = 'geo:json' ;
82
+ obj . value = NGSIUtils . getLngLats ( 'MultiLineString' , attr . value ) ;
83
+ break ;
84
+ case 'multipolygon' :
85
+ case 'geo:multipolygon' :
86
+ obj . type = 'geo:json' ;
87
+ obj . value = NGSIUtils . getLngLats ( 'MultiPolygon' , attr . value ) ;
88
+ break ;
89
+ }
90
+ }
91
+ return obj ;
92
+ }
43
93
44
94
function addTimestampNgsi2 ( payload , timezone ) {
45
95
function addTimestampEntity ( entity , timezone ) {
@@ -384,6 +434,12 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
384
434
if ( options . json . entities [ entity ] [ att ] . multi ) {
385
435
delete options . json . entities [ entity ] [ att ] . multi ;
386
436
}
437
+ try {
438
+ // Format any GeoJSON attrs properly
439
+ options . json . entities [ entity ] [ att ] = formatGeoAttrs ( options . json . entities [ entity ] [ att ] ) ;
440
+ } catch ( error ) {
441
+ return callback ( new errors . BadGeocoordinates ( JSON . stringify ( options . json ) ) ) ;
442
+ }
387
443
}
388
444
}
389
445
} else {
@@ -396,6 +452,13 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
396
452
if ( options . json [ att ] . multi ) {
397
453
delete options . json [ att ] . multi ;
398
454
}
455
+
456
+ try {
457
+ // Format any GeoJSON attrs properly
458
+ options . json [ att ] = formatGeoAttrs ( options . json [ att ] ) ;
459
+ } catch ( error ) {
460
+ return callback ( new errors . BadGeocoordinates ( JSON . stringify ( options . json ) ) ) ;
461
+ }
399
462
}
400
463
}
401
464
@@ -418,3 +481,4 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
418
481
exports . sendQueryValue = sendQueryValueNgsi2 ;
419
482
exports . sendUpdateValue = sendUpdateValueNgsi2 ;
420
483
exports . addTimestamp = addTimestampNgsi2 ;
484
+ exports . formatGeoAttrs = formatGeoAttrs ;
0 commit comments