-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathned_combine_01.R
226 lines (164 loc) · 6.43 KB
/
ned_combine_01.R
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
############################
## Process_elevation.R
## Original Author: James T. Durant ([email protected] ; 770.488.0668)
## Location: ATSDR/DCHI/SSB: Atlanta, GA
##
## Purpose: This script merges 2 10m NED files and clips to model domain.
## Produces KML files of elevation, monitor data and text files for AERMAP.
##
###########################
############################
### Libraries-------------------------------------------------------------------
############################
##
## *** start of code
##
library(sp)
library(rgdal)
library(raster)
library(plotKML)
##
## *** end of code
##
############################
### Libraries-------------------------------------------------------------------
############################
############################
### Import and Project Data-----------------------------------------------------
############################
##
## *** start of code
##
##NED Data to Rasters-----------------------------------------------------------
myFile1 <- "./data/imgn33w111_13.img"
myFile2 <- "./data/imgn34w111_13.img"
ned_33w111_13 <- raster(myFile1)
ned_34w111_13 <- raster(myFile2)
ned_merged <- merge(ned_33w111_13, ned_34w111_13)
##Import Point Data to determine domain-----------------------------------------
set.seed(1)
# to protect identities I am using random locations.
people <- data.frame(Longitude = runif(50, -110.8, -110.7),
Latitude = runif(50, 32.95, 33.01))
monitors <- read.csv("./data/Station_locations.csv")
sources <- read.csv("./data/sources.csv")
names(monitors)[names(monitors) %in% c("longitude", "latitude")] <- c("Longitude", "Latitude")
monitors <- monitors[c("Longitude", "Latitude")]
sources <- sources[c("Longitude", "Latitude")]
people$type <- "People"
monitors$type <- "Monitor"
sources$type <- "Source"
mergePoints <- plyr::rbind.fill(list(people, monitors,sources))
coordinates(mergePoints) <- c("Longitude", "Latitude")
proj4string(mergePoints) <- CRS("+init=epsg:4326")
mergePoints2 <- spTransform(mergePoints, CRS(proj4string(ned_merged)))
##Plot Data---------------------------------------------------------------------
plot(ned_merged)
points(mergePoints, pch=16, col="red", cex=1)
##
## *** end of code
##
############################
### Import and Project Data-----------------------------------------------------
############################
############################
### Determine Model Domain------------------------------------------------------
############################
##
## *** start of code
##
##Function as.SpatialPolygons.bbox translates bounding box to polygon-----------
as.SpatialPolygons.bbox <- function( bbox, proj4stringFrom=CRS("+proj=longlat
+datum=WGS84"), proj4stringTo=NULL ) {
# Create unprojected bbox as spatial object
bboxMat <- rbind( c(bbox['Longitude','min'],bbox['Latitude','min']),
c(bbox['Longitude','min'],bbox['Latitude','max']),
c(bbox['Longitude','max'],bbox['Latitude','max']),
c(bbox['Longitude','max'],bbox['Latitude','min']),
c(bbox['Longitude','min'],bbox['Latitude','min']) ) # clockwise, 5 points to close it
bboxSP <- SpatialPolygons( list(Polygons(list(Polygon(bboxMat)),"bbox")),
proj4string=proj4stringFrom )
if(!is.null(proj4stringTo)) {
bboxSP <- spTransform( bboxSP, proj4stringTo )
}
bboxSP
}
merge.bbox <- bbox(mergePoints) #get bounding box of point data
merge.bbox[1,1] <- merge.bbox[1,1] - 0.01 #add 0.01 degree to bounding box
merge.bbox[2,1] <- merge.bbox[2,1] -0.01
merge.bbox[1,2] <- merge.bbox[1,2] + 0.01
merge.bbox[2,2] <- merge.bbox[2,2] + 0.01
model.domain <- as.SpatialPolygons.bbox(merge.bbox)
##Output domain to KML file-----------------------------------------------------
## We create a clip slightly bigger than model domain to allow for interperalation
## in AERMAP
curDir <- getwd()
setwd("./output")
kml(model.domain, file.name=
"modelDomain.kml")
clip.bbox <- merge.bbox
clip.bbox[1,1] <- merge.bbox[1,1] - 0.01
clip.bbox[2,1] <- merge.bbox[2,1] -0.01
clip.bbox[1,2] <- merge.bbox[1,2] + 0.01
clip.bbox[2,2] <- merge.bbox[2,2] + 0.01
clip.domain <- as.SpatialPolygons.bbox(clip.bbox)
kml(clip.domain, file.name = "clipDomain.kml")
plot(clip.domain)
plot(model.domain, add=TRUE) #nice
##
## *** end of code
##
############################
### Determine Model Domain------------------------------------------------------
############################
############################
### Clip Raster-----------------------------------------------------------------
############################
##
## *** start of code
##
ned_cropped <- crop(ned_merged, clip.domain)
pdf(file="ned_cropped.pdf")
plot(ned_cropped)
plot(model.domain, add=TRUE)
dev.off()
##Output to tiff and KML--------------------------------------------------------
writeRaster(ned_cropped, filename = "./hayden_ned.tif",
options=c("COMPRESS=NONE", "TFW=YES"), overwrite=TRUE)
kml(ned_cropped, colour=layer, folder.name="hayden_elevation",
file.name="hayden_elevation.kml")
kml(mergePoints, colour=mergePoints$type, file.name="hayden_receptors.kml")
##
## *** end of code
##
############################
### Clip Raster-----------------------------------------------------------------
############################
############################
### Create text outputs of receptors for AERMAP---------------------------------
############################
##
## *** start of code
##
# transform points to UTM
desc_receptors <- spTransform(mergePoints, CRS("+proj=utm +zone=12 +datum=WGS84"))
receptors <- coordinates(desc_receptors)
monitors <- receptors[mergePoints@data == "Monitor",]
people <- receptors[mergePoints@data == "People",]
people <- unique(people)
monitors <- apply(monitors, 1, function(x) paste(round(x,0), collapse=" "))
people <- apply(people, 1, function(x) paste(round(x,0), collapse=" "))
monitors <- paste(" DISCCART ", monitors)
people <- paste(" DISCCART ", people)
writeLines(monitors, "monitor_recpt.txt")
writeLines(people, "people_recpt.txt")
# get spatial extent of model domain
model.domain.utm <- spTransform(model.domain, CRS("+proj=utm +zone=12 +datum=WGS84"))
write(bbox(model.domain.utm), "model_domain.txt")
setwd(curDir)
##
## *** end of code
##
############################
### Create text outputs of receptors for AERMAP---------------------------------
############################