-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
from app.db import getConnection | ||
from app.nodes.conflation import add_conflated_nodes | ||
|
||
SQL = ''' | ||
SELECT | ||
ST_AsGeoJSON(geom) AS geojson, | ||
ARRAY_AGG(DISTINCT linear_name_full_from) AS street_names | ||
FROM gis_core.intersection_latest | ||
WHERE intersection_id = %(node_id)s | ||
GROUP BY geom; | ||
''' | ||
|
||
def get_centreline_node(node_id, doConflation=False): | ||
"""fetch a specific centreline node by it's ID""" | ||
node = {} | ||
with getConnection() as connection: | ||
with connection.cursor() as cursor: | ||
cursor.execute(SQL, {"node_id": node_id}) | ||
if cursor.rowcount != 1: | ||
return None | ||
geojson, street_names = cursor.fetchone() | ||
node = { | ||
'node_id': node_id, | ||
'network': 'centreline', | ||
'street_names': street_names, | ||
'geometry': json.loads(geojson) | ||
} | ||
connection.close() | ||
if doConflation: | ||
node = add_conflated_nodes(node) | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
from app.db import getConnection | ||
|
||
SQL = ''' | ||
SELECT | ||
tts.px, | ||
ST_AsGeoJSON(tts.geom) AS geojson, | ||
array_remove( | ||
ARRAY[ | ||
InitCap(gts.main_street), | ||
InitCap(gts.side1_street), | ||
InitCap(gts.side2_street) | ||
], | ||
NULL | ||
) AS street_names | ||
FROM traffic.traffic_signal AS tts | ||
JOIN gis.traffic_signal AS gts ON tts.px = gts.px::int | ||
WHERE tts."centrelineId" = %(centreline_id)s; | ||
''' | ||
|
||
def get_px_node(centreline_id): | ||
node = {} | ||
with getConnection() as connection: | ||
with connection.cursor() as cursor: | ||
cursor.execute(SQL, {"centreline_id": centreline_id}) | ||
if cursor.rowcount != 1: | ||
return None | ||
px, geojson, street_names = cursor.fetchone() | ||
node = { | ||
'node_id': px, | ||
'network': 'px', | ||
'street_names': street_names, | ||
'geometry': json.loads(geojson) | ||
} | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from app.nodes.nearby.here import get_here_nodes_within | ||
from app.nodes.byID.px import get_px_node | ||
from app.nodes.nearby.centreline import get_nearest_centreline_node | ||
from haversine import haversine | ||
|
||
def add_conflated_nodes(node): | ||
"""adds "conflated" field to node objects""" | ||
|
||
node['conflated'] = {} | ||
lon = node['geometry']['coordinates'][0] | ||
lat = node['geometry']['coordinates'][1] | ||
|
||
if node['network'] == 'centreline': | ||
# adds px and here nodes | ||
# px search is based on the centreline_id | ||
node['conflated']['px'] = get_px_node(node['node_id']) | ||
try: | ||
# here search is based on distance | ||
node['conflated']['here'] = get_here_nodes_within(50, lon, lat, 1)[0] | ||
except: | ||
pass | ||
elif node['network'] == 'here': | ||
# adds centreline and px nodes | ||
# get centreline by nearest | ||
node['conflated']['centreline'] = get_nearest_centreline_node(lon, lat) | ||
# get px from Id of nearest centreline | ||
node['conflated']['px'] = get_px_node(node['conflated']['centreline']['node_id']) | ||
|
||
# now get distances between selected and conflated points | ||
for network, conflatedNode in node['conflated'].items(): | ||
try: | ||
conflatedNode['distance'] = haversine( | ||
(lat, lon), | ||
( | ||
conflatedNode['geometry']['coordinates'][1], | ||
conflatedNode['geometry']['coordinates'][0] | ||
), | ||
unit='m' | ||
) | ||
except: | ||
pass | ||
|
||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ pandas==2.2.2 | |
psycopg==3.1.18 | ||
python-dotenv==0.15.0 | ||
numpy==1.26.0 | ||
git+ssh://[email protected]/Toronto-Big-Data-Innovation-Team/[email protected] | ||
git+ssh://[email protected]/Toronto-Big-Data-Innovation-Team/[email protected] | ||
haversine==2.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters