-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_studyAreaShapeFiles.r
More file actions
executable file
·49 lines (37 loc) · 1.78 KB
/
get_studyAreaShapeFiles.r
File metadata and controls
executable file
·49 lines (37 loc) · 1.78 KB
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
# Extract shapefiles covering the area;
# Shapefiles are clipped and stored in db, see src_sql/stm_shapes.sql
# Date: February 10th, 2015
# This script only extract and write shapefiles from the db
## ---------------------------------------------
# Database connection
#source('./con_quicc_db.r')
source('./con_quicc_db_local.r')
source('./Rpostgis.r') #Extract spatial layer form the DB
# Library
library(ggplot2)
library(raster)
library(sp)
library(rgeos)
# Extract shapefiles from the database
countries <- dbReadSpatial(con, schemaname="temp_quicc", tablename="stm_countries_shapes", geomcol="geom")
lakes <- dbReadSpatial(con, schemaname="temp_quicc", tablename="stm_lakes_shapes", geomcol="geom")
great_lakes <- dbReadSpatial(con, schemaname="temp_quicc", tablename="stm_great_lakes_shapes", geomcol="geom")
system('mkdir -p ./out_files/shapefiles/')
# Write shapefiles
writeOGR(lakes, "./out_files/shapefiles/", "lakes_stm_area", driver="ESRI Shapefile")
writeOGR(countries, "./out_files/shapefiles/", "countries_stm_area", driver="ESRI Shapefile")
writeOGR(great_lakes, "./out_files/shapefiles/", "great_lakes_stm_area", driver="ESRI Shapefile")
#Convert to ggplot2 format
ext_geo <- extent(c(-79.95454,-60.04625,43.04572,50.95411 ))
#ext_geo <- extent(c(-96.98904, -57.30813, 35.25441, 52.90010))
#Set CRS
#newproj <- CRS("+init=epsg:4269 +proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs +towgs84=0,0,0")
#Crop, simplify and fortify
lakes <- crop(lakes,ext_geo)
great_lakes <- crop(great_lakes,ext_geo)
countries <- gSimplify(crop(countries,ext_geo),0.005)
df.lakes <- fortify(lakes)
df.countries <- fortify(countries)
df.great_lakes <- fortify(great_lakes)
#Save ggplot2 format as an R object
save(lakes,great_lakes,countries,df.lakes,df.countries,df.great_lakes,file="./out_files/shp_stm_area.rdata")