Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Select trees in drawing polygon #369

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"mock-server": "prism mock ./docs/api/spec/query-api.yaml",
"prepare": "husky install",
"server-test": "DEBUG=express:* NODE_LOG_LEVEL=debug nodemon server/serverTest.js",
"start": "NODE_TLS_REJECT_UNAUTHORIZED='0' NODE_PATH=dist/ node dist/server.js",
"start:dev": "NODE_PATH=server/ ts-node -r dotenv/config --project tsconfig.build.json server/server.ts",
"start": "cross-env NODE_TLS_REJECT_UNAUTHORIZED='0' NODE_PATH=dist/ node dist/server.js",
"start:dev": "cross-env NODE_PATH=server/ ts-node -r dotenv/config --project tsconfig.build.json server/server.ts",
"test": "npm run test-unit; npm run test-integration;npm run test-repository",
"test-e2e": "jest ./__tests__/e2e/",
"test-integration": "NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --exit --timeout 20000 './__tests__/supertest.js'",
"test-integration": "cross-env NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --exit --timeout 20000 './__tests__/supertest.js'",
"test-repository": "jest ./server/infra/database/",
"test-seedDB": "NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 './**/*.spec.js'",
"test-unit": "jest ./server/models/",
Expand All @@ -44,6 +44,7 @@
"@sentry/node": "^5.1.0",
"body-parser": "^1.18.2",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"expect-runtime": "^0.7.0",
"express": "^4.16.2",
Expand Down
21 changes: 21 additions & 0 deletions server/infra/database/GisRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bounds from 'interfaces/Bounds';

Check warning on line 1 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

'Bounds' is defined but never used
import HttpError from 'utils/HttpError';

Check warning on line 2 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

'HttpError' is defined but never used
import Session from './Session';

export default class GisRepository {
Expand All @@ -14,7 +14,7 @@
zoom_level = parseInt(zoom_level);
lng = parseFloat(lng);
lat = parseFloat(lat);
console.log('lng:', lng);

Check warning on line 17 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

Unexpected console statement
let sql;
if (zoom_level <= 11) {
sql = `
Expand Down Expand Up @@ -86,11 +86,32 @@
LIMIT 1;
`;
}
console.log('query:', sql);

Check warning on line 89 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

Unexpected console statement
const result = await this.session.getDB().raw(sql);
// {"st_asgeojson":"{\"type\":\"Point\",\"coordinates\":[39.1089215842116,-5.12839483715479]}"}
return result.rows.length > 0
? JSON.parse(result.rows[0].st_asgeojson)
: null;
}

async getPointsInsidePolygon(params): Promise<unknown> {
const { polygon } = params;
let polygonPoints = `${polygon[0].lon} ${polygon[0].lat}`;
for (let i = 1; i < polygon.length; i++) {
polygonPoints += `, ${polygon[i].lon} ${polygon[i].lat}`;
}
const sql = `
SELECT id, time_created,planter_id,image_url,lat,lon,active,verified,uuid,approved,species_id,token_issued,token_id
FROM trees
WHERE ST_Contains(ST_MakePolygon(ST_GeomFromText('LINESTRING(${polygonPoints})')), ST_MakePoint(lon,lat))
`;
// using spatial indexes
// const sql = `
// SELECT id, time_created,planter_id,image_url,lat,lon,active,verified,uuid,approved,species_id,token_issued,token_id
// FROM trees
// WHERE ST_Contains(ST_MakePolygon(ST_GeomFromText('LINESTRING(${polygonPoints})',4326)), estimated_geometric_location)
// `;
const result = await this.session.getDB().raw(sql);
return result.rows;
}
}
3 changes: 3 additions & 0 deletions server/models/Gis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import GisRepository from '../infra/database/GisRepository';

export default {
getNearest: delegateRepository<GisRepository, unknown>('getNearest'),
getPointsInsidePolygon: delegateRepository<GisRepository, unknown>(
'getPointsInsidePolygon',
),
};
27 changes: 26 additions & 1 deletion server/routers/gisRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,30 @@ router.get(
res.end();
}),
);

router.post(
'/',
handlerWrapper(async (req: Request, res: Response) => {
// console.log('Reached');
Joi.assert(
req.body,
Joi.object().keys({
polygon: Joi.array()
.items(
Joi.object({
lat: Joi.number().required(),
lon: Joi.number().required(),
}),
)
.min(4)
.required(),
}),
);
const repo = new GisRepository(new Session());
const result = await GisModel.getPointsInsidePolygon(repo)(req.body);
res.send({
trees: result,
});
res.end();
}),
);
export default router;
Loading