Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created December 27, 2016 16:58
Show Gist options
  • Save mdsumner/e10c6e24e4bcd5f123c7a108f50d7048 to your computer and use it in GitHub Desktop.
Save mdsumner/e10c6e24e4bcd5f123c7a108f50d7048 to your computer and use it in GitHub Desktop.
dsn <- file.path(getOption("default.datadir"), "data/listdata.thelist.tas.gov.au/opendata/data")
layer <- "list_parcels_hobart"
library(dplyr)
db <- src_sqlite("list_parcels_hobart.sqlite", create = TRUE)
iFeature <- 1
while(iFeature < Inf) {
x <- try(sf:::st_read1(dsn, layer, iFeature = iFeature, quiet = TRUE, promote_to_multi = TRUE))
if (inherits(x, "try-error")) break;
geometry <- spbabel::sptable(x)
if (! "island_" %in% names(geometry)) geometry$island_ <- TRUE
geometry$object_ <- iFeature
x$geometry <- NULL
data <- as_tibble(x)
data$object_ <- iFeature
if (iFeature == 1) {
geometry <- copy_to(db, geometry, temporary = FALSE)
data <- copy_to(db, data, temporary = FALSE)
} else {
dplyr::db_insert_into( con = db$con, table = "geometry", values = geometry)
dplyr::db_insert_into( con = db$con, table = "data", values = data)
}
iFeature <- iFeature + 1
print(iFeature)
}
## just because
> x <- tbl(db, "data") %>% filter(PID == 5567065 ) %>% select(object_, STRATA_LEV) %>% inner_join(tbl(db, "geometry")) %>% collect()
#Joining, by = "object_"
x
# # A tibble: 9 × 7
# object_ STRATA_LEV x_ y_ branch_ order_ island_
# <dbl> <chr> <dbl> <dbl> <int> <int> <int>
# 1 9 Not Applicable 520300.2 5247616 1 1 1
# 2 9 Not Applicable 520280.6 5247489 1 2 1
# 3 9 Not Applicable 520269.4 5247416 1 3 1
# 4 9 Not Applicable 520119.3 5247441 1 4 1
# 5 9 Not Applicable 520002.5 5247460 1 5 1
# 6 9 Not Applicable 519791.8 5247493 1 6 1
# 7 9 Not Applicable 519830.2 5247691 1 7 1
# 8 9 Not Applicable 520117.4 5247646 1 8 1
# 9 9 Not Applicable 520300.2 5247616 1 9 1
a <- spbabel::sp(x)
plot(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment