Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. for (n in files) {
  2. image <- stack(n)
  3. image <- clip(image,subset)
  4.  
  5. ###classify raster
  6. image.df <- as.data.frame(image)
  7. cluster.image <- kmeans(na.omit(image.df), 10, iter.max = 10, nstart = 25) ### kmeans, with 10 clusters
  8.  
  9. #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
  10. image.df.factor <- rep(NA, length(image.df[,1]))
  11. image.df.factor[!is.na(image.df[,1])] <- cluster.image$cluster
  12.  
  13. #create raster output
  14. clusters <- raster(image) ## create an empty raster with same extent than "image"
  15. clusters <- setValues(clusters, image.df.factor) ## fill the empty raster with the class results
  16. plot(clusters)
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement