exercise 2:Click_here
exercise2 solutions: Click_here
solutions to the last windturbine exercise: Click_here
############################################################################
### the purpose of this is to learn to do some simple GIS calculations
############################################################################
###########################################################################
### installing and downloading relavant libraries
###########################################################################
#removing ervery ting in R
rm(list=ls())
# sometimes you need to define the loaction of the libraries
.libPaths("E:/Dropbox/R_packges")
.libPaths()
# list of relevant libraries
pack<-c("rgdal", "classInt", "GISTools", "maps", "dismo", "raster", "foreign", "shape",
"ggmap","knitr", "reshape", "rgeos","spdep","sp","‘maptools")
# installing relavnt libraries from list
# for (i in pack){
# install.packages(pkgs=i, dependencies=TRUE)
# }
# update.packages()
# calling libraries
lapply(pack, require, character.only=T)
###########################################################################
### setting working directory
###########################################################################
# defining location
data <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/data/GIS_data"
results <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/exercise_2"
# what is my working directory
getwd()
# changing directory
setwd(data)
# looking in the directory folder
dir()
###########################################################################
### loading data
###########################################################################
#
load("Ex_data.RData")
# looking at the data
head(data.set)
dim(data.set)
# cheking the class of the data
class(data.set)
# creating a spatial dataframe
coordinates(data.set)=c("coord_x","coord_y")
proj4string(data.set)<-CRS("+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs")
# looking on the datas spatial distribution
plot(data.set)
###############################################################################
## reading in spatial data
##############################################################################
# creating a list
shape_file_names<-c("Nord_jylland", "coast","forest","harbour","highway","high_way_2","lake"
,"large_roads","railways","municipality"
,"parish","sea","urban_area","windturbines","Aalborg", "postal_codes")
# reading in shape files from data shape_file_names
for(i in shape_file_names){
shape_file <- readOGR(dsn=data,
layer=i)
assign(i, shape_file)
}
###############################################################################
## plotting data
##############################################################################
# setting the spatial scale of the map
plot(data.set)
# plotting region
plot(Nord_jylland, col=colors()[c(18)], add=TRUE)
# plotting sea
plot(sea, col=colors()[c(26)], add=TRUE)
# plot urban area
plot(urban_area, col="skyblue1", add=TRUE, lty=0)
# highway
plot(highway, col="gray40", add=TRUE, lty=1,lwd=1)
#highway_2
plot(high_way_2, col="gray40", add=TRUE, lty=1,lwd=1)
# large_roads
plot(large_roads, col="gray60", add=TRUE, lty=1,lwd=1)
# railways
plot(railways, col="gray40", add=TRUE, lty=1,lwd=1)
# plot dataset
plot(data.set,col="black", pch=16, cex=1, add=TRUE)
###############################################################################
## exercise 2.1: check the projection of all the shape files
##############################################################################
# check the projection of all the shape files
# find the best solution in your group and sent it to toke - tepp@ifro.ku.dk
# hint: If you are using a loop check out ?get
###############################################################################
## calculate size of lake and removing lakes below 1 Ha
##############################################################################
# is there a size varaible
head(data.frame(lake))
# creating a size calculation in m2
lake$area<-gArea(lake, byid=TRUE)
# looking at the distribution of the lake
summary(lake$area)
lake<-subset(lake, area>10000)
dim(lake)
###############################################################################
## calculate distance matrix between the houseprice data and lake
##############################################################################
## calculate distance
dist<-gDistance(data.set, lake , byid=TRUE, hausdorff=FALSE, densifyFrac = NULL)
# transposing the matrix
dist<-t(dist)
# transforming the matrix to a dataframe
dist<-data.frame(dist)
# looking at the dimensions
dim(dist)
# finding minimum distance for the first obs in the house price data
min(dist[1,])
# how many lakes within 5 km
sum((dist[1,])<5000)
###############################################################################
## add the minimum lake distance to the house price data
##############################################################################
# the size of the dataset - 2000 obs
number<-dim(data.set)[1]
# creating a lake dataset
data.set$lake<-0
# adding the minimum distance to
for (i in 1:number){
print(i)
dist_min<-min(dist[i,])
data.set$lake[i]<-dist_min
}
# Adding the minimum distance a faster
data.set$lake_1<-apply(dist, 1, min)
###############################################################################
## exercise 2.1: calculate the distance to large windturbines higher than 79 meters
##############################################################################
# calculate the distance to large windturbines higher than 79 meters
# check the distance claculations in Qgis - did it work?
# do all people in the group get the same distance.
# sent the minimum distance to toke and your best solution to toke tepp@ifro.ku.dk
###############################################################################
## overlay analysis parish
##############################################################################
### looking at the parish data
plot(parish)
head(parish)
# creating a new variable
data.set$parishes<-0
# overlay analysis
within<-over(data.set, parish, returnList = FALSE)
## checking if some observation is located outside the parishes
summary(is.na(within$ADM_KODE))
# adding the overlay results to the dataset
data.set$parishes<-within$ADM_KODE
# checking the type of variable
str(data.set$parishes)
# how many obs in each parish
summary(as.factor(data.set$parishes))
###############################################################################
### plot the number of sold houses in the parishes
##############################################################################
## looking at the administration codes where the house price dataset is located
unique(data.set$parishes)
## selecting parishes according to unique administration codes
parish<-parish[parish$ADM_KODE %in% unique(data.set$parishes),]
## creating a dataframe consisting of the aggregated number of sales in each parish
sum_parish<-data.frame(summary(as.factor(data.set$parishes)))
table(data.set$parishes)
# renaming the variable
names(sum_parish)<-"sum"
# creating the merge key from the ADM_KODE
sum_parish$ADM_KODE<-as.integer(rownames(sum_parish))
# checking the type of variable
str(parish$ADM_KODE)
str(sum_parish$ADM_KODE)
# merging/joining the variables based on ADM_kode
parish_merge<-merge(parish, sum_parish, by.x=c("ADM_KODE"),
by.y=c("ADM_KODE"), all= FALSE)
# creating map
plot(parish_merge)
plot(sea, col=colors()[c(26)], add=TRUE)
plot(Nord_jylland, col=colors()[c(18)], add=TRUE)
# didvid hights into intervals
q5 <- classIntervals(parish_merge$sum, n=5, style="pretty", dataPrecision =0)
# choosing colors acording to intervals
q5Colours <- findColours(q5, heat.colors(5))
# plotting municipalities
plot(parish_merge, axes=TRUE, col=q5Colours, add=TRUE)
# Add legend
legend("topright",
fill = attr(q5Colours, "palette"),
border="black",
legend=gsub(",", " - ", names(attr(q5Colours, "table"))),
bg ="white",
title="Number of sales")
# you can also create the map in QGIS
writeOGR(parish, dsn=results,
layer="parish_s", driver="ESRI Shapefile")
###############################################################################
## exercise 2.2: what is the number of windturbines in the postal areas in Nordjylland
##############################################################################
# plot the number windturbines in the postal areas in Nord_jylland?
# Get inspiration from the script above
###############################################################################
## end of exercise
##############################################################################