Skip to content

Instantly share code, notes, and snippets.

@evz
Created October 29, 2011 17:40
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 evz/1324841 to your computer and use it in GitHub Desktop.
Save evz/1324841 to your computer and use it in GitHub Desktop.
Import shapefiles to models
#!/usr/bin/env python
# For this script to work you must set the Django settings file
# as an environment setting before importing LayerMapping
# Alternatively you can place
# export DJANGO_SETTINGS_MODULE=settings
# in your .bash_profile
# or paste this code into a $ manage.py shell
import sys
import site
import os
vepath = '/home/web/sites/demo/lib/python2.7/site-packages' # adjust for your env
prev_sys_path = list(sys.path)
# add the site-packages of our virtualenv as a site dir
site.addsitedir(vepath)
sys.path.append('/home/web/sites/demo/map_demo') # adjust this, too
sys.path.append('/home/web/sites/demo/') # and this
new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
sys.path.remove(item)
sys.path[:0] = new_sys_path
os.environ['DJANGO_SETTINGS_MODULE'] = 'map_demo.settings' # put your settings info here
from psycopg2 import IntegrityError
from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs
from map_demo.california.models import County, Place # put your models here
try:
add_postgis_srs(900913)
except IntegrityError:
print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert"
# Adjust to find your data files
County_shp = '/home/web/sites/demo/data/county/tl_2010_06_county10.shp'
Place_shp = '/home/web/sites/demo/data/place/tl_2010_06_place10.shp'
County_mapping = {
'state_fips' : 'STATEFP10',
'county_fips' : 'COUNTYFP10',
'county_ansi' : 'COUNTYNS10',
'geo_id' : 'GEOID10',
'name' : 'NAME10',
'name_trans' : 'NAMELSAD10',
'mpoly' : 'MULTIPOLYGON',
}
Place_mapping = {
'state_fips' : 'STATEFP10',
'place_fips' : 'PLACEFP10',
'place_ansi' : 'PLACENS10',
'geo_id' : 'GEOID10',
'name' : 'NAME10',
'name_trans' : 'NAMELSAD10',
'mpoly' : 'MULTIPOLYGON',
}
County_layer = LayerMapping(County,
County_shp,
County_mapping,
transform=False,
encoding='iso-8859-1')
County_layer.save(verbose=True, strict=True, progress=True)
Place_layer = LayerMapping(Place,
Place_shp,
Place_mapping,
transform=False,
encoding='iso-8859-1')
Place_layer.save(verbose=True, strict=True, progress=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment