[R-sig-Geo] Using sf package to calculate line midpoints from Spatial Line data (both LINESTRING and MULTILINESTRING)

@bo m@iii@g oii eirick@de @bo m@iii@g oii eirick@de
Wed Feb 3 14:29:08 CET 2021


Dear Julian,

To get the midpoint of a line data just use st_centroid(). Here is a  
brief example:

library(sf)

# example with two lines
ds <- data.frame(id = 1:2,
              geom = c('LINESTRING(0 0, 2 2)', 'LINESTRING(3 2, 4 5)'))
ds <- st_as_sf(ds, wkt = "geom")
st_centroid(ds)

# example with a multi line string
ds <- data.frame(id = 1:1,
              geom = c('MULTILINESTRING ((0 0, 2 2),(3 2, 4 5))'))
ds <- st_as_sf(ds, wkt = "geom")
st_centroid(ds)

Hope that helps,
Tim

Zitat von Julian Parnelle <julianparnelle using outlook.com>:

> Dear list,
>
> My question is straightforward: how could one use the sf package to  
> calculate the Longitude and Latitude of the line midpoints in  
> spatial line data, in a way that works both for LINESTRING and  
> MULTILINESTRING sf data.frames? (one per spatial line, in case the  
> spatial line is composed of multiple line segments). After searching  
> online for quite a bit, the only direct reference I found was this  
> GIS Stack Exchange question from three years ago:  
> https://gis.stackexchange.com/questions/277219/sf-equivalent-of-r-maptools-packages-spatiallinesmidpoints. However, the solution proposed there is only more or less reliable for LINESTRING data and I lack sufficient knowledge of sf objects and MULTILINESTRING representation to update that and make it more  
> reliable.
>
> I was used to doing that with the SpatialLinesMidPoints command, but  
> I am finding it quite difficult to accomplish the same with the sf  
> package. I do know that this task could perhaps be achieved by  
> interpolating across the lines up to 50% of its extension, using for  
> example ST_Line_Interpolate_Point. However, I don't see to be able  
> to achieve exactly the same with either st_line_sample or  
> st_segmentize.
>
> Any suggestions would be very welcome.
>
> Julian
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list