"""
crash_geocoding

Normalizes CRASH data into collision _events_ and collision _involved persons_, then matches
collision events to the centreline conflation target that was created by
`centreline_conflation_target`.

Our legacy schema in Oracle stores both event-related and involved-person-related information
in a single table, `TRAFFIC.ACC`.  That table has one record per involved person, with event-level
details copied across all persons involved in a collision.  To make this easier to work with in
MOVE, we transform `TRAFFIC.ACC` into a normalized representation.

To match collisions to the centreline, we use the following heuristic:

- if there are any intersections within 20m, match to the closest such intersection;
- otherwise, if there are any midblocks within 20m, match to the closest such midblock;
- otherwise, do not match.

This same heuristic was used by the legacy CRASH system to assign collisions to intersections
and midblocks.  (However, CRASH did not use the Toronto Centreline, but instead used a legacy
map layer that has been deprecated and is no longer maintained by the City.)

This is intended to run after `replicator_transfer_crash` and `centreline_conflation_target`.
"""
# pylint: disable=pointless-statement
from datetime import datetime

from airflow_utils import create_dag, create_bash_task_nested

START_DATE = datetime(2019, 7, 17)
SCHEDULE_INTERVAL = '20 19 * * 1-5'
DAG = create_dag(__file__, __doc__, START_DATE, SCHEDULE_INTERVAL)

A1_EVENTS_FIELDS_RAW = create_bash_task_nested(DAG, 'A1_events_fields_raw')
A2_EVENTS_FIELDS_NORM = create_bash_task_nested(DAG, 'A2_events_fields_norm')
A2_INVOLVED_FIELDS_RAW = create_bash_task_nested(DAG, 'A2_involved_fields_raw')
A3_INVOLVED_FIELDS_NORM = create_bash_task_nested(DAG, 'A3_involved_fields_norm')
A4_INVOLVED = create_bash_task_nested(DAG, 'A4_involved')
A5_EVENTS = create_bash_task_nested(DAG, 'A5_events')
A6_EVENTS_INTERSECTIONS = create_bash_task_nested(DAG, 'A6_events_intersections')
A6_EVENTS_SEGMENTS = create_bash_task_nested(DAG, 'A6_events_segments')
A7_EVENTS_CENTRELINE = create_bash_task_nested(DAG, 'A7_events_centreline')

A1_EVENTS_FIELDS_RAW >> A2_EVENTS_FIELDS_NORM
A1_EVENTS_FIELDS_RAW >> A2_INVOLVED_FIELDS_RAW
A2_EVENTS_FIELDS_NORM >> A3_INVOLVED_FIELDS_NORM
A2_INVOLVED_FIELDS_RAW >> A3_INVOLVED_FIELDS_NORM
A3_INVOLVED_FIELDS_NORM >> A4_INVOLVED
A4_INVOLVED >> A5_EVENTS
A5_EVENTS >> A6_EVENTS_INTERSECTIONS
A5_EVENTS >> A6_EVENTS_SEGMENTS
A6_EVENTS_INTERSECTIONS >> A7_EVENTS_CENTRELINE
A6_EVENTS_SEGMENTS >> A7_EVENTS_CENTRELINE

"""
crash_geocoding

Normalizes CRASH data into collision _events_ and collision _involved persons_, then matches
collision events to the centreline conflation target that was created by
`centreline_conflation_target`.

Our legacy schema in Oracle stores both event-related and involved-person-related information
in a single table, `TRAFFIC.ACC`.  That table has one record per involved person, with event-level
details copied across all persons involved in a collision.  To make this easier to work with in
MOVE, we transform `TRAFFIC.ACC` into a normalized representation.

To match collisions to the centreline, we use the following heuristic:

- if there are any intersections within 20m, match to the closest such intersection;
- otherwise, if there are any midblocks within 20m, match to the closest such midblock;
- otherwise, do not match.

This same heuristic was used by the legacy CRASH system to assign collisions to intersections
and midblocks.  (However, CRASH did not use the Toronto Centreline, but instead used a legacy
map layer that has been deprecated and is no longer maintained by the City.)

This is intended to run after `replicator_transfer_crash` and `centreline_conflation_target`.
"""
# pylint: disable=pointless-statement
from datetime import datetime

from airflow_utils import create_dag, create_bash_task_nested

START_DATE = datetime(2019, 7, 17)
SCHEDULE_INTERVAL = '20 19 * * 1-5'
DAG = create_dag(__file__, __doc__, START_DATE, SCHEDULE_INTERVAL)

A1_EVENTS_FIELDS_RAW = create_bash_task_nested(DAG, 'A1_events_fields_raw')
A2_EVENTS_FIELDS_NORM = create_bash_task_nested(DAG, 'A2_events_fields_norm')
A2_INVOLVED_FIELDS_RAW = create_bash_task_nested(DAG, 'A2_involved_fields_raw')
A3_INVOLVED_FIELDS_NORM = create_bash_task_nested(DAG, 'A3_involved_fields_norm')
A4_INVOLVED = create_bash_task_nested(DAG, 'A4_involved')
A5_EVENTS = create_bash_task_nested(DAG, 'A5_events')
A6_EVENTS_INTERSECTIONS = create_bash_task_nested(DAG, 'A6_events_intersections')
A6_EVENTS_SEGMENTS = create_bash_task_nested(DAG, 'A6_events_segments')
A7_EVENTS_CENTRELINE = create_bash_task_nested(DAG, 'A7_events_centreline')

A1_EVENTS_FIELDS_RAW >> A2_EVENTS_FIELDS_NORM
A1_EVENTS_FIELDS_RAW >> A2_INVOLVED_FIELDS_RAW
A2_EVENTS_FIELDS_NORM >> A3_INVOLVED_FIELDS_NORM
A2_INVOLVED_FIELDS_RAW >> A3_INVOLVED_FIELDS_NORM
A3_INVOLVED_FIELDS_NORM >> A4_INVOLVED
A4_INVOLVED >> A5_EVENTS
A5_EVENTS >> A6_EVENTS_INTERSECTIONS
A5_EVENTS >> A6_EVENTS_SEGMENTS
A6_EVENTS_INTERSECTIONS >> A7_EVENTS_CENTRELINE
A6_EVENTS_SEGMENTS >> A7_EVENTS_CENTRELINE

crash_geocoding  Normalizes CRASH data into collision _events_ and collision _involved persons_, then matches collision events to the centreline conflation target that was created by `centreline_conflation_target`.  Our legacy schema in Oracle stores both event-related and involved-person-related information in a single table, `TRAFFIC.ACC`.  That table has one record per involved person, with event-level details copied across all persons involved in a collision.  To make this easier to work with in MOVE, we transform `TRAFFIC.ACC` into a normalized representation.  To match collisions to the centreline, we use the following heuristic:  - if there are any intersections within 20m, match to the closest such intersection; - otherwise, if there are any midblocks within 20m, match to the closest such midblock; - otherwise, do not match.  This same heuristic was used by the legacy CRASH system to assign collisions to intersections and midblocks.  (However, CRASH did not use the Toronto Centreline, but instead used a legacy map layer that has been deprecated and is no longer maintained by the City.)  This is intended to run after `replicator_transfer_crash` and `centreline_conflation_target`. # pylint: disable=pointless-statement