#install.packages(c(“remotes”, “rlang”, “cli”, “magrittr”, “tibble”), dependencies = TRUE) #remotes::install_github(“yonghah/esri2sf”, force = TRUE)
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)
open_space <- st_read(content(res, “text”), quiet = FALSE)
st_geometry_type(open_space)
st_crs(open_space)
head(open_space)
sum(open_space$GISACRES)
invalid <- !st_is_valid(open_space) sum(invalid) # number of invalid polygons
open_space <- st_make_valid(open_space)
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 )
acres_by_ownertype <- acres_by_ownertype %>% mutate(avg_acres = total_acres / count) %>% arrange(desc(total_acres))
acres_by_ownertype
print(acres_by_ownertype)
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 )
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 )
acres_by_owner_sorted <- acres_by_ownertype %>% mutate(avg_acres = total_acres / count) %>% arrange(desc(total_acres))
acres_by_owner_sorted
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) )
open_space_df <- st_drop_geometry(open_space)
write.csv(open_space_df, “open_space.csv”, row.names = FALSE)