This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from openkfw/140-geodata-route
#140-geodata route for fetching values from geodata tables in db
- Loading branch information
Showing
9 changed files
with
356 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import config from '../config/config'; | ||
import APIError from '../helpers/APIError'; | ||
|
||
import mongoDb from './mongoDb/models/geoDataModel'; | ||
import postgis from './postgis/models/geoDataModel'; | ||
|
||
export const getGeoData = async (tableName: string, bottomLeft: string, topRight: string, proj: string) => { | ||
if (config.postgresUser && config.postgresPassword && config.postgresDb) { | ||
return postgis.getGeoData(tableName, bottomLeft, topRight, proj); | ||
} | ||
if (config.mongoUri) { | ||
return mongoDb.getGeoData(tableName, bottomLeft, topRight); | ||
} | ||
throw new APIError('No credentials for database', 500, false, undefined); | ||
}; | ||
|
||
export const getProperty = async (tableName, propertyName) => { | ||
if (config.postgresUser && config.postgresPassword && config.postgresDb) { | ||
return postgis.getProperty(tableName, propertyName); | ||
} | ||
if (config.mongoUri) { | ||
return mongoDb.getProperty(tableName, propertyName); | ||
} | ||
}; | ||
|
||
export const getUniqueValuesForProperty = async (tableName, propertyName) => { | ||
if (config.postgresUser && config.postgresPassword && config.postgresDb) { | ||
return postgis.getUniqueValuesForProperty(tableName, propertyName); | ||
} | ||
if (config.mongoUri) { | ||
return mongoDb.getUniqueValuesForProperty(tableName, propertyName); | ||
} | ||
throw new APIError('No credentials for database', 500, false, undefined); | ||
}; | ||
|
||
export const getPropertySum = async (tableName, propertyName) => { | ||
if (config.postgresUser && config.postgresPassword && config.postgresDb) { | ||
return postgis.getPropertySum(tableName, propertyName); | ||
} | ||
if (config.mongoUri) { | ||
return mongoDb.getPropertySum(tableName, propertyName); | ||
} | ||
}; | ||
|
||
export default { getGeoData, getProperty, getUniqueValuesForProperty, getPropertySum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import mongoose from 'mongoose'; | ||
import { filterCoordinates } from '../filters'; | ||
|
||
const getGeoData = async (tableName: string, bottomLeft: string, topRight: string) => { | ||
const { connection } = mongoose; | ||
const { db } = connection; | ||
|
||
let filter = {}; | ||
if (bottomLeft && topRight) { | ||
filter = { bbox: filterCoordinates(filter, bottomLeft, topRight) }; | ||
} | ||
|
||
const features = await db | ||
.collection(tableName) | ||
.aggregate([ | ||
{ $match: filter }, | ||
{ | ||
$project: { | ||
_id: 0, | ||
id: '$_id', | ||
type: 1, | ||
properties: 1, | ||
geometry: 1, | ||
}, | ||
}, | ||
]) | ||
.toArray(); | ||
return features; | ||
}; | ||
|
||
const getProperty = async (tableName: string, propertyName: string) => { | ||
const { connection } = mongoose; | ||
const { db } = connection; | ||
|
||
const data = await db | ||
.collection(tableName) | ||
.aggregate([{ $project: { _id: 0, id: `$_id`, value: `$properties.${propertyName}` } }]) | ||
.toArray(); | ||
return data; | ||
}; | ||
|
||
const getUniqueValuesForProperty = async (tableName: string, propertyName: string) => { | ||
const { connection } = mongoose; | ||
const { db } = connection; | ||
|
||
const data = await db | ||
.collection(tableName) | ||
.aggregate([{ $group: { _id: `$properties.${propertyName}` } }]) | ||
.toArray(); | ||
return data.map((item) => item._id); | ||
}; | ||
|
||
const getPropertySum = async (tableName: string, propertyName: string) => { | ||
const { connection } = mongoose; | ||
const { db } = connection; | ||
|
||
const data = await db | ||
.collection(tableName) | ||
.aggregate([{ $group: { _id: null, value: { $sum: `$properties.${propertyName}` } } }]) | ||
.toArray(); | ||
return data; | ||
}; | ||
|
||
export default { getGeoData, getProperty, getUniqueValuesForProperty, getPropertySum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { getDb } from '../index'; | ||
import { getBoundingBox, getProjectionFilter } from '../filters'; | ||
import APIError from '../../../helpers/APIError'; | ||
|
||
const getGeoData = async (tableName: string, bottomLeft?: string, topRight?: string, proj?: string, db = getDb()) => { | ||
const filter: { | ||
geometry?: { | ||
type: string, | ||
coordinates: Array<Array<Array<number>>>, | ||
}, | ||
proj?: number, | ||
} = {}; | ||
if (bottomLeft && topRight) { | ||
filter.geometry = getBoundingBox(bottomLeft, topRight); | ||
filter.proj = await getProjectionFilter(proj, tableName); | ||
} | ||
|
||
const data = await db | ||
.from(tableName) | ||
.select('id', db.raw('ST_AsGeoJSON(geometry) as geometry'), 'properties') | ||
.where((qb) => { | ||
if (filter.geometry && filter.proj) { | ||
qb.andWhere( | ||
db.raw(`ST_Intersects(geometry, ST_SetSRID(ST_GeomFromGeoJSON(?), ${filter.proj}))`, [ | ||
JSON.stringify(filter.geometry), | ||
]), | ||
); | ||
} | ||
}); | ||
return data.map((item) => ({ ...item, geometry: JSON.parse(item.geometry) })); | ||
}; | ||
|
||
const getProperty = async (tableName: string, propertyName: string, db = getDb()) => { | ||
const data = await db.from(tableName).select('id', db.raw(`properties->>'${propertyName}' as value`)); | ||
return data; | ||
}; | ||
|
||
const getUniqueValuesForProperty = async (tableName: string, propertyName: string, db = getDb()) => { | ||
const data = await db | ||
.from(tableName) | ||
.select(db.raw(`properties->>'${propertyName}' as property`)) | ||
.distinct(db.raw(`properties->>'${propertyName}'`)); | ||
return data.map((item) => item.property); | ||
}; | ||
|
||
const getPropertySum = async (tableName: string, propertyName: string, db = getDb()) => { | ||
try { | ||
const data = await db.from(tableName).sum(db.raw(`properties->>'${propertyName}'`)); | ||
return data; | ||
} catch (error) { | ||
throw new APIError(`Attempting to sum non-numeric values for property ${propertyName}.`, 500, false, error); | ||
} | ||
}; | ||
|
||
export default { getGeoData, getProperty, getUniqueValuesForProperty, getPropertySum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.