Created
June 26, 2014 14:18
-
-
Save vallettea/5703ba6aff00dee26032 to your computer and use it in GitHub Desktop.
closest neighbour in python postgis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import psycopg2 | |
| import pandas | |
| from collections import defaultdict | |
| data = pandas.read_csv("learning_dataset.csv", sep=";", names="gid,borough,block,lot,zipcode,firecomp,policeprct,ltdheight,bldgclass,landuse,easements,ownertype,lotarea,bldgarea,comarea,resarea,officearea,retailarea,garagearea,strgearea,factryarea,otherarea,areasource,numbldgs,numfloors,unitsres,unitstotal,lotfront,lotdepth,bldgfront,bldgdepth,ext,proxcode,irrlotcode,lottype,bsmtcode,assessland,assesstot,exemptland,exempttot,yearbuilt,builtcode,yearalter1,yearalter2,histdist,landmark,builtfar,residfar,commfar,facilfar".split(",")) | |
| # Connect to an existing database | |
| conn = psycopg2.connect("dbname=tahar user=vallette") | |
| # Open a cursor to perform database operations | |
| cur = conn.cursor() | |
| cur2 = conn.cursor() | |
| # for each fire find the closest building | |
| fire_to_building = {} | |
| cur.execute("SELECT unique_key, st_x(st_centroid(st_transform(geom,4326))), st_y(st_centroid(st_transform(geom,4326))) FROM nyc_fireaccidents;") | |
| for fire in cur.fetchall(): | |
| cur2.execute( | |
| "SELECT gid FROM mappluto_manhattan \ | |
| ORDER BY st_transform(geom,4326) <-> st_setsrid(st_makepoint(%s,%s),4326) \ | |
| LIMIT 1;" % (str(fire[1]), str(fire[2])) | |
| ) | |
| fire_to_building[int(fire[0])] = cur2.fetchone()[0] | |
| print fire, fire_to_building[int(fire[0])] | |
| building_to_fires = defaultdict(list) | |
| for key, value in sorted(fire_to_building.iteritems()): | |
| building_to_fires[value].append(key) | |
| data["number_of_fires"] = data["gid"].apply(lambda x: len(building_to_fires[x])) | |
| # Close communication with the database | |
| cur.close() | |
| conn.close() | |
| data.to_csv("to_learn.csv",sep=";") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment