# 2 Part 2

## 2.1 Create a polygon shapefile basegrid of radar sample volumes

This will create a polygon shapefile based on the polar coordinates of the radar sample volumes at the lowest tilt angle based on the sample volumes dimensions declared earlier. The maximum range (maxrange) for creating the basegrid was set to 5 km so that the process will run quickly for this exercise. However, usually maxrange should usually be set to 100 km.

###use "destPoint" from package geosphere to estimate the destination latitude and
##longitude given a starting latitude and longitude(radar origin), bearing and distance
#######################################################################################
ptm <- proc.time()

##lon1,lat1
points<-data.frame()
for (i in 1:length(ranges)){
loop<-data.frame(destPoint(c(X_origin,Y_origin),b=azimuths, d=ranges[i]))
loop\$azimuth<-azimuths
points<-data.frame(rbind(points, loop))}

colnames(points)[1]<-"lon1"
colnames(points)[2]<-"lat1"
points\$range<-range

#lon2,lat2
points2<-data.frame()
for (i in 1:length(ranges)){
points2<-data.frame(rbind(points2, loop2))}
colnames(points2)[1]<-"lon2"
colnames(points2)[2]<-"lat2"

#lon3,lat3
points3<-data.frame()
for (i in 1:length(ranges)){
points3<-data.frame(rbind(points3, loop3))}

colnames(points3)[1]<-"lon3"
colnames(points3)[2]<-"lat3"

#lon4,lat4
points4<-data.frame()
for (i in 1:length(ranges)){
loop4<-data.frame(destPoint(c(X_origin,Y_origin),b=azimuths+step, d=ranges[i]))
points4<-data.frame(rbind(points4, loop4))}

colnames(points4)[1]<-"lon4"
colnames(points4)[2]<-"lat4"

points<-cbind(points, points2, points3, points4)
points\$lon5<-points\$lon1
points\$lat5<-points\$lat1
##reorder columns
points<-points[,c(4,3,1,2,5,6,7,8,9,10,11,12)]

##make a data frame with coordinates for each polygon grouped with "id" field
##should be 5 rows of data for each polygon (sample volume)
#######################################################################################
x<-c(sapply(seq(nrow(points)), function(s) c(points\$lon1[s], points\$lon2[s], points\$lon3[s], points\$lon4[s], points\$lon5[s])))
y<-c(sapply(seq(nrow(points)), function(r) c(points\$lat1[r], points\$lat2[r], points\$lat3[r], points\$lat4[r], points\$lat5[r])))

########################################################
## assign projection and create sf object of polygons ##
########################################################

st_as_sf(coords = c("X", "Y"), crs = 4326) %>%
group_by(id) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON")

#####################################
## If another projection is needed ##
#####################################

my_spatial_polys_rp <- st_transform(my_spatial_polys, 26918) #EPSG code for UTM Zone 18

# assign value labels to each polygon (id, azimuth, range).
# setup for midpoint of pulse volume for super-res; change values for other resolution data
my_spatial_polys_rp\$azimuth <- points\$azimuth+0.25
my_spatial_polys_rp\$range <- points\$range+125

# Stop the clock
proc.time() - ptm
##    user  system elapsed
##   24.33    0.61   26.88