-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from olincollege/SAN-92-vehicle-dir
SAN-92 Vehicle Direction
- Loading branch information
Showing
4 changed files
with
160 additions
and
5 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ pytest~=8.3.3 | |
sphinx~=8.1.3 | ||
pygris~=0.1.6 | ||
aiohttp~=3.10.10 | ||
duckdb~=1.1.3 |
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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import duckdb | ||
|
||
from night_light.bronze_db import util | ||
|
||
|
||
def identify_vehicle_direction(con: duckdb.DuckDBPyConnection): | ||
""" | ||
Identify the direction of the vehicle for each side of the road segment. | ||
- Case 1: If the crosswalk center’s X value is greater than the street center | ||
point’s X value, we assume the vehicle is moving from “y_smaller” to “y_larger.” | ||
– For the from_coord, choose the vertex of the pedestrian edge with the smaller | ||
Y value. | ||
– For the to_coord, choose the vertex with the larger Y value. | ||
- Case 2: If the crosswalk center’s X is less than the street center point’s X | ||
value, we assume the reverse: | ||
– For from_coord, choose the vertex with the larger Y. | ||
– For to_coord, choose the vertex with the smaller Y. | ||
- Case 3: If the crosswalk center’s Y is greater than the street center point’s Y | ||
value, we assume the vehicle is moving from “x_larger” to “x_smaller.” | ||
– For from_coord, choose the vertex with the larger X value. | ||
– For to_coord, choose the vertex with the smaller X value. | ||
- Case 4: If the crosswalk center’s Y is less than the street center point’s Y | ||
value, then: | ||
– For from_coord, choose the vertex with the smaller X value. | ||
– For to_coord, choose the vertex with the larger X value. | ||
""" | ||
con.execute( | ||
""" | ||
-- Add columns to store the from/to direction | ||
ALTER TABLE crosswalk_centers DROP COLUMN IF EXISTS from_coord; | ||
ALTER TABLE crosswalk_centers DROP COLUMN IF EXISTS to_coord; | ||
ALTER TABLE crosswalk_centers ADD COLUMN from_coord VARCHAR; | ||
ALTER TABLE crosswalk_centers ADD COLUMN to_coord VARCHAR; | ||
-- Update them based on the comparison result | ||
UPDATE crosswalk_centers | ||
SET | ||
from_coord = CASE | ||
WHEN ST_X(ST_GeomFromText(geometry)) > ST_X(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
< ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_X(ST_GeomFromText(geometry)) < ST_X(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
> ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_Y(ST_GeomFromText(geometry)) > ST_Y(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
> ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_Y(ST_GeomFromText(geometry)) < ST_Y(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
< ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
ELSE 'undefined' | ||
END, | ||
to_coord = CASE | ||
WHEN ST_X(ST_GeomFromText(geometry)) > ST_X(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
> ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_X(ST_GeomFromText(geometry)) < ST_X(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
< ST_Y(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_Y(ST_GeomFromText(geometry)) > ST_Y(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
< ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
WHEN ST_Y(ST_GeomFromText(geometry)) < ST_Y(ST_GeomFromText(street_center_point)) | ||
THEN ( | ||
CASE | ||
WHEN ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
> ST_X(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
THEN ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 1)) | ||
ELSE ST_AsText(ST_PointN(ST_GeomFromText(ped_edge_geom), 2)) | ||
END | ||
) | ||
ELSE 'undefined' | ||
END; | ||
""" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
con = util.connect_to_duckdb("edge_classifier/edge_classifier.db") | ||
identify_vehicle_direction(con) | ||
util.save_table_to_geojson( | ||
con, | ||
"crosswalk_centers", | ||
"crosswalk_centers.geojson", | ||
) |