-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCity_GEOJSON.py
271 lines (243 loc) · 12.4 KB
/
City_GEOJSON.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import pandas as pd
import json
import datetime
from pandas import DataFrame
import boto3
import logging
import os
from shapely.geometry import shape, Point
import psycopg2
from django.conf import settings
from UnoCPI import settings
logger = logging.getLogger("UNO CPI CITY COUNCIL Application")
dirname = os.path.dirname(__file__)
county_file = os.path.join(dirname, 'home/static/GEOJSON/USCounties_final.geojson')
district_file = os.path.join(dirname, 'home/static/GEOJSON/CityCouncilDistricts.geojson')
output_filename = os.path.join(dirname,
'home/static/GEOJSON/City.geojson') # The file will be saved under static/GEOJSON
currentDT = datetime.datetime.now()
ACCESS_ID = settings.AWS_ACCESS_KEY_ID
ACCESS_KEY = settings.AWS_SECRET_ACCESS_KEY
s3 = boto3.resource('s3',
aws_access_key_id=ACCESS_ID,
aws_secret_access_key=ACCESS_KEY)
with open(county_file) as f:
geojson1 = json.load(f)
county = geojson1["features"]
# Get lat long details of all the districts within State Nebraska to get populate City Council Districts
with open(district_file) as f:
geojson = json.load(f)
district = geojson["features"]
# setup connection to database
conn = psycopg2.connect(user=settings.DATABASES['default']['USER'],
password=settings.DATABASES['default']['PASSWORD'],
host=settings.DATABASES['default']['HOST'],
port=settings.DATABASES['default']['PORT'],
database=settings.DATABASES['default']['NAME'],
sslmode="require")
if (conn):
cursor = conn.cursor()
logger.info("Connection Successful!")
else:
logger.info("Connection Error!")
collection = {'type': 'FeatureCollection', 'features': []}
# Function that generates GEOJSON
def feature_from_row(Community, commId, Address, Mission, MissionType, City, CommunityType, longitude, latitude,
Website, Cec_status, legislative_district):
feature = {
'type': 'Feature',
'properties': {
'CommunityPartner': '',
'Address': '',
'Projects': '',
'College Name': '',
'Mission Type': '',
'Project Name': '',
'Legislative District Number': '',
'Number of projects': '',
'Income': '',
'City': '',
'County': '',
'Mission Area': '',
'CommunityType': '',
'Campus Partner': '',
'Academic Year': '',
'Website': '',
'Community CEC Status': ''
},
'geometry': {
'type': 'Point',
'coordinates': []
},
'communityprojects': [
{
'name': '',
'academicYear': [],
'campuspartner': [],
'engagementType': '',
'collegeNames': []}]
}
feature['geometry']['coordinates'] = [longitude, latitude]
coord = Point([longitude, latitude])
print('latitude--', latitude, ' longitude--', longitude, ' address--', Address, ' Community--', Community)
for i in range(len(district)): # iterate through a list of district polygons
property = district[i]
polygon = shape(property['geometry']) # get the polygons
if polygon.contains(coord): # check if a partner is in a polygon
# print('property["properties"]--',property["properties"]["id"])
# print('legislative_district--from database,',legislative_district)
feature['properties']['Legislative District Number'] = property["properties"]["DISTRICT"]
print(property['properties'])# assign the district number to a partner
for m in range(len(county)): # iterate through the County Geojson
properties2 = county[m]
polygon = shape(properties2['geometry']) # get the polygon
if polygon.contains(coord): # check if the partner in question belongs to a polygon
feature['properties']['County'] = properties2['properties']['NAME']
feature['properties']['Income'] = properties2['properties']['Income']
feature['properties']['CommunityPartner'] = Community
feature['properties']['CommunityType'] = CommunityType
feature['properties']['Website'] = Website
feature['properties']['Community CEC Status'] = Cec_status
feature['properties']['Mission Area'] = Mission
feature['properties']['Mission Type'] = MissionType
feature['properties']['City'] = City
get_project_sql = "SELECT project_name, (select academic_year from projects_academicyear ay where ay.id = p.academic_year_id) as startyear , \
(select academic_year from projects_academicyear where id = COALESCE(p.end_academic_year_id,p.academic_year_id)) as endyear, \
pem.name as engagement_type , array_agg(distinct pc2.name) as campus_partner ,\
array_agg(um.college_name) as collegeList \
FROM projects_project P \
join projects_academicyear pa on P.academic_year_id = pa.id \
join projects_projectcampuspartner pc on P.id = pc.project_name_id \
join projects_projectcommunitypartner ppc on P.id = ppc.project_name_id \
join partners_communitypartner ppcp on ppc.community_partner_id = ppcp.id \
join partners_campuspartner pc2 on pc.campus_partner_id= pc2.id \
join university_college um on um.id = pc2.college_name_id \
join projects_engagementtype pem on p.engagement_type_id = pem.id \
join projects_status ps on ps.id = p.status_id \
WHERE ps.name != 'Drafts' and ppcp.id = %s group by project_name, \
startyear, endyear, engagement_type"
communityprojectsList = []
count = 0
yearlist = []
campuslist = []
cursor.execute(get_project_sql, (str(commId),))
# print('project cursor.fetchall()--',cursor.fetchall())
projectsCount = cursor.fetchall()
if len(projectsCount) > 0:
count = len(projectsCount)
for x in projectsCount:
projYearList = []
name = x[0]
start_academic_year = x[1]
end_academic_year = x[2]
eng_type = x[3]
campList = x[4]
collegeList = x[5]
for x in campList:
if x not in campuslist:
campuslist.append(x)
if (start_academic_year not in yearlist):
yearlist.append(start_academic_year)
if (start_academic_year not in projYearList):
projYearList.append(start_academic_year)
if (end_academic_year not in yearlist):
yearlist.append(end_academic_year)
if (end_academic_year not in projYearList):
projYearList.append(end_academic_year)
if (start_academic_year is not None and end_academic_year is not None):
cursor.execute("select academic_year from projects_academicyear \
where id < (select id from projects_academicyear where academic_year = %s) \
and id > (select id from projects_academicyear where academic_year = %s)",
(str(end_academic_year), str(start_academic_year),))
academicList = cursor.fetchall()
if len(academicList) != 0:
for obj in academicList:
if (obj[0] not in yearlist):
yearlist.append(obj[0])
if (obj[0] not in projYearList):
projYearList.append(obj[0])
else:
print('Academic Year not found')
projname = ''
try:
Projectname = name.split(':')
except ValueError:
print('name does not have year')
projname = Projectname
else:
for i in range(0, len(Projectname) - 1):
projname += Projectname[i]
projObj = {'name': str(projname) + ';' + str(start_academic_year), 'academicYear': projYearList,
'campuspartner': campList, 'engagementType': eng_type, 'collegeNames': collegeList}
communityprojectsList.append(projObj)
feature['properties']['Number of projects'] = count
if len(yearlist) > 0:
yearlist.sort()
feature['properties']['Academic Year'] = yearlist
feature['properties']['Campus Partner'] = campuslist
feature['communityprojects'] = communityprojectsList
return feature
# Get all the Community Partners from the database
select_comm_partner = "SELECT distinct pc.name as Community_Partner, pc.id as commId, pc.address_line1, pc.address_line2, \
pc.city, pc.state,pc.zip, hm.mission_name ,pm.mission_type, \
pc.legislative_district,pc.median_household_income, pct.community_type,\
pc.website_url, pc.longitude, pc.latitude, COALESCE(ces.name, 'Never') as name \
FROM partners_communitypartner PC \
join projects_projectcommunitypartner pcp on PC.id = pcp.community_partner_id \
join projects_project proj on pcp.project_name_id = proj.id \
join projects_status ps on ps.id = proj.status_id \
join projects_projectcampuspartner pcam on proj.id = pcam.project_name_id \
join partners_communitypartnermission pm on PC.id = pm.community_partner_id \
join home_missionarea hm on pm.mission_area_id = hm.id \
join partners_communitytype pct on PC.community_type_id = pct.id \
left join partners_cecpartnerstatus ces on PC.cec_partner_status_id = ces.id \
where \
(pc.address_line1 not in ('','NA','N/A') or pc.city not in ('','NA','N/A') or pc.state not in ('','NA','N/A')) \
and pc.longitude is not null \
and pc.latitude is not null \
and ps.name != 'Drafts' \
and lower(pm.mission_type) = 'primary'"
cursor.execute(select_comm_partner)
commPartnerList = cursor.fetchall()
count_partners = repr(len(commPartnerList)) if len(commPartnerList) != 0 else 0
previous_file = 'home/static/GEOJSON/City.geojson'
previous_count = 0
try:
with open(previous_file, 'r') as file:
previous_geojson = json.load(file)
previous_count = int(previous_geojson.get('total_count', 0))
except FileNotFoundError:
print("Previous file not found.")
except json.JSONDecodeError:
print("Error reading JSON from the previous file.")
geojson_with_count = {'type': 'FeatureCollection', 'total_count': count_partners, 'previous_file_count': previous_count, 'features': []}
geojson_with_count['features'] = collection['features']
if commPartnerList is not None:
print('length of comm partners---', len(commPartnerList))
for obj in commPartnerList:
fulladdress = str(obj[2]) + ' ' + str(obj[4]) + ' ' + str(obj[5])
print('fulladdress--', fulladdress)
print(
'Community, commId, Address, Mission, MissionType, City, CommunityType, longitude,latitude, Website,Cec_status,legislative_district')
print('-feature row-', obj[0], obj[1], fulladdress, obj[7], obj[8], obj[4], obj[11], obj[13], obj[14], obj[12],
obj[15], obj[9])
feature = feature_from_row(obj[0], obj[1], str(fulladdress), obj[7], obj[8], obj[4], obj[11], obj[13], obj[14],
obj[12], obj[15], obj[9])
collection['features'].append(feature)
jsonstring = pd.io.json.dumps(geojson_with_count,indent=2)
logger.info("City Council District GEOSON is written at output directory" + str(output_filename))
with open(output_filename, 'w') as output_file:
output_file.write(format(jsonstring))
# print('jsonstring--',jsonstring)
s3.Object(settings.AWS_STORAGE_BUCKET_NAME, 'geojson/City.geojson').put(Body=format(jsonstring))
print("City Council District GEOJSON file written having total records of " + repr(
len(commPartnerList)) + " in S3 bucket " + settings.AWS_STORAGE_BUCKET_NAME + " at " + str(currentDT))
logger.info("City Council District GEOJSON file written having total records of " + repr(
len(commPartnerList)) + " in S3 bucket " + settings.AWS_STORAGE_BUCKET_NAME + " at " + str(currentDT))
else:
print("City Council District GEOJSON file NOT written having total records of " + repr(
len(commPartnerList)) + " in S3 bucket " + settings.AWS_STORAGE_BUCKET_NAME + " at " + str(currentDT))
logger.info("City Council District GEOJSON file NOT written having total records of " + repr(
len(commPartnerList)) + " in S3 bucket " + settings.AWS_STORAGE_BUCKET_NAME + " at " + str(currentDT))
cursor.close()
conn.close()