-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
bfe1fd1
commit 357d8bf
Showing
1 changed file
with
29 additions
and
0 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,29 @@ | ||
-- This trigger function updates the location_id of a record in atd_txdot_crashes on insert or update. | ||
CREATE OR REPLACE FUNCTION update_cr3_location() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
-- If crash is level 5 try to associate crash to level 5 polygon and return the location id | ||
IF (NEW.rpt_road_part_id != 2 AND UPPER(LTRIM(NEW.rpt_hwy_num)) IN ('35', '183','183A','1','290','71','360','620','45','130')) THEN | ||
NEW.location_id = ( | ||
SELECT location_id | ||
FROM atd_txdot_locations | ||
WHERE location_group = 2 -- level 5 polygons | ||
AND (geometry && NEW.position) | ||
AND ST_Contains(geometry, NEW.position) | ||
LIMIT 1 --TODO: This should be temporary until we get our polygons in order and there are no overlaps | ||
); | ||
-- Return the location id of the crash by finding which location polygon the crash | ||
-- geographic position resides in | ||
ELSE | ||
NEW.location_id = ( | ||
SELECT location_id | ||
FROM atd_txdot_locations | ||
WHERE location_group = 1 -- level 1-4 polygons | ||
AND (geometry && NEW.position) | ||
AND ST_Contains(geometry, NEW.position) | ||
LIMIT 1 --TODO: This should be temporary until we get our polygons in order | ||
); | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |