Wednesday, September 18, 2013

Quick and Dirty Mapping with Geographic Data

Barely a day goes by without seeing a map on the web. And the more eye-catching maps are usually fairly involved productions. Consider, for example, Bomb Sight, or the World Water Risk Atlas.  These maps can be quite beautiful, and offer all sorts of capabilities to explore data. But they aren't the kind of mapping I want to talk about here.

I'm interested in a much lower-end question: what can a person do to quickly post his or her geographic data onto a map? Let's say I have a couple of KML files (or GPX, or shapefiles... we'll get to discussing formats in a bit): if I just need to visualize them, what's my best place to go? Let's say that I'm not making a presentation and I don't need to be able to share the data, or let others edit it. Nor do I need visitors to be able to draw on the map.

For the moment I'll also assume that I don't even want to be able to customize how the data looks, or be able to direct others to this quick and dirty map. I'll cover that in a later post.

The user should not have to know anything about the world of GIS. He or she should not have to re-project the data, or convert formats. There shouldn't be a need to understand map projections. There is a broad range of people who receive data -- maybe it's downloaded from the web, handed over by a collaborator, sent by a friend or produced by the company -- and they just want to see it.


GPSVisualizer is probably your best single bet for a quick and dirty map, whether your geographic data is KML, KMZ, CSV or XLS.

On the home page, where it says "Get started now," browse for your data file and click "Go!"
You'll get a small inset map of your data over a Google maps background. If your data included lines, these are summarized in the box in the upper right side of the map.

There are several nice features available to you now:
  • With one click you can go to a full page view of your map. (Click on "view" in the fine print at the top where it says your map is "also temporarily available to view.")
  • You can click on your points or lines and see the data embedded within them.
  • You can change the background of your map, with over 20 backgrounds to choose from. These include Yahoo maps, OpenStreet map, and topographic maps for the US and Canada
  • You can copy that address for the full-page view of your map and send it to others to visit -- at least for a few hours. (GPSVisualizer says the map is there "temporarily". It's probably good for the rest of the day unless use of the site is heavy.)
If you go back a page, back to that "Get started now" box, you can re-generate the map and, instead of getting a Google maps mash-up, receive:
  • a Google Earth (KMZ) version of your data
  • a JPEG image
  • a PNG image
  • an SVG image
  • an elevation profile
  • a GPX version of your data
  • a plain-text
Nice. Simple. Effective.


If the data you have is in the GPX format, GPSVisualizer will still work just fine. (GPX is a format that usually comes out of GPS devices, or software people use to manage their GPSs.) However if you have GPX data, in many ways Hillmap is a more delightful site, and even easier to use.

With Hillmap, you can drag and drop that GPX file right onto either of their double maps. It will upload the file and move the map to view your data. By default the maps a show "map" and "satellite" views, but there are 14 different backgrounds to choose from.
Now you can:
  • Switch to the Input and Output tab, and use the Link To button to get a link to this map, or an iframe tag to paste into a blog post (example below). (Note: set you preferred background before you get the link or iframe code.)
  • Also on the Input and Output tab, you can print your map. 
Hillmap is very clever: they've thought about how awkward most things printed out of web browsers look, and they've created an interface where you can get a decent looking map. You choose the dimensions of the printed map you want, and the dpi. It will then tell you what scaling to apply when sending it to the printer, and open a new tab with just the map image on it.
In Firefox, for example, you go File - Print Preview, set the scale to the given percentage, and hit Print. Very nice results, and, as a real bonus, it has a scale bar on it!

A short digression on formats

There are six common formats that geographic data comes in:
  • KML This is the original Google Earth data format. (KML stands for "Keyhole Markup Language".) You might have a KML file if you drew some points, lines or areas in Google Earth. Or you might have downloaded a KML file from the web. KML files can contain points, lines and areas.
  • KMZ This is a zipped version of KML.
  • GPX  A format for sharing data among GPSs, as discussed above. GPX files can contain points (which they call "waypoints") and lines (which they call "tracks").
  • shapefile This is a format originally created by a company called ESRI, but now used by most GIS software. It's common in academic and scientific circles. Note that a "shapefile" is actually a set of four files. These files all share the same name but have different extensions, like trail.shp, trail.dbx, trail.shx, and trail.prj. When sites ask you to upload a shapefile, they usually want you to zip the four files together into a .ZIP file first. A shapefile can only contain one kind of data: points, lines OR areas.
  • CSV or "Comma Separated Values." This is a plain text file with columns of data separated by, well, commas. Other variants of this format use tabs or semicolons to separate the fields. These files are generated by spreadsheet programs like Excel or OpenOffice Calc, and they can be displayed by those programs. When a CSV file contains geographic data it typically has columns named latitude and longitude. (It's also possible that instead the columns are northing and easting. This means the data is in a UTM projection, which I won't go into that here.) CSV data is usually just the locations of points.
  • XLS Microsoft Excel's spreadsheet format. Like CSV data, it will have latitude and longitude columns, and represent the locations of points.
There are also more obscure ones, like GeoRSS, GeoJSON, and GML, but if you have data in one of those formats you're probably beyond the scope of this post already.


The format we haven't covered yet is shapefile. If you have this kind of data, neither of the two sites above will work, but CartoDB will. 

Like other sites that can accept shapefiles (such as ArcGIS Online and GeoCommons), CartoDB requires you to get a free account and insists that data you upload be publicly discoverable by other users. But it is simpler and more intuitive than those other services.  

Do note the limitations of the free CartoDB account. It will only let you have five uploaded shapefiles (which it calls "tables") at once, 5 MB of data total, and ten thousand views of your map. For our purposes this is not particularly onerous, but note that because a single shapefile can only hold a single kind of geographic data (points, lines or areas), you will be uploading more than one shapefile if, for example, you have both line and points to view.

Once you have your free account, log in and upload your shapefile by clicking on the large "New Table" button with the plus sign on it. Click select a file, locate your zipped shapefile, and when the progress bar finishes you're looking at a table of data for the geographic objects in your shapefile. Click on Map View. and you see the map. From upload to map is pretty fast.

Notice that you can change among the seven background maps (Nokia Day;  Google terrain, roads, satellite, hybrid, grey roadmap, or dark).

If you click on one of your features  you don't see the data yet, but a select fields link is offered, which pulls a set of toggle switches out from the right side of the map. Toggle "on" all the fields you want to see displayed.

To add a second shapefile to the map, you have to first convert it to a "Visualization." (Use the "Visualize" button in the upper right.) Once you give your visualization a name, the Visualize button becomes a "Publish" button. Right below it is a small "+" button which will allow you to add another shapefile (that you've already uploaded) to the map.

Next post: websites of choice for quick styling and sharing of maps.


Saturday, September 14, 2013

A Geometry Puzzle: Alternating Hexagons and Squares In a Ring


This intriguing pattern is an alternating sequence of six hexagons and six squares. Both basic shapes have the same edge length, and they pack perfectly around a centre.

Furthermore, this fairly complex construction can be done with only a ruler and straightedge. Typically, you start with a circle, within which you construct what will be the dodecagonal centre of the figure above. The alternating hexagons and squares then sit on the sides of this dodecagon.

Because we will have to create other hexagons along the way, I'll call the hexagons in the final pattern around the outside ring hexagons.

The construction goes like this. (I will assume you know how to construct a hexagon within a circle, and how to bisect a line segment.)
1. Draw a circle.

2. Construct a hexagon within it.
3. Bisect one of the sides of the hexagon and draw a ray from the circle's centre through it. Construct another hexagon beginning with a vertex that falls where this ray intersects the circle.
4. Connect the successive vertices of the two hexagons to make a dodecagon, a 12-sided regular polygon.
5. Using the side length of the dodecagon as radius, draw circles around each vertex of the dodecagon.
6. Using every other intersection of these circles as a centre, draw six more circles of the same radius.
7. Construct hexagons within these last six circles. One side of each will coincide with a side of the dodecagon.
8. Connect hexagons to form squares.
9. The final figure without the construction lines. Rotating it 15° counterclockwise will make it look like the one at the very top of the post.

The Puzzle

OK, now for the puzzle. If we connect the centres of the six hexagons in the ring, we get another, larger hexagon.

This hexagon, which I'll call the master hexagon, can be used as the repeating frame for tiling a larger area with the pattern.

But if you were to do this, you would draw the pattern of master hexagons first, and would then construct the ring pattern based on it.

So here's the puzzle: how do you construct the ring pattern of hexagons and squares, given only the master hexagon?


The basic problem is to locate the dodecagon that forms the inside of the ring. Once we have that, we can construct  the ring, as above. But how do we get from the master hexagon to the dodecagon? 
10. Begin with the master hexagon.
11. Locate its centre by connecting vertices, and construct the circumscribing circle.
12. Bisect one side of the hexagon, and construct a second hexagon, much as you did in step 3 above, starting from the point where this bisector meets the circle.
13. Connect vertices of one hexagon to make a six pointed star.
14. Connect the vertices of the other hexagon in a similar fashion.
15. Connect the intersections of those two six-pointed stars, to make a dodecagon. This is the dodecagon that will form the inside of the ring.
16. Using steps 5, 6 and 7 above, construct ring hexagons from the sides of the dodecagon.
17. And, as in step 8 above, connect the ring hexagons to form squares.
It's interesting to compare the construction lines one uses when beginning with a circle to those drawn when beginning with the master hexagon.
18. "Forward" construction lines (that is, those beginning from the circle that circumscribes the dodecagon) are black. "Reverse" construction lines (beginning with the master hexagon) are blue.


Why does using this method to construct the dodecagon within the master hexagon work?

Well, we know the master hexagon has a concentric dodecagon within it somewhere. But which dodecagon?

Because each dodecagon has a different edge length, each implies a different size of hexagons arrayed around it. We want the dodecagon where the hexagon's centre will fall at a vertex of the master hexagon (the red one, below, in this case).

The "right" dodecagon will have vertices that are 60° apart when viewed from a vertex of the master hexagon. Necessarily then, these dodecagon vertices will fall somewhere on the sides of equilateral triangles drawn within the master hexagon.
Drawing the other equilateral triangle within this hexagon gives us a general idea of where these dodecagon vertices will fall, but nothing precise. As well, this pattern so far only has 6-fold rotational symmetry.
If we add another master hexagon, rotated 15°, and its inner triangles, we get a pattern with the necessary 12-fold rotational symmetry.

The set of four equilateral triangles has 3 sets of common intersections, all of which will make dodecagons (red, orange and yellow, below). But only the outermost set (red) creates dodecagon sides that subtend a 60° angle when viewed from the vertices of the master hexagon.