sp_left_join: Function for point-in-polygon tests.

View source: R/sp_left_join.R

sp_left_joinR Documentation

Function for point-in-polygon tests.

Description

sp_left_join tests if a point is within a polygon and joins data from a spatial object (the data slot of the spatial-polygon data frame) if the match is TRUE. This process is analogous to a SQL left join but with spatial objects. If variable names collide, they will be suffixed with "_polygons".

Usage

sp_left_join(sp_points, sp_polygons)

Arguments

sp_points

Spatial points object. Spatial points will usually have a data slot but sp_left_join does not require this.

sp_polygons

Spatial polygons object. sp_polygons will have a data slot (i.e. it will be a SpatialPolygonsDataFrame) because this is what is joined.

Details

over is used for the point-in-polygon test and the projection systems must be identical for the two geometries.

Value

A tibble, not a spatial object.

Author(s)

Stuart K. Grange

See Also

sp_transform, over, merge, sp_from_data_frame

Examples


## Not run: 

# I am in London, I know my latitude and longitude. Which borough am I in? 

# Load geojson containing the London boroughs
sp_london <- sp_read(
  "http://skgrange.github.io/www/data/london_sport.json",
  verbose = FALSE
) %>% 
  sp_transform()

# Make latitude and longitude a data frame, observation is optional
data_points <- data.frame(
  observation = "current_location",
  latitude = 51.523595, 
  longitude = -0.027114
)

# Promote to spatial points, this will use wgs84 when not stated
sp_points <- sp_from_data_frame(data_points, type = "points")

# Test point with 33 polygons (boroughs) in the shape file
data_point_test <- sp_left_join(sp_points, sp_london)

# Print
data_point_test$name

"Tower Hamlets"


## End(Not run)


skgrange/gissr documentation built on June 20, 2024, 12:02 a.m.