-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeospatial.py
44 lines (34 loc) · 1.41 KB
/
geospatial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''
Created on Dec 4, 2014
@author: jeromethai
'''
import numpy as np
import psycopg2
import utm
latmax, latmin, longmax, longmin = 34.288621 , 33.9185, -117.646497, -118.394048
def isInBox(lat, long):
return (lat < latmax and lat > latmin and long < longmax and long > longmin)
def selectTAZinBox():
"""Select TAZ ids that are in LA county and in the bounding box specified by isInBox
"""
tazInBox = {}
conn = psycopg2.connect(dbname='ca-census', user='postgres')
cur = conn.cursor()
cur.execute('SELECT ST_AsText(ST_Centroid(geom)), gid, ST_AsText(geom) FROM taz_data WHERE county = \'037\'')
for row in cur.fetchall():
tmp = map(float, row[0][6:-1].split())
lat, long = utm.to_latlon(tmp[0], tmp[1] , 11, 'N') #shapefiles are in the UTM "11N" coordinates
if isInBox(lat, long): tazInBox[row[1]] = [lat, long, row[2]]# if centroid is in box
return tazInBox
def countNodeInTAZ(tazInBox):
"""count number of nodes in each TAZ
"""
conn = psycopg2.connect(dbname='ca-census', user='postgres')
cur = conn.cursor()
for k,v in tazInBox.items():
cur.execute("SELECT count(*) FROM la_nodes WHERE ST_Contains( ST_GeomFromText('"+ v[2] +"'), geom)")
v.append(cur.fetchall()[0][0])
return tazInBox
if __name__ == '__main__':
tazInfo = countNodeInTAZ(selectTAZinBox())
for k,v in tazInfo.items(): print k,v[3]