-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS2processing.py
More file actions
240 lines (174 loc) · 8.39 KB
/
S2processing.py
File metadata and controls
240 lines (174 loc) · 8.39 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
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
##### DEFINE PRODUCT TO BE PLOTTED AND SET UP THE VARIABLES & NAMES
import numpy as np
import os
import fnmatch
import matplotlib as plt
import matplotlib
import matplotlib.pylab as plt
from matplotlib.transforms import Bbox
from skimage import exposure, transform
from mpl_toolkits.basemap import Basemap
import glymur
'''
------------------------------------------------------------------------------------------------------------------------
Functions for Image Processing and Enhancement
------------------------------------------------------------------------------------------------------------------------
'''
class S2processing:
def __init__(self, productName, outputName=True):
'''import image data (bands) from chosen product'''
self.prefix = productName
prod_name = self.prefix[-66:-6]
prod_date = self.prefix[-55:-47]
colors = [None]*3
for item in os.listdir(self.prefix):
if os.path.isdir(os.path.join(self.prefix, item)):
if item == 'GRANULE':
granule = os.path.join(self.prefix,item)
for item1 in os.listdir(granule):
subgranule = os.path.join(granule,item1)
tileID = subgranule[-30:-24]
for item2 in os.listdir(subgranule):
if item2 == 'IMG_DATA':
img_data = os.path.join(subgranule, item2+'\\')
for item3 in os.listdir(img_data):
if fnmatch.fnmatch(item3, '*B04*'):
colors[2] = item3
if fnmatch.fnmatch(item3, '*B03*'):
colors[1] = item3
if fnmatch.fnmatch(item3, '*B02*'):
colors[0] = item3
if outputName:
self.nameOut = tileID+'_'+prod_date
else:
self.nameOut = outputName
# the png will be saved under this nameOut
##### READ RGB VALUES OUT OF JP2000 FILES
print('import RGB values and scale down the array by a factor of 8.')
# bands are read in the order B04 B03 B02 for RGB
ims = list( reversed( list( map( glymur.Jp2k, [ img_data + c for c in colors ]))))
scale = 8 # 8 is a good compromise between speed and detail
raw_bands = [i[::scale, ::scale] for i in ims]
#rgb, nx, ny = np.shape(raw_bands)
self.rgb, self.nx, self.ny = np.shape(raw_bands)
self.rgb = np.asarray(raw_bands)
##########################################################################################
##########################################################################################
def getCoordinates(self):
'''extract tile coordinates from meta data xml file '''
import xml.etree.ElementTree as et
print('get lat/lon values of image corners.')
tree = et.parse(self.prefix+'MTD_MSIL1C.xml')
root = tree.getroot()
a = root.find('.//EXT_POS_LIST')
x = [float(i) for i in a.text.split()] # convert values from string to float
#del(x[-4:]) # delete obsolete values at end of array
self.lat = x[0::2]
self.lon = x[1::2]
lat, lon = self.lat, self.lon
# the full-size image consists of 10'980 x 10'980 datapoints, i.e. pixel. due to memory
# restrictions, the number of pixels is reduced to 10'980 / x where x = 2^n.
phi_SWtoNW = np.linspace(lat[3], lat[0], num=self.nx)
phi_SEtoNE = np.linspace(lat[2], lat[1], num=self.nx)
lam_SWtoNW = np.linspace(lon[3], lon[0], num=self.ny)
lam_SEtoNE = np.linspace(lon[2], lon[1], num=self.ny)
self.lam = np.ones((self.nx,self.ny))
self.phi = np.ones((self.nx,self.ny))
a = 0
for i in range(self.nx):
a = a + 1
self.phi[:][-a] = np.linspace(phi_SWtoNW[i], phi_SEtoNE[i], num=self.ny)
self.lam[:][-a] = np.linspace(lam_SWtoNW[i], lam_SEtoNE[i], num=self.nx)
##########################################################################################
##########################################################################################
def createBasemap(self):
lonCorners = self.getCorners(self.lam)
latCorners = self.getCorners(self.phi)
self.basemap = Basemap(projection = 'merc',
llcrnrlat = latCorners.min(),
urcrnrlat = latCorners.max(),
llcrnrlon = lonCorners.min(),
urcrnrlon = lonCorners.max(),
resolution = 'c')
self.xCorners, self.yCorners = self.basemap(lonCorners, latCorners)
def getCorners(self, centers):
one = centers[:-1,:]
two = centers[1:, :]
dl = (two - one) / 2.
one = one - dl
two = two + dl
stepOne = np.zeros((centers.shape[0] + 1, centers.shape[1]))
stepOne[:-2, :] = one
stepOne[-2:, :] = two[-2:, :]
one = stepOne[:, :-1]
two = stepOne[:, 1:]
d2 = (two - one) / 2.
one = one - d2
two = two + d2
stepTwo = np.zeros((centers.shape[0] + 1, centers.shape[1] + 1))
stepTwo[:, :-2] = one
stepTwo[:, -2:] = two[:, -2:]
return stepTwo
def savePNG(self, array=None, nameOutNew=None):
'''acutal plot section where the final products are saved as .png file.'''
plt.clf()
plt.close('all')
rgb0 = np.empty(self.rgb.shape)
for i in range(3):
rgb0[i] = array[:,:,i]
rgb = rgb0.T
colorTuple = rgb.transpose((1,0,2)).reshape((rgb.shape[0]*rgb.shape[1],rgb.shape[2]))/rgb.max()
plt.rcParams['figure.figsize'] = (50, 50)
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.axis('off')
self.cm = self.basemap.pcolormesh(self.xCorners,
self.yCorners,
rgb0[1,:,:],
color=colorTuple,
linewidth = 0)
self.cm.set_array(None)
try:
self.nameOutNew = nameOutNew
plt.savefig(nameOutNew+'.png', transparent=True, bbox_inches=extent, origin='lower')
except:
plt.savefig(nameOut+'.png', transparent=True, bbox_inches=extent, origin='lower')
plt.clf()
plt.close('all')
def plotImg(self, array=None):
plt.clf()
plt.close('all')
rgb0 = np.empty(self.rgb.shape)
for i in range(3):
rgb0[i] = array[:,:,i]
rgb = rgb0.T
self.basemap.drawmapboundary()
self.basemap.drawcoastlines()
self.basemap.drawparallels(np.arange(-80., 80., 5.))
self.basemap.drawmeridians(np.arange(-180., 181., 5.))
colorTuple = rgb.transpose((1,0,2)).reshape((rgb.shape[0]*rgb.shape[1],rgb.shape[2]))/rgb.max()
plt.rcParams['figure.figsize'] = (50, 50)
self.cm = self.basemap.pcolormesh(self.xCorners,
self.yCorners,
rgb0[1,:,:],
color=colorTuple,
linewidth = 0)
self.cm.set_array(None)
plt.show()
plt.close('all')
plt.clf()
def plotWebMap(self):
from ipyleaflet import Map, ImageOverlay
center = [(self.phi.max()+self.phi.min())/2.,
(self.lam.max()+self.lam.max())/2.]
M = Map(center=center,
zoom=8,
width='100%',
heigth=6000)
imgurl = self.nameOutNew + '.png'
img_bounds = [[self.phi.min(), self.lam.min()],
[self.phi.max(), self.lam.max()]]
io = ImageOverlay(url=imgurl, bounds=img_bounds)
M.add_layer(io)
return M