@@ -144,10 +144,14 @@ and shown in figure \ref{fig:toy}.
144
144
145
145
\begin {figure }[htb]
146
146
<<fig =TRUE ,echo =FALSE >>=
147
- library(RColorBrewer )
148
- pal = brewer.pal(5 , " Set2" )
149
- plot(pol , xlim = c(- 1.1 , 2.1 ), ylim = c(- 1.1 , 1.6 ), border = pal , axes = TRUE ,
150
- col = paste0(pal , " 4D" ))
147
+ if (require(RColorBrewer , quietly = TRUE )) {
148
+ pal = brewer.pal(5 , " Set2" )
149
+ col = paste0(pal , " 4D" )
150
+ } else {
151
+ pal = 1 : 5
152
+ col = pal
153
+ }
154
+ plot(pol , xlim = c(- 1.1 , 2.1 ), ylim = c(- 1.1 , 1.6 ), border = pal , axes = TRUE , col = col )
151
155
points(pts , col = ' red' )
152
156
text(c(- 1 ,0.1 ,0.1 ,1.9 ,0.45 ), c(0.05 ,0.05 ,- .95 ,0.05 ,0.15 ),
153
157
c(" x1" , " x2" , " x3" , " x4" , " x5" ))
@@ -188,7 +192,9 @@ row.names(pol[pts])
188
192
\code {over} can also be used to query polygons in a single object overlay each other:
189
193
190
194
<<>>=
191
- over(pol , pol , returnList = TRUE )
195
+ if (require(rgeos , quietly = TRUE )) {
196
+ over(pol , pol , returnList = TRUE )
197
+ }
192
198
@
193
199
194
200
\noindent
@@ -300,7 +306,7 @@ text(c(0.52, 1.52), c(1.5, 1.5), c("L1", "L2"))
300
306
The set of \code {over} operations on the polygons, lines and points
301
307
is shown below (note that lists and vectors are named in this case):
302
308
<<>>=
303
- library( rgeos )
309
+ if (require( rgeos , quietly = TRUE )) {
304
310
over(pol , pol )
305
311
over(pol , pol ,returnList = TRUE )
306
312
over(pol , L )
@@ -309,6 +315,7 @@ over(L, pol, returnList = TRUE)
309
315
over(L , L )
310
316
over(pts , L )
311
317
over(L , pts )
318
+ }
312
319
@
313
320
314
321
Another example overlays a line with a grid, shown in figure \ref {fig:grid }.
@@ -319,9 +326,11 @@ gridded(meuse.grid) = ~x+y
319
326
Pt = list (x = c(178274.9 ,181639.6 ), y = c(329760.4 ,333343.7 ))
320
327
sl = SpatialLines(list (Lines(Line(cbind(Pt $ x ,Pt $ y )), " L1" )))
321
328
image(meuse.grid )
322
- xo = over(sl , geometry(meuse.grid ), returnList = TRUE )
323
- image(meuse.grid [xo [[1 ]], ],col = grey(0.5 ),add = T )
324
- lines(sl )
329
+ if (require(rgeos , quietly = TRUE )) {
330
+ xo = over(sl , geometry(meuse.grid ), returnList = TRUE )
331
+ image(meuse.grid [xo [[1 ]], ],col = grey(0.5 ),add = T )
332
+ lines(sl )
333
+ }
325
334
@
326
335
\caption { Overlay of line with grid, identifying cells crossed (or touched)
327
336
by the line }
@@ -342,17 +351,21 @@ text(coordinates(g), labels = 1:9)
342
351
343
352
We can match these geometries with themselves by
344
353
<<>>=
354
+ if (require(rgeos , quietly = TRUE )) {
345
355
over(g ,g )
346
356
over(p ,p )
347
357
over(p ,g )
348
358
over(g ,p )
359
+ }
349
360
@
350
361
and see that most give a 1:1 match, except for polygons-polygons \code {(p,p)}.
351
362
352
363
When we ask for the full set of matches, we see
353
364
<<>>=
365
+ if (require(rgeos , quietly = TRUE )) {
354
366
over(px [5 ], g , returnList = TRUE )
355
367
over(p [c(1 ,5 )], p , returnList = TRUE )
368
+ }
356
369
@
357
370
and note that the implementation lets grids/pixels not match
358
371
(intersect) with neighbour grid cells, but that polygons do.
@@ -369,8 +382,10 @@ By default, polygon-polygon features are matched by
369
382
{\em any} order (feature order, it seems). Although it is slower,
370
383
we can however improve on this by switching to \code {rgeos::gRelate}, and see
371
384
<<>>=
385
+ if (require(rgeos , quietly = TRUE )) {
372
386
over(px [5 ], g , returnList = TRUE , minDimension = 0 )
373
387
over(p [c(1 ,5 )], p , returnList = TRUE , minDimension = 0 )
388
+ }
374
389
@
375
390
When \code {minDimension = 0} is specified, the matching geometries
376
391
are being returned based on a nested ordering. First, ordering
@@ -391,7 +406,9 @@ Note that the ordering also determines which feature is matched
391
406
when \code {returnList=FALSE}, as in this case the first element of
392
407
the ordered set is taken:
393
408
<<>>=
409
+ if (require(rgeos , quietly = TRUE )) {
394
410
over(p , p , minDimension = 0 )
411
+ }
395
412
@
396
413
397
414
Consider the following example where a point is {\em on} \code {x1} and {\em in} \code {x2}:
@@ -413,12 +430,14 @@ When matching the point \code{pt} with the two polygons, the
413
430
sp method (default) gives no preference of the second polygon
414
431
that (fully) contains the point; the rgeos method however does:
415
432
<<>>=
433
+ if (require(rgeos , quietly = TRUE )) {
416
434
over(pt ,sp )
417
435
over(pt ,sp ,minDimension = 0 )
418
436
over(pt ,sp ,returnList = TRUE )
419
437
rgeos :: overGeomGeom(pt ,sp )
420
438
rgeos :: overGeomGeom(pt ,sp ,returnList = TRUE )
421
439
rgeos :: overGeomGeom(pt ,sp ,returnList = TRUE ,minDimension = 0 )
440
+ }
422
441
@
423
442
424
443
% # x1 x2
@@ -435,12 +454,14 @@ area overlap {\em or} line in common. This can be done using the
435
454
parameter \code {minDimension}:
436
455
437
456
<<>>=
457
+ if (require(rgeos , quietly = TRUE )) {
438
458
over(p [5 ], p , returnList = TRUE , minDimension = 0 )
439
459
over(p [5 ], p , returnList = TRUE , minDimension = 1 )
440
460
over(p [5 ], p , returnList = TRUE , minDimension = 2 )
441
461
rgeos :: overGeomGeom(pt , pt , minDimension = 2 ) # empty
442
462
rgeos :: overGeomGeom(pt , pt , minDimension = 1 ) # empty
443
463
rgeos :: overGeomGeom(pt , pt , minDimension = 0 )
464
+ }
444
465
@
445
466
446
467
\section {Aggregation }
@@ -474,9 +495,11 @@ x 400 m grid}
474
495
An example of the aggregated values of \code {meuse.grid} along
475
496
(or under) the line shown in Figure \ref {fig:lines } are
476
497
<<>>=
498
+ if (require(rgeos , quietly = TRUE )) {
477
499
sl.agg = aggregate(meuse.grid [,1 : 3 ], sl , mean )
478
500
class(sl.agg )
479
501
as.data.frame(sl.agg )
502
+ }
480
503
@
481
504
Function \code {aggregate} returns a spatial object of the same
482
505
class of \code {sl} (\code {SpatialLines}), and \code {as.data.frame}
@@ -490,6 +513,7 @@ specifying {\em how} polygons intersect\footnote{sp versions
490
513
in Figure \ref {fig:agg }.
491
514
492
515
<<>>=
516
+ if (require(rgeos , quietly = TRUE )) {
493
517
g = SpatialGrid(GridTopology(c(5 ,5 ), c(10 ,10 ), c(3 ,3 )))
494
518
p = as(g , " SpatialPolygons" )
495
519
p $ z = c(1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 )
@@ -499,10 +523,12 @@ p$ag1a = aggregate(p, p, mean, minDimension = 0)[[1]]
499
523
p $ ag2 = aggregate(p , p , mean , minDimension = 1 )[[1 ]]
500
524
p $ ag3 = aggregate(p , p , mean , minDimension = 2 )[[1 ]]
501
525
p $ ag4 = aggregate(p , p , areaWeighted = TRUE )[[1 ]]
526
+ }
502
527
@
503
528
504
529
\begin {figure }[ht]
505
530
<<echo =FALSE ,fig =TRUE >>=
531
+ if (require(rgeos , quietly = TRUE )) {
506
532
pts = cbind(c(9 ,21 ,21 ,9 ,9 ),c(9 ,9 ,21 ,21 ,9 ))
507
533
sq = SpatialPolygons(list (Polygons(list (Polygon(pts )), " ID" )))
508
534
rnd2 = function (x ) round(x , 2 )
@@ -519,6 +545,8 @@ spplot(p, names.attr = c("source", "default aggregate", "minDimension=0",
519
545
" minDimension=1" , " minDimension=2" , " areaWeighted=TRUE" ), layout = c(3 ,2 ),
520
546
as.table = TRUE , col.regions = bpy.colors(151 )[50 : 151 ], cuts = 100 ,
521
547
sp.layout = l , scales = list (draw = TRUE ))
548
+ } else
549
+ plot(1 )
522
550
@
523
551
\caption {Effect of aggregating checker board {\tt SpatialPolygons} by themselves, for
524
552
different values of {\tt minDimension} and {\tt areaWeighted}; the green square example
@@ -536,12 +564,14 @@ using {\tt minDimension}, and area weighting for aggregating the 0-1 checker
536
564
board of figure \ref {fig:agg } by the green square polygon ({\tt sq})
537
565
shown in the last panel of that figure:
538
566
<<>>=
567
+ if (require(rgeos , quietly = TRUE )) {
539
568
round(c(
540
569
aggDefault = aggregate(p , sq , mean )[[1 ]],
541
570
aggMinDim0 = aggregate(p , sq , mean , minDimension = 0 )[[1 ]],
542
571
aggMinDim1 = aggregate(p , sq , mean , minDimension = 1 )[[1 ]],
543
572
aggMinDim2 = aggregate(p , sq , mean , minDimension = 2 )[[1 ]],
544
573
areaWeighted = aggregate(p , sq , areaWeighted = TRUE )[[1 ]]), 3 )
574
+ }
545
575
@
546
576
547
577
0 commit comments