def import_county_shape(file): counties = DataSource(file) lm = LayerMapping(CountyMap, counties, mapping(counties, geom_name='geometry')) lm.save( verbose=True ) # Saves to database - need to first migrate County and State model to database
def run(): import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from world.models import WorldBorders try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" WORLD_SHP = 'apps/world/data/TM_WORLD_BORDERS-0.3.shp' layer = LayerMapping(WorldBorders, WORLD_SHP, mapping(WORLD_SHP,geom_name='mpoly',multi_geom=True), transform=False, encoding='iso-8859-1') layer.save(verbose=True,strict=True,progress=True) print 'Fixing invalid polygons...' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)'])) for item in WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)']): corrected = item.mpoly.buffer(0) item.mpoly = corrected item.save() print '....' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)'])) if num: print '%s invalid polygons remain' % num print WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)'])
#!/usr/bin/python """Utility for adding the Ward layermapping from data/ to the cape.lionhead project.""" import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from lionshead.models import Ward try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" wards = 'data/wards_4326.shp' wlayer = LayerMapping(Ward, wards, mapping(wards, geom_name='geometry', multi_geom=True), encoding='Latin1') wlayer.save(verbose=True, progress=True)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from world.models import WorldBorders try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" WORLD_SHP = 'world/data/TM_WORLD_BORDERS-0.3.shp' layer = LayerMapping(WorldBorders, WORLD_SHP, mapping(WORLD_SHP, geom_name='geometry', multi_geom=True), transform=False, encoding='iso-8859-1') layer.save(verbose=True, strict=True, progress=True) print 'Fixing invalid polygons...' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)'])) for item in WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)']): corrected = item.geometry.buffer(0) item.geometry = corrected item.save() print '....' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)'])) if num:
def gishandle( self, model_name, filepath, *args, **options ): from django.contrib.gis.utils import ogrinspect, mapping print ogrinspect( filepath, model_name, srid=options['srid'], blank=True ) print "\n\n%s_mapping = %s" % ( model_name.lower(), mapping( filepath ))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from world.models import WorldBorders try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" WORLD_SHP = 'world/data/TM_WORLD_BORDERS-0.3.shp' layer = LayerMapping(WorldBorders, WORLD_SHP, mapping(WORLD_SHP,geom_name='geometry',multi_geom=True), transform=False, encoding='iso-8859-1') layer.save(verbose=True,strict=True,progress=True) print 'Fixing invalid polygons...' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)'])) for item in WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)']): corrected = item.geometry.buffer(0) item.geometry = corrected item.save() print '....' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(geometry)']))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from world.models import WorldBorders try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" WORLD_SHP = 'apps/world/data/TM_WORLD_BORDERS-0.3.shp' layer = LayerMapping(WorldBorders, WORLD_SHP, mapping(WORLD_SHP,geom_name='mpoly',multi_geom=True), transform=False, encoding='iso-8859-1') layer.save(verbose=True,strict=True,progress=True) print 'Fixing invalid polygons...' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)'])) for item in WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)']): corrected = item.mpoly.buffer(0) item.mpoly = corrected item.save() print '....' num = len(WorldBorders.objects.extra(where=['NOT ST_IsValid(mpoly)']))
#!/usr/bin/python """Utility for adding the Ward layermapping from data/ to the cape.lionhead project.""" import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from psycopg2 import IntegrityError from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from lionshead.models import Ward try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" wards = 'data/wards_4326.shp' wlayer = LayerMapping(Ward, wards, mapping(wards, geom_name='geometry',multi_geom=True), encoding='Latin1') wlayer.save(verbose=True, progress=True)
transaction.commit_unless_managed() from django.contrib.gis.utils import mapping, LayerMapping, add_postgis_srs from world.models import WorldBorders try: add_postgis_srs(900913) except IntegrityError: print "The Google Spherical Mercator projection, or a projection with srid 900913, already exists, skipping insert" WORLD_SHP = "world/data/TM_WORLD_BORDERS-0.3.shp" layer = LayerMapping( WorldBorders, WORLD_SHP, mapping(WORLD_SHP, geom_name="geometry", multi_geom=True), transform=False, encoding="iso-8859-1", ) layer.save(verbose=True, strict=True, progress=True) print "Fixing invalid polygons..." num = len(WorldBorders.objects.extra(where=["NOT ST_IsValid(geometry)"])) for item in WorldBorders.objects.extra(where=["NOT ST_IsValid(geometry)"]): corrected = item.geometry.buffer(0) item.geometry = corrected item.save() print "...."
def import_state_shape(file): states = DataSource(file) lm = LayerMapping(StateMap, states, mapping(states, geom_name='geometry')) lm.save(verbose=True)