@@ -33,25 +33,52 @@ function executeQuery(query, callback) {
3333}
3434
3535function getById ( query , callback ) {
36- let getQuery = `SELECT ${ buildQueryColumns ( query ) } FROM features WHERE id = ${ escapeSql ( query . id ) } ` ;
36+ const ids = query . id . constructor === Array ? query . id : [ query . id ] ;
37+ const getQuery = `SELECT ${ buildQueryColumns ( query ) } FROM features WHERE id IN (${ escapeSql ( ids . join ( ',' ) ) } )` ;
3738 executeQuery ( getQuery , ( err , rows ) => {
3839 if ( err ) return callback ( err ) ;
3940 if ( ! rows || rows . length === 0 ) return callback ( null , null ) ;
4041
41- return callback ( null , rows [ 0 ] ) ;
42+ return callback ( null , rows ) ;
4243 } ) ;
4344}
4445
46+ function parseBoundingBox ( bbox_geo_json ) {
47+ const match = / ^ P O L Y G O N \( \( ( .* ) \) \) $ / . exec ( bbox_geo_json )
48+ if ( ! match || match . length !== 2 ) {
49+ return null ;
50+ }
51+
52+ const coords = match [ 1 ] . split ( ',' ) . map ( point => point . split ( ' ' ) ) ;
53+ if ( coords . length !== 5 || coords . some ( point => point . length !== 2 ) ) {
54+ return null ;
55+ }
56+
57+ const minX = parseFloat ( coords [ 0 ] [ 0 ] ) ;
58+ const minY = parseFloat ( coords [ 0 ] [ 1 ] ) ;
59+ const maxX = parseFloat ( coords [ 2 ] [ 0 ] ) ;
60+ const maxY = parseFloat ( coords [ 2 ] [ 1 ] ) ;
61+
62+ return [
63+ maxY , // north
64+ minX , // west
65+ minY , // south
66+ maxX // east
67+ ] ;
68+ }
69+
4570function rowToFeature ( row , columns ) {
4671 if ( ! row ) return ;
4772
4873 row [ 'createdAt' ] = row [ 'created_at' ] ;
4974 row [ 'updatedAt' ] = row [ 'updated_at' ] ;
5075 if ( row [ 'hull_geo_json' ] ) row [ 'hull' ] = JSON . parse ( row [ 'hull_geo_json' ] ) ;
76+ if ( row [ 'bbox_geo_json' ] ) row [ 'bbox' ] = parseBoundingBox ( row [ 'bbox_geo_json' ] ) ;
5177
5278 delete row [ 'created_at' ] ;
5379 delete row [ 'updated_at' ] ;
5480 delete row [ 'hull_geo_json' ] ;
81+ delete row [ 'bbox_geo_json' ] ;
5582
5683 return row ;
5784}
@@ -74,6 +101,7 @@ function buildQueryColumns(query) {
74101 if ( query . include ) {
75102 let includeArray = query . include . split ( ',' ) ;
76103
104+ if ( includeArray . indexOf ( 'bbox' ) !== - 1 ) queryColumns += ',ST_AsText(ST_Envelope(hull)) as bbox_geo_json' ;
77105 if ( includeArray . indexOf ( 'hull' ) !== - 1 ) queryColumns += ',ST_AsGeoJSON(hull) as hull_geo_json' ;
78106 if ( includeArray . indexOf ( 'properties' ) !== - 1 ) queryColumns += ',properties' ;
79107 }
0 commit comments