Created
July 23, 2021 21:47
-
-
Save mbjones/6583d9c51c2532a6dc217aa499f07d82 to your computer and use it in GitHub Desktop.
geopackage.Rmd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "geopackage" | |
author: "Matt Jones" | |
date: "7/23/2021" | |
output: html_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` | |
```{r load_libraries} | |
library(sf) | |
``` | |
This is a simple overview/test of using GeoPackage in R, building off of the tutorial [Reading and writing spatial data in R](https://mgimond.github.io/Spatial/app1-1.html#app1_2). | |
## Determine the layers in a GeoPackage | |
```{r layers} | |
temp <- tempdir() | |
gpkg_file <- paste0(temp, "/rail_inters.gpkg") | |
download.file("http://github.com/mgimond/Spatial/raw/master/Data/rail_inters.gpkg", | |
destfile = gpkg_file, mode='wb') | |
st_layers(gpkg_file) | |
``` | |
## Read in the layers | |
```{r} | |
inter.sf <- st_read(gpkg_file, layer="Interstate") | |
rail.sf <- st_read(gpkg_file, layer="Rail") | |
``` | |
## Write a geopackage | |
```{r gpkg_export} | |
st_write(inter.sf, "rail_inters_export.gpkg", layer="interstates", delete_layer = TRUE) | |
st_write(rail.sf, "rail_inters_export.gpkg", layer="railways", delete_layer = TRUE) | |
st_layers("rail_inters_export.gpkg") | |
``` | |
## Add a layer from a shapefile | |
```{r from_shp} | |
nc.sf = st_read(system.file("shape/nc.shp", package="sf")) | |
st_write(nc.sf, "rail_inters_export.gpkg", layer="nc", delete_layer = TRUE) | |
st_layers("rail_inters_export.gpkg") | |
``` | |
## Add a shapefile from the KNB | |
```{r} | |
# Shapefile from: | |
# Christopher J Lortie, Jenna Braun, and Michael Belanger. 2019. York University Tree Inventory. Knowledge Network for Biocomplexity. doi:10.5063/F13F4MZC | |
trees_url <- "https://knb.ecoinformatics.org/knb/d1/mn/v2/object/urn%3Auuid%3Adb829485-7d77-4b06-b35f-58f6e8f02e9f" | |
download.file(trees_url, destfile = paste0(temp, "/YorkUTreeMap.zip"), mode='wb') | |
unzip(paste0(temp,"/YorkUTreeMap.zip"), exdir=temp) | |
trees.sf <- st_read(paste0(temp, "/YorkUTreeMap.shp")) | |
st_write(trees.sf, "rail_inters_export.gpkg", layer="trees", delete_layer = TRUE) | |
st_layers("rail_inters_export.gpkg") | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment