Skip to content

Instantly share code, notes, and snippets.

@eightysteele
Created October 12, 2011 22:12
Show Gist options
  • Select an option

  • Save eightysteele/1282803 to your computer and use it in GitHub Desktop.

Select an option

Save eightysteele/1282803 to your computer and use it in GitHub Desktop.
hackery
# Rename .dbf file temporarily
dbf_filename = '%s.dbf' % sf
dbf_filename_temp = '%s~' % dbf_filename_temp
os.rename(dbf_filename, dbf_filename_temp)
# Create the .dbf blank (it has no fields)
w = shapefile.Writer(shapefile.POLYGONM)
w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
w.save(dbf_filename)
# Get the projection SRID (default 3857)
proj = open('%s.prj' % sf, 'r').read()
srs = osr.SpatialReference()
srs.ImportFromESRI([proj])
srs.AutoIdentifyEPSG()
srid = srs.GetAuthorityCode(None)
if srid == None:
srid = 3857
# Create SQL file that contains the_geom
sql_file = open('%s.sql' % f, 'wr+')
command = 'shp2pgsql -I -D -d -s %s %s polygons > %s.sql' % (srid, sf, sf)
args = shlex.split(command)
subprocess.call(args, stdout=sql_file)
# Parse SQL file for a list of the_geom data
sql_file.seek(0)
the_geom = sql_file.read().split('\n')[8:-4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment