[R-sig-Geo] enumerate grid clusters/patches with size of contiguous cluster

Roger Bivand Roger.Bivand at nhh.no
Fri Mar 14 14:18:34 CET 2008


On Fri, 14 Mar 2008, Murray Richardson wrote:

> Hi Roger and others,
>
> Thanks again for this helpful script.  It made me see some potential to use 
> spdep package for another related problem that I was previously using CGAL 
> (C++) alpha shapes for.
>
> Is there a way to output a list of nearest neighbour pairs in an xy 
> coordinate list (or binary grid for that matter I guess).  I would like to 
> convert a binary grid of lines (1 cell thick) to a constituent line segment 
> list of the form (startx, starty, end x end y), which I then polygonize using 
> RODBC/postGIS, as we discussed in a previous thread.

Would nb2lines() in spdep help? It was written for a user wanting to write 
a shapefile of lines, so I guess that it either is what you are looking 
for, or close enough to serve as an example?

Roger

>
> Thanks in advance!
>
> Murray
>
> Roger Bivand wrote:
>>  On Wed, 12 Mar 2008, Agustin Lobo wrote:
>> 
>> >  and a combination of r.clump and r.stats
>> >  in grass (which is "linked" to R) can also do the job.
>>
>>  Yes, that will be very robust. I've tried an R attack through neighbour
>>  lists:
>>
>>  library(sp)
>>  data(meuse.grid)
>>  coordinates(meuse.grid) <- c("x", "y")
>>  gridded(meuse.grid) <- TRUE
>>  fullgrid(meuse.grid) <- TRUE
>>  class(meuse.grid)
>>  names(meuse.grid)
>>  meuse.grid$ffreq1 <- meuse.grid$ffreq == 1
>>  is.na(meuse.grid$ffreq1) <- !meuse.grid$ffreq1
>>  image(meuse.grid, "ffreq1")
>>  pix <- as(meuse.grid["ffreq1"], "SpatialPixelsDataFrame")
>>  summary(pix)
>>  image(pix)
>>  library(spdep)
>>  nb_rook <- dnearneigh(coordinates(pix), 0, 41)
>>  nb_queen <- dnearneigh(coordinates(pix), 0, 57)
>>  comp_rook <- n.comp.nb(nb_rook)
>>  comp_queen <- n.comp.nb(nb_queen)
>>  t_rook <- table(comp_rook$comp.id)
>>  t_queen <- table(comp_queen$comp.id)
>>  t_rook
>>  t_queen
>>  pix$rook <- comp_rook$comp.id
>>  pix$queen <- comp_queen$comp.id
>>  image(pix, "rook", col=rainbow(31))
>>  pix$nrook <- t_rook[pix$rook]
>>  pix$nqueen <- t_queen[pix$queen]
>>  summary(pix)
>>  image(pix, "nqueen")
>>  spplot(pix, "nqueen")
>>
>>  using cut-off distances for distance-based neighbours just larger than the
>>  resolution and the diagonal resolution. There were only 779 cells in
>>  patches here, but it ran so fast that scaling up shouldn't be a problem.
>>
>>  Roger
>> 
>> > 
>> >  Agus
>> > 
>> >  Andrew Niccolai escribió:
>> > >  If you aren't dedicated to an R specific solution, ImageJ is open 
>> > >  source and
>> > >  does this under Analysis/Binary.
>> > > 
>> > >  -----Original Message-----
>> > >  From: r-sig-geo-bounces at stat.math.ethz.ch
>> > >  [mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of Murray 
>> > >  Richardson
>> > >  Sent: Wednesday, March 12, 2008 12:59 PM
>> > >  To: r-sig-geo at stat.math.ethz.ch
>> > >  Subject: [R-sig-Geo] enumerate grid clusters/patches with size of 
>> > >  contiguous
>> > >  cluster
>> > > 
>> > >  Hello friends,
>> > > 
>> > >  I am trying to take a binary input grid of patches (connected cells 
>> > >  with
>> > >  value = 1), and assign to each non-zero cell the total number of 
>> > >  pixels
>> > >  comprising the patch to which it belongs.
>> > > 
>> > >  So for a connected patch of 27 pixels, all cells belonging to that 
>> > >  patch
>> > >  will have a value of 27.
>> > > 
>> > >  Just wondering if anyone has any suggestions.  Maybe a function I 
>> > >  don't
>> > >  know about yet...
>> > > 
>> > >  Thanks in advance,
>> > > 
>> > >  Murray Richardson
>> > > 
>>>> _______________________________________________
>> > >  R-sig-Geo mailing list
>> > >  R-sig-Geo at stat.math.ethz.ch
>> > >  https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> > > 
>>>> _______________________________________________
>> > >  R-sig-Geo mailing list
>> > >  R-sig-Geo at stat.math.ethz.ch
>> > >  https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> > > 
>> > 
>> > 
>> 
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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


More information about the R-sig-Geo mailing list