@@ -64,18 +64,33 @@ function parseBoundingBox(bbox_geo_json) {
6464 ] ;
6565}
6666
67+ function parseCentroid ( centroid_geo_json ) {
68+ const feature = JSON . parse ( centroid_geo_json ) ;
69+ const coords = feature . coordinates ;
70+ if ( feature . type !== 'Point' || coords . length !== 2 ) {
71+ return null ;
72+ }
73+
74+ const x = coords [ 0 ] ;
75+ const y = coords [ 1 ] ;
76+
77+ return [ x , y ] ;
78+ }
79+
6780function rowToFeature ( row , columns ) {
6881 if ( ! row ) return ;
6982
7083 row [ 'createdAt' ] = row [ 'created_at' ] ;
7184 row [ 'updatedAt' ] = row [ 'updated_at' ] ;
7285 if ( row [ 'hull_geo_json' ] ) row [ 'hull' ] = JSON . parse ( row [ 'hull_geo_json' ] ) ;
7386 if ( row [ 'bbox_geo_json' ] ) row [ 'bbox' ] = parseBoundingBox ( row [ 'bbox_geo_json' ] ) ;
87+ if ( row [ 'centroid_geo_json' ] ) row [ 'centroid' ] = parseCentroid ( row [ 'centroid_geo_json' ] ) ;
7488
7589 delete row [ 'created_at' ] ;
7690 delete row [ 'updated_at' ] ;
7791 delete row [ 'hull_geo_json' ] ;
7892 delete row [ 'bbox_geo_json' ] ;
93+ delete row [ 'centroid_geo_json' ] ;
7994
8095 return row ;
8196}
@@ -99,6 +114,7 @@ function buildQueryColumns(query) {
99114 let includeArray = query . include . split ( ',' ) ;
100115
101116 if ( includeArray . indexOf ( 'bbox' ) !== - 1 ) queryColumns += ',ST_AsGeoJSON(ST_Envelope(hull)) as bbox_geo_json' ;
117+ if ( includeArray . indexOf ( 'centroid' ) !== - 1 ) queryColumns += ',ST_AsGeoJSON(ST_Centroid(hull)) as centroid_geo_json' ;
102118 if ( includeArray . indexOf ( 'hull' ) !== - 1 ) queryColumns += ',ST_AsGeoJSON(hull) as hull_geo_json' ;
103119 if ( includeArray . indexOf ( 'properties' ) !== - 1 ) queryColumns += ',properties' ;
104120 }
0 commit comments