forked from MichaelJendryke/NyxHyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing.py
167 lines (149 loc) · 5.92 KB
/
processing.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
import subprocess
import os
import uuid
try:
from osgeo import ogr
except Exception as e:
print(e)
import sql
# from threading import Thread, current_thread
import concurrent.futures
import time
import utilities
class footprint:
def info():
print('Generating Footprints')
def generate(datadir, tempdir):
rows = sql.select("SELECT * FROM footprintmissing", '')
# https://stackoverflow.com/a/15143994/1623867
# ThreadPoolExecutor for I/O bound operations
# ProcessPoolExecutor for CPU bound
multicore = False
start = time.time()
if multicore is True:
executor = concurrent.futures.ProcessPoolExecutor(1)
futures = [executor.submit(
footprint.processor, row, datadir, tempdir
) for row in rows]
concurrent.futures.wait(futures)
else:
for row in rows:
footprint.processor(row, datadir, tempdir)
end = time.time()
print(end - start)
def try_my_operation(row, datadir, tempdir):
try:
print('ID: {ID} {row} {dd} {td}'.format(
ID=os.getpid(), row=row, dd=datadir, td=tempdir
))
except:
print('error with item')
def processor(row, datadir, tempdir):
print('INFO: Process ID: {ID} {row} {dd} {td}'.format(
ID=os.getpid(),
row=row,
dd=datadir,
td=tempdir
))
orderNumber = str(row[0])
filename = str(row[1])
noaaid = str(row[2])
workingdir = os.path.join(tempdir, 'temp')
if not os.path.isdir(workingdir):
os.mkdir(os.path.expanduser(workingdir))
file = os.path.join(datadir, orderNumber, filename)
if not os.path.isfile(file):
print('WARNING: File {f} is not there'.format(f=file))
else:
print(workingdir, orderNumber, file, filename, noaaid)
out = footprint.extract(workingdir, orderNumber, file)
# This will load the shape to the database if the file exists
if os.path.exists(out):
footprint.loadgeomtopgsql(out, filename, noaaid, orderNumber)
utilities.filesandfolders.deletefiles(workingdir)
def extract(workingdir, orderNumber, file):
layer = '//All_Data/VIIRS-DNB-SDR_All/Radiance'
# gdal_translate -of GTiff HDF5:"D:\TEMP\noaa2\GDNBO-SVDNB_npp_d20171125_t2100535_e2106339_b31503_c20171128024404585872_nobc_ops.h5"://All_Data/VIIRS-DNB-SDR_All/Radiance test.tif
infile = 'HDF5:"{file}":{layer}'.format(file=file, layer=layer)
r1 = '{u}{end}'.format(u=str(uuid.uuid4()), end='.tif')
outfile = os.path.join(workingdir, r1) # this should be different from basedir
gdaltranslate = '{tool} {of} {infile} {outfile}'.format(
tool='gdal_translate',
of='-of GTiff',
infile=infile,
outfile=outfile
)
print(gdaltranslate)
subprocess.check_call(gdaltranslate)
# gdalwarp -dstnodata 0 -dstalpha -of GTiff test.tif test2.tif
infile = os.path.join(workingdir, r1)
r2 = '{u}{end}'.format(u=str(uuid.uuid4()), end='.tif')
outfile = os.path.join(workingdir, r2)
gdalwarp = '{tool} {param} {of} {infile} {outfile}'.format(
tool='gdalwarp',
param='-ot Int16 -wt Int16 -dstnodata 0 -dstalpha',
of='-of GTiff',
infile=infile,
outfile=outfile
)
print(gdalwarp)
subprocess.check_call(gdalwarp)
# gdal_polygonize.py -q test2.tif -b 2 -f "ESRI Shapefile" testshape.shp
infile = os.path.join(workingdir, r2)
r3 = '{u}{end}'.format(u=str(uuid.uuid4()), end='.shp')
outfile = os.path.join(workingdir, r3)
polygonize = '{tool} {param1} {infile} {param2} {outfile}'.format(
tool='gdal_polygonize',
param1='-8 -q',
infile=infile,
param2='-b 2 -f "ESRI Shapefile"',
outfile=outfile
)
print(polygonize)
# shell=True is very unsecure we have to fix this!
subprocess.check_call(polygonize, shell=True)
# simplify
# ogr2ogr output.shp input.shp -simplify 0.0001
infile = os.path.join(workingdir, r3)
outfile = os.path.join(workingdir, '{u}{end}'.format(
u=str(uuid.uuid4()),
end='.shp')
)
simplify = '{tool} {outfile} {infile} {param}'.format(
tool='ogr2ogr',
outfile=outfile,
infile=infile,
param='-simplify 0.2'
)
print(simplify)
subprocess.check_call(simplify)
return outfile
def loadgeomtopgsql(file, filename, noaaid, orderNumber):
reader = ogr.Open(file)
layer = reader.GetLayer(0)
try:
epsg = layer.GetSpatialRef().GetAuthorityCode("GEOGCS")
except:
print('Cannot find EPSG code')
for i in range(layer.GetFeatureCount()):
feature = layer.GetFeature(i)
# print(feature.ExportToJson())
geom = feature.geometry()
SQL = "INSERT INTO {table}(file_name, noaaid, orderNumber, footprint) SELECT '{fn}', {ni}, {on}, ST_GeomFromText('{geom}',{epsg})".format(
table='imagedata',
fn=filename,
ni=noaaid,
on=orderNumber,
geom=str(geom),
epsg=str(epsg)
)
data = ('',)
try:
sql.insert(SQL,data)
except Exception as e:
raise
else:
pass
finally:
pass
# INSERT INTO imagedata(footprint) SELECT ST_GeomFromText('POLYGON ((47.8986402364685 -19.9761359737374,77.2019166143206 -24.5331415521829,75.348830485111 -44.4051468911004,38.8567335982238 -38.6872585624496,47.8986402364685 -19.9761359737374))',4326)