This is an attempt to create the most basic example of a web map for crowdsourcing... anything that users can draw on a map (points, squares, circles, lines, polygons...). It uses Leaflet.draw (demo) to crowdsource user-generated shapes on a map and then inserts them in a CartoDB table.
- Perform all the steps under the CartoDB heading
- Modify the following variables in
index.html
(search for "TODO")cartoDBusername
to your cartodb usernamecartoDBinsertfunction
to the name of your insert functioncartoDBtablename
to the name of your table in CartoDB - Go to http://YOURGITHUBUSERNAME.github.io/crowdmap-basic to play.
- Modify the code to your whims.
- Get a free CartoDB account
- Create a new CartoDB dataset. The default dataset comes with the following fields:
{cartodb_id, the_geom, description, name}
Each row represents one submission from the map with the first field a unique id assigned by CartoDB to each geometry.the_geom
is the geographic object.description
is the user input description of the shape, andname
is the user's name. - In the view for the table, click on the "SQL" tab on the write to execute arbitrary SQL.
- Copy and paste the contents of
insert_function.sql
(here) This allows you to send data from the map to the CartoDB using a publicly accessible URL while limiting what functions the public can perform on the data (for example, modifying or deleting existing data). This function takes the drawn shape as a GeoJSON, the description, and the username. It converts the GeoJSON to a PostGIS geometry object and then inserts a new row in the table with the geometry, and the other two user-input values. If you have different tables you need to create a unique function for each, it's probably a good idea to save each function as a separate file so you can recall what is on your CartoDB account. - Your CartoDB API endpoint is http://YOURUSERNAME.cartodb.com/api/v2/sql
- Inspiring myself from the excellent tutorial by Mike Foster (@mjfoster83). If this is your first introduction to leaflet, you should probably go through the entire webmapping workshop
- Modify the
setData()
function to construct the SQL query which calls the function to insert the data to CartoDB.//Convert the drawing to a GeoJSON to pass to the CartoDB sql database var drawing = "'"+JSON.stringify(layer.toGeoJSON().geometry)+"'"; //Construct the SQL query to insert data from the three parameters: the drawing, //the input username, and the input description of the drawn shape var sql = "SELECT insert_crowd_mapping_data("; sql += drawing; sql += ","+enteredDescription; sql += ","+enteredUsername; sql += ");";
- And then add the sql query to an AJAX call in order to pass the data to your CartoDB table
//TODO: Change to your username var cartoDBusername = "raphaeld" //Sending the data $.ajax({ type: 'POST', url: 'https://'+cartoDBusername+'.cartodb.com/api/v2/sql', crossDomain: true, data: {"q":sql}, dataType: 'json', success: function(responseData, textStatus, jqXHR) { console.log("Data saved"); }, error: function (responseData, textStatus, errorThrown) { console.log("Problem saving the data"); } });
- After each new drawing is inserted, the data from the
drawnItems
layer is passed to theCartoDBData
layer without re-querying the database. This does mean that a user won't see others' edits to the map after load. See Mike Foster's tutorial for the easy fix to reload the data from CartoDB after every draw.// Transfer drawing to the CartoDB layer var newData = layer.toGeoJSON(); newData.properties.description = description.value; newData.properties.name = username.value; cartoDBData.addData(newData);