-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overture_Maps.R
177 lines (140 loc) · 5.2 KB
/
Overture_Maps.R
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
####################################################
#' Overture Map Foundation Data Download
#' Project Name - Practice
#'
#' Author - Pukar Bhandari
#' Team Members -
####################################################
# ==================================================
# 0. References
# ==================================================
# Overture Maps Extract Data Locally: https://docs.overturemaps.org/getting-data/locally
# DuckDB R API: https://duckdb.org/docs/api/r.html
# Other references:
# 1. https://til.simonwillison.net/overture-maps/overture-maps-parquet
# 2. https://dev.to/savo/spatial-data-analysis-with-duckdb-40j9
# 3. https://cheginit.github.io/til/python/buildings.html#overture-buildings-using-duckdb
# 4. https://walker-data.com/posts/overture-buildings/
# ==================================================
# 1. Setting up the environment
# ==================================================
# -------------------------------------
# Install and Load packages
# -------------------------------------
# Install and load the pacman package
if (!require("pacman")) {
install.packages("pacman")
}
# library("pacman")
# Install and load multiple desired packages at once
p_load(
tidyverse, # data manipulation and visualization
sf, # spatial data
tmap, # view spatial data
DBI, # database interface: Connect-Disconnect
duckdb, # use duckdb connection
arrow # read parquet data
)
# -------------------------------------
# Global Options
# -------------------------------------
options(scipen = 999) # avoiding scientific notation
tmap_mode(mode = "view")
# ==================================================
# 2. Load and Prepare data
# ==================================================
# -------------------------------------
# Set Working Directory
# -------------------------------------
# Set your main data folder
wd = "M:/MA_Project/GA_Albany_Resilence/GIS"
project = "Albany_Resilience_Task1/Default.gdb"
# setwd(wd)
# -------------------------------------
# Albany Boundary
# -------------------------------------
st_layers(file.path(wd, project))
albany_boundary <- read_sf(file.path(wd, project), layer = "City_of_Albany") %>%
st_transform(4326)
albany_bbox <- albany_boundary %>%
st_bbox() %>%
as.vector()
# ==================================================
# 2. Download Data
# ==================================================
# -------------------------------------
# Setup Function to Download Data
# -------------------------------------
# Define the function to query Overture's buildings data
overture_data <- function(bbox, overture_type, dst_parquet) {
# Define the theme map
map_themes <- list(
"locality" = "admins",
"locality_area" = "admins",
"administrative_boundary" = "admins",
"building" = "buildings",
"building_part" = "buildings",
"place" = "places",
"segment" = "transportation",
"connector" = "transportation",
"infrastructure" = "base",
"land" = "base",
"land_use" = "base",
"water" = "base"
)
# Validate overture_type
if (!overture_type %in% names(map_themes)) {
stop(paste("Valid Overture types are:", paste(names(map_themes), collapse = ", ")))
}
s3_region <- "us-west-2"
base_url <- sprintf("s3://overturemaps-%s/release", s3_region)
version <- "2024-04-16-beta.0"
theme <- map_themes[[overture_type]]
remote_path <- sprintf("%s/%s/theme=%s/type=%s/*", base_url, version, theme, overture_type)
# Connect to DuckDB
conn <- dbConnect(duckdb::duckdb())
dbExecute(conn, "INSTALL httpfs;")
dbExecute(conn, "INSTALL spatial;")
dbExecute(conn, "LOAD httpfs;")
dbExecute(conn, "LOAD spatial;")
dbExecute(conn, sprintf("SET s3_region='%s';", s3_region))
# Create SQL query
read_parquet <- sprintf("read_parquet('%s', filename=TRUE, hive_partitioning=1);", remote_path)
dbExecute(conn, sprintf("CREATE OR REPLACE VIEW data_view AS SELECT * FROM %s", read_parquet))
query <- sprintf("
SELECT
data.*
--ST_GeomFromWKB(data.geometry) as geometry,
FROM data_view AS data
WHERE data.bbox.xmin <= %f AND data.bbox.xmax >= %f
AND data.bbox.ymin <= %f AND data.bbox.ymax >= %f
", bbox[3], bbox[1], bbox[4], bbox[2])
# Define output file path
file <- normalizePath(dst_parquet, mustWork = FALSE)
# Execute the query and save the results to a Parquet file
dbExecute(conn, sprintf("COPY (%s) TO '%s' WITH (FORMAT 'parquet');", query, file))
# Close the connection
dbDisconnect(conn, shutdown = TRUE)
}
# -------------------------------------
# Download Parquet Data
# -------------------------------------
albany_bbox # Format: xmin, ymin, xmax, ymax
overture_data(albany_bbox, "place", "albany_places_subset.parquet")
# -------------------------------------
# Read Parquet Data
# -------------------------------------
# Load and process the Parquet file
albany_places <- read_parquet("albany_places_subset.parquet")
# -------------------------------------
# Convert to sf objtect
# -------------------------------------
albany_places_sf <- st_as_sf(
albany_places %>% select(-sources),
geometry = albany_places$geometry,
crs = 4326
)
albany_places_sf %>%
select(names$primary, categories$main, confidence) %>%
# mapview::mapview()
tmap::qtm()