Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from MITLibraries/shapereader
Browse files Browse the repository at this point in the history
Use context manager for shapefile reader
  • Loading branch information
Mike Graves committed Jun 15, 2017
2 parents 4b07510 + f98fb00 commit 677bc03
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions slingshot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,37 @@ def load_layer(bag):
encoding = fp.read().strip()
except:
encoding = 'UTF-8'
sf = Reader(bag.shp)
geom_type = GEOM_TYPES[sf.shapeType]
fields = sf.fields[1:]
t = table(bag.name, geom_type, srid, fields)
if t.exists():
raise Exception('Table {} already exists'.format(bag.name))
t.create()
try:
with engine().begin() as conn:
reader = PGShapeReader(sf, srid, encoding)
cursor = conn.connection.cursor()
cursor.copy_from(reader, '"{}"'.format(bag.name))
with engine().connect() as conn:
conn.execute('CREATE INDEX "idx_{}_geom" ON "{}" USING GIST '
'(geom)'.format(bag.name, bag.name))
except:
t.drop()
raise
with ShapeReader(bag.shp) as sf:
geom_type = GEOM_TYPES[sf.shapeType]
fields = sf.fields[1:]
t = table(bag.name, geom_type, srid, fields)
if t.exists():
raise Exception('Table {} already exists'.format(bag.name))
t.create()
try:
with engine().begin() as conn:
reader = PGShapeReader(sf, srid, encoding)
cursor = conn.connection.cursor()
cursor.copy_from(reader, '"{}"'.format(bag.name))
with engine().connect() as conn:
conn.execute('CREATE INDEX "idx_{}_geom" ON "{}" USING GIST '
'(geom)'.format(bag.name, bag.name))
except:
t.drop()
raise


class ShapeReader(Reader):
def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, tb):
self.close()

def close(self):
if self.shp:
self.shp.close()
if self.shx:
self.shx.close()
if self.dbf:
self.dbf.close()

0 comments on commit 677bc03

Please sign in to comment.