Chapter 1 Prerequisites

library(giscourse)
conn<-connect()
## Loading required package: DBI
## Loading required package: sp
## Checking rgeos availability: TRUE
## Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
## Loading required package: leaflet
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#dbListFields(conn,"nat_earth_countries_hi")
query1<-"select * from nat_earth_countries_hi where name ilike '%roatia%'"
query2<-"select * from fires"


query<-sprintf("select f.* from (%s) c, (%s) f where st_intersects(f.geom,c.geometry)",query1,query2)

query
## [1] "select f.* from (select * from nat_earth_countries_hi where name ilike '%roatia%') c, (select * from fires) f where st_intersects(f.geom,c.geometry)"
st_read(conn,query=query) -> croatia_fires
query1<-"select * from nat_earth_countries_hi where name ilike '%united%ingdom%'"
query2<-"select * from fires"


query<-sprintf("select f.* from (%s) c, (%s) f where st_intersects(f.geom,c.geometry)",query1,query2)

query
## [1] "select f.* from (select * from nat_earth_countries_hi where name ilike '%united%ingdom%') c, (select * from fires) f where st_intersects(f.geom,c.geometry)"
st_read(conn,query=query) -> uk_fires
mapview(uk_fires)
fires<-rbind(data.frame(country="uk",area=242495,uk_fires),data.frame(country="croatia",area= 56594,croatia_fires))

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
fires$year<-year(fires$day)

library(dplyr)
library(ggplot2)

fires %>% group_by(year,country,area) %>% summarise(n=n()) %>% ggplot(aes(x=year,y=n/area*100,col=country)) + geom_line()