[R-sig-Geo] split SLDF with attributes

Bacou, Melanie mel at mbacou.com
Fri Feb 24 22:13:41 CET 2017


Marta,
Since you have intersecting lines in your SLDF, another approach might 
be to use a loop (!) through your SpatialLines objects and carry the 
attributes, something like:

library(raster)

sldf_in <- shapefile("mergedDF_1_DF_short")

# Init a list to keep all segments
sldf_list <- vector("list", nrow(sldf_in))
names(sldf_list) <- row.names(sldf_in)

for (i in names(sldf_list)) {
   sl <- SegmentSpatialLines(as(sldf_in[i,], "SpatialLines"), 
length=10000, merge.last=TRUE)
   df <- sldf_in[i,]@data
   df <- df[rep(seq_len(nrow(df)), length(sl)), ]
   df$ID <- i
   sldf_list[[i]] <- SpatialLinesDataFrame(sl, df, match.ID=FALSE)
   # Make arbitrary unique IDs (for combining in next step)
   sldf_list[[i]] <- spChFIDs(sldf_list[[i]], paste(i, 
row.names(sldf_list[[i]]), sep="-"))
}

# Combine into a unique SLDF
sldf_out <- do.call(rbind, sldf_list)


--Mel.



On 2/24/2017 10:31 AM, marta azores wrote:
> Thanks for your answer Mel,
> Option1) Createsegments: this script cut the SLDF...but as autoridades 
> said um the website...The results are spatiallines, not SLDF. Then, 
> they don't jeep the attributes.
> Option2) rgeos:over I know this tool, but O have several lines 
> crossing among them. I am afraid, that can be a bit messy.
> I doesn't know the gTouches...I check it after your suggestion..
> But I think the rgeos:Over os the best option for me.
> Thanks for your suggestion
> Marta
>
>
>
> No dia sexta-feira, 24 de fevereiro de 2017, Bacou, Melanie 
> <mel at mbacou.com <mailto:mel at mbacou.com>> escreveu:
>
>     Marta,
>     Option 1) using CreateSegments() seems to do what you want. Why
>     don't you simply use rgeos::over() or rgeos::gTouches() between
>     your old and new SLDFs to match the index positions of your old
>     attributes to your new segments?
>
>     --Mel.
>
>
>     On 2/23/2017 2:27 PM, marta azores wrote:
>
>         Hi all,
>
>         I am working with a large datasets of boat positions, and
>         based on this
>         points I created a SpatialLinesDataFrame. Now, I would like to
>         split/divide SpatialLinesDataframe
>         into n segments of 10km and keep the attributes. So far, I
>         have tried
>         multiple things:
>
>         1.      The function from this website:
>         http://rstudio-pubs-static.s3.
>         amazonaws.com/10685_1f7266d60db7432486517a111c76ac8b.html
>         <http://amazonaws.com/10685_1f7266d60db7432486517a111c76ac8b.html>.
>         The problem with
>         it is that the function cuts the lines in segments of 10km but
>         it is not
>         possible to get the attributes.
>
>         2.      I have also tried the function from this website:
>         http://stackoverflow.com/questions/38700246/how-do-i-split-divide-polyline-
>         <http://stackoverflow.com/questions/38700246/how-do-i-split-divide-polyline->
>         shapefiles-into-equally-length-smaller-segments. The problem
>         is that do not
>         cut the segments in 10km, and it seems to cut the segments
>         with random
>         lengths.
>
>         3.      On the follwoing website: http://r-sig-geo.2731867.n2.
>         nabble.com/split-divide-SpatialLines-sp-into-n-segments-td7583234.html#
>         <http://nabble.com/split-divide-SpatialLines-sp-into-n-segments-td7583234.html#>
>         a7583629 propose to divide lines by segments but do not
>         specify how it is
>         possible to do in based on the length of the segment.
>
>         Is there any function to do this in R? Or does somebody know
>         the solution
>         for that?
>
>         My data looks like this:
>
>         #Script:
>
>         path_data <- "C:/R/"
>
>         mergedDF_1_DF_short<- shapefile(path_data,"mergedDF_1_DF_short")
>
>
>         https://drive.google.com/drive/folders/0B7IbvWhE5JNPT0oxUklpd3ZnRzA?usp=sharing
>         <https://drive.google.com/drive/folders/0B7IbvWhE5JNPT0oxUklpd3ZnRzA?usp=sharing>
>
>                 [[alternative HTML version deleted]]
>
>         _______________________________________________
>         R-sig-Geo mailing list
>         R-sig-Geo at r-project.org
>         https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
>


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list