Code Monkey home page Code Monkey logo

ny-gis-cartodb-fun's Introduction

Using CartoDB to analyze and map New York demographic and crime data

Some draft notes and documentation on using CartoDB to do some data visualization and GIS, including how to collect that data from the source, and how to use SQL to wrangle it for analytical and visualization purposes.

  • The Maps section contains example maps, as well as the SQL needed to create their datasets.
  • The Datasets section documents where on the Web I found the data as well as the actual file downloaded.
  • The Join/lookup tables section shows the SQL for deriving the intermediary datasets needed to link the official datasets, e.g. how the pretracts_lookup is used to associate each Census tract to a NYPD precinct, which allows for the calculation of Census population by NYPD precinct.

Maps (so far)

Census total population by NYPD precinct

Census total population by NYPD precinct

Dataset and SQL

You can see the underlying dataset here: nypd_precincts_and_2010_census_pop

The SQL to create it from my source datasets:

SELECT 
  precinct_id,
  precinct_pop,
  cartodb_id,
  the_geom_webmercator
FROM nypd_precincts
INNER JOIN  
    (SELECT 
      precinct_id,
      SUM(total_pop) AS precinct_pop
    FROM 
      pretracts_lookup
    INNER JOIN census_pop
      ON
        pretracts_lookup.tract_id = census_pop.census_tract_code
         AND 
        pretracts_lookup.borocode = census_pop.dcp_borough_code
    GROUP BY precinct_id) 
  AS tx
  ON tx.precinct_id = nypd_precincts.precinct;

Datasets

Join/lookup tables

pretracts_lookup

Requires a spatial join of the Census tracts to each precinct -- not knowing a lot about PostGIS but also knowing how annoying it is to define containment/intersection of real-life boundaries in general, I opted to join the tracts to precincts on the basis of whether the tract's centroid was within a precinct's boundary.

SELECT
  borocode,
  ct2010 AS tract_id,
  precinct AS precinct_id
FROM census_tracts
INNER JOIN nypd_precincts 
  ON ST_WITHIN(
                ST_Centroid(census_tracts.the_geom), 
                nypd_precincts.the_geom);

ny-gis-cartodb-fun's People

Contributors

dannguyen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.