-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassify.py
More file actions
153 lines (124 loc) · 4.9 KB
/
classify.py
File metadata and controls
153 lines (124 loc) · 4.9 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
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import rasterio
from rasterio.plot import show, show_hist, reshape_as_raster, reshape_as_image
from rasterio.windows import Window
from rasterio.mask import mask
from shapely.geometry import mapping
import geopandas as gpd
import folium
import os
from sklearn.naive_bayes import GaussianNB
# --- Auxiliar functions
colors = dict((
(0, (48, 156, 214, 255)), # Blue - Water
(1, (139,69,19, 255)), # Brown - WetSand
(2, (96, 19, 134, 255)), # Purple - Emergent Wetland
(3, (244, 164, 96, 255)), # Tan - Sand
(4, (206, 224, 196, 255)), # Lime - Herbaceous
(5, (34, 139, 34, 255)), # Forest Green - Forest
))
def color_stretch(image, index):
colors = image[:, :, index].astype(np.float64)
for b in range(colors.shape[2]):
colors[:, :, b] = rasterio.plot.adjust_band(colors[:, :, b])
return colors
def str_class_to_int(class_array):
class_array[class_array == 'Subtidal Haline'] = 0
class_array[class_array == 'WetSand'] = 1
class_array[class_array == 'Emergent Wetland'] = 2
class_array[class_array == 'Sand'] = 3
class_array[class_array == 'Herbaceous'] = 4
class_array[class_array == 'Forested Wetland'] = 5
return(class_array.astype(int))
# Dataset selection, default: outFile
def dataSetSelect():
filePath = "data/data_01"
bandPaths = [os.path.join(filePath, f) for f in os.listdir(filePath) if os.path.isfile(os.path.join(filePath, f))]
bandPaths.sort()
return bandPaths
# --- Loading bands
bandPaths = dataSetSelect()
# --- New Dir and preparing raster
dir = "data/rasters/"
# Check if already exists
if not os.path.exists(dir):
os.makedirs(dir)
newFilePath = dir + 'raster.tif'
with rasterio.open(bandPaths[0]) as src0:
meta = src0.meta
meta.update(count = len(bandPaths))
with rasterio.open(newFilePath, 'w', **meta) as dst:
for id, layer in enumerate(bandPaths, start=1):
with rasterio.open(layer) as src1:
dst.write_band(id, src1.read(1))
# ---
data = rasterio.open(newFilePath)
# Raster check
clipped_img = data.read([4,3,2])[:, 150:600, 250:1400]
fig, ax = plt.subplots(figsize=(10,7))
show(clipped_img[:, :, :], ax=ax, transform=data.transform)
# Training set
shapefile = gpd.read_file("data/rcr/rcr_landcover.shp")
# Converting the projections
shapefile = shapefile.to_crs({'init': 'epsg:4326'})
# Shapely
geoms = shapefile.geometry.values
geometry = geoms[0]
# transform to GeoJSON format
feature = [mapping(geometry)] # can also do this using polygon.__geo_interface__
# Raster values
img, transform = mask(data, feature, crop=True)
data.close()
# --- Training Data for random forest
x = np.array([], dtype=np.int8).reshape(0,8)
y = np.array([], dtype=np.string_)
with rasterio.open(newFilePath) as src:
bandCount = src.count
for index, geom in enumerate(geoms):
feature = [mapping(geom)]
# the mask function returns an array of the raster pixels within this feature
out_image, out_transform = mask(src, feature, crop=True)
# eliminate all the pixels with 0 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image[:,~np.all(out_image == 0, axis=0)]
# eliminate all the pixels with 255 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image_trimmed[:,~np.all(out_image_trimmed == 255, axis=0)]
# reshape the array to [pixel count, bands]
out_image_reshaped = out_image_trimmed.reshape(-1, bandCount)
# append the labels to the y array
y = np.append(y,[shapefile["Classname"][index]] * out_image_reshaped.shape[0])
# stack the pizels onto the pixel array
x = np.vstack((x,out_image_reshaped))
# What are our classification labels?
labels = np.unique(shapefile["Classname"])
print('\n')
print('The training data include {n} classes: {classes}\n'.format(n=labels.size,
classes=labels))
# --- ML Model
model = GaussianNB()
model.fit(x, y)
# --- Classification process
with rasterio.open(newFilePath) as src:
img = src.read()[:, 150:600, 250:1400]
reshaped_img = reshape_as_image(img)
predict = model.predict(reshaped_img.reshape(-1, 8))
predict = predict.reshape(reshaped_img[:, :, 0].shape)
predict = str_class_to_int(predict)
# Max pixel value
max = int(np.max(predict))
# Normalize to float 0, 1
for k in colors:
v = colors[k]
_v = [_v / 255.0 for _v in v]
colors[k] = _v
index_colors = [colors[key] if key in colors else
(255, 255, 255, 0) for key in range(0, max+1)]
cmap = plt.matplotlib.colors.ListedColormap(index_colors, 'Classification', max+1)
# --- Visualization
fig, axs = plt.subplots(2,1,figsize=(10,7))
img_stretched = color_stretch(reshaped_img, [4, 3, 2])
axs[0].imshow(img_stretched)
axs[1].imshow(predict, cmap=cmap, interpolation='none')
fig.show()
plt.show()