Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for (n in files) {
- image <- stack(n)
- image <- clip(image,subset)
- ###classify raster
- image.df <- as.data.frame(image)
- cluster.image <- kmeans(na.omit(image.df), 10, iter.max = 10, nstart = 25) ### kmeans, with 10 clusters
- #add back NAs using the NAs in band 1 (identic NA positions in all bands), see http://stackoverflow.com/questions/12006366/add-back-nas-after-removing-them/12006502#12006502
- image.df.factor <- rep(NA, length(image.df[,1]))
- image.df.factor[!is.na(image.df[,1])] <- cluster.image$cluster
- #create raster output
- clusters <- raster(image) ## create an empty raster with same extent than "image"
- clusters <- setValues(clusters, image.df.factor) ## fill the empty raster with the class results
- plot(clusters)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement