[R-sig-Geo] R Help

Roger Bivand Roger.Bivand at nhh.no
Fri Jan 8 10:42:06 CET 2016


On Fri, 8 Jan 2016, chris english wrote:

> Krishan,
>
> Create functions to compute the area, centroid and perimeter of a polygon
> list (as in the format of georgia.polys).
> The secret here is the specificity of georgia.polys, which googling leads
> us to Cran R "GISTools". Looks like this handles the polygon area calc.

Right, the data referred to here are in:

library(GISTools)
data(georgia)

where georgia.polys is a list of matrices of projected boundary 
coordinates. None of this is difficult, all is described in detail in 
Bivand et al. (2013), rather less detail in Brunsdon & Comber (2015).

Recreate the sp class object needed (the classes were designed to make 
things easier):

library(sp)
Pslist <- lapply(seq(along=georgia.polys), function(i)
   Polygons(list(Polygon(georgia.polys[[i]])), ID=as.character(i)))
GP <- SpatialPolygons(Pslist)

The objects inside GP already contain area and centroid values used in 
plotting which are inaccurate in the presence of holes, but we can check 
with rgeos functions to be sure (centroids accessed from the "labpt" slot 
by the coordinates() accessor method):

library(rgeos)
all.equal(coordinates(gCentroid(GP, byid=TRUE)), coordinates(GP),
  check.attributes=FALSE)
all.equal(coordinates(gCentroid(GP, byid=TRUE)), t(sapply(slot(GP,
  "polygons"), slot, "labpt")), check.attributes=FALSE)
all.equal(gArea(GP, byid=TRUE), sapply(slot(GP, "polygons"), slot,
  "area"), check.attributes=FALSE)

This will not be the same if there are interior rings (holes) - in which 
case the rgeos function values should be used.

Perimeter is harder, but here there are only exterior rings, so we can 
use:

gLength(GP, byid=TRUE)

If the perimeter lengths for proximate neighbours are needed, use 
GRASS in conjunction with rgrass7::vect2neigh().

I agree with Sarah that this looks like homework, possibly from chapter 4 
in Brunsdon & Comber (2015). However, I prefer this explanation - it may 
look harder, but steep learning curves are time and cost efficient.

Hope this clarifies,

Roger

> Perimeter can also be referred to as envelope which might lead one to
> either "geosphere" or "spatstat". Thence to "rgeos" for centroid, and a
> small walking tour of aspects of spatial R.
>
> I'm imagining the idea is to use what has already been validated as correct
> code rather than writing the same.
> HTH,
> Chris
>
> On Thu, Jan 7, 2016 at 10:38 PM, Sarah Goslee <sarah.goslee at gmail.com>
> wrote:
>
>> You'd get better results if you send your information to the list, and
>> as plain-text email with data either included or using one of the
>> built-in datasets in R. I don't necessarily know the answer even
>> though I know the right way to ask the question.
>>
>> Sarah
>>
>> On Thu, Jan 7, 2016 at 3:35 PM, Mistry, Krishan <km314 at student.le.ac.uk>
>> wrote:
>>> Dear Sarah,
>>>
>>> This isn't homework, but in fact optional extra work for a R practical
>> session. I am new to R and haven't done any programming before.  Would you
>> be able to assist me, if i am able to send you the question on a word
>> document along the data that is used.
>>>
>>> Kind Regards
>>> Krishan
>>>
>>> ________________________________________
>>> From: Sarah Goslee <sarah.goslee at gmail.com>
>>> Sent: 07 January 2016 20:31
>>> To: Mistry, Krishan
>>> Cc: r-sig-geo at r-project.org
>>> Subject: Re: [R-sig-Geo] R Help
>>>
>>> As you will see below, posting in HTML format made your question
>> unreadable.
>>>
>>> However, this looks rather like homework, and if so then you should
>>> look for help from the resources associated with your course.
>>>
>>> If it isn't homework, you will get much more assistance if you provide
>>> a reproducible example including data and the code you've tried
>>> already.
>>>
>>> Sarah
>>>
>>> On Thu, Jan 7, 2016 at 3:24 PM, Mistry, Krishan <km314 at student.le.ac.uk>
>> wrote:
>>>> Dear Sir/Madame,
>>>>
>>>> I am currently a Masters student at the University of Leicester in the
>> UK studying MSc Geographical Information Science and am having a bit of
>> difficulty with the programme R. I am a new user of the programme and don't
>> have any previous experience in programming. Having looked at various web
>> sites, I wondered if you are able to help me with some trouble i am having
>> in trying to write some code for a mathematical equation.
>>>> I have attached the question to this email and it can be seen below.
>> The dataset is a list of polygons with each polygon containing another list
>> within it, of x and y coordinates.
>>>>
>>>> I would really appreciate it, if you could help me with this question.
>>>>
>>>> Question
>>>> Create functions to compute the area, centroid and perimeter of a
>> polygon list (as in the format of georgia.polys). The formula for the area
>> of a polygon is
>>>>
>>>> A=0.5*?(x? y???) (x??? y?)      for i until n-1
>>>>
>>>> where A is the polygon area, xi is the ith x-coordinate of the polygon
>> boundary (x[i] in R), yi is the ith ycoordinate of the polygon boundary
>> (y[i] in R) - and n is the number of points used to specify the polygon
>> boundary. The polygon is assumed to be in closed form so that the x1 and y1
>> take the same value as xn and yn.
>>>>
>>>> Hope to hear from you soon.
>>>>
>>>> Yours sincerely
>>>>
>>>> Krishan Mistry
>>>>
>>
>> _______________________________________________
>> 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]]
>
> _______________________________________________
> 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, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
http://depsy.org/person/434412



More information about the R-sig-Geo mailing list