Skip to content

Instantly share code, notes, and snippets.

@ian29
Created July 18, 2012 22:44
Show Gist options
  • Save ian29/3139459 to your computer and use it in GitHub Desktop.
Save ian29/3139459 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import psycopg2
con = psycopg2.connect("dbname=nyc3 user=postgres") # CONnect to a database
cur = con.cursor()
cur.execute('''select ctidfp00 from tracts;''')
results = cur.fetchall()
for result in results:
tract_id = result[0] # create a variable from the first column of the results
cmd = ''.join([
'pgsql2shp -u postgres -f tract_', tract_id, ' nyc3 ',
'"select a.gid,a.the_geom,a.overlap from import_buildings a ',
'where st_dwithin(a.the_geom, (select b.the_geom from tracts b where cast(ctidfp00 as numeric) = ', tract_id, '), 0);"',
])
print(cmd)
cur.close()
con.close()
test_tract_building.py (END)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment