Skip to content

Commit

Permalink
create endpoint for node lookup by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wessel committed Jan 16, 2024
1 parent cba954c commit 785770d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
31 changes: 31 additions & 0 deletions backend/app/get_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
from app.db import getConnection

sql = '''
SELECT
cg_nodes.node_id::int,
ST_AsGeoJSON(cg_nodes.geom) AS geom,
array_agg(DISTINCT InitCap(streets.st_name)) FILTER (WHERE streets.st_name IS NOT NULL) AS street_names
FROM congestion.network_nodes AS cg_nodes
JOIN here.routing_nodes_21_1 AS here_nodes USING (node_id)
JOIN here_gis.streets_att_21_1 AS streets USING (link_id)
WHERE node_id = %(node_id)s
GROUP BY
cg_nodes.node_id,
cg_nodes.geom;
'''

# TODO code could use some tidying up
def get_node(node_id):
with getConnection() as connection:
with connection.cursor() as cursor:
cursor.execute(sql, {"node_id": node_id})
nodes = []
for node_id, geojson, street_names in cursor.fetchall():
nodes.append( {
'node_id': node_id,
'street_names': street_names,
'geometry': json.loads(geojson)
} )
connection.close()
return nodes[0] if len(nodes) > 0 else {}
9 changes: 9 additions & 0 deletions backend/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from app import app
from app.db import getConnection
from app.get_closest_nodes import get_closest_nodes
from app.get_node import get_node
from app.get_travel_time import get_travel_time

from app.get_links import get_links
Expand All @@ -25,6 +26,14 @@ def closest_node(longitude,latitude):
return jsonify({'error': "Longitude and latitude must be decimal numbers!"})
return jsonify(get_closest_nodes(longitude,latitude))

# test URL /node/30357505
@app.route('/node/<node_id>', methods=['GET'])
def node(node_id):
try:
node_id = int(node_id)
except:
return jsonify({'error': "node_id should be an integer"})
return jsonify(get_node(node_id))

# test URL /link-nodes/30421154/30421153
#shell function - outputs json for use on frontend
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/Sidebar/restoreStateFromFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ export async function restoreStateFromFile(fileDropEvent,stateData){
}
// days of week
// TODO: drop the default selection?
[... new Set(URIs.map(uri=>uri.dow))].forEach( dows => {
[... new Set(URIs.map(uri=>uri.dow))].forEach( dowsString => {
let daysFactor = stateData.createDays()
let days = dows.split('').map(Number)
daysFactor.setFromSet(new Set(days))
daysFactor.setFromSet(new Set(dowsString.split('').map(Number)))
} )
} )
}
Expand Down

0 comments on commit 785770d

Please sign in to comment.