Install once (if not already done)

#install.packages(c(“remotes”, “rlang”, “cli”, “magrittr”, “tibble”), dependencies = TRUE) #remotes::install_github(“yonghah/esri2sf”, force = TRUE)

Load libraries

library(httr) library(jsonlite) library(sf) library(esri2sf) library(leaflet) library(RColorBrewer) library(dplyr)

url <- “https://services1.arcgis.com/QWdNfRs7lkPq4g4Q/arcgis/rest/services/Open_Space/FeatureServer/66/query

params <- list( where = “1=1”, outFields = “*“, outSR =”4326”, # WGS84 f = “geojson”, returnGeometry = “true” )

res <- GET(url, query = params)

Read as sf object

open_space <- st_read(content(res, “text”), quiet = FALSE)

Check geometry

st_geometry_type(open_space)

Check coordinate reference system

st_crs(open_space)

Check the first few rows and attributes

head(open_space)

sum(open_space$GISACRES)

Check for invalid geometries

invalid <- !st_is_valid(open_space) sum(invalid) # number of invalid polygons

Fix invalid geometries

open_space <- st_make_valid(open_space)

Optional: re-check

sum(!st_is_valid(open_space)) # should now be 0

acres_by_ownertype <- open_space %>% group_by(OWNERTYPE) %>% summarise( count = n(), # number of parcels total_acres = sum(GISACRES, na.rm = TRUE) # sum acres )

Sort ascending by total_acres

acres_by_ownertype <- acres_by_ownertype %>% mutate(avg_acres = total_acres / count) %>% arrange(desc(total_acres))

acres_by_ownertype

print(acres_by_ownertype)

Create the barplot and save bar midpoints

bp <- barplot( acres_by_ownertype\(total_acres, names.arg = acres_by_ownertype\)OWNERTYPE, # sorted labels col = “skyblue”, border = “white”, #xlab = “Owner Type”, ylab = “Acres”, main = “Total Acres by Owner Type”, las = 1, log = “y” # log scale# rotate x-axis labels vertically )

Add labels on top of bars

text( x = bp, y = acres_by_ownertype\(total_acres, labels = round(acres_by_ownertype\)total_acres, 1), pos = 1, # above the bar cex = 0.8, col = “black” )

acres_by_owner <- open_space %>% group_by(OWNER) %>% summarise( count = n(), # number of parcels total_acres = sum(GISACRES, na.rm = TRUE) # sum acres )

Sort ascending by total_acres

acres_by_owner_sorted <- acres_by_ownertype %>% mutate(avg_acres = total_acres / count) %>% arrange(desc(total_acres))

acres_by_owner_sorted

Increase left margin to fit long owner names

par(mar = c(5, 10, 4, 2)) # bottom, left, top, right

leaflet(open_space) %>% addProviderTiles(providers$Esri.WorldGrayCanvas) %>% addPolygons( fillColor = “hotpink”, color = “hotpink”, # darker outline weight =1, # thicker border fillOpacity = 1, popup = ~paste0(“Name:”, NAME_LABEL, “
Managed by:”, MANAGED_BY) )

Drop geometry for simple CSV

open_space_df <- st_drop_geometry(open_space)

Write to CSV

write.csv(open_space_df, “open_space.csv”, row.names = FALSE)