Wednesday, August 28, 2013

City boundaries with Google Maps and OpenStreetMap

As per my post about comparing city boundaries (limits), this post will explain some of the technical details behind getting and drawing city boundaries.

Firstly, I have written an example web app to illustrate drawing city boundaries on Google Maps with OpenStreetMap here:


This does much of the same thing as the web app I published last week, but with a lot stripped out to make it easier to understand. The GitHub page also has more of the technical detail laid out.

When I initially started this project I looked into various sources for this information. The Google Maps API v3 itself does not provide it, and many people recommend Twitter. This would have been great but it looks like Twitter changed the API for this particular call to require user authentication and limit use to only 15 calls per user per 15 minute window. So I ended up implementing this with OpenStreetMap using the Overpass API.

The main two issues with this:

  • Creating OpenStreetMap queries to find the data which is labelled inconsistently. 
  • The data not existing for some cities at all, including major cities like Dallas and Los Angeles.
If you need great data without major holes like this I recommend against using OpenStreetMap data. The first issue I have attempted to mitigate with the queries I have set up (see the GitHub page above), but more work will have to be done here to make it perfect.

Here is the Twitter API call I mentioned:

Line Segment Intersection

I wrote a small JavaScript function to test whether two lines segments intersect. It uses the vector cross product approach described here:

The code is available here: line-segments-intersect

Saturday, August 17, 2013

Comparing City Limits

Compare City Sizes

More comparing city awesomeness! This uses the OpenStreetMap Overpass API to retrieve city boundaries and then draw them as polygons using the Google Maps API. It then moves the first city to the location of the second and overlays them to allow for easy visual comparison.

The OpenStreetMap Overpass API
The Google Maps API

I will be posting the core code here on GitHub once I get a chance to fix it up and utilitize it a bit better. It does look like there are people trying to do similar stuff. I will also walk through how I interpreted the OpenStreetMap boundaries to get a polygon because it was fairly unintuitive.

Of note is that due to inconsistently labelled data some cities will not return boundaries from OpenStreetMap. I may investigate making the search more robust. For others cities the boundaries do not exist in OpenStreetMap at all (i.e. Dallas and LA). Another issue is my census data seems to think that the lat and lon for San Francisco are the Farallon islands (they are technically part of SF). So it centers maps there when comparing cities to San Francisco, I have not seen this elsewhere.

It also looks like I have to fix up how this displays on blogger as it seems to be displaying a few elements of map incorrectly. Same constraints as the previous post, only cities of 50,000 and enter as "City Name, State Name".

Compare to Ajax Loading

Sunday, August 4, 2013

Comparing Two Cities

Compare Two Cities

I like comparing city populations and sizes to see how they stack up against each other. You have to look beyond just the population to really gauge the size and how they compare. So I made a little web app to do it! It's written in JavaScript and currently only supports cities with a population of at least 50,000. Enter a city in the format "New York, NY" and Give it a try!

Enter two cities to compare:

1) I would like to transition the app to use the census API found here:

2) Eventually I would like to compare cities by taking the actual land area of one city and expanding/shrinking the city limits to the land area of a different city and measuring the population from those new city limits.

Some hurdles would potentially be: Expanding a city in an intelligent way so as to avoid uninhabitable area, getting the population of an area defined by a polygon and maybe getting the population to enough detail.

After some research I believe this may be theoretically possible by expanding/shrinking the city limits in the direction of population centers of the surrounding area. For example, if you looked at a choropleth map of population density you would easily be able to tell in what direction it logically made sense to expand or shrink a city while avoiding uninhabited areas. It also appears that census data is accurate down to the block, which should certainly be sufficient for these purposes.