Skip to content

Instantly share code, notes, and snippets.

@lxbarth
Created November 16, 2011 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lxbarth/1370472 to your computer and use it in GitHub Desktop.
Save lxbarth/1370472 to your computer and use it in GitHub Desktop.
Creating a choropleth map with TileMill, QGIS and SQLite

Creating a choropleth map with TileMill, QGIS and SQLite

1. Download shape file

For instance "Presidential General Election, State Results, 2008" from

http://www.nationalatlas.gov/atlasftp.html?openChapters=chphist%2Cchpref%2Cchpgeol#chpgeol

2. Convert shape file to sqlite

3. Create a new Tile Mill project, add the sqlite data layer

Note: Here I found out that database names with numbers are best avoided in TileMill < 0.7. On my TileMill 0.6 installation I could add the table name 'election2008' but I could not use it in a subquery.

4. Theme it

This is a coloring of the map by voting results favoring democrats (somewhat naive because an absence of a democrat vote assumes a republican vote).

https://skitch.com/alexbarth/gjnju/tilemill

#results {
  polygon-fill: #f5f5f5;
}

#results::fill[zoom>=0] {
  [percent_de > '70.0']{ polygon-fill:#0a66c2; }
  [percent_de > '60.0'][percent_de <= '70.0']{ polygon-fill:#2f53a5; }
  [percent_de > '50.0'][percent_de <= '60.0']{ polygon-fill:#544189; }
  [percent_de > '40.0'][percent_de <= '50.0']{ polygon-fill:#782f6c; }
  [percent_de > '30.0'][percent_de <= '40.0']{ polygon-fill:#9d1c4f; }
  [percent_de <= '30.0']{ polygon-fill:#c20a33; }
  [percent_de = null]{ polygon-fill:#f5f5f5; }
}

Further reading

Will White explains how to use SQLite's attach feature in TileMill. This is useful for joining SQLite databases containing the data to be displayed to SQLite databases containing the geometry to use.

http://developmentseed.org/blog/2011/aug/30/tilemill-05-adds-sqlite-datasource-support/

@lxbarth
Copy link
Author

lxbarth commented Nov 16, 2011

This is the map on Mapbox Hosting (pan to US): https://tiles.mapbox.com/lxbarth/map/map-nsduv0gi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment