Questions tagged [sf]
us this tag when asking questions about sf R package (sf: Simple Features for R)
386
questions
4
votes
1answer
366 views
fit two sf polygons seamlessly
Copied from my SO question
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 ...
8
votes
2answers
1k 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
2answers
3k 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 ...
0
votes
2answers
806 views
How to apply log scale when plotting sf data?
I want to plot some sf data, similar to this example:
library (sf)
fishnet <- st_make_grid ()
n <- length (fishnet)
vals <- rlnorm (n)
fishnet <- st_sf (fishnet)
fishnet <- cbind (...
6
votes
1answer
2k 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). ...
1
vote
0answers
247 views
sf::st_read_db() considerably slower than postGIStools::get_postgis_query()
I have a large (extent not size) polygon layer of the German federal state borders on my PostgreSQL data base. There is also an index on the geom column.
CREATE INDEX germany_bld_geom_idx
ON ...
0
votes
2answers
901 views
Get length of components of a multilinestring
I am looking to get not the length of the overall multilinestring but rather the length of the component parts. Here is what I have so far:
library(sf)
s1 <- rbind(c(0,3),c(0,4),c(1,5),c(2,5))
s2 ...
1
vote
1answer
744 views
lines of a SpatialLinesDataFrame corresponding to points
I have a spatial lines data frame which can be projected on to a map. The projection is defined as WGS84. I have a Spatial Points Data Frame which is also in the same location.
How can I isolate the ...
0
votes
2answers
300 views
How to get a sf with line segments from a data table?
Given the following code:
library(data.table)
x<-runif(10)
y<-runif(10)
dt<-data.table( someAtt=sample(c("a","b","c"),10,replace = T),
long=x, lat=y, prevLong=shift(x), prevLat=shift(y) ...
0
votes
1answer
83 views
Coercing to sp and dealing with duplicate field names
I am dealing with a very similar situation as described at:
rgdal::writeOGR collision in column names results in NULL data
I have an sf object with duplicate field names (in a case-insensitive way). ...
2
votes
1answer
554 views
Is there a way to create dot density maps with R package sf?
That is, is there a way to place dots at random positions inside a polygon with sf?
It's easy enough to do with maptools, using dotsinPolys, but since I like sf's "tidyness", I'd love to be able to ...
2
votes
1answer
1k views
sf lines to polygons with holes
With sp package I generate automatically boundaries for different sets of spatial points. I'm trying to perform the same workflow with sf package but i had problems for detecting holes in the way sp ...
1
vote
0answers
423 views
Using st_read_db in R to read postGIS rasters
I use the st_read_db function in the sf package to read vector data from PostGIS into R. Can i use it to query rasters? ideally extract a raster value at a point:
require(sf)
# create connection to ...
1
vote
1answer
1k views
How to read a csv dataset with multipoint column in sf
i have a csv with a multipoint WGS84 column:
head(df, n=3)
CUP the_geom
<chr> <chr>
1 ...
15
votes
3answers
6k 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? (...
8
votes
3answers
2k 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))))
...
8
votes
1answer
3k 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 ...
2
votes
1answer
342 views
CRS for US Census 1999 ShapeFile
I'm working with the shapefiles for the US census 1999 msa files which can be downloaded here:
https://www2.census.gov/geo/tiger/PREVGENZ/ma/ma99/cm_sa_99_shp.zip
Unfortunately, there is no CRS ...
3
votes
1answer
189 views
edit sf point conditionally
I have an sf point dataframe, with a single incorrect point. I know what it's coordinates should be, but I'm not sure how best to directly edit them. I'm aiming for a conditional edit like the ...
7
votes
2answers
7k 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 ...
18
votes
1answer
10k 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")
...
1
vote
3answers
2k views
Is there an equivalent of dplyr::bind_rows (or the old plyr::rbind.fill) for spatial objects?
bind_rows can append dataframes even when they do not have identical columns. It fills any non-matching columns with NAs.
Is there an equivalent for spatial point data frames in R?
I have two ...
2
votes
2answers
2k 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/...
34
votes
2answers
26k 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 ...
1
vote
0answers
618 views
How to execute st_read function including multibyte character?
how to execute st_read function including multibyte character?
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
st_write(obj = nc, dsn = enc2utf8("ćć¼ćæ"),layer = "nc", driver = ...
1
vote
1answer
260 views
Is it possible to read tables from a specific schema with sf::st_read_db()
How can I specify the schema from where to read a table in the st_read_db() function in the sf package in R?
When I try
require(RPostgreSQL)
require(sf)
drv = dbDriver("PostgreSQL")
con = dbConnect(...
7
votes
3answers
3k 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 ...
3
votes
1answer
254 views
Can't find function st_join in R sf package
I am trying to run the st_join example from the sf user manual https://cran.r-project.org/web/packages/sf/sf.pdf (p. 43).
a = st_sf(a = 1:3, geom = st_sfc(st_point(c(1,1)), st_point(c(2,2)), st_point(...
22
votes
4answers
13k 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 ...
5
votes
1answer
1k views
Creating an sf object from the maps package
I am trying to use data from the maps package in an sf workflow. I'm not sure if I have the operation correct (see below). It seems to work until state 20 (massachusetts). What am I doing wrong? Maybe ...
10
votes
1answer
10k 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 ...
2
votes
1answer
773 views
left_join breaking sf
Trying to add a data column to a simplefeatures point object using dplyr::left_join, but it appears to drop the sf class:
class(sf)
[1] "sf" "data.frame"
class(df)
[1] "tbl_df" "tbl" "...
57
votes
4answers
26k 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 ...
4
votes
1answer
593 views
Iterate through shapefile and output an image for each feature using sf in R
I would like to create some statistical profiles for a range of features; specifically, 2016 Census profiles for the Census Divisions (CD) within British Columbia (BC), Canada. This would require a ...
12
votes
1answer
3k 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 ...
28
votes
1answer
17k 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....