2

I have a large collection (and growing) of geospatial data (lat, lon) points (stored in mongodb, if that helps). I'd like to generate a choropleth map (http://vis.stanford.edu/protovis/ex/choropleth.html), which requires knowing the state which contains that point. Is there a database or algorithm that can do this without requiring call to external APIs (i.e. I'm aware of things like geopy and the google maps API).

Andres
  • 2,880
  • 4
  • 32
  • 38
  • My knee jerk reaction was to suggest reverse geocoding the locations, but I can see you don't want to use external APIs. I imagine you would need to build a library that was cognizant of the state border data and was able to map a lat/long based on that data. I wonder if someone has already done the work here? Interesting question :) – RedBlueThing Mar 11 '11 at 03:23

5 Answers5

3

Actually, the web app you linked to contains the data you need -

If you look at http://vis.stanford.edu/protovis/ex/us_lowres.js for each state, borders[] contains a [lat,long] polyline which outlines the state. Load this data and check for point-in-polygon - http://en.wikipedia.org/wiki/Point_in_polygon

Per Reverse Geocoding Without Web Access you can speed it up a lot by pre-calculating a bounding box on each state and only testing point-in-polygon if point-in-bounding-box.

Community
  • 1
  • 1
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
1

Here's how to do it in FORTRAN. Remember FORTRAN? Me neither. Anyway, it looks pretty simple, as every state has its own range.

EDIT It's been point out to me that your starting point is LAT-LONG, not the zipcode.

The algorithm for converting a lat-long to a political division is called "a map". Seriously, that's allan ordinary map is, a mapping of every point in some range to the division it belongs to. A detailed digital map of all 48 contiguous states would be a big database, and then you would need some (fairly simple) code to determine for each state (described as a series of line segments outlining the border) whether a given point was inside it or out.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
1

you can try using Geonames database. It has long/lat as well as city, postal and other location type data. It is free as well.

but If you need to host it locally or import it into your own database , the USGS and NGA provide a comprehensive list of cities with lat/lon. It's updated reguarly, free, and reliable.

  1. http://geonames.usgs.gov
  2. http://earth-info.nga.mil/gns/html/index.html
Arpit Agarwal
  • 3,993
  • 24
  • 30
0

Not sure the quality of the data, but give this a shot: http://www.boutell.com/zipcodes/

Mike G
  • 4,713
  • 2
  • 28
  • 31
  • Thanks! I had seen that before. After taking a second look at it, it looks like the latlon of the zipcode is it's center. Do zipcode areas have a particular shape? I guess otherwise I could do a kNN voting on the zipcode. – Andres Mar 11 '11 at 04:15
  • I doubt these shapes are regular, but perhaps you could approximate with a Voronoi map (http://www.sgsi.com/MIUserGroup/Tech_FindNearest_SGSI.htm). – PaulMcG Mar 11 '11 at 15:35
0

If you don't mind a very crude solution, you could adapt the click-map here.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144