Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active December 28, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsundram/7508604 to your computer and use it in GitHub Desktop.
Save jsundram/7508604 to your computer and use it in GitHub Desktop.
Working with my shapefile index code UPDATE: See this gist instead: https://gist.github.com/jsundram/7509686
import csv
# Assumes you have read.py, defined here https://gist.github.com/jsundram/1918435
# in the same directory
# TODO: you'll need to install its dependencies, too:
# http://toblerity.org/rtree/install.html#nix
# pip install rtree (requires libspatialindex)
# pip install pyshp
import read
def get_latlon(filename='my_data.csv'):
"""Reads the input csv, returns lat/lng as tuples"""
data = []
with open(filename) as f:
r = csv.DictReader(f)
for row in r:
data.append((row['Latitude'], row['Longitude']))
return data
def main():
# get the data from the csv
# TODO: provide your filename
your_data = get_latlon()
# TODO: you'll want to provide your own shapefile here:
shapes = read.read_shapefile('./us_county') # http://www2.census.gov/geo/tiger/TIGER2009/tl_2009_us_county.zip
idx = read.make_index(shapes)
summary = defaultdict(int)
for (lat, lng) in your_data:
shape = query_index(idx, shapes, lat, lng)
summary[shape] += 1
json.dump(summary, open('data.json', 'w')) # scale this later
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment