[R-sig-Geo] Order a SpatialLine passes through a SpatialPolygons

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Sep 1 12:08:55 CEST 2017


Slightly hacky method: sample lots of points along the line, over() the
points with the polygons, returns polygon IDs in order:

>
unique(over(spsample(the.line,10000,"regular"),as(columbus,"SpatialPolygons")))
[1] 21 24 25 26 22

if the line goes back into a polygon with the same ID you'll see that too -
"21 24 25 26 24 22" for example.

Problem with this is that you have to make enough points on the line to
make sure one point hits each intersecting polygon. Maybe compare the list
from the points with the polygon intersection and then...

Wait, another idea. Get all the line-polygon intersections points to cut
your line into segments, and then compute the midpoints of each segment,
and see which polygon they lie in.  Coffee time now, so can't implement
this...

Note that SpatialLines objects can be more than one line segment in which
case the ordering is undefined, so make sure your SpatialLines have one
Line component.

B






On Thu, Aug 31, 2017 at 9:06 PM, Thayn, Jonathan <jthayn at ilstu.edu> wrote:

> I’m trying to write a function that returns the polygons through which a
> line passes, but I want the polygons to be listed in order. For example, in
> the code below, I get a list of polygons over which the lines passes, but
> the polygons are listed in numeric order, not spatial order. I would like
> the polygons to be listed as 21, 24, 25, 26, and 22 (the reverse would be
> fine to). Any ideas.
>
>
> library(spdep)
> example(columbus)
> plot(columbus)
> coords <- coordinates(columbus)
> text(coords,labels=1:49)
>
> the.line <- SpatialLines(list(Lines(Line(coords[c(21,22),]),ID="A")))
> plot(the.line,col="red",add=T)
>
> which(!is.na(over(columbus,the.line)))
>
>
>
> Jonathan B. Thayn, Ph.D.
>
> Associate Professor
> Department of Geography – Geology
> Illinois State University
> Felmley Hall of Science, Rm 200A
> Normal, IL 61790
>
> jthayn at ilstu.edu<mailto:jthayn at ilstu.edu>
> my.ilstu.edu/~jthayn<http://my.ilstu.edu/~jthayn>
>
>
>
>
>
>         [[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

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list