Advanced GIS

Class 4

upcoming: 3/5 final project proposals due

final project proposals

2 pages

final project proposals

work plan:

data (where's it coming from? what will you have to do to it?)

final project proposals

work plan:

what will it look like? include a sketch.

final project proposals

work plan:

what do you need to learn to make this happen?

final project proposals

this will likely change as you do your project, and that's okay

final project proposals

define a milestone for yourself. it will be due 4/9.

upcoming: 3/13 guest speaker

CartoCSS

#earthquakes {
    marker-fill: #ff307a;
    marker-line-color: #FFF;
    marker-line-width: 0.25;
    marker-line-opacity: 0.4;
    marker-width: 2;
    marker-allow-overlap: true;
}
#earthquakes {
    marker-width: 3;
    ...
    [mag >= 6.5] {
        marker-width: 8;
    }
}
#earthquakes {
    marker-width: 3;
    ...
    [zoom >= 10][zoom < 16] {
        marker-width: 8;
    }
}
#earthquakes {
    marker-width: 3;
    ...
    [mag > 5.5],
    [place = 'Northern Mid-Atlantic Ridge'] {
        marker-width: 8;
    }
}
@width: 8;
#earthquakes {
    marker-width: @width;
    marker-fill: #ff307a;
    marker-allow-overlap: true;
    
    [zoom >= 4] {
        marker-width: @width * 2;
    }
    [zoom >= 8] {
        marker-width: @width * 3;
    }
    [zoom >= 12] {
        marker-width: @width * 4;
    }
}

Advanced GIS

Class 4

CartoDB follow up

adding features

adding columns

useful if you want to add data to your features

or, say, you have one thing in the table but want to display another

advanced legend and infowindow stuff

advanced legend and infowindow stuff

you need HTML and CSS. we'll get there.

other things

I recommend checking CartoDB's tutorials:

developers.cartodb.com/tutorials.html

but I wanted 6.5!

SQL

SQL

Structured Query Language

SQL

the language databases understand

Give me the pages that refer to properties in Brooklyn.

Give me the addresses of the properties in Brooklyn.

databases are great at these types of questions

it's what they were made for

SQL helps you ask these questions in a way databases understand

CartoDB

SELECT: choose columns from a table

just list the columns. for example:

SELECT mag, place
FROM earthquakes

SELECT *: choose all columns

WHERE: choose rows from a table

for example:

SELECT *
FROM earthquakes
WHERE mag > 6 
  AND mag < 7

try it yourself

get just the earthquakes with mag between 7 and 7.5

these conditions are the same as the ones in CartoCSS:

>
<
=
!=
>=
<=

and you combine them with AND / OR

for example:

SELECT *
FROM earthquakes
WHERE mag > 6 
  AND place =
  'Northern Mid-Atlantic Ridge'

for example:

SELECT *
FROM earthquakes
WHERE mag > 6 
  OR place =
  'Northern Mid-Atlantic Ridge'

or you can negate a condition with NOT

for example:

SELECT *
FROM earthquakes
WHERE NOT (mag > 6 
  OR place =
  'Northern Mid-Atlantic Ridge')

for example:

SELECT *
FROM earthquakes
WHERE NOT (mag > 6 
  OR place =
  'Northern Mid-Atlantic Ridge')

(match any rows that do not match the condition)

try it yourself

get just the earthquakes with mag not between 7 and 7.5

SELECT does not change the table, it only changes your view of the table

you can also use SELECT to get a better idea of what data is in your table

get a count of matching rows:

SELECT COUNT(*)
FROM earthquakes
WHERE mag > 6 
  OR place =
  'Northern Mid-Atlantic Ridge'

but you'll almost never want to use this statement for your map

try it yourself

get the number of earthquakes with mag between 7 and 7.5

you can also use SQL to quickly UPDATE or DELETE the data in your table

these will destroy data, so have a backup or test it with a table you don't need

UPDATE earthquakes
SET mag_display = 'HUGE'
WHERE mag > 8
DELETE FROM earthquakes
WHERE ...

more in the SQL cheatsheet in Lore

SQL shows up everywhere online

getting data from CartoDB using SQL

http://eric.cartodb.com/api/v2/
    sql?q=SELECT COUNT(*) 
          FROM earthquakes 
          WHERE mag > 7
view results

this should all go on one line, but then we have to make it small:

http://eric.cartodb.com/api/v2/sql?q=SELECT COUNT(*) FROM earthquakes WHERE mag > 7
http://eric.cartodb.com/api/v2/
    sql?q=SELECT * 
          FROM earthquakes 
          WHERE mag > 7
view results

with public tables you can do this:

http://{your username}.cartodb.com
    /api/v2/sql?q={your query}

write your query in a text editor, then turn it into the URL

let's talk about this GeoJSON thing

we all know and love shapefiles, but...

shapefiles are difficult to read

(without proper software) shapefiles are difficult to read

(without proper software) shapefiles are difficult / impossible to write, too

how to write a shapefile:

and the people writing that software are in for some fun

gis.stackexchange.com

"It's like they had a bunch of smart developers with one lunatic thrown in."

okay, maybe you have the software already. there are some other problems:

1. column names limited to ten characters

how do you pronounce "C_DIG2DESC"?

2. multiple files make up a shapefile

3. one geometry type per shapefile

okay, back to GeoJSON

where do file formats come from?

where does GeoJSON work?

AnnieGitUrGun on github

GeoJSON is readable

{
    ...
}
{
    "type": "FeatureCollection",
    "features": [ ... ]
}
{
    "type": "FeatureCollection",
    "features": [
        { ... },
        { ... },
        { ... }
    ]
}
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "id": 1,
                "Name": "My House",
                "City": "White Hall",
                "Comments": "It's OK"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-76.596, 39.684]
            }
        }
    ]
}

GeoJSON allows multiple geometry types in one file

GeoJSON is easy for web apps to understand

it's not without its flaws

the files can get big:

the files can get big:

getting GeoJSON files from CartoDB

http://eric.cartodb.com/api/v2/
    sql?q=SELECT * 
          FROM earthquakes 
          WHERE mag > 7
http://eric.cartodb.com/api/v2/
    sql?q=SELECT * 
          FROM earthquakes 
          WHERE mag > 7
    &format=geojson
view results

let's put GeoJSON files online

geojson.io

try it: go to geojson.io, make some features, and save your map

Philly farmers markets
National Park Service: NPMap
The Atlantic

create a github account

fork this repository:

github.com/ebrelsford/geojson-examples

login with geojson.io

find the repository you forked

make some changes and save them

view the commit

or through github