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

Commit

Permalink
#157-changed function for getting SRID from table
Browse files Browse the repository at this point in the history
  • Loading branch information
bariela committed Sep 23, 2022
1 parent ece6933 commit 4049a24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit 4049a24

Please sign in to comment.