Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #158 from openkfw/157-geodata-route
Browse files Browse the repository at this point in the history
157 geodata route
  • Loading branch information
srezacova authored Oct 11, 2022
2 parents 20044c5 + 1e1ccff commit 32dd16b
Show file tree
Hide file tree
Showing 27 changed files with 356 additions and 125 deletions.
22 changes: 12 additions & 10 deletions api/src/actions/isochrones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ export const getIsochronesByPointsSource = async (pointsSource: string, bottomLe
// const pointStrings = points.map((point) => JSON.stringify(point));
const isochrones = await getIsochronesByCoordinates(true);
const isochroneFeatures = [];
isochrones.forEach((item) => {
item.features.forEach((feature) => {
const enhancedFeature = {
type: feature.type,
geometry: feature.geometry,
properties: feature.properties,
id: `${item._id}-${feature.properties.value}`,
};
isochroneFeatures.push(enhancedFeature);
if (isochrones && isochrones.length) {
isochrones.forEach((item) => {
item.features.forEach((feature) => {
const enhancedFeature = {
type: feature.type,
geometry: feature.geometry,
properties: feature.properties,
id: `${item._id}-${feature.properties.value}`,
};
isochroneFeatures.push(enhancedFeature);
});
});
});
}
return isochroneFeatures;
}
throw new APIError(`pointsSource ${pointsSource} is not valid.`, 400, true);
Expand Down
30 changes: 16 additions & 14 deletions api/src/database/postgis/filters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getDb } from './index';
import APIError from '../../helpers/APIError';
import config from '../../config/config';
import logger from '../../config/winston';

/**
* Return bounding box in GeoJSON format used for filtering
Expand Down Expand Up @@ -35,23 +37,23 @@ export const getBoundingBox = (bottomLeft: string, topRight: string) => {
export const getProjectionFilter = async (proj, tableName, db = getDb()) => {
// projection in query must correspond to SRID in geometry column
if (proj) {
const SRIDarr = await db.distinct(db.raw(`ST_SRID(${tableName}.geometry)`)).from(tableName);
try {
const tableSRID = await db.select(db.raw(`Find_SRID('${config.postgresDb}', '${tableName}', 'geometry')`));

if (!SRIDarr.length) {
return;
}
const SRID = SRIDarr[0].st_srid;
const projNum = parseInt(proj.split(':')[1], 10);
const projNum = parseInt(proj.split(':')[1], 10);

if (SRID === projNum) {
return projNum;
if (tableSRID === projNum) {
return projNum;
}
throw new APIError(
`Projection SRID ${projNum} doesn't correspond to geometry column SRID ${tableSRID}`,
400,
true,
undefined,
);
} catch (err) {
logger.error(`Table ${tableName} is missing SRID`);
}
throw new APIError(
`Projection SRID ${projNum} doesn't correspond to geometry column SRID ${SRID}`,
400,
true,
undefined,
);
}
};

Expand Down
4 changes: 4 additions & 0 deletions api/src/database/postgis/models/geoDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const getGeoData = async (
JSON.stringify(filter.geometry),
]),
);
} else if (filter.geometry) {
qb.andWhere(
db.raw(`ST_Intersects(geometry, ST_SetSRID(ST_GeomFromGeoJSON(?), 0))`, [JSON.stringify(filter.geometry)]),
);
}
if (Object.keys(properties).length) {
Object.keys(properties).forEach((key) => {
Expand Down
4 changes: 2 additions & 2 deletions api/src/database/postgis/models/layerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const getMapLayersWithGeoData = async (db = getDb()): Promise<Array<MapLayerWith
.innerJoin(LAYER_GEO_DATA_TABLE, function () {
this.on(`${MAP_LAYERS_TABLE}.geo_reference_id`, '=', `${LAYER_GEO_DATA_TABLE}.reference_id`);
})
.innerJoin(ATTRIBUTES_TABLE, function () {
this.on(`${MAP_LAYERS_TABLE}.geo_reference_id`, '=', db.raw(`${ATTRIBUTES_TABLE}.geo_data->>'referenceId'`));
.leftJoin(ATTRIBUTES_TABLE, function () {
this.on(`${MAP_LAYERS_TABLE}.attribute_id`, '=', `${ATTRIBUTES_TABLE}.attribute_id`);
})
.distinct(`${MAP_LAYERS_TABLE}.reference_id`);

Expand Down
Loading

0 comments on commit 32dd16b

Please sign in to comment.