[R-sig-Geo] Clip a SpatialLinesDataFrame

Jim Burke javajimburke at gmail.com
Fri Jul 10 20:17:33 CEST 2015


Clipping a SpatialLinesDataFrame by an envelope "SpatialPolygons" results
in a bare bones "SpatialLines" with no "@data" slot. The "out" file is a
roads file which plots fine but has no road names available.

My question is how to restore nontopogical attribute information, such as
"@data", from the original large SpatialLinesDataFrame? .

in_df <-readOGR(dsn="new.shp", layer="new", verbose = TRUE)
env = gEnvelope( neighborhood_sp, byid=FALSE, id = NULL)  # neighborhood_sp
is class "SpatialPolygonsDataFrame"
out <- gIntersection(in_df, env, byid=TRUE) #works quite quickly

Additional information

The out class "SpatialLines"
> getSLLinesIDSlots(out)
   [1] "3239 1"   "3240 1"   "3543 1"   "7405 1"   "7406 1"   "7407 1"
   [7] "7408 1"   "7409 1"   "7410 1"   "7411 1"   "7412 1"   "7413 1"
  [13] "7424 1"   "7425 1"   "7426 1"   "7427 1"   "7428 1"   "7429 1"
> str(out)
Formal class 'SpatialLines' [package "sp"] with 3 slots
  ..@ lines      :List of 5870
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 2499508 2499546 2499545
2499542 2499540 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "3239 1"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:2, 1:2] 2499506 2499508 7023664
7023801

The in_df class "SpatialLinesDataFrame"
> head(In_df at data, n = 12)
    DC_ID Prefix Pretype        Name Type Suffix           Class
 Label
0  843772   <NA>    <NA>    Freeport Pkwy   <NA>  Minor Arterial  Freeport
Pkwy
1  843773   <NA>    <NA>    Freeport Pkwy   <NA>  Minor Arterial  Freeport
Pkwy
2  844139   <NA>    STHY         183 <NA>      E Primary Highway    Airport
Fwy

Let me know if you want additional information.

Thanks in advance
Jim Burke

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list