Last active
April 9, 2021 22:18
-
-
Save SPLOpenData/a3e81104b5b8f4f9f7e2fb3279556ec3 to your computer and use it in GitHub Desktop.
This R script downloads King County WA parcel open data in R as an sf object, and creates point-based centroids for each_parcel.
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
#This R script downloads King County WA parcel open data in R as an sf object, and creates point-based centroids for each_parcel. | |
#This script is intented to help you get up and running. | |
#Tempfile used for downloading. | |
temp <- tempfile() | |
temp2 <- tempfile() | |
#Download the open data ArcGIS zip folder containing the parcel data as a .shp file. | |
# As an alternative, you can download KML or csv version from here: https://gis-kingcounty.opendata.arcgis.com/datasets/parcels-for-king-county-with-address-with-property-information-parcel-address-area | |
download.file("https://opendata.arcgis.com/datasets/c7a17b7ad3ec44b7ae64796dca691d72_1722.zip",temp) | |
#Unzip the contents of 'temp' object and save unzipped content in 'temp2' | |
unzip(zipfile = temp, exdir = temp2) | |
#Finds the filepath of the shapefile (.shp) file in the temp2 unzip folder. | |
#the $ at the end of ".shp$" ensures you are not also finding files such as .shp.xml | |
your_SHP_file<-list.files(temp2, pattern = ".shp$",full.names=TRUE) | |
#Read the shapefile. | |
library(sf) | |
library(ggplot2) | |
KC<- sf::st_read(your_SHP_file) | |
#Create usable centroid points for each parcel | |
KC$centroids<-st_transform(KC, 4326) %>% st_centroid(KC) %>% st_geometry() | |
KC$long<-st_coordinates(KC$centroids)[,1] | |
KC$lat<-st_coordinates(KC$centroids)[,2] | |
head(KC$centroids) | |
#Make a basic ggplot of all KC parcels. | |
#NOTE: Try subsetting to something specific for faster load, since this could take minutes. | |
p<- ggplot() + geom_sf(data=KC,aes(fill=APPR_IMPR))+ | |
scale_fill_gradient(low = "#FFFFE0", high = "#6f42c1")+ | |
geom_sf(data = KC$centroids, size = 1, shape = 21, fill = "#0061AC") + | |
coord_sf(xlim =c(-122.45, -122.0), ylim = c(47.4,47.77), expand = FALSE,crs = st_crs(4326)) | |
#Add labels and view. | |
p+ theme_bw() +labs(title="King Count Parcel Data", subtitle="What will you uncover?", x="", y="") | |
#Start your exploratory data analysis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment