Skip to content

Instantly share code, notes, and snippets.

@garethrees
Created December 22, 2017 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garethrees/7525bf2896f10e3a9e68ba09c4483bc4 to your computer and use it in GitHub Desktop.
Save garethrees/7525bf2896f10e3a9e68ba09c4483bc4 to your computer and use it in GitHub Desktop.
Wauntreoda Road Accident Data 2007-2016
Year PoliceForce AccidentReference DateOfAccident TimeOfAccident LocalAuthority Easting Northing RoadType RoadNumber TrunkRoadStatus Pedestrians Agricultural vehicles Bus or coach or minibus Cars Light vans or goods vehicles Motorcyclists Pedal cyclists Ridden horse Taxis or private hire vehicles Tram or light rail Other road users TotalVehicles Fatal Serious Slight TotalCasualties Casualty_Age_0-15 Casualty_Age_16-19 Casualty_Age_20-24 Casualty_Age_25-29 Casualty_Age_30-39 Casualty_Age_40-49 Casualty_Age_50-59 Casualty_Age_60-69 Casualty_Age_70+ Casualty_Age_Not_Known Latitude Longitude
2007 62 60030 20/03/2007 2003 W06000015 316460 179640 Single carriageway U 0 0 0 0 1 1 0 0 0 0 0 0 2 0 1 0 1 0 0 0 0 0 0 1 0 0 0 51.509490691886064 -3.2051613661007514
2012 62 1201144 16/10/2012 1710 W06000015 316450 179460 Single carriageway U 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 1 1 0 0 0 0 1 0 0 0 0 0 51.507871080090716 -3.205262704728876
@garethrees
Copy link
Author

garethrees commented Dec 22, 2017

Here's the code I used to generate the filtered list:

# ./Gemfile
source 'https://rubygems.org'

gem 'national_grid', '~> 0.2.0'
# ./filter
#!/usr/bin/env ruby
require 'csv'
require 'bundler'
Bundler.require

Signal.trap('SIGPIPE', 'SYSTEM_DEFAULT')

DEFAULT_HEADERS = ["Year","PoliceForce","AccidentReference","DateOfAccident","TimeOfAccident","LocalAuthority","Easting","Northing","RoadType","RoadNumber","TrunkRoadStatus","Pedestrians","Agricultural vehicles","Bus or coach or minibus","Cars","Light vans or goods vehicles","Motorcyclists","Pedal cyclists","Ridden horse","Taxis or private hire vehicles","Tram or light rail","Other road users","TotalVehicles","Fatal","Serious","Slight","TotalCasualties","Casualty_Age_0-15","Casualty_Age_16-19","Casualty_Age_20-24","Casualty_Age_25-29","Casualty_Age_30-39","Casualty_Age_40-49","Casualty_Age_50-59","Casualty_Age_60-69","Casualty_Age_70+","Casualty_Age_Not_Known","Latitude","Longitude"]

class Accident
  MIN_EASTING = 316_430
  MAX_EASTING = 316_480
  MIN_NORTHING = 179_415
  MAX_NORTHING = 179_650

  attr_reader :csv_data

  def initialize(csv_data)
    @csv_data = csv_data
  end

  def within_target_area?
    within_easting? && within_northing?
  end

  def easting
    Integer(csv_data['Easting'])
  end

  def northing
    csv_data['Northing']
  end

  def latitude
    to_lat_long.latitude
  end

  def longitude
    to_lat_long.longitude
  end

  def to_csv
    csv_data['Latitude'] = latitude
    csv_data['Longitude'] = longitude
    csv_data
  end

  private

  def within_easting?
    easting.between?(MIN_EASTING, MAX_EASTING)
  end

  def within_northing?
    northing.between?(MIN_NORTHING,MAX_NORTHING)
  end

  def to_lat_long
    NationalGrid::EastingNorthing.new(easting, northing).to_latitude_longitude
  end

end

within_range_accidents = []

CSV.parse(STDIN.read, headers: true, converters: [:integer]) do |row|
  next if row['Easting'] == 'Easting'
  accident = Accident.new(row)
  within_range_accidents << accident if accident.within_target_area?
end

exit 0 unless within_range_accidents.any?

csv_string = CSV.generate do |csv|
  csv << DEFAULT_HEADERS
  within_range_accidents.each { |accident| csv << accident.to_csv }
end

puts csv_string
cat data/AccLvlData-*.csv | ./filter > output/wauntreoda-road-accident-data-2007-2016.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment