Questions tagged [r-sf]
The motivation behind the `sf` package is to provide a complete, standardized implementation of simple features in R, with links to GDAL, GEOS and Proj.4. There is a also https://gis.stackexchange.com/ for spatial R questions.
r-sf
1,995
questions
92
votes
1
answer
4k
views
Why does tmap render 80 times faster than ggplot2? [Plotting shapefiles in R with ggplot2::geom_sf(), using XQuartz/X11 graphics device on macOS]
Update/Edit/Reprex: Rendering the same spatial data with the same graphics device takes 1 second with tmap versus 80 seconds with ggplot2, even though the tmap plot's R object is 80x larger in size. ...
50
votes
3
answers
35k
views
sf: Write Lat/Long from geometry into separate column and keep ID column
I have a df with polygon ID's from a shapefile and their centre-points in a geometry column:
# A tibble: 3 x 2
ID geometry
<dbl> <POINT [°]>
1 1 (117.2 31.8)
2 2 (116....
42
votes
2
answers
20k
views
How to resolve spherical geometry failures when joining spatial data
I have a shapefile (with several polygons) and a dataframe with coordinates. I want to assign each coordinate in a dataframe to a polygon in a shapefile. So to add a column in a data frame with a ...
41
votes
3
answers
14k
views
How to put a geom_sf produced map on top of a ggmap produced raster
I tried the following code:
library(ggplot2)
library(ggmap)
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
str(nc)
Classes ‘sf’ and 'data.frame': 100 obs. of 15 ...
30
votes
1
answer
29k
views
Why use st_intersection rather than st_intersects?
st_intersection is very slow compared to st_intersects. So why not use the latter instead of the former? Here's an example with a small toy dataset, but the difference in execution time is huge for my ...
27
votes
3
answers
3k
views
Efficient extraction of all sub-polygons generated by self-intersecting features in a MultiPolygon
Starting from a shapefile containing a fairly large number (about 20000) of potentially partially-overlapping polygons, I'd need to extract all the sub-polygons originated by intersecting their ...
26
votes
2
answers
28k
views
How to make a data frame into a simple features data frame?
I've got a table with place references and x and y coordinates in a given coordinate reference system. I want to turn that into a simple feature data frame. How can I create that?
I thought it might ...
26
votes
1
answer
34k
views
How to calculate centroid of polygon using sf::st_centroid?
I am trying to manipulate some Brazilian Census data in R using the new "sf" package. I am able to import the data, but I get an error when I try to create the centroids of the original polygons
...
26
votes
3
answers
14k
views
How can I do a spatial join with the sf package using st_join()
Here's a toy example I've been wrestling with
# Make points
point1 <- c(.5, .5)
point2 <- c(.6, .6)
point3 <- c(3, 3)
mpt <- st_multipoint(rbind(point1, point2, point3)) # create ...
23
votes
1
answer
1k
views
fit two sf polygons seamlessly
The problem
Suppose we have two shapefiles that should border seamlessly. Only, they don't. Is there a way to force them to stick to one another without gaps?
The specific case
I have two ...
21
votes
4
answers
8k
views
Removing holes from polygons in R sf
Is there a way to remove holes from a polygon in R with the package sf? I would be interested in solutions that include other packages, too.
Here's an example of a polygon with two holes.
library(sf)
...
21
votes
3
answers
8k
views
Convert a list of sf objects into one sf
I have a list of sf objects that I would like to row bind to create a single sf object. I'm looking for a function similar to data.table::rbindlist, that would stack the individual objects in an ...
20
votes
4
answers
13k
views
Convert sequence of longitude and latitude to polygon via sf in R
I have five longitude and latitude that form a shape like this.
df <- c(order=1:5,
lon=c(119.4,119.4,119.4,119.5,119.5),
lat=c(-5.192,-5.192,-5.187,-5.187,-5.191))
How could I ...
20
votes
4
answers
7k
views
Snap a point to the closest point on a line segment using sf
I would like to snap a point to the closest point on a road segment using sf::st_snap. However, the function seems to return the wrong result, it's snapping my point to a point at the start of the ...
20
votes
1
answer
7k
views
R: Handling of sf objects in raster package
Previously I was using raster::crop and raster::mask with shapefiles of class Spatial*, read in using rgal::readOGR.
I am just "upgrading" my scripts to use sf for reading and manipulating polygons. ...