Giter Club home page Giter Club logo

musa-620-week-6's Introduction

MUSA-620-Week-6

Spatial databases (notes)

Distance from nearest SEPTA station Distance from each building in Philadelpha to the nearest SEPTA station

Assignment

This assignment is required. You may turn it in by email (galkamaxd at gmail) or in person at class.

Due: the first class after spring break, 15-Mar

Task:

Using the last 10 years of data from the FARS database and Philadelphia's road network, investigate the geospatial distribution of fatal vehicle crashes in Philadelphia and whether alcohol was a contributing factor.

Deliverable:

A map of Philadelphia's road network that displays the number of fatal accidents on each street segment

Please also include all SQL queries and any code used to construct the map.

Fatal accidents in Philadelphia

FARS Database

The FARS (Fatality Analysis Reporting System) is a relational database containing a record of every fatal traffic accident in the U.S. back to 1975. The database is made up of three primary tables.

  • Accident table: information about the accident itelf (where it took place, weather conditions, time and date, etc).
  • Vehicle table: information about the vehicles involved in the accidents (make/model, estimated speed at time of impact, damage, etc).
  • Person table: information about the people involved in the accidents (demographics, which seat of the car they were in, whether or not they were injured, etc).

We will be using data from the period 2004-2013. For the purposes of this project, only the accident table is needed.

(Download clean, standardized versions of the FARS tables here).

Set up PostGIS Database

First, you will need to create a PostGIS database with three spatial tables:

  1. Philadelphia city borders (download shapefile here)
  2. Philadelphia road network (download shapefile here)
  3. FARS accident table -- use the "latitude" and "longitud" columns to create a point layer, then import to PostGIS.

We will not be using the two tables below, but you may want to import tham anyway so you have them avaialable to work with FARS in the future.

  • FARS vehicle table
  • FARS person table

PostGIS Queries

You can pull this data together by constructing the following three SQL queries.

  1. Remove all accidents that did not happen in Philadelphia. You can do this by running a spatial join query of this form:

     SELECT ???
    
     FROM ???
    
     WHERE ST_WITHIN(???)
    

Save this table as a view (or eExport the results as a layer and reimport it into your database as a new table ). Edit: Saving the table as a view will also work, but it will be much slower. Every time you reference a view, it has to be rerun from scratch.

  1. Match each accident to a street segment. This should be a spatial join query, similar to the example we did in class to find the nearest SEPTA station. In this case, you are finding the nearest street segment for each point. The result should be the same as the table you created in the last step with one additional column, the nearest street id.

     SELECT DISTINCT ON ???
    
     FROM ???
     
     ORDER BY ???
    

Save this table as a new view.

  1. Join the accident data to the Philly road network The final query should be a left join, to combine the table you created in the last step with the Philadelphia street network.

     SELECT ???, COUNT( your_accident_table.* ) as num_accidents, ???
     
     FROM ???
     
     LEFT JOIN ???
     
     ON ???
     
     GROUP BY ???
    

Export the results as a QGIS layer, and save it as a shapefile.

Visualize the Results

Use ArcMap or QGIS to visualize the data. This video shows how to style the layer in QGIS: varying the line width and color according to the number of accidents (your colors do not have to match mine).

Useful Links

Postgres / PostGIS

QGIS

Philadelphia road network

FARS database: cleaned & standardized tables for download

FARS database: original source & documentation

musa-620-week-6's People

Contributors

galkamax avatar

Watchers

 avatar  avatar

musa-620-week-6's Issues

Unable to open querying features in DB manager

@galkamax I recently reopened my qgis project and can no longer open the querying features in DB manager. PgAdmin is open and connected, and I wasn't having this issue before. Any idea as to why this is happening?

screen shot 2017-03-14 at 4 59 50 pm

screen shot 2017-03-14 at 5 00 37 pm

** RESOLVED**
Apparently I manually turned off the querying feature. If this happens to anyone else, in the database window, click "database", and then click "SQL window"

MUSA 620: Spatial Query Assignment

@MUSA-620-Fall-2017/musa-620 In preparation for next class, you should have two items installed.

  1. Postgres / PostGIS. Most of you should already have this set up from last class.

  2. Qgis (an open source alternative to ArcGIS). We will be using Qgis to import the data into our database. You can download it here: http://www.qgis.org/en/site/

The next required assignment is posted here: https://github.com/MUSA-620-Fall-2017/MUSA-620-Week-6

It is not due until the first class after break (March 15th), but it is a challenging one, so I recommend not waiting until the last minute to get started.

Max

ERROR: Schema 'ST' does not exist

When I try to run the second query with ST.Distance in my SELECT DISTINCT ON statement, I get an error saying schema ST does not exist. This is the code that I am trying to run -

SELECT DISTINCT ON (philly_accidents.id) philly_accidents.latitude, philly_accidents.longitud, streets.id, ST.Distance(philly_accidents.geom, streets.geom) as distance

FROM philly_accidents, streets

ORDER BY philly_accidents.id, ST.Distance (philly_accidents.geom, streets.geom)

Thanks for the help!

Join the accident data to the Philly road network

Hi!
When I ran my code for the 3rd step, I got the error "syntax error at or near "FROM""!
See my code below.
Is anyone else having an issue?
Sincerely,
Laura

SELECT accidents_philadelphia_street_id.street_id, accidents_philadelphia_street_id.distance, SUM(accidents_philadelphia_street_id.* ) as num_accidents, ???
FROM accidents_philadelphia_street_id, streets
LEFT JOIN accidents_philadelphia_street_id ON streets.id = accidents_philadelphia_street_id.street_id
GROUP BY streets.id

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.