Skip to content

Commit

Permalink
Merge pull request #326 from Greenstand/nearest
Browse files Browse the repository at this point in the history
nearest
  • Loading branch information
dadiorchen authored Jun 3, 2023
2 parents ebe4430 + b4cb0b6 commit afde3db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 13 additions & 7 deletions server/infra/database/GisRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,32 @@ export default class GisRepository {
trees
${
params.wallet_id
? 'join wallet.token on trees.uuid = wallet.token.capture_id::text'
? 'join wallet.token on trees.uuid = wallet.token.capture_id::text join wallet.wallet on wallet.token.wallet_id = wallet.wallet.id '
: ''
}
${
params.organization_id
params.organization_id || params.map_name
? 'join planter on trees.planter_id = planter.id'
: ''
}
WHERE
active = true
${
params.wallet_id ? `and wallet.token.wallet_id = '${params.wallet_id}'` : ''
params.wallet_id
? `and (wallet.token.wallet_id::text = '${params.wallet_id}' or wallet.wallet.name = '${params.wallet_id}')`
: ''
}
${params.planter_id ? `and planter_id = ${params.planter_id}` : ''}
${
params.organization_id
? `and planter.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${params.organization_id}))`
: ''
}
${
params.map_name
? `and planter.organization_id in ( SELECT entity_id from getEntityRelationshipChildren((select id from entity where map_name = '${params.map_name}')))`
: ''
}
and estimated_geometric_location && (select geom from box)
ORDER BY
estimated_geometric_location <->
Expand All @@ -80,9 +87,8 @@ export default class GisRepository {
console.log('query:', sql);
const result = await this.session.getDB().raw(sql);
// {"st_asgeojson":"{\"type\":\"Point\",\"coordinates\":[39.1089215842116,-5.12839483715479]}"}
return {
nearest:
result.rows.length > 0 ? JSON.parse(result.rows[0].st_asgeojson) : null,
};
return result.rows.length > 0
? JSON.parse(result.rows[0].st_asgeojson)
: null;
}
}
3 changes: 2 additions & 1 deletion server/routers/gisRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ router.get(
zoom_level: Joi.number().integer().min(0).required(),
lat: Joi.number().min(-90).max(90).required(),
lng: Joi.number().min(-180).max(180).required(),
wallet_id: Joi.string().uuid().optional(),
wallet_id: Joi.string().optional(),
planter_id: Joi.number().integer().optional(),
organization_id: Joi.number().integer().optional(),
map_name: Joi.string().optional(),
}),
);
const repo = new GisRepository(new Session());
Expand Down

0 comments on commit afde3db

Please sign in to comment.