# [R-sig-Geo] gDifference

Fri Jun 7 13:05:14 CEST 2013

```Many thanks for that Roger. Didn't know lines were defined as having zero width.

Ross

On 7 Jun 2013, at 11:45, Roger Bivand <Roger.Bivand at nhh.no> wrote:

> On Fri, 7 Jun 2013, Ross Ahmed wrote:
>
>> Hi all
>>
>> I have this polygon and these lines:
>>
>> library(sp); library(rgeos)
>> # polygon
>> coords <- matrix(c(-1.798123, -1.793072, -1.805767, -1.804129, -1.798123,
>> 55.68066, 55.67369, 55.67508, 55.68139, 55.68066), ncol=2)
>> myPolygon <- Polygons(list(Polygon(coords)),  "myPolygon")
>> myPolygon <- SpatialPolygons(list(myPolygon))
>> proj4string(myPolygon) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
>> +towgs84=0,0,0")
>> plot(myPolygon)
>>
>> # lines
>> line1 <- matrix(c(-1.79880, -1.79517, 55.67737, 55.67920), ncol=2)
>> line2 <- matrix(c(-1.80231, -1.80679, 55.67764, 55.68004), ncol=2)
>> line1L <- Line(line1)
>> line2L <- Line(line2)
>> my.lines <- Lines(list(line1L, line2L), ID="my.lines")
>> myLines <- SpatialLines(list(my.lines))
>> proj4string(myLines) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
>> +towgs84=0,0,0")
>>
>>
>>
>> I expected that the code below would remove parts of myPolygon that contain
>> myLines:
>>
>> d <- gDifference(myPolygon, myLines)
>> plot(d)
>>
>>
>> However myPolygon seems unaffected. Why are the parts of myPolygon that
>> contain myLines not being removed?
>
> Well, lines have by definition zero width, so:
>
> gIntersects(myPolygon, myLines)
> gArea(gIntersection(myPolygon, myLines))
>
> so there is no difference between myPolygon and d because nothing is being removed. If you buffer the lines, you see a difference:
>
> gArea(myPolygon)
> d <- gDifference(myPolygon, gBuffer(myLines, width=0.000001))
> gArea(d)
> plot(d)
>
> http://www.vividsolutions.com/jts/bin/JTS%20Technical%20Specs.pdf
> http://www.vividsolutions.com/jts/bin/JTS%20Developer%20Guide.pdf
>
> Note that you get warnings for using operations on unprojected objects, because your assumption that apparently straight lines are straight is not justified.
>
> Hope this clarifies,
>
> Roger
>
>>
>> Thanks
>> Ross
>>
>>
>>
>>    [[alternative HTML version deleted]]
>
> Please post plain text only.
>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> --
> Roger Bivand
> Department of Economics, NHH Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand at nhh.no
>

```