-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtomnod.py
148 lines (135 loc) · 6.1 KB
/
tomnod.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
"""created by Daniel Cao
This script creates a table from coordinates to corresponding tif images where you can find those coordinates and
will look for the catalog IDs of those which are missing that information
Steps:
1. Loads the tomnod geojson file and tifRange file
2. Cleans the list of catalog IDs into a column called 'complete_catalog_id'
3. Creates a reference table for the damage points <-> tif file
"""
import geopandas as gpd
import pandas as pd
import csv
import os
os.chdir('/gscratch/choe/dds/postevent')
#load the tomnod geojson file
tomnod = gpd.read_file('digitalglobe_crowdsourcing_hurricane_harvey_20170915.geojson')
tomnod_x = tomnod['geometry'].x
tomnod_y = tomnod['geometry'].y
tomnod['tomnod_x'] = tomnod_x
tomnod['tomnod_y'] = tomnod_y
def processTup(tup):
return [float(ele) for ele in (tup.strip('()').split(','))]
tifRange = pd.read_csv('tifRange-post-1.csv')
def getTifFromCoor(x, y, catalog):
for index, row in tifRange.iterrows():
minxy = processTup(row['minxy'])
maxxy = processTup(row['maxxy'])
if x >= minxy[0] and x <= maxxy[0] \
and y >= minxy[1] and y <= maxxy[1] \
and catalog == row['catalog_id']:
return row['tif']
#print(tifRange.head(5))
cat_id_range = dict()
#get range of catalog_id
for catalog_id in tifRange['catalog_id'].unique():
for index,row in tifRange.iterrows():
if row['catalog_id'] == catalog_id:
minx = processTup(row['minxy'])[0]
miny = processTup(row['minxy'])[1]
maxx = processTup(row['maxxy'])[0]
maxy = processTup(row['maxxy'])[1]
if catalog_id not in cat_id_range:
cat_id_range[catalog_id] = ((minx,miny),(maxx,maxy))
else:
cat_id_range[catalog_id] = ((min(minx,cat_id_range[catalog_id][0][0]),
min(miny,cat_id_range[catalog_id][0][1])),
(max(maxx,cat_id_range[catalog_id][1][0]),
max(maxy,cat_id_range[catalog_id][1][1])))
for k,v in cat_id_range.items():
print (k,v)
print(tomnod.head(5))
#process tomnod further to recover the missing catalog id
post_event_catalog = ['105001000B95E200', '105001000B95E100', '1040010032211E00']
for index, row in tomnod.iterrows():
if row['catalog_id'] in post_event_catalog:
s = row['catalog_id']
#row['complete_catalog_id'] = s
tomnod.set_value(index,'complete_catalog_id',s)
#print("found existing post catalog", s)
elif row['catalog_id'] == '':
for k,v in cat_id_range.items():
if row['tomnod_x'] >= v[0][0] and row['tomnod_x'] <= v[1][0] and \
row['tomnod_y'] >= v[0][1] and row['tomnod_y'] <= v[1][1]:
#row['complete_catalog_id'] = k
tomnod.set_value(index,'complete_catalog_id',k)
tomnod.set_value(index,'catalog_id',k)
print(index)
print(tomnod.loc[tomnod['catalog_id'] == ''])
print(tomnod.loc[tomnod['complete_catalog_id'] == ''])
os.chdir('/gscratch/choe/andrew_test')
#be careful when uncomment this
tomnod.to_file('tomnod_complete-both-columns.geojson', driver='GeoJSON')
os.chdir('/gscratch/choe/dds/postevent')
print(tomnod.complete_catalog_id.unique())
#make a complete table for corresponding tif to images of POST EVENT
#this is very costly
#be careful with the output file name
list_of_post = ['105001000B95E200', '105001000B95E100', '1040010032211E00',
'105001000B9D8100', '1030010070C13600', '105001000B9D7F00',
'10400100324DAE00', '1020010065DF2700', '1020010068D6F400',
'1020010067290D00']
'''
with open('coordinateAndTif-post-3.csv','w') as myFile:
writer = csv.writer(myFile)
writer.writerow(['post_catalog_id','post_tif','pre_catalog_id','pre_tif','min_xy','max_xy','x_coord', 'y_coord', 'label'])
for index, row in tomnod.iterrows():
print("row in tomnod: ",index)
if row['complete_catalog_id'] in list_of_post:
for index_t, row_t in tifRange.iterrows():
minxy = processTup(row_t['minxy'])
maxxy = processTup(row_t['maxxy'])
if row['tomnod_x'] >= minxy[0] and row['tomnod_x'] <= maxxy[0] \
and row['tomnod_y'] >= minxy[1] and row['tomnod_y'] <= maxxy[1] and row['catalog_id'] == row_t['catalog_id']:
temp = [row['complete_catalog_id'], getTifFromCoor(row['tomnod_x'],row['tomnod_y'],row['complete_catalog_id']),
row_t['catalog_id'],getTifFromCoor(row['tomnod_x'],row['tomnod_y'],row_t['catalog_id']),
row_t['minxy'], row_t['maxxy'],
row['tomnod_x'], row['tomnod_y'], row['label']]
#if (row['catalog_id'] == row_t['catalog_id']):
# temp.append(row['label'])
#else:
# temp.append('non-damaged '+ row['label'])
writer.writerow(temp)
break
'''
import os
import gdal
def getRangeTif(tif):
#print(tif)
ds = gdal.Open(tif)
width = ds.RasterXSize
height = ds.RasterYSize
gt = ds.GetGeoTransform()
minx = gt[0]
miny = gt[3] + width*gt[4] + height*gt[5]
# from http://gdal.org/gdal_datamodel.html
maxx = gt[0] + width*gt[1] + height*gt[2]
# from http://gdal.org/gdal_datamodel.html
maxy = gt[3]
range = ((minx,miny),(maxx,maxy))
return range
for index, row in tomnod.iterrows():
if index%1000 == 0:
print('tomnod row: ',index)
s = row['complete_catalog_id']
for file in os.listdir(s+'/'):
if file.endswith('.tif'):
minmax = getRangeTif(s+'/'+file)
minxy = minmax[0]
maxxy = minmax[1]
if row['tomnod_x'] >= minxy[0] and row['tomnod_x'] <= maxxy[0] \
and row['tomnod_y'] >= minxy[1] and row['tomnod_y'] <= maxxy[1]:
#print(file)
tomnod.set_value(index,'tif',file)
break
os.chdir('/gscratch/choe/andrew_test')
tomnod.to_file('coordinateAndTif.geojson', driver="GeoJSON")