[R-sig-Geo] intersect function: how to retain an id for each intersect between spatialpolygon and spatiaLineDataFrame: rgeos

Navinder Singh Navinder.Singh at slu.se
Wed Jun 24 15:40:58 CEST 2015


I have a slightly complex problem.

First, i would like to get some help on how to generate spatial line object from spatial points data frame. The complexity here is to be able to retain some of the attributes when generating a spatial line object. For e.g. i have height information associated with each point and i would like to find out if it is possible to generate some sort of a average height of each line that joins the points. I used a function written by Kyle Walker from the following link to do so, but couldn't retain any attributes in the spatial lines. See https://rpubs.com/walkerke/points_to_line

vlines<-    points_to_line(data=stora.df,long="x",lat="y",id_field="id",sort_field="date")

The result is:

vlines
class       : SpatialLines
features    : 4
extent      : 1562946, 1649621, 7074782, 7194770  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:2400 +proj=tmerc +lat_0=0 +lon_0=15.80827777777778  +k=1 +x_0=1500000 +y_0=0 +ellps=bessel +towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +units=m +no_defs

Second, I am trying to intersect a gbuffer generated object with above spatial line object.

buffer<-gBuffer(wfSp,byid=TRUE,width=150)
buffer at data$id<-1:length(wfSp)
buffer
Object of class "SpatialPolygonsDataFrame" (package sp):

Number of SpatialPolygons:  40

Variables measured:
  id
1  1
2  2
3  3
4  4
5  5
6  6
...

So this creates 40 buffers that i need. All seems to be okay at this step. Except the first issue above.

Now when i do the intersect, between buffer and vlines, i get

Int.1<-intersect(buffer, vlines[1,])
Int.1
class       : SpatialLinesDataFrame
features    : 25
extent      : 1622933, 1625909, 7124940, 7128742  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:2400 +proj=tmerc +lat_0=0 +lon_0=15.80827777777778   +k=1 +x_0=1500000 +y_0=0 +ellps=bessel   +towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +units=m +no_defs
variables   : 1
names       : id
min values  :  2
max values  : 40
+towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +units=m +no_defs

Int.1.df<-as.data.frame(Int.1)
Int.1.df
   id
1   2
2  15
3  18
4  19
5  20
6  21
7  22
8  23
9  24
10 25
11 26
12 27
13 28
14 29
15 30
16 31
17 32
18 33
19 34
20 35
21 36
22 37
23 38
24 39
25 40

This basically gives me a list of id's of the buffers my line intersects. Now what i want is to be able to generate a separate id for each intersection. In short, how many lines intersect each buffer separately. Right now, it is mainly a list of buffers intersected by a line. So from above, it is clear that my line passed 25 buffers but not an estimate of how many times, each buffer was passed.

Basically a count of intersections per buffer.

Please ask, if something is not clear here.

Thanks,

Navi




	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list