---
title: "Using GSODR with sf"
author: "Adam H Sparks"
output:
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Using GSODR with sf}
  %\VignetteEncoding{UTF-8}
---



# Creating Spatial Files

Because the stations provide geospatial location information, it is possible to create a spatial file. [GeoPackage files](http://www.geopackage.org) are a open, standards-based, platform-independent, portable, self-describing compact format for transferring geospatial information, which handle vector files much like shapefiles do, but eliminate many of the issues that shapefiles have with field names and the number of files.

## Converting to an _sf_ object

Simple features are an ISO defined standard that now have support in R. From the [_sf_ vignette](https://r-spatial.github.io/sf/articles/sf1.html):

> Simple features or simple feature access refers to a formal standard (ISO19125-1:2004) that describes how objects in the real world can be represented in computers, with emphasis on the spatial geometry of these objects.
It also describes how such objects can be stored in and retrieved from databases, and which geometrical operations should be defined for them.
The standard is widely implemented in spatial databases (such as PostGIS), commercial GIS (e.g., ESRI ArcGIS) and forms the vector data basis for libraries such as GDAL.
A subset of simple features forms the GeoJSON standard.

For this example, we convert the GSOD data for Australia in 2017 to an _sf_ object.


```r
library(GSODR)
library(future)
library(sf)

plan("multisession")
GSOD <- get_GSOD(years = 2017, country = "AUS")
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:stats' may not be
## available when loading
```

```
## Warning in serialize(data, node$con): 'package:utils' may not be
## available when loading
```

```r
GSOD_SF <- st_as_sf(x = GSOD,
                    coords = c("LONGITUDE", "LATITUDE"),
                    crs = "+proj=longlat +datum=WGS84")

str(GSOD_SF)
```

```
## Classes 'sf', 'data.table' and 'data.frame':	182508 obs. of  43 variables:
##  $ STNID           : chr  "941000-99999" "941000-99999" "941000-99999" "941000-99999" ...
##  $ NAME            : chr  "KALUMBURU" "KALUMBURU" "KALUMBURU" "KALUMBURU" ...
##  $ CTRY            : chr  "AS" "AS" "AS" "AS" ...
##  $ STATE           : chr  "" "" "" "" ...
##  $ ELEVATION       : num  24 24 24 24 24 24 24 24 24 24 ...
##  $ BEGIN           : int  20010912 20010912 20010912 20010912 20010912 20010912 20010912 20010912 20010912 20010912 ...
##  $ END             : int  20200310 20200310 20200310 20200310 20200310 20200310 20200310 20200310 20200310 20200310 ...
##  $ YEARMODA        : Date, format: "2017-01-01" ...
##  $ YEAR            : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
##  $ MONTH           : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ DAY             : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ YDAY            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ TEMP            : num  29.3 29 26.8 27.3 28.9 29.2 29.1 30.1 30.4 28.4 ...
##  $ TEMP_ATTRIBUTES : chr  "16" "16" "16" "16" ...
##  $ DEWP            : num  22.8 23.2 23.8 22.7 23.2 22.6 22.3 23.2 23.1 22.8 ...
##  $ DEWP_ATTRIBUTES : chr  "16" "16" "16" "16" ...
##  $ SLP             : num  1003 1002 1000 999 1003 ...
##  $ SLP_ATTRIBUTES  : chr  "16" "16" "16" "16" ...
##  $ STP             : num  NA 1000 998 996 NA ...
##  $ STP_ATTRIBUTES  : chr  "16" "16" "16" "16" ...
##  $ VISIB           : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ VISIB_ATTRIBUTES: chr  " 0" " 0" " 0" " 0" ...
##  $ WDSP            : num  2.3 2 1.5 3.7 3.3 2.4 2.2 2.2 2.3 2 ...
##  $ WDSP_ATTRIBUTES : chr  "16" "16" "16" "16" ...
##  $ MXSPD           : num  4.6 3.6 3.1 5.7 6.2 5.1 5.1 4.1 5.1 4.1 ...
##  $ GUST            : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ MAX             : num  34.7 34.7 31.4 31.6 32.5 34.2 34.5 35.1 35.5 35.4 ...
##  $ MAX_ATTRIBUTES  : chr  "*" "*" "*" "*" ...
##  $ MIN             : num  24.7 24.6 24.5 24.2 25.2 25.2 24.6 24.5 25.2 24.2 ...
##  $ MIN_ATTRIBUTES  : chr  "*" NA "*" NA ...
##  $ PRCP            : num  5.1 6.1 2 64 15 0.3 0 0 0 25.9 ...
##  $ PRCP_ATTRIBUTES : chr  "A" "G" "G" "G" ...
##  $ SNDP            : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ I_FOG           : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ I_RAIN_DRIZZLE  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ I_SNOW_ICE      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ I_HAIL          : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ I_THUNDER       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ I_TORNADO_FUNNEL: int  NA NA NA NA NA NA NA NA NA NA ...
##  $ EA              : num  2.8 2.8 2.9 2.8 2.8 2.7 2.7 2.8 2.8 2.8 ...
##  $ ES              : num  4.1 4 3.5 3.6 4 4 4 4.3 4.3 3.9 ...
##  $ RH              : num  68.1 71 83.7 76 71.4 67.6 66.8 66.6 65.1 71.7 ...
##  $ geometry        :sfc_POINT of length 182508; first list element:  'XY' num  126.6 -14.3
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr  "STNID" "NAME" "CTRY" "STATE" ...
```

## Saving _sf_ objects

Writing a shapefile is a simple matter.


```r
write_sf(obj = GSOD_SF, dsn = file.path(tempdir(), "GSOD.shp"))
```

```
## Warning in abbreviate_shapefile_names(obj): Field names abbreviated
## for ESRI Shapefile driver
```

As is writing a GeoPackage from the _sf_ object.


```r
write_sf(obj = GSOD_SF, dsn = file.path(tempdir(), "GSOD.gpkg"))
```

After getting weather stations for Australia and creating a GeoPackage file, _sf_ can import the data into R again in a spatial format.


```r
library(rgdal)

AUS_stations <-
  st_read(dsn = file.path(tempdir(), "GSOD.gpkg"), layer = "GSOD")
```

```
## Reading layer `GSOD' from data source `/private/var/folders/yv/4xy6zs3x5qsgj96389nkjvtw0000gn/T/RtmpEinr0P/GSOD.gpkg' using driver `GPKG'
## Simple feature collection with 182508 features and 42 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 113.6 ymin: -54.5 xmax: 159.1 ymax: -10.05
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
```

```r
class(AUS_stations)
```

```
## [1] "sf"         "data.frame"
```

Since GeoPackage files are formatted as SQLite databases you can also use other R tools for SQLite files
[(J. Stachelek 2016)](https://jsta.github.io/2016/07/14/geopackage-r.html).
One easy way is using _dplyr_. This option is much faster to load since it does not load the geometry.


```r
library(dplyr)
AUS_sqlite <- tbl(src_sqlite(file.path(tempdir(), "GSOD.gpkg")), "GSOD")
class(AUS_sqlite)
```

```
## [1] "tbl_SQLiteConnection" "tbl_dbi"             
## [3] "tbl_sql"              "tbl_lazy"            
## [5] "tbl"
```

# References
Stachelek, J. (2016) Using the Geopackage Format with R.
URL: https://jsta.github.io/2016/07/14/geopackage-r.html
