To drop the geometry column, use st_drop_geometry():
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc_df2 <- nc %>% st_drop_geometry()
#>  "data.frame"
Before st_drop_geometry() was added to the sf package (in November, 2018), one could produce the same result ...
Have you tried st_as_sf() which converts object (sp, dataframe, ...) to an sf object?
# your data (removed crs column)
DT <- data.table(
place=c("Finland", "Canada", "Tanzania", "Bolivia", "France"),
longitude=c(27.472918, -90.476303, 34.679950, -65.691146, 4.533465),
Since today, there is a st_crop function in the github version of sf (devtools::install_github("r-spatial/sf"), probably on CRAN in the near future too).
st_crop(nc, c(xmin=-82, xmax=-80, ymin=35, ymax=36))
The vector must be named with xmin xmax ymin ymax (in whichever order).
You can also use any object that can be read by st_bbox as ...
You can get the same result by using st_join:
First create a demo polygon and some points with sf.
poly <- st_as_sfc(c("POLYGON((0 0 , 0 1 , 1 1 , 1 0, 0 0))")) %>%
st_sf(ID = "poly1")
pts <- st_as_sfc(c("POINT(0.5 0.5)",
"POINT(3 3)")) %>%
You can cast an sf object to sp, for packages that don't yet support sf - I do this a fair bit for raster/polygon interactions. So you could do:
simplepolys <- rmapshaper::ms_simplify(input = as(sfobj, 'Spatial')) %>%
st_intersection is probably the best way. Find whatever way works best to get an sf object to intersect with your input. Here's a way using the convenience of raster::extent and a mix of old and new. nc is created by example(st_read):
st_intersection(nc, st_set_crs(st_as_sf(as(raster::extent(-82, -80, 35, 36), "SpatialPolygons")), st_crs(nc)))
I don't ...
Just use c like its a vector:
> (sfc12 = c(sfc1, sfc2))
Geometry set for 2 features
geometry type: POINT
bbox: xmin: 0 ymin: 1 xmax: 1 ymax: 1
epsg (SRID): NA
And the length is 2:
It's faster and easier to use st_intersects, no need to loop. The output is a bit obscure, essentially a classed list of feature IDs intersected, so we get the lengths() which is the number of points inside each feature.
First polygon has six points in it, in this example.
#> Linking to GEOS 3.7.0, GDAL 2.4.0, PROJ 5.2.0
poly <- ...
You've not attached the dplyr package:
> e %>% filter(ADMIN_NAME=="Durham")
Error in data.matrix(data) :
(list) object cannot be coerced to type 'double'
When you do, you'll see that dplyr stomps on the base R filter function with its own filter function:
Attaching package: ‘dplyr’
The following objects are masked from ‘...
You can do this using the append flag on sf::st_write():
nc <- st_read(system.file("shape/nc.shp", package="sf"))
storms <- st_read(system.file("shape/storms_xyz.shp", package="sf"))
st_write(nc, "nc.gpkg", "nc")
st_write(storms, "nc.gpkg", "storms", append = TRUE)
## Driver: GPKG
## Available layers:
What you are looking can be done using sf::st_intersects() as commented. I provide a full working example using USA states.
tt <- read_sf(path, "USA_adm1")
# subset some states to make it plot faster
tt1 <- tt[tt$NAME_1 %in% c("South Dakota", "Wyoming",
"Nebraska", "Iowa"), ]
I had to use sf:::as_Spatial() as workaround.
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# sf -> sp
nc_sp <- as_Spatial(nc$geom) # Error: could not find function "as_Spatial"
nc_sp <- sf:::as_Spatial(nc$geom) # This works
Here is an answer that applies sf package functions to the reproducible data kindly provided by rcs.
A <- st_as_sfc("LINESTRING(458.1 768.23, 455.3 413.29, 522.3 325.77, 664.8 282.01, 726.3 121.56)")
B <- st_as_sfc("MULTIPOLYGON(((402.2 893.03, 664.8 800.65, 611.7 666.13, 368.7 623.99, 215.1 692.06, 402.2 893.03)), ((703.9 366.29, 821.2 ...
I think this should do what you want.
Essentially what I mentioned in my comment. One thing I didn't mention is that you will want to transform to an appropriate equal-area projection that uses metres so that you can buffer by 100m and be confident that the areas calculated across your study are equivalent. I don't know the correct one for Switzerland, but ...
I think that the sf package need to know first how you want to create the lines from your points. I mean which pair of POINT make every LINESTRING. In my example that was defined inside the lapply function. Follow the reproducible and commented code below, hope that helps:
# Load library
# Create points data
multipoints <- st_multipoint(...
I figured it out. The polygons need to be manually closed by rbinding the first row:
state_data <- ggplot2::map_data("state")
state_data_sf <- lapply(unique(state_data$region),
state_data[state_data$region == x,][,c("long", "lat")],
st_cast is what you are looking for.
ls <- st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
ptns = st_cast(ls, "POINT")
mapview(ls, color = "red") + ptns
You can also cast to MULTIPOINT if you wish.
As mentioned by @Spacedman in the comments, this returns 4 points, ...
The answer provided is related to this question How to subset a SpatialPoints object to get the points located on each side of a SpatialLines object using R? but using sf library instead of sp.
Check the commented code below.
# Load Libraries ----------------------------------------------------------
# Test data ----------------------------...
I get much faster results with velox if I crop the raster before running extract, e.g.:
r <- velox("testras_so.tif")
I've also been working on a package with an optimized extract function that may be of interest:
exact_extract(ras, poly) # get a matrix with weights and values,
When you read a CSV with no options the driver doesn't know where the coordinate columns are so returns a data frame:
Reading layer `pts' from data source `/home/rowlings/Downloads/SO/csv/pts.csv' using driver `CSV'
id x y
1 1 10.1 2.1
2 2 2.4 12.1
3 3 3.2 4.5
no simple feature ...
STOP USING SO MANY PIPES!!!
n_grid <- (area_box / (area_box - area_shp ) )*n_points %>% sqrt()
n_grid <- (area_box/(area_box - area_shp)) * (n_points %>% sqrt())
in other words its square-rooting n_points. Fix that and I get exactly 200 points.
The pipe operator has high priority:
> 3 * 9 %>% sqrt()
> 3 * (9 %>...
While sf package don't have a built-in function or geosphere is not compatible with sf objects I would use a wrapper around geosphere::dist2Line function: just getting the matrix of coordinates instead using the entire sf object.
I also tried @jsta answer based on sampling the line and I compared the differences between both approaches. Since I'm working ...
If you snap the point to the line then you can split the line and then extract the parts from the resulting collection. Use a really small tolerance, I don't know how small it needs to be...
> site_snap = st_snap(site, reach, tol=1e-9)
> parts = st_collection_extract(st_split(reach$geometry, site_snap$geometry),"LINESTRING")
parts is now two features,...