Welcome to Advanced GIS, Lecture 11

This is a web page that can be viewed as slides.

→ to move forward

← to go back

Advanced GIS

Class 11

APIs are the "nubby bits" of a lego
—Aaron Straup Cope

various bits

APIs let you connect to other applications

Application
Programming
Interface

Interface

(the "nubby bits")

Interface

(the "nubby bits")

for

Application
Programming

(writing applications)

the interface is a common language between you and someone else

the CartoDB SQL API

the SQL API lets you run SQL through code

start with the URL "endpoint" for the API

start with the URL "endpoint" for the API

https://{account}.cartodb.com/
 api/v2/sql

for me this is

https://eric.cartodb.com/
 api/v2/sql

then add some parameters

these help you specify exactly what you want out of the API

the CartoDB API has a q parameter that expects an SQL query

parameter=value

q=SELECT count(*)
FROM {table_name}

the ? in a url lets the server know that the parameters start there

so you end up with endpoint + ? + parameters

https://{account}.cartodb.com/
 api/v2/sql?q=
 SELECT count(*) 
 FROM {table_name}

more concretely

https://eric.cartodb.com/
 api/v2/sql?q=
 SELECT count(*) 
 FROM graffiti_information_2015

JSON

basically JavaScript objects and arrays, but written to a file

you can add HTML to a document using jQuery

$('body').append(
    $('<div></div>').text(count);           
);

for example

APIs often accept multiple parameters

when they do, separate parameters using &

http://...cartodb.com/.../sql?
  q={query}&
  format=GeoJSON

then you can add it to Leaflet

$.getJSON(cartodbUrl, function (data) {
    L.geoJson(data).addTo(map);
});

for example

proprietary software

open source software

software can get complicated

open source software requires infrastructure

collaborators need to track changes in code

version control

version control

  1. what changed?

version control

  1. what changed?
  2. who changed it?

version control

  1. what changed?
  2. who changed it?
  3. how is what I have different from that version?

collaborators need to communicate changes in code

linux patches sent via email

but most of us use

GitHub will host your pages for free

you need three concepts to work with it:

1. repositories

repositories are just folders that git knows about

repositories can be shared on github

2. commits

every time you change your code in your code editor, git notices

when you commit the code you add a bookmark in your code's history

commit messages help you read the story of your code

this becomes more important when working with others

3. pushing

"pushing" your changes puts them on a server such as GitHub's

until you push, your changes stay on your computer

you need to commit your changes before pushing them

  1. repositories
  2. commits
  3. push

GitHub for Mac

as long as you know these, GitHub for Mac is pretty usable

GitHub for Mac
more details

making a repository

more details
more details

committing changes

more details

(you still make your changes in a code editor as usual)

more details
more details

push

more details

getting a page on GitHub Pages

create a repository called

{your_github_username}.github.io

add an HTML page called

index.html

commit it

push it to GitHub

you'll then have a website at

{your_github_username}.github.io

you'll then have a website at

{your_github_username}.github.io

(give it a few minutes the first time)

did you know: you can make input to Google Forms go straight to CartoDB?

cartodb
cartodb

1. create a form

2. create a CartoDB table for the incoming data

3. open the spreadsheet the form created

4. go to Tools > Script Editor

paste code from here, customize to your needs

5. add a "trigger" to run your function when the form is submitted

d-bird