Advanced Interactive Web Mapping, Programming, and Design

In-class Excercises: PostGIS SQL, CartoDB.js

Part 1: PostGIS SQL and Leaflet: buffers

  1. Create an SQL query that buffers your Airbnb listings data (or some other point dataset) by 100 meters. The documentation for ST_Buffer might be of use.
  2. Test your SQL query in CartoDB to confirm that it works.
  3. Use the CartoDB SQL API to add the buffered data to a Leaflet map. Feel free to use this jsbin if it helps.
  4. Style the features.

Part 2: PostGIS SQL and Leaflet: convex hulls

  1. Create an SQL query that finds the convex hull of your Airbnb listings data (or some subset of them). You will need to use ST_Collect and ST_ConvexHull.
  2. Test your SQL query in CartoDB to confirm that it works.
  3. Use the CartoDB SQL API to add the convex hull to a Leaflet map. Feel free to use this jsbin if it helps.
  4. Style the feature.

Part 3: CartoDB.js

  1. Start a new HTML file, either on your computer or on JS Bin.
  2. Load the CartoDB.js library by copying the two lines from the documentation under "Linking cartodb.js on your html file" and pasting those into the head of your HTML file.
  3. As with Leaflet, add a div with id "map" to the body of your HTML file.
  4. Use CSS to give the map div some dimensions.
  5. Open your CartoDB account, find a visualization you've created.
  6. Open the visualization, go to Share, and copy the CartoDB.js URL.
  7. Finally, load the CartoDB visualization. Add the following to your JavaScript:
    $(document).ready(function () {
        cartodb.createVis('map', /* Your CartoDB visualization's CartoDB.js URL */);
    });
  8. If you run into trouble, here's a working example.

Part 4: CartoDB.js options

  1. Start with the code from Part 1.
  2. Add at least three options to your call to createVis().
  3. This will look something like:
    $(document).ready(function () {
        cartodb.createVis('map', /* Your CartoDB visualization's CartoDB.js URL */, {
            option1: option1-value,
            option2: option2-value,
            option3: option3-value
        });
    });