Skip to content

Instantly share code, notes, and snippets.

@andy-esch
Last active October 15, 2016 00:10
Show Gist options
  • Save andy-esch/cc7ea97388fafc71afedc03586868039 to your computer and use it in GitHub Desktop.
Save andy-esch/cc7ea97388fafc71afedc03586868039 to your computer and use it in GitHub Desktop.
Data Science Academy -- Oct 14, 2016

CARTO

Oct 14, 2016

Andy Eschbacher, [email protected], @MrEPhysics

This link is here: http://bit.ly/carto-data-sci-oct14

What is CARTO?

  • It depends on how you use it ;)
    • Builder -- Cloud-based Analysis and mapping platform
    • Engine -- APIs for building custom applications
  • Geo data matters

Maps I like

Data Science projects


Demo analysis workflow

Importing the data

First we need to sync a new dataset. Search for "usgs earthquake spreadsheet" to find the page, or use the following dataset of all recorded seismic events from the past 30 days:

http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv

This is updated every 15 minutes, so it's a great candidate for making a map which updates based on measurements in the world. Let's import that into our accounts. Even cooler: the map we publish will be updated automatically, and the underlying analysis changes dynamically based on the new points added to the dataset.

Building the analysis workflow

To build our analysis workflow, let's focus on the output we would like.

Visualize significant, recent earthquake events by communicating the total population who are living within 50 miles of the epicenter.

We can build this analysis workflow in the Builder by chaining together some of the tools.

  1. We need to restrict our analysis to California (within 50 miles of the borders)
  2. Filter to only show significant events
  3. Find the number of people living with 50 miles of the earthquake events
  4. Visualize the points by the number of people (proportional symbol map)

Getting California

There are many ways to get the boundary of California. One ways is just grab all the states from Natural Earth Data. Another ways is to grab it from my CARTO account:

http://eschbacher.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20ne_50m_admin_1_states&format=shp&filename=us_states

Copy that link, and import it just like you did for the earthquake dataset above. Finally, we can do some Analysis with the Builder to get it into the state that we're interested in (50 miles outside of current borders).

Finding Earthquakes that happened in or around California

We can again use the Builder to find all earthquakes which intersect California or are within 50 miles of its border by using the "Aggregate intersection" method.

Filtering out non-significant events

Using the "Filter by column value" analysis method, we can surface only the significant earthquakes (by magnitude).

Finding 50 mile buffers

Using the "Create areas of influence" tool, we can get the regions that are within a 50 mile distance of the epicenter of an earthquake.

Enrich with the Data Observatory

This analysis allows us to attach valuable information from sources such as the US Census. For our demo we will use Total Population that are within 50 miles of the epicenter of the earthquakes.

Visualize by the number of people

Analysis Chain


CARTO's Data Science toolkit

Signing up

Our APIs

JavaScript Libraries

  • Carto.js -- workhorse library for building custom applications off of CARTO's platform
  • Torque.js -- create spatio-temporal maps from your time-series data

Programmatic access / R and Python workflows


Demos with JavaScript

<!DOCTYPE html>
<html>
<head>
<title>CartoDB Workshop | CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
button {
display: inline-block;
padding: 10px 20px 10px 20px;
background: #ff5a5f;
border-radius: 3px;
color: #eee;
font-weight: 400;
font-size: 90%;
margin: 0px 5px 0px 5px;
z-index: 100;
position: absolute;
top: 20px;
right: 20px;
}
button.hidden {
display: none;
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.14/themes/css/cartodb.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.14/themes/css/cartodb.ie.css" />
<![endif]-->
</head>
<body>
<div id="map"></div>
<button class="hidden">Clear filter</button>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<script>
function main() {
var map = new L.Map('map', {
zoomControl: false,
center: [38.893, -77.03],
zoom: 13
});
var layer = L.tileLayer('http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png',{
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
cartodb.createLayer(map, 'https://team.carto.com/u/eschbacher/api/v2/viz/5f709a68-f9e3-11e4-abb5-0e4fddd5de28/viz.json')
.addTo(map)
.done(function(layer) {
// Do further things here
var lots = layer.getSubLayer(0);
lots.setInteractivity('cartodb_id, elevation');
lots.setInteraction(true);
lots.on('featureOver', function(e, pos, pixel, data) {
console.log("Elevation: " + data.elevation + " feet.");
});
lots.on('featureClick', function(e, pos, pixel, data) {
var newSql = "SELECT * FROM dc_tax_lots WHERE elevation >= "+(data.elevation - 30)+" AND elevation <= "+(data.elevation + 30);
console.log("SQL: " + newSql);
lots.setSQL(newSql);
$('button').removeClass('hidden');
$('button').click(function(){
$('button').addClass('hidden');
lots.setSQL("SELECT * FROM dc_tax_lots");
});
});
})
.error(function(err) {
console.log('error error');
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment