As soon as you’ve got completed mapping your holes or programs, export all that arduous work to a KML file. To do that, click on on her three vertical dots on the left aspect of the display screen the place the venture is positioned. This venture works finest with geoJSON knowledge, the place you may simply convert KML information utilizing the next steps. Now you might be able to go to R.
The packages required to arrange the plot are: science fiction (for manipulating geospatial knowledge), Tidiverse (for knowledge cleansing and plotting), stringer (for string matching), and geosonf (For KML to geoJSON conversion). Step one is to learn the KML file. This may be accomplished with the next command: st_read() Features from SF.
# load libraries
library(sf)
library(tidyverse)
library(stringr)
library(geojsonsf)kml_df <- st_read("/Customers/adambeaudet/Downloads/erin_hills.kml")
fantastic! It is best to now have his KML knowledge for the golf course in R. The info body ought to have two columns. identify (venture identify, or course identify on this case), and Geometry (a listing of all the person factors that make up the traced polygon). As we briefly mentioned earlier, let’s convert the KML knowledge to geoJSON and likewise extract the course identify and gap quantity.
# convert from KML to geoJSON
geojson_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 polygons
geojson_df$course_name <- str_match(geojson_df$Title, “^(.+)_hole”)[,2]
geojson_df$hole_num <- gsub(“.*_hole_(d+)_.*”, “1”, geojson_df$Title)
To orient the map to true north, you want to venture it in a means that maintains orientation. To do that, st_transform() operate.
# outline a CRS for therefore map all the time factors due north
crs <- "+proj=lcc +lat_1=33 +lat_2=45 +lat_0=39 +lon_0=-96 +x_0=0 +y_0=0 +datum=WGS84 +models=m +no_defs"# remodel knowledge to CRS
geojson_df <- st_transform(geojson_df, crs)
The plot is nearly prepared, however first we have to inform ggplot2 the way to coloration every polygon. Under is the colour palette I take advantage of in my venture, however be happy to customise it to your wants.
choice: On this step, you may as well calculate the centroid of the polygon by: st_centroid() The operate lets you overlay gap numbers onto every inexperienced.
geojson_df <- geojson_df %>%
mutate(coloration = 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))
You are actually formally able to plot.You should utilize the next combos geom_sf(), geom_text(),Moreover geom_point() If you wish to get extra fancy and plot your pictures on a map. Sometimes, you need to take away gridlines, axis labels, and legends for a cleaner look.
ggplot() +
geom_sf(knowledge = geojson_df, aes(fill = coloration), coloration = "black") +
geom_text(knowledge = filter(geojson_df, grepl("_green", Title)),
aes(x = st_coordinates(centroid)[, 1],
y = st_coordinates(centroid)[, 2],
label = hole_num),
measurement = 3, coloration = "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(measurement = 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 gotten it – a golf course plotted in R. What an idea!
To see different programs I’ve plotted as of this writing, go to my Shiny app. https://abodesy14.shinyapps.io/golfMapsR/
Should you’ve been following alongside, loved it, or have an interest, please be happy to map your favourite programs and create a pull request. golf map R Repositories I handle: https://github.com/abodesy14/golfMapsR
By combining some efforts, you may create a pleasant little database of plottable golf programs around the globe.