def accident_processor(collision_file_name, hour_file_name, location_file_name): """ Retrieve data about the time, in hour, of the ottawa's collision record from a csv file. :param collision_file_name: The name of the collision file, which has been generated from preprocessor :param hour_file_name: The processed hour table file :param location_file_name: the processed location table file """ global total_record global total_valid_record hours = [] locations = [] collisions = [] with open(hour_file_name, 'r') as readHour: # r represent read model print("Start to read file: " + hour_file_name + ". This may take a while...") file = csv.reader(readHour) for row in file: if "HOUR_ID" not in row[0]: hour_id = row[0] hours.append(hour_id) readHour.close() print("Finished reading data from hour table") with open(location_file_name, 'r') as readLocation: # r represent read model print("Start to read file: " + location_file_name + ". This may take a while...") file = csv.reader(readLocation) for row in file: if "LOCATION_ID" not in row[0]: location_id = row[0] locations.append(location_id) readLocation.close() print("Finished reading data from location table") with open(collision_file_name, 'r') as readCollision: # r represent read model print("Start to read file: " + collision_file_name + ". This may take a while...") file = csv.reader(readCollision) ptr = 0 for row in file: if "COLLISION_ID" not in row[0]: collision = Collision() collision.collision_id = row[0] collision.location_id = locations[ptr] # append corresponding id collision.hour_id = hours[ptr] # append corresponding id collision.environment = row[6] collision.light = row[7] collision.surface_condition = row[8] collision.traffic_control = row[9] collision.traffic_control_condition = row[10] collision.collision_classification = row[11] collision.impace_type = row[12] collision.no_of_pedestrians = row[13] ptr = ptr + 1 collisions.append(collision) readCollision.close() print("Finished processing collision table") return collisions
def read_source_file(file_name, hour_file_name): with open(file_name, 'r') as readCollision: # r represent read model print("Start to read file: " + file_name + ". This may take a while...") file = csv.reader(readCollision) for row in file: if "COLLISION_ID" not in row[0]: collision = Collision() collision.collision_id = row[0] collision.location_id = row[1] collision.hour_id = row[2] collision.environment = row[3] collision.light = row[4] collision.surface_condition = row[5] collision.traffic_control = row[6] collision.traffic_control_condition = row[7] collision.collision_classification = row[8] collision.impace_type = row[9] collision.no_of_pedestrians = row[10] collisions.append(collision) readCollision.close() with open(hour_file_name, 'r') as readHour: # r represent read model print("Start to read file: " + hour_file_name + ". This may take a while...") file = csv.reader(readHour) for row in file: if "HOUR_ID" not in row[0]: hour = Hour() hour.hour_id = row[0] hour.hour_start = row[1] hour.hour_end = row[2] hour.date = row[3] hour.day_of_week = row[4] hour.day = row[5] hour.month = row[6] hour.year = row[7] hour.weekend = row[8] hour.holiday = row[9] hour.holiday_name = row[10] hours.append(hour) readHour.close()
def read_source_file(file_name, locoation_file_name): with open(file_name, 'r') as readCollision: # r represent read model print("Start to read file: " + file_name + ". This may take a while...") file = csv.reader(readCollision) for row in file: if "COLLISION_ID" not in row[0]: collision = Collision() collision.collision_id = row[0] collision.location_id = row[1] collision.hour_key = row[2] collision.environment = row[3] collision.light = row[4] collision.surface_condition = row[5] collision.traffic_control = row[6] collision.traffic_control_condition = row[7] collision.collision_classification = row[8] collision.impace_type = row[9] collision.no_of_pedestrians = row[10] collision.date = row[11] collisions.append(collision) readCollision.close() with open(locoation_file_name, 'r') as readLocation: # r represent read model print("Start to read file: " + locoation_file_name + ". This may take a while...") file = csv.reader(readLocation) for row in file: if "LOCATION_ID" not in row[0]: location = Location() location.location_id = row[0] location.street_name = row[1] location.intersection_one = row[2] location.intersection_two = row[3] location.longitude = row[4] location.latitude = row[5] location.neighborhood = row[6] location.closest_weather_station = row[7] locations.append(location) readLocation.close()