Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. ## Function to convert gridNum to row, col, lat, lng -----
  2.  
  3. setwd("E:/prism")
  4. library(foreign)
  5.  
  6. nrow <- 621
  7. ncol <- 1405
  8. ul.lng <- -125
  9. ul.lat <- 49.9166666666687
  10. res <- 0.04166666666667
  11.  
  12. gridNum.to.lnglat <- function(gridNum) {
  13. row <- floor((gridNum - 1) / ncol) + 1
  14. col <- (gridNum - 1) %% ncol + 1
  15. cent.lat <- ul.lat - res * (row - 1)
  16. cent.lng <- ul.lng + res * (col - 1)
  17. df <- data.frame(gridNum, row, col, cent.lng, cent.lat)
  18. }
  19.  
  20. allcells <- 1:(621*1405)
  21. allcells.df <- gridNum.to.lnglat(allcells)
  22.  
  23. write.dta(allcells.df, "gridNum_to_lnglat.dta")
  24.  
  25. # Function to convert latlng to gridnum
  26.  
  27. lnglat.to.gridNum <- function(lng, lat) {
  28. row <- ceiling((ul.lat - lat + res/2) / res)
  29. col <- ceiling((lng - ul.lng + res/2) / res)
  30. gridNum <- (row - 1) * ncol + col
  31. df <- data.frame(lng, lat, gridNum, row, col)
  32. }
  33.  
  34. # Just to check...
  35. allcells2.df <- lnglat.to.gridNum(allcells.df$cent.lng, allcells.df$cent.lat)
  36.  
  37. # Find Max's LA gridNum
  38. max.test <- lnglat.to.gridNum(-118.2917, 33.95848)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement