We want to build your map.

Archive of articles classified as' "Code"

Back home

Don’t Panic: An Absolute Beginner’s Guide to Building a Map Server

January 15, 2012

There are a lot of great mapping applications out there that run on a server. They can be intimidating to install and configure so I thought I would document my steps so everything would be in one place. This a a guide for the absolute beginner so if you have some command-line experience, I promise I’m not being condescending. Future posts will cover how we’re actually using these tools to build our maps.

This tutorial should take you from absolutely nothing to a fully-functional web server containing:

  • PostGIS: A PostgreSQL database optimize to store spatial information. It can easily import shapefiles and OSM data using command line tools as well as connect to mapping services like QGIS and Mapnik.
  • Mapnik: A very powerful tool for automatically generating maps from geographic data with lots of control over cartographic display and rendering.
  • TileStache: A simple way to efficiently serve geographic data to mapping applications. It can send tiled vector or raster data and will speed up any application that needs to load lots of data.

Basic Setup

I’m going to be using a Rackspace Cloud Server for this example. It’s a cheap way to get a server up and running and I’ve found them to be great with support. They automatically build your server and install the operating system. You just need to select 3 things:

  1. Operating System: Ubuntu 11.10 (Oneric Ocelot)
  2. Server Name: tiles
  3. Server Size: 1024 MB of RAM and 40GB of disk

The RAM and disk space are the bare minimum requirements. Fortunately, Rackspace let’s you upgrade your server at any time so it’s easy to configure it small as a sandbox and then beef it up if you decide to put it into production later on.

Once you click “Create server” you’ll see your new root password. Copy it to the clipboard but don’t worry about keeping it super-safe. We’ll change it as soon as we log in for the first time (they also email it to you). Setup will take about 5 minutes to complete and they’ll email you when it’s finished.

Terminal and Remote Access

Since this server is in the cloud (ooooooh), the only way to access it is remotely through SSH. Open any SSH client you like (Terminal is already installed on OSX) and get comfortable. First thing we need to do is to log on to our remote server. Make sure you have that email with your root password and IP address and type into the terminal window:

ssh root@. Replace your stuff there.)

That command tells the terminal to start an SSH session, logging in as root to the server at the specified IP address. The root username is the default admin of the server. We’ll do most of this setup as root since it has full control over the system.

When it asks, just paste your root password from the email and you should be logged in and should see something like this:

root@tiles:~#

The # tells you the system is ready to receive commands. Let’s now change the root password into something we’ll remember. Type:

passwd

and hit enter and follow the prompts.

Now we want to do a quick software update to make sure everything is secure. Run both of these commands:

sudo aptitude update
sudo aptitude upgrade

We’ll use the sudo command often. It tells the server to perform the task as a super-user which gives you extra permissions to modify the system. Here’s another top-tip: At the command prompt, you can hit the up arrow on your keyboard to cycle through your previous commands.

Installing the Web Server Bits

This part of the tutorial is taken from symana.labs

The next step is to install the LAMP stack. LAMP stands for (Linux, Apache, MySQL, PHP) and has all the basics to turn your server into a web server. It can be installed with a single command:

sudo tasksel install lamp-server

and follow the prompts. To secure the MySQL server from common database attacks, run:

mysql_secure_installation

Enter the password you set in the previous step and then enter “n” to keep it. Enter “Y” for the rest of the questions.

To configure a fully qualified domain name for apache type:

echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

If you think you are going to be using MySQL for other applications on the server, you can install phpmyadmin to give you a graphical way to interact with the DB by running:

sudo apt-get install phpmyadmin

This isn’t required since we’ll be using a different database to load our geodata into. When you’re done configuring everything, restart apache with:

sudo apachectl restart

Now we have to configure the server’s hostname so we can contact it via a URL instead of the IP address. If you enter:

hostname -f

you should see the server name you entered when you setup the server. We want to turn that into a subdomain for our primary domain (for me it will be tiles.axismaps.com) but you can skip this if you don’t have your own domain (you’ll just keep using your IP address to connect to the server). To tell the server what the rest of the domain is, we’ll need to edit a few text files using nano, the built-in terminal text editor.

sudo nano /etc/hostname

Will show you something like:

tiles

You’ll want to change that to:

tiles..com

We are using tiles.axismaps.com for ours. When you’ve edited the file, hit Ctrl-X (not command if you’re on a Mac) then enter Y to save changes and hit Enter to overwrite the file. Do the same thing and add your domain to the second entry in:

sudo nano /etc/hosts

To make the changes stick, reboot your server with:

sudo reboot

The final step is to add a DNS record with your web host that points back to the server. From your server management page in Rackspace, click the DNS tab then click your domain name. Click “Add” to enter a new record with values:

  • Type: A
  • Name: tiles..com
  • Content: By this point, the server will have rebooted so you can log in again with:
    ssh root@

    To allow established connections to continue to function:

    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

    To allow SSH traffic:

    iptables -A INPUT -p tcp --dport 22 -j ACCEPT

    To allow HTTP traffic:

    iptables -A INPUT -p tcp --dport 80 -j ACCEPT

    To allow HTTPS traffic:

    iptables -A INPUT -p tcp --dport 443 -j ACCEPT

    To allow remote database connections:

    iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

    Drop all remaining traffic:

    iptables -A INPUT -j DROP

    Save all the rules to a file:

    sudo iptables-save > /etc/iptables.rules

    To enable the firewall, we need to add the rules to the network adapter by editing the interfaces file:

    sudo nano /etc/network/interfaces

    and add the line:

    pre-up iptables-restore < /etc/iptables.rules

    just after iface eth0 inet static and make sure it is indented like the other lines. Save and exit the file then reboot the server.

    This is a good time to create a server backup. In Rackspace, click on “Images” then click “New On-Demand Image” to create a backup of your server. This way, if something goes wrong, you can be up and running again quickly.

    Installing PostGIS

    Some parts of this tutorial is taken from OpenStreetMap Wiki

    To give us access to all the software we’ll need, we need to add an additional software repository. First, enter:

    sudo apt-get install python-software-properties

    to install the command that will allow us to add new repositories. Then we can add a GIS-specific software repository by doing:

    sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable

    Now we need to update the system to allow it to pull down the available software from the new repository:

    sudo aptitude update

    We should be ready to install all of the PostGIS packages with:

    sudo apt-get install postgresql-8.4-postgis postgresql-contrib-8.4

    It will warn you about an old version of PostgreSQL but don’t worry about it. Now we need to setup PostGIS to make the newly installed database ready for GIS. Swtich to the database user:

    sudo -u postgres -i -H

    Now we will create a user within the database that can access your maps:

    createuser -SdRP gisuser

    Enter a password for connecting to the database (it should be different from your root password).

    Now we will create and configure a database to hold your spatial data:

    createdb -E UTF8 -O gisuser gis
    createlang plpgsql gis
    psql -d gis -f /usr/share/postgresql/8.4/contrib/_int.sql
    psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
    psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
    psql gis -c "ALTER TABLE geometry_columns OWNER TO gisuser"
    psql gis -c "ALTER TABLE spatial_ref_sys OWNER TO gisuser"
    exit

    Now we need to configure access to our database first by editing the access file:

    sudo nano /etc/postgresql/8.4/main/pg_hba.conf

    Change the words ident and md5 to “trust” (there should be 3). If you want to connect to this database remotely (to view your data in an external manager or view it in QGIS) you should add the line:

    # Enable remote connections:
    host    all         all         0.0.0.0/0             md5

    to the bottom of the file and then save and close. You’ll also need to enable remote listening by editing the main configuration file here:

    sudo nano /etc/postgresql/8.4/main/postgresql.conf

    and change the line:

    #listen_addresses = 'localhost'

    to

    listen_addresses = '*'

    (don’t forget to remove the “#” in front). Save and overwrite the file. To apply the changes, restart the database server:

    sudo /etc/init.d/postgresql reload

    To test if everything has been installed properly, log into the database as the new user we created.

    psql gis gisuser

    If you type \d you should be able to see all 3 tables. Then type \q to return.

    Installing Mapnik

    Copied exactly from the Mapnik Wiki

    To install Mapnik, enter:

    sudo apt-get install libmapnik0.7 mapnik-utils python-mapnik

    That’s it!

    Installing TileStache

    Some parts of this tutorial is taken from TileStache on GitHub

    The first step in installing TileStache is to install mod_python which is the interface TileStache will use to communicate with the web server. You can install it with:

    sudo apt-get install libapache2-mod-python

    Then restart your web server with:

    sudo /etc/init.d/apache2 restart

    Now we need to install some more packages that TileStache depends on. First we’ll switch to the directory where we’ll keep the new applications:

    cd /etc

    Install packages Curl and Git via aptitude to help with the install:

    sudo apt-get install curl
    sudo apt-get install git-core

    Now install some python tools and libraries that are required:

    sudo apt-get install python-setuptools
    sudo aptitude install python-dev
    sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev

    We’ll grab and install PIP to easily install python modules:

    curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
    sudo python get-pip.py

    Now install the required python modules

    sudo pip install -U werkzeug
    sudo pip install -U simplejson
    sudo pip install -U modestmaps

    The Python Image Library module has some quirks in Ubuntu 11.10 so we need to do some quick fixes:

    sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
    sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
    sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

    Before we can install it:

    sudo pip install -U pil

    Finally we’ll download TileStache from GitHub:

    git clone https://github.com/migurski/TileStache.git

    And install it globally by running the install script:

    cd TileStache/
    python setup.py install

    Finally, we’ll have to add the mod_python configuration to tell our web server which URLs to have TileStache process. Start by editing the apache configuration file:

    sudo nano /etc/apache2/httpd.conf

    and add this:

    
    
      AddHandler mod_python .py
      PythonHandler TileStache::modpythonHandler
      PythonOption config /etc/TileStache/tilestache.cfg
    
    

    This will direct any web traffic to the “tiles” folder containing the file extension “.py” to TileStache. We just need to add a tiles directory to the web directory so we don’t get an error:

    mkdir /var/www/tiles

    Reboot your server to finish it off:

    reboot

    Testing Your Server

    Once your server reboots, we can test to make sure TileStache is installed correctly and is running through mod_python and receiving maps from Mapnik.

    In your browser, go to: http://tiles..com/tiles/tiles.py/osm/preview.html

    You should see a OSM tiled map fullscreen, confirming TileStache is correctly installed and running. When you work with TileStache, you can always preview your tilesets at …/tiles/tiles.py//preview.html

    Now go to: http://tiles..com/tiles/tiles.py/example/preview.html

    You should see a simple gray country map which confirms that TileStache is talking to Mapnik, rendering a shapefile stored in the TileStache directory.

    Finished

    Now that everything is installed, you can go nuts with TileStache, Mapnik and PostGIS to render your own tiled maps. We’ll come back to this point in the coming weeks to show examples of how you can actually use these tools to make some maps. Some sample topics might include:

    • Thematic cartography with TileStache and Mapnik
    • Combining raster and vector tiles to optimize mapping for iPad
    • Custom map tiles from OSM data
4 Comments

Collecting Data from Non-Mapmakers

February 4, 2011

A few months back, we partnered with the UW Cart Lab to build a map for the University of Wisconsin Arboretum. We wanted to create a map that was populated with content generated by users and experts, built on top of free existing web-services, easy to maintain and great-looking. The map itself is relatively accessible so I’ll let you explore it on your own, however, I did want to talk a little bit about a major piece of functionality that is completely transparent to the end-user. First, a little background…

The University of Wisconsin Arboretum is easily the fourth best thing about living in Madison, WI after the Memorial Union Terrace, The Farmer’s Market, and being able to bike to anywhere (coming in fifth: that bonkers taxidermy museum). It’s a relatively vast piece of natural land on the near-West side of town. In just a 5-minute bike ride from downtown, you can feel like you are in the middle of the wilderness. Residents and researchers alike use the Arboretum for everything from running and biking to invasive species research, from snow-shoeing and hiking to painting and nature writing. This piece of land is very meaningful in many different ways to many different people.

Credit: University of Wisconsin Arboretum

That diversity of experience was the main challenge we faced coming into this project. There were lots of voices that wanted to be heard and have their perspective on the Arboretum reflected on the map. How do we engage these researchers, volunteers and nature enthusiasts without having to train each of them on the minutiae of collecting and preparing geographic data?

After looking around for a service that was already doing something similar, we landed on Google My Maps (since it was Google, we didn’t have to look very far). We had already planned on using Flickr to allow users to place their own photos on the map so this didn’t seem like to far of a conceptual jump. My Maps is a simple way for users to edit maps, adding vector points, lines and polygons just by drawing on a Google Map. Best of all, it outputs to KML, the format of the data already included in the map.

After adding their feature and attribute data to My Maps, all the user would need to do is send the URL of the KML file to the map administrator and it would be added to the map. Done? Nope. This is where things get cool.

While the input methods for Google My Maps are fantastically easy and well done, the symbology needed some work to fit in with our map. We didn’t want to have to sacrifice our cartography just for ease of input. We wanted to have full control over the colors and the point icons used by this incoming data.

Changing point styles is relatively simple. We built the map to use a lengthy XML file that the map administrator at the Arboretum uses to configure the map. Through this file, the administrator has a large amount of control over the data included in the map and can change it to instantly meet the Arboretum’s needs. It allows the admin to specify:

  1. The location and title of each layer
  2. Where the layers appear within the categories of the map
  3. What control users have over the layers (which show up in the legend, which start visible, which remain on)
  4. The available basemaps for each category
  5. All text content in the map
  6. If a layer’s feature contains photos or a slideshow
  7. Which layers are grouped into map animations and their corresponding date

The last thing the admin can do, is override the default point style by defining the URL to a PNG stored on the server. This icon will replace the default Google pushpin. We’ve designed a few of the default icons currently shown but with a little Photoshop work, any image can be used as a point symbol.

arbMap_icons.jpg 1150×900 pixels

Changing the stroke and fill color of a line or area symbol was a little trickier. Google offers a choice of 70 different colors for use on My Maps. Not every single one of those colors is going to look good on our basemaps, displayed with the other layers. To solve this, we created a color compatibility chart. Every single color on the Google My Maps selection corresponds to a color deemed compatible with our map. When a My Maps KML is loaded into the map, it automatically adjusts the colors based on the chart below. It is not a 1:1 relationship, as we’ve had to limit our palette to less than 70 colors. However, it gives the user the expected control over color and makes the finished product more visually pleasing.

myMapsArbMapColors

 

This solution is not 100% perfect and there were some sacrifices that had to be made to get this compatibility. Firstly, the data-model we used for each feature was limited. Google allows just one name and one description per feature. This eliminated the possibility of doing quantitative mapping and classification on the fly. (For data created outside of Google My Maps, we enabled categorical mapping using standard KML styles and including the name of the category as the name of the style).

Secondly, KML is not known for its friendly file sizes and the data used in this map is HUGE. We’ve tried to optimize it as much as possible but have resorted to inserting loading screens with informational content about the Arboretum to make those download times seem much shorter.

Hopefully this gives you a clear picture of what’s going on behind the scenes of the Arboretum map. The flexibility we’ve given to the map administrator to configure the map as well as the power we’ve given to the stakeholders to add their own data was new for us at the time, but is something we’ve continued to add to our more recent projects, albeit to a lesser degree. The UW Arboretum map still feels young and in its infancy. The map is a “living document” of the UW Arboretum. As more Arboretum volunteers and researchers get involved and add their considerable expertise to the map, we’re looking forward to watching it grow and change in the future.

No Comments

[Cartogrammar] Simple shapefile drawing in ActionScript 3

July 17, 2009

Need a quick and easy way to get shapefiles into your AS3 project? Fear not! Over on his blog, Andy has posted a set of supplemental classes to Edwin van Rijkom’s SHP code library. It’s a simple solution that will help you get from data to interactive map faster than ever.

Link: Simple shapefile drawing in ActionScript 3 (via Cartogrammar)

No Comments

[Indiemapper] How indieprojector shaped the world

May 26, 2009

added edge points

Looking for a more in-depth view into map projections and indieprojector? Head over to the indiemapper blog to read Andy’s post about working with geographic projections in ActionScript 3. There’s a basic round-up of getting geo-data into Flash with simple projection support and a more detailed discussion about some of the challenges encountered with re-centering and polygon splitting.

It’s a must-read if you’re thinking about rolling your own geographic projection-support in AS3!

Link: How indieprojector shaped the world (via indiemapper blog)

No Comments

Spicing up Google Maps in Flash

February 25, 2009

Note from the future: the example in this post broke somewhere along the line, but this whole post is obsolete anyway now that the Google Maps API allows styled maps!

This isn’t news to everyone, but it’s worth pointing out the fun things one can do with maps using the ActionScript ColorMatrixFilter. Tired of the boring old yellow and orange Google map in the Flash API (or any other map in Flash/Flex)? Lay down a ColorMatrixFilter on that sucker!

The ColorMatrixFilter, if it needs to be pointed out, essentially allows you to mix up the red, green, blue, and alpha channels of vector or raster graphics to produce exciting new colors. Adobe has a nice little article explaining it, along with an interactive demo.

Here’s a little example of simple effects I threw together for Google Maps. Click the links at the bottom for different looks.

Get Adobe Flash player

I poked around the Google Maps Flash API to find exactly where to apply the filter. If you apply it directly to the Map instance, you’ll color everything, including the Google logo and whatever else floats on top of the map. One level deeper is better, but will still color makers and info windows. A second level deeper is the spot. It’s basically like this, where map is the Map instance:

var a:Sprite = map.getChildAt(1) as Sprite;
var b:Sprite = a.getChildAt(0) as Sprite;
b.filters = [new ColorMatrixFilter([ /* color matrix values */ ])];

In the example above, “Winter” takes the red and green input channels and distributes them equally across all channels, but the blue input remains blue on output. The result is an desaturated, icy-looking blue. “Inverted Grayscale” turns everything to grayscale, but additionally sets the map’s blendMode property to “subtract” and sets it against a white background. That inverts the grayscale image for a somewhat stylish effect.

Now, let’s face it: this is a quick and easy but very limited method for “customizing” a map. (And to be honest, I’m not sure how kosher it is according to the terms of service.) It can make a map look cool, but applying the effect to pre-made tiles means that you’re altering the colors of all features on the map. You can’t keep the official blue and red of interstate highway shields, for example.

So keep this little trick in mind, but be more excited about actual customization (and open data) with CloudMade.

4 Comments

Panning and zooming tutorial

December 16, 2008

Perhaps the most basic capability of any custom interactive map we make is the ability to pan and zoom the map.  That is, after all, the way to make something that might be the size of a wall poster in print fit on a computer screen and still be readable.

On my personal site I have posted a very basic tutorial and example of ActionScript code for a simple version of the way I typically code panning and zooming.  If you’re looking for a starting point for panning and zooming, check it out.

Based on my own experiences, if you’re looking for basic ways to improve upon that minimal functionality, consider these:

  • Tweening zoom changes
  • Replacing vector graphics with raster while moving the map (faster performance)
  • Dynamically drawing and placing symbols on the map
  • Drawing geographic data (shapefiles, kml, etc.) into a pan/zoom map
No Comments

The geography of presidential campaign rhetoric

October 29, 2008

A few months ago I started on a little side project to visualize presidential campaign speeches spatially. My idea was to collect speeches by the 2008 US presidential candidates, generate a word cloud of the most common words in each, and each word cloud on a map in the location where the speech was given.  We’ve seen a number of text visualizations and analyses, sometimes in-depth, during this campaign, but so far not by geography that I can recall.  (See those from Martin Krzywinski, and The New York Times with help from Many Eyes, for just a few examples.)  Are the candidates speaking to different issues in different parts of the country?  Are they talking about jobs in Michigan and immigration in New Mexico?  Are they pandering to everyone, everywhere they go?  (Can we call this project PanderViz?)  Visualizing campaign words on a map might answer such questions.

Campaign speeches by John McCain and Barack Obama as word clouds. (Click for a larger map)

We hoped to develop this idea into a sophisticated interactive map in which a user could search for words, filter speeches by date, and so on.  Other work has kept us from doing that before the election next week, but it seems worth showing some screenshots from what I did manage to get done originally.

I went to the official websites of the Obama and McCain campaigns, where the text of speeches is transcribed, and ran the speeches through a simple PHP script to count words and record the location of the speech.  This week I revisited the sites to catch up on speeches since the summer.  These sources have their drawbacks, of course.  For one, although as prepared speeches they contain perhaps the most carefully chosen words for a particular audiences, they do not represent the complete vocabulary used on the campaign trail.  Also, Obama’s team has been more diligent in posting speeches, it seems, providing close to 80 speeches since May, compared to about 30 for McCain, a disparity that makes comparison between the two candidates a bit difficult.

As far as I got with the capabilities of this map was generating scale-dependent word clouds (I’ve written more about those on my personal site) and searching for individual words to display proportional symbols representing the frequency of use.  With less than a week until election day, we might as well get out of it what we can, so I’ve generated a series of maps of word clouds and individual word frequencies.

Use of the word war by John McCain

Use of the word war by Barack Obama

The whole series is long—obnoxiously long for a blog page—so it’s at a separate page, linked below.  Enjoy, and please comment if there’s an interesting word to look up that I didn’t think of!

See the full article: The Geography of Presidential Campaign Speeches

No Comments