This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
326 lines (274 loc) · 12.5 KB
/
upload.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import sys
import secrets
__author__ = 'RESarwas'
# dependency pyodbc
# C:\Python27\ArcGIS10.3\Scripts\pip.exe install pyodbc
# dependency cartodb
# C:\Python27\ArcGIS10.3\Scripts\pip.exe install cartodb
try:
from cartodb import CartoDBAPIKey, CartoDBException
except ImportError:
CartoDBAPIKey, CartoDBException = None, None
print 'cartodb module not found, make sure it is installed with'
print 'C:\Python27\ArcGIS10.3\Scripts\pip.exe install cartodb'
sys.exit()
try:
import pyodbc
except ImportError:
pyodbc = None
print 'pyodbc module not found, make sure it is installed with'
print 'C:\Python27\ArcGIS10.3\Scripts\pip.exe install pyodbc'
sys.exit()
def get_connection_or_die():
conn_string = ("DRIVER={{SQL Server Native Client 10.0}};"
"SERVER={0};DATABASE={1};Trusted_Connection=Yes;")
conn_string = conn_string.format('inpakrovmais', 'animal_movement')
try:
connection = pyodbc.connect(conn_string)
except pyodbc.Error as e:
print("Rats!! Unable to connect to the database.")
print("Make sure your AD account has the proper DB permissions.")
print("Contact Regan (regan_sarwas@nps.gov) for assistance.")
print(" Connection: " + conn_string)
print(" Error: " + e[1])
sys.exit()
return connection
def make_location_table_in_cartodb(carto):
sql = ("CREATE TABLE Animal_Locations (ProjectId text NOT NULL, AnimalId text NOT NULL, "
"FixDate timestamp NOT NULL, FixId int NOT NULL)")
execute_sql_in_cartodb(carto, sql)
sql = "select cdb_cartodbfytable('"+secrets.domain+"','Animal_Locations')"
execute_sql_in_cartodb(carto, sql)
def make_movement_table_in_cartodb(carto):
sql = ("CREATE TABLE Animal_Movements (ProjectId text NOT NULL, AnimalId text NOT NULL, "
"StartDate timestamp NOT NULL, EndDate timestamp NOT NULL, Duration real NOT NULL, "
"Distance real NOT NULL, Speed real NOT NULL, "
"Duration_t text NULL, Distance_t text NULL, Speed_t text NULL)")
execute_sql_in_cartodb(carto, sql)
sql = "select cdb_cartodbfytable('"+secrets.domain+"','Animal_Movements')"
execute_sql_in_cartodb(carto, sql)
def execute_sql_in_cartodb(carto, sql):
try:
carto.sql(sql)
except CartoDBException as ce:
print ("CartoDB error ocurred", ce)
def chunks(l, n):
"""Yield successive n-sized chunks from list l."""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def make_cartodb_tracking_tables(connection):
sql = ("if not exists (select * from sys.tables where name='Locations_In_CartoDB')"
" create table Locations_In_CartoDB (fixid int NOT NULL PRIMARY KEY)")
sql2 = ("if not exists (select * from sys.tables where name='Movements_In_CartoDB')"
" create table Movements_In_CartoDB ("
" ProjectId varchar(16) NOT NULL,"
" AnimalId varchar(16) NOT NULL,"
" StartDate datetime2(7) NOT NULL,"
" EndDate datetime2(7) NOT NULL"
" CONSTRAINT PK_Movements_In_CartoDB PRIMARY KEY CLUSTERED ("
" ProjectId ASC, AnimalId ASC, StartDate ASC, EndDate ASC))")
wcursor = connection.cursor()
wcursor.execute(sql)
wcursor.execute(sql2)
try:
wcursor.commit()
except pyodbc.Error as de:
print ("Database error ocurred", de)
print ("Unable to add create the 'Locations_In_CartoDB' table.")
def add_locations_to_carto_tracking_table(connection, fids):
wcursor = connection.cursor()
for fid in fids:
sql = "INSERT Locations_In_CartoDB (fixid)values({0})"
sql = sql.format(fid)
# print(sql)
wcursor.execute(sql)
try:
wcursor.commit()
except pyodbc.Error as de:
print ("Database error ocurred", de)
print ("Unable to add these ids to the 'Locations_In_CartoDB' table.")
print (fids)
def add_movements_to_carto_tracking_table(connection, rows):
# SQL Server is limited to 1000 rows in an insert
wcursor = connection.cursor()
sql = "insert into Movements_In_CartoDB (projectid, animalid, startdate, enddate) values "
for chunk in chunks(rows, 900):
values = ','.join(["('{0}','{1}','{2}','{3}')".format(*row) for row in chunk])
# print sql + values
wcursor.execute(sql + values)
try:
wcursor.commit()
except pyodbc.Error as de:
print ("Database error ocurred", de)
print ("Unable to add these rows to the 'Movements_In_CartoDB' table.")
print (rows)
def remove_locations_from_carto_tracking_table(connection, fids):
wcursor = connection.cursor()
sql = "delete from Locations_In_CartoDB where fixid in "
ids = '(' + ','.join([str(i) for i in fids]) + ')'
wcursor.execute(sql+ids)
try:
wcursor.commit()
except pyodbc.Error as de:
print ("Database error ocurred", de)
print ("Unable to delete these ids from the 'Locations_In_CartoDB' table.")
print (fids)
def remove_movements_from_carto_tracking_table(connection, rows):
wcursor = connection.cursor()
sql = ("delete from Movements_In_CartoDB where "
"projectid = '{0}' and animalid = '{1}' and startdate = '{2}' and enddate = '{3}'")
for row in rows:
sql1 = sql.format(row[0], row[1], row[2], row[3])
wcursor.execute(sql1)
try:
wcursor.commit()
except pyodbc.Error as de:
print ("Database error ocurred", de)
print ("Unable to delete these rows from the 'Movements_In_CartoDB' table.")
print (rows)
def fetch_rows(connection, sql):
rcursor = connection.cursor()
try:
rows = rcursor.execute(sql).fetchall()
except pyodbc.Error as de:
print ("Database error ocurred", de)
rows = None
return rows
def get_locations_for_carto(connection, project):
sql = ("select l.projectid,l.animalid,l.fixid,l.fixdate,"
"location.Lat,Location.Long from locations as l "
"inner join ProjectExportBoundaries as b on l.Location.STIntersects(b.Shape) = 1 "
"left join Locations_In_CartoDB as c on l.fixid = c.fixid "
"where l.ProjectID = '" + project + "' and c.fixid IS NULL and l.[status] IS NULL")
return fetch_rows(connection, sql)
def get_vectors_for_carto(connection, project):
sql = ("select m.Projectid, m.AnimalId, m.StartDate, m.EndDate, m.Duration, m.Distance, m.Speed, "
"m.Shape.ToString() from movements as m "
"inner join ProjectExportBoundaries as b on m.Shape.STIntersects(b.Shape) = 1 "
"left join Movements_In_CartoDB as c "
"on m.ProjectId = c.ProjectId and m.AnimalId = c.AnimalId "
"and m.StartDate = c.StartDate and m.EndDate = c.EndDate "
"where m.ProjectId = '" + project + "' and c.ProjectId IS NULL and Distance > 0")
return fetch_rows(connection, sql)
def fixlocationrow(row):
s = "('{0}','{1}',{2},'{3}',ST_SetSRID(ST_Point({5},{4}),4326))"
return s.format(*row)
def fixmovementrow(row):
s = "('{0}','{1}','{2}','{3}',{4},{5},{6},ST_GeometryFromText('{7}',4326))"
return s.format(*row)
def insert(am, carto, lrows, vrows):
if not lrows:
print('No locations to send to CartoDB.')
if not vrows:
print('No movements to send to CartoDB.')
if not lrows and not vrows:
return
if vrows:
try:
sql = ("insert into animal_movements "
"(projectid, animalid, startdate, enddate, duration, distance, speed, the_geom) values ")
values = ','.join([fixmovementrow(row) for row in vrows])
# print sql + values
carto.sql(sql + values)
try:
add_movements_to_carto_tracking_table(am, vrows)
print('Wrote ' + str(len(vrows)) + ' movements to CartoDB.')
except pyodbc.Error as de:
print ("Database error ocurred", de)
except CartoDBException as ce:
print ("CartoDB error ocurred", ce)
if lrows:
try:
sql = ("insert into animal_locations "
"(projectid,animalid,fixid,fixdate,the_geom) values ")
ids, values = zip(*[(row[2], fixlocationrow(row)) for row in lrows])
values = ','.join(values)
# print sql + values
carto.sql(sql + values)
try:
add_locations_to_carto_tracking_table(am, ids)
print('Wrote ' + str(len(ids)) + ' locations to CartoDB.')
except pyodbc.Error as de:
print ("Database error ocurred", de)
except CartoDBException as ce:
print ("CartoDB error ocurred", ce)
def get_locations_to_remove(connection, project):
# status is the only mutable field in the locations table, so that is all we need to check
# if the boundary changes, then clear then kill and fill
sql = ("select l.fixid from locations as l "
"left join Locations_In_CartoDB as c on l.fixid = c.fixid "
"where ProjectID = '" + project + "' and c.fixid IS NOT NULL "
"and [status] IS NOT NULL")
return fetch_rows(connection, sql)
def get_vectors_to_remove(connection, project):
# movement records are immutable, however changes to location status add/delete records
# if the boundary changes, then clear then kill and fill
sql = ("select c.Projectid, c.AnimalId, c.StartDate, c.EndDate "
"from Movements_In_CartoDB as c left join movements as m "
"on m.ProjectId = c.ProjectId and m.AnimalId = c.AnimalId "
"and m.StartDate = c.StartDate and m.EndDate = c.EndDate "
"where c.ProjectId = '" + project + "' and m.projectid is null")
return fetch_rows(connection, sql)
def remove(am, carto, lrows, vrows):
if not lrows:
print('No locations to remove from CartoDB.')
if not vrows:
print('No movements to remove from CartoDB.')
if not lrows and not vrows:
return
if vrows:
try:
sql = ("delete from animal_movements where "
"projectid = '{0}' and animalid = '{1}' and startdate = '{2}' and enddate = '{3}'")
for row in vrows:
sql1 = sql.format(row[0], row[1], row[2], row[3])
carto.sql(sql1)
try:
remove_movements_from_carto_tracking_table(am, vrows)
print('Removed ' + str(len(vrows)) + ' Movements from CartoDB.')
except pyodbc.Error as de:
print ("Database error ocurred", de)
except CartoDBException as ce:
print ("CartoDB error ocurred", ce)
if lrows:
try:
sql = "delete from animal_locations where fixid in "
ids = [row[0] for row in lrows]
idstr = '(' + ','.join([str(i) for i in ids]) + ')'
carto.sql(sql + idstr)
try:
remove_locations_from_carto_tracking_table(am, ids)
print('Removed ' + str(len(ids)) + ' locations from CartoDB.')
except pyodbc.Error as de:
print ("Database error ocurred", de)
except CartoDBException as ce:
print ("CartoDB error ocurred", ce)
def fix_format_of_vector_columns(carto):
sql = "update animal_movements set distance_t=round(cast(distance as numeric),1) where distance_t is null"
execute_sql_in_cartodb(carto, sql)
sql = "update animal_movements set duration_t=round(cast(duration as numeric),1) where duration_t is null"
execute_sql_in_cartodb(carto, sql)
sql = "update animal_movements set speed_t=round(cast(speed as numeric),1) where speed_t is null"
execute_sql_in_cartodb(carto, sql)
def make_carto_tables():
carto_conn = CartoDBAPIKey(secrets.apikey, secrets.domain)
make_location_table_in_cartodb(carto_conn)
make_movement_table_in_cartodb(carto_conn)
def make_sqlserver_tables():
am_conn = get_connection_or_die()
make_cartodb_tracking_tables(am_conn)
def main():
carto_conn = CartoDBAPIKey(secrets.apikey, secrets.domain)
am_conn = get_connection_or_die()
for project in ['KATM_BrownBear']:
locations = get_locations_to_remove(am_conn, project)
vectors = get_vectors_to_remove(am_conn, project)
remove(am_conn, carto_conn, locations, vectors)
locations = get_locations_for_carto(am_conn, project)
vectors = get_vectors_for_carto(am_conn, project)
insert(am_conn, carto_conn, locations, vectors)
fix_format_of_vector_columns(carto_conn)
if __name__ == '__main__':
# make_carto_tables()
# make_sqlserver_tables()
main()