-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConDistAreas.py
More file actions
115 lines (103 loc) · 4.01 KB
/
ConDistAreas.py
File metadata and controls
115 lines (103 loc) · 4.01 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#Coded by Matthew Harrison, July, 2015.
#Read ESRI shapefiles and calculate district areas
#Using Albers Equal Area Projection for North America
#Including Alaska and Hawaii
from mpl_toolkits.basemap import Basemap
from pyproj import Proj
from shapely.geometry import LineString, Point, shape
import fiona
from fiona import collection
import numpy as np
import pandas
import argparse
#Shapefiles should have been downladed from
#http://cdmaps.polisci.ucla.edu/
#and unzipped in the current directory.
#for con in np.arange(106,114):
for con in [114]:
fnam='districtShapes/districts'+str(con)+'.shp'
print fnam
districts=fiona.open(fnam)
lat1=districts.bounds[1]
lat2=districts.bounds[3]
m = Proj(proj='aea',lat_1=lat1,lat_2=lat2,lat_0=np.mean((lat1,lat2)))
Districts=[]
for pol in fiona.open(fnam):
if pol['geometry'] is None:
print 'Bad polygon',pol['properties']
continue
# Polygons
coords=pol['geometry']['coordinates']
if pol['geometry']['type'] == 'Polygon':
lons=[];lats=[]
for c in coords[0]:
lons.append(c[0])
lats.append(c[1])
try:
x,y=m(lons,lats)
except:
print pol['properties']
print pol['geometry']['type']
raise
poly={'type':'Polygon','coordinates':[zip(x,y)]}
center=shape(poly).centroid
ccoords= shape(center).coords[:][0]
xc=ccoords[0];yc=ccoords[1]
lonc,latc=m(xc,yc,inverse=True,radians=False)
Districts.append({'STATENAME':pol['properties']['STATENAME'],
'DISTRICT':pol['properties']['DISTRICT'],
'COUNTY':pol['properties']['COUNTY'],
'ID':pol['properties']['ID'],'area':shape(poly).area,'centroid':[lonc,latc]})
# print shape(poly).centroid
elif pol['geometry']['type'] == 'MultiPolygon':
# Multiple Polygons
for p in coords:
lons=[];lats=[]
for c in p[0]:
lons.append(c[0])
lats.append(c[1])
try:
x,y=m(lons,lats)
except:
print pol['properties']
print pol['geometry']['type']
raise
poly={'type':'Polygon','coordinates':[zip(x,y)]}
center=shape(poly).centroid
ccoords= shape(center).coords[:][0]
xc=ccoords[0];yc=ccoords[1]
lonc,latc=m(xc,yc,inverse=True,radians=False)
Districts.append({'STATENAME':pol['properties']['STATENAME'],
'DISTRICT':pol['properties']['DISTRICT'],
'COUNTY':pol['properties']['COUNTY'],
'ID':pol['properties']['ID'],'area':shape(poly).area,'centroid':[lonc,latc]})
# print shape(poly).centroid.wkt
Districts=sorted(Districts,key=lambda d:(d['STATENAME'],int(d['DISTRICT'])))
# Write Areas to csv
filenam='areas'+str(con)+'.txt'
f=open(filenam,'w')
pr=None
for d in Districts:
if pr is not None:
if d['STATENAME'] != pr['STATENAME']:
print d['STATENAME']
if d['DISTRICT']==pr['DISTRICT']:
a=a+d['area']
center.append(d['centroid'])
else:
line=pr['ID'],pr['DISTRICT'],'area='+str(a/1.e6),pr['STATENAME']+'\n'
f.write(','.join(line))
line=pr['ID'],pr['DISTRICT'],'centroid='+str(center)+'\n'
f.write(','.join(line))
a=d['area']
center=[d['centroid']]
pr=d.copy()
else:
pr=d.copy()
a=d['area']
center=[d['centroid']]
line=pr['ID'],pr['DISTRICT'],'area='+str(a/1.e6),pr['STATENAME']+'\n'
f.write(','.join(line))
line=pr['ID'],pr['DISTRICT'],'centroid='+str(center)+'\n'
f.write(','.join(line))
f.close()