Skip to content

Instantly share code, notes, and snippets.

@mtigas
Created May 7, 2010 01:32
Show Gist options
  • Save mtigas/392914 to your computer and use it in GitHub Desktop.
Save mtigas/392914 to your computer and use it in GitHub Desktop.
# See http://www.census.gov/tiger/tms/gazetteer/zips.txt
import csv
from places.models import ZipCode
from django.contrib.gis.geos import fromstr as geo_from_str
state_cache = dict()
reader = csv.reader(open('zips.txt'),delimiter=',',quotechar='"')
for row in reader:
z,created = None,False
if not state_cache.has_key(row[2]):
state_cache[row[2]] = State.objects.get(abbr=row[2])
z,created = ZipCode.objects.get_or_create(
zip_id=int(row[1]),
defaults = {
'state':state_cache[row[2]],
'location':geo_from_str("POINT(-%s %s)" % (row[4],row[5]))
}
)
if created:
z.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment