In this Notebook you’ll practice importing some tabular data in to R, converting it to a simple feature data frame, projecting it, and saving it to disk as a GeoJSON file.
miss_pips_df <- read.csv("./data/yosemite_missing_people.csv", stringsAsFactors = FALSE)
head(miss_pips_df)
# View(miss_pips_df)
Let’s reduce the number of columns:
library(dplyr)
miss_pips_thin_df <- miss_pips_df |>
select(Long, Lat, Type, CaseNumber, IncidYear, ContactMet, IncidType, NumberofSu, GroupDynam,
SubjectCat, SubSex, SubAge, IncidContr, Scenario, RescueMeth, Incident_N, Found_GR_N)
head(miss_pips_thin_df)
For convenience, here are some EPSG numbers:
epsg_geo_wgs84 <- 4326 ## General long-lat
epsg_utm11n_nad83 <- 26911 ## use this one for YNP
Convert a data frame to a spatial object with
st_as_sf(). You have to specify the names of the columns
that contain the coordinates, and the EPSG number of the
coordinates.
library(sf)
miss_pips_sf <- st_as_sf(miss_pips_thin_df,
coords = c("Long", "Lat"),
crs = epsg_geo_wgs84)
miss_pips_sf
Simple feature collection with 213 features and 15 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -119.8801 ymin: 37.50101 xmax: -119.2493 ymax: 38.03343
Geodetic CRS: WGS 84
First 10 features:
Type CaseNumber IncidYear ContactMet IncidType NumberofSu GroupDynam
1 IPP 20090248 2009 Subject Cell Phone Search 1 Solo Subject
2 IPP 20090652 2009 Reported Missing Separated Party 1 Solo Subject
3 IPP 20090940 2009 Reported Missing Overdue 1 Solo Subject
4 IPP 20091134 2009 Subject Cell Phone Search 1 Solo Subject
5 IPP 20091252 2009 Reported Missing Separated Party 1 Solo Subject
6 IPP 20091345 2009 Reported Missing Overdue 1 Solo Subject
7 IPP 20091382 2009 Reported Missing Overdue 2 Group Stayed Together
8 IPP 20091583 2009 Reported Missing Overdue 2 Group Stayed Together
9 IPP 20091755 2009 Subject Cell Phone Search 3 Group Stayed Together
10 IPP 20091760 2009 Reported Missing Search 1 Solo Subject
SubjectCat SubSex SubAge IncidContr Scenario RescueMeth
1 Mental Retardation Male 31 Darkness Lost Snow Machine
2 Hiker Male 0 Unknown Separated Walkout
3 Child (13-15) Male 14 Unknown Overdue Other
4 Hiker Male 35 Snow/Ice Lost Helicopter
5 Child (4-6) Male 6 Unknown Separated Other
6 Hiker Male 29 Unknown Overdue Other
7 Climber Group - Mixed Sex 0 Weather - Cold Overdue Walkout
8 Hiker Group - All Males 0 Darkness Lost Vehicle
9 Hiker Group - All Males 0 Weather - Visibility Lost Helicopter
10 Substance Abuse Male 23 Drugs Despondent Other
Incident_N
1 Subject was snowshoeing, became disoriented, and called for help. Subject described as mentally challenged.
2 Subject described as teenage male; found in under half an hour from reported missing time
3 Subject hiked Yosemite Falls Trail and was found at his bike after being reported overdue
4 Subject hiked up Yosemite falls Trail and to North Dome; planned to hike back to the Valley via Snow Creek Trail but lost tral due to snow
5 Subject became separated from father and returned to his hotel room to wait for his return
6 Subject took shortcut off trail while hiking Upper Yosemite Falls Trail and became separated from hiking partner
7 Two subjects, one M and one F, overdue after climbing
8 Two M subjects lost the trail in the darkness after hiking to Taft Point; attempted to find a high point to reorient themselves
9 Three M subjects intentionally left the trail to take a shortcut to some bathrooms that they saw
10 Subject ingested LSD and was last seen running nude through Upper Pines Campground; subject found deceased with fatal injuries consistent with a fall from great heights
Found_GR_N
1 Found just south of the top of the Eale Chair Lift at Badger Pass Ski at an elevation of approximately 8,200 ft
2 Found in the parking lot looking for his mother
3 Found at LKP after hike was complete
4 Found in the area of Indian Ridge, approximately 300 feet off the trail
5 Found at Yosemite Lodge room 4623
6 Found near the Bank3-Way intersection
7 Found near the base of Drug Dome and in the forested area leading to the trailhead
8 Found walking along the road approximately 2 miles west of the Sentinel Dome parking area
9 Found on the west-facing slope of Grizzly Peak at approximately -119 33.235 and 37 43.906
10 Found along the cliff bands below the Porcelain Wall on the west shoulder of Half Dome near Mirror Lake
geometry
1 POINT (-119.6632 37.66355)
2 POINT (-119.8099 37.7691)
3 POINT (-119.5958 37.74595)
4 POINT (-119.5599 37.75631)
5 POINT (-119.5937 37.74561)
6 POINT (-119.6064 37.74521)
7 POINT (-119.4291 37.86868)
8 POINT (-119.5864 37.71233)
9 POINT (-119.5271 37.74873)
10 POINT (-119.5634 37.73601)
epsg_utm11n_nad83 <- 26911
yose_miss_pips_utm11n_sf <- miss_pips_sf |>
st_transform(epsg_utm11n_nad83)
You can save a spatial object to disk with st_write(). R
will figure out what format to use based on the file name extension.
Save the Missing Persons layer as GeoJSON: Answer
st_write(yose_miss_pips_utm11n_sf, "yose_miss_pips.geojson", delete_dsn = TRUE)
Deleting source `yose_miss_pips.geojson' using driver `GeoJSON'
Writing layer `yose_miss_pips' to data source `yose_miss_pips.geojson' using driver `GeoJSON'
Writing 213 features with 15 fields and geometry type Point.