As soon as we’ve completed mapping our gap or course, it’s time to export all that onerous work right into a KML file. This may be completed by clicking the three vertical dots on the left aspect of the display the place your venture resides. This venture works finest with geoJSON knowledge, which we are able to simply convert our KML file to within the subsequent steps. Now we’re prepared to go to R.
The packages we might want to put together us for plotting are: sf (for working with geospatial knowledge), tidyverse (for knowledge cleansing and plotting), stringr (for string matching), and geojsonsf (for changing from KML to geoJSON). Our first step is studying within the KML file, which will be completed with the st_read() perform from sf.
# load librarieslibrary(sf)library(tidyverse)library(stringr)library(geojsonsf)
kml_df <- st_read(“/Customers/adambeaudet/Downloads/erin_hills.kml”)
Nice! Now we must always have our golf course KML knowledge in R. The info body ought to have 2 columns: Title (venture identify, or course identify in our case), and geometry (an inventory of all particular person factors comprising the polygons we traced). As briefly talked about earlier, let’s convert our KML knowledge to geoJSON and likewise extract the course identify and gap numbers.
# convert from KML to geoJSONgeojson_df <- st_as_sf(kml_df, “POLYGON”)
# extracting course identify and gap quantity from polygon identify# assuming “course_hole_element” naming conference is used for polygonsgeojson_df$course_name <- str_match(geojson_df$Title, “^(.+)_hole”)[,2] geojson_df$hole_num <- gsub(“.*_hole_(d+)_.*”, “1”, geojson_df$Title)
To get our maps to level due north we have to venture them in a approach that preserves route. We will do that with the st_transform() perform.
# outline a CRS for therefore map all the time factors due northcrs <- “+proj=lcc +lat_1=33 +lat_2=45 +lat_0=39 +lon_0=-96 +x_0=0 +y_0=0 +datum=WGS84 +items=m +no_defs”
# remodel knowledge to CRSgeojson_df <- st_transform(geojson_df, crs)
We’re virtually able to plot, however first, we have to inform ggplot2 how every polygon ought to be coloured. Beneath is the colour palette my venture is utilizing, however be happy to customise as you want.
Non-compulsory: on this step we are able to additionally calculate the centroids of our polygons with the st_centroid() perform so we are able to overlay the opening quantity onto every inexperienced.
geojson_df <- geojson_df %>%mutate(shade = case_when(grepl(“_tee$”, Title) ~ “#57B740”,grepl(“_bunker$”, Title) ~ “#EDE6D3”,grepl(“_water$”, Title) ~ “#2243b6”,grepl(“_fairway$”, Title) ~ “#57B740”,grepl(“_green$”, Title) ~ “#86D14A”,grepl(“_hazard$”, Title) ~ “#094d1d”)) %>%mutate(centroid = st_centroid(geometry))
We’re formally able to plot. We will use a mix of geom_sf(), geom_text(), and even geom_point() if we need to get fancy and plot photographs on prime of our map. I sometimes take away gridlines, axis labels, and the legend for a cleaner look.
ggplot() +geom_sf(knowledge = geojson_df, aes(fill = shade), shade = “black”) +geom_text(knowledge = filter(geojson_df, grepl(“_green”, Title)),aes(x = st_coordinates(centroid)[, 1],y = st_coordinates(centroid)[, 2],label = hole_num),dimension = 3, shade = “black”, fontface = “daring”, hjust = 0.5, vjust = 0.5) +scale_fill_identity() +theme_minimal() +theme(axis.title.x = element_blank(),axis.title.y = element_blank(),axis.textual content.x = element_blank(),axis.textual content.y = element_blank(),plot.title = element_text(dimension = 16),panel.grid.main = element_blank(),panel.grid.minor = element_blank()) +theme(legend.place = “none”) +labs(title = ‘Erin Hills | Hartford, WI’)
And there you’ve it — a golf course plotted in R, what an idea!
To view different programs I’ve plotted on the time of writing this text, you possibly can go to my Shiny app: https://abodesy14.shinyapps.io/golfMapsR/
In the event you adopted alongside, had enjoyable in doing so, or are intrigued, be happy to attempt mapping your favourite programs and create a Pull Request for the golfMapsR repository that I preserve: https://github.com/abodesy14/golfMapsRWith some mixed effort, we are able to create a pleasant little database of plottable golf programs world wide!