Questions tagged [sf]
Use this tag when asking questions about the R package sf (Simple Features for R)
679
questions
76
votes
4
answers
65k
views
Extracting data.frame from simple features object in R
Is there an sf-native (i.e. "correct") way of extracting everything except the geometry column from a simple features object? This works
df <- dplyr::select(as.data.frame(sf), -geometry)
but the ...
45
votes
2
answers
49k
views
R convert sf object back to SpatialPolygonsDataFrame
How do I convert an sf object back to a SpatialPolygonsDataFrame?
I am running R 3.4.0 and sf version 0.4.1. The sf documentation mentions an as_Spatial function, but this isn't available in my ...
41
votes
1
answer
37k
views
Lon-Lat to Simple Features (sfg and sfc) in R
How can I convert lon-lat points to simple features (sfg), and then put them in a simple feature collection (sfc)?
Here is a MWE that doesn't work but is the closest I have gotten to.
library(data....
28
votes
4
answers
23k
views
Crop simple features object in R
Is there a function to crop sf map object, similar to maptools::pruneMap(lines, xlim= c(4, 10), ylim= c(10, 15)) used for SpatialPolygon or SpatialLine?
I am considering st_intersection() but there ...
25
votes
1
answer
18k
views
Simplify polygons of sf object
How do I simplify an sf polygon without introducing gaps and slivers?
With a shapefile, for example, I would use rmapshaper::ms_simplify():
library("pryr")
library("rgdal")
library("rmapshaper")
...
23
votes
2
answers
12k
views
equivalent of sp package's point in polygon / overing using sf
I am migrating code from sp package to the newer sf package. My previous code I had a polygon SpatialDataFrame (censimentoMap) and a SpatialPointDataFrame (indirizzi.sp) and I got the polygon cell id (...
19
votes
3
answers
9k
views
How to combine sfc objects from R package sf
Using the R package sf, how does one combine sfc objects? For example, given the following code, how would one create a single sfc object sfc12 that includes the geometries from both sfc1 and sfc2? (...
16
votes
3
answers
13k
views
Removing empty polygon from sf object in R?
I have such object:
> p_mb
Simple feature collection with 52 features and 0 fields (with 9 geometries empty)
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 585001.4 ymin: 211001....
16
votes
1
answer
19k
views
Create a circle of defined radius around a point and then find the overlapping area on a shapefile
My use case is related to calculating the area of a polygon in a shapefile that is covered by another polygon.
Specifically, I want to do the following:
Create circles that have a 100m radius, from ...
15
votes
4
answers
11k
views
How do I combine geometries in a shapefile based on a grouping variable?
Here is a simple example. I have a shapefile of three US states:
This is the code that creates the shapefile.
library(USAboundaries)
library(sf)
library(dplyr)
states <- us_states(map_date = "...
15
votes
5
answers
30k
views
Identify polygon containing point with R sf package
I would like to be able to map geocoded coordinates to a region on a .shp file in R using the sf package. I can load up the map and plot it but I am struggling with the code to return the region for ...
14
votes
1
answer
11k
views
Counting points in polygons with sf package of R?
I'm quite new to R and especially GIS with R. So recently I had to proceed to a quite simple analysis: counting health centers (points) per administrative unit (polygons). Quite common process in ...
14
votes
2
answers
6k
views
sf geometry column naming differences R
I've got several shapefiles stored as geopackages and I want to merge all of them by using rbind. But unfortunately the geometry columns are namend differently as geometry or geom and because of that ...
14
votes
4
answers
13k
views
Converting points to polygons by group
I have a dataframe with around 40 points. They are grouped by an ID variable (their name), which four points in each group. I'm trying to turn these points into polygons, but I've had a lot of trouble ...
13
votes
1
answer
6k
views
Writing multiple layers to GeoPackage using writeOGR() in R?
I'm trying to write multiple layers to the same GeoPackage in R, but I get an error, Creation of output file failed. I have tried to search for documentation of reading and writing to .gpkg files with ...
12
votes
2
answers
15k
views
R: sf package points to multiple lines with st_cast
I want to create multiple lines out of given points as sf objects.
If I have a number of points as
library(sf)
pts <- st_multipoint(matrix(c(10, 10, 15, 20, 30, 30), nrow = 3, byrow = TRUE), dim =...
12
votes
3
answers
5k
views
Return end-vertices of an sf linestring
I am trying to return the vertices of a linestring using the sf package. So far I have:
ls <- st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
st_linestring(rbind(c(0,0),c(10,0))))
...
12
votes
3
answers
9k
views
R/sf: Some edges are crossing in a MULTIPOLYGON. How to make it valid when using S2 geometry?
Goal: I would like to know the NUTS region (2016 version) of points in Europe.
Method: (1) Download the official geodata from Eurostat, (2) Use st_join from sf with R.
Problem: I've updated sf, now ...
11
votes
3
answers
9k
views
Load a CSV as Simple Features R
Is there a direct way to load a CSV as an sf? I've looked through the sf documentation and nothing jumps out at me, currently using:
my_csv <- read.csv('my_data.csv', header = TRUE)
my_sp <- ...
11
votes
1
answer
6k
views
Split polyline by point using 'sf' package in R
I'm learning the sf package in R and would like to locate about 450 sites along the nearest reaches of a river network and to obtain the upstream and downstream distances along the reach where the ...
11
votes
1
answer
3k
views
r - create multipolygon from overlapping polygons using sf package
I'm trying to remove overlapping areas from multiple polygons using sf like the following:
# sample polygon
poly <- data.frame(
lon = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 2, 0.8, 1, 1, 2, ...
11
votes
2
answers
8k
views
Breaking long line segments into shorter ones in R using sf
I have a shapefile of river segments that I've imported into R. The segments range in length from 5-115km. I want to split the segments that are over 10km into 10km chunks. So I use st_segmentize. But ...
10
votes
3
answers
11k
views
Getting a new column with distance to the nearest feature in R
Having two sf data frames (a is points and b is polygons),
I can get columns from the nearest feature by for instance:
c <- st_join(a, b, st_nearest_feature),
But how is the best way to get an ...
10
votes
4
answers
5k
views
Split a line when it crosses a polygon in R
This should be a simple problem but I can't seem to find a solution.
I have a line which crosses several polygons and I want to split it into multiple lines at the intersection of each polygon using ...
10
votes
2
answers
17k
views
sf create polygon from minimum x and y coordinates
I am trying to create a polygon from a coordinate dataframe. I have the minimum and maximum x-y coordinate information. But, I am getting the following error. How can this be fixed?
library(tidyverse)
...
10
votes
2
answers
11k
views
Unable to install ‘sf’ R package on Ubuntu 16.04 LTS
I'm not able to install sf R package on Ubuntu 16.04 LTS. I tried
install.packages("sf")
as well as
devtools::install_github("r-spatial/sf")
after running
sudo add-apt-repository ppa:ubuntugis/...
10
votes
1
answer
5k
views
Calculating distances between two geometry columns using R?
I'd like to calculate the pairwise distance between two geometry columns. That is, I want to calculate the distance between several pairs of geometries without calculating the full distance matrix ...
10
votes
2
answers
14k
views
How to calculate distance from POINT to LINESTRING in R using `sf` library and get all POINT features from LINESTRING where distances were calculated?
I'm trying to calculate distances from a set of POINT features (1, 2, 3, 4, 5, 6 & 7) to a LINESTRING feature using sf library in R. I could calculate all the distances using the function ...
9
votes
5
answers
9k
views
Find nearest point along polyline using 'sf' package in R
This is a follow-up question related to my earlier post about using the sf package to split polylines using nearby points.
I have 450 points with XY coordinates and a large river network (~214,000 ...
9
votes
2
answers
1k
views
Split linestrings at pairwise intersections [closed]
I have a set of line strings in a single sf object R which I want to turn into a (sf)network. Not in all cases are the intersections marked. So I want to transform the left data to the right, dividing ...
9
votes
2
answers
19k
views
spTransform error (unable to find inherited method for function spTransform)
I am trying to reproject a shapefile. For example, the land shapefile from Natural Earth.
library(sf)
shape <- read_sf(".../ne_10m_land.shp")
shape_proj<-spTransform(shape, CRS("+proj=gnom +...
9
votes
1
answer
3k
views
r - sf::st_cast("LINESTRING") - keeping first linestring only warning
I have a dataset with geometry column in which there are linestrings and multilinestrings. While keeping the linestrings I want to convert the multilinestrings to linestrings (which should potentially ...
9
votes
1
answer
4k
views
How to convert a polygon to a polyline using the sf package in R?
I have some simple polygons that I would like to convert to polylines using the sf package in R. I have found st_polygonize to convert polylines to polygons but I cannot but the reserve operation. ...
9
votes
1
answer
4k
views
Kriging example with sf object
I am new to geo spacial analysis. I would like to learn kriging from a simple example. Say, we have four points with some z value.
library(sf)
library(gstat)
library(tidyverse)
dt<-tibble::...
8
votes
2
answers
7k
views
Calculate percentage overlap of 2 sets of polygons in R
I have a map of NC counties. I have overlayed two circles, representing the coverage area of two radio transmitters in the state:
### Make transmitters df
rm(list = ls())
library(tidyverse)
library(...
8
votes
2
answers
3k
views
Selecting features "above" or "below" a line using R
Given a line and a set of points, I can't figure out how to use sf to identify which side of the line each point falls.
A small reproducible example follows, adapted from a different question
# Load ...
8
votes
1
answer
7k
views
Applying filter with the sf package not working
Question:
I have a sf-object in R and want to select a few polygons according to their location name. I would like to apply the dpylr approach using the filter function (analog to this example). ...
8
votes
2
answers
7k
views
Using simple features (sf) in R, how do I erase polygons overlapping with another layer
With a regular spatialDataFrame I could use erase(x, y) or x-y. How can I perform the equivalent using sf objects?
As an example:
##Create layers based on this answer: http://stackoverflow.com/...
8
votes
3
answers
7k
views
Dissolve only overlapping polygons in R using sf
I extracted information from open street map that contains many polygons, some of them overlapping. I want to union all polygons that overlap with other polygons. This question has been asked before ...
8
votes
1
answer
6k
views
Convert a linestring into a closed polygon when the points are not in order
How can I convert an open linestring into a closed polygon, but when the points are not in order?
Here is the easy case, when the points are in order (from an sf vignette):
library(sf)
#> Linking ...
7
votes
3
answers
4k
views
R st_centroid GEOS error Unknown WKB type 12
I’m trying to calculate centroids of polygons of postcode areas (wfs shapefile), but I’m getting the following error message:
Error in CPL_geos_op(“centroid”, x, numeric(0), integer(0), numeric(0), : ...
7
votes
3
answers
9k
views
Using sf to combine polygons that share borders?
I have rows of respondents with their residential census tract polygon and am trying to generate first-order aggregated areas. Meaning I want a given respondent’s census tract polygon to be merged ...
7
votes
1
answer
5k
views
Remove z-value from XYZ point in sf
I have a sfc-object consisting of just points. Unfortunately, when I read in the shapefile, it created points of type XYZ. Now I removed some points and can't figure out how to export the remaining ...
7
votes
1
answer
7k
views
How to update CRS r object from PROJ4 to PROJ6
I have a script which contains several r objects which are CRSs that I use for transforming spatial objects. After updating R and many packages, there is a warning which wasn't there before.
LCC <- ...
7
votes
2
answers
416
views
Generate a stars object from tabular spatial temporal data?
I have tabular data with columns longitude, latitude, time, and intensity, like so:
library(lubridate)
## mock example data
ex <- tibble(longitude = 1:10,
latitude = 10:1,
...
7
votes
0
answers
1k
views
R sf package - link to GDAL
Is it possible to ask R {sf} package to use a GDAL installation I prefer?
So far I found how to check external software it uses:
library(sf)
sf_extSoftVersion()
# GEOS GDAL ...
6
votes
1
answer
18k
views
R: Using st_intersects to classify points inside, outside and within a buffer
I have two objects, a shapefile of location points, points, and a shapefile of polygons, world_buffer (both +proj=utm +datum=WGS84). The world_buffer is the world's coastlines, found here, with a 5 km ...
6
votes
1
answer
3k
views
What is sfc in sf Package of R?
The following picture describes a simple feature collection of the package sf in R.
However, I don't get the meaning of sfc.
A feature collections consists of a set of simple features, three in the ...
6
votes
3
answers
11k
views
Cropping sf object in R? [duplicate]
Newish to R
Very new to GIS
I've grabbed some vector spatial census data using the cancensus package.
I now have a spatial data frame with variables and geometry
I plot it and decide to focus on a ...
6
votes
2
answers
6k
views
Combining st_join with st_nn to include all points within, as well as within a given distance of a polygon
This is a follow up to my earlier question (Spatial join in R - Adding points to polygons with multiple corresponding points).
I have successfully joined a spatial points file to a polygon file in R ...