Re: [R] random location in polygons sp spsample splancs csr

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Sun, 17 Feb 2008 18:34:04 +0100 (CET)

On Sun, 17 Feb 2008, Patrick Giraudoux wrote:

> Dear all,
>
> I had to place points at random, one in each of larger number of polygons
> (actually in objects of class 'SpatialPolygonsDataFrame' , see sp library),
> and tried first to do it using spsample (from sp). Surprisingly, every 5-15
> trials, the output was a NULL value. The doc says that ' this may occur when
> trying to hit a small and awkwardly shaped polygon in a large bounding box
> with a small number of points', but in my case, the shapes were not really
> awkward, and the bounding box just the smallest rectangle including the
> shape, just the number of points was 1 in each polygon.

Dear Patrick,

All the different packages providing functions for generating random 2D points in an irregular polygon or polygons do it similarly - they increase the number of points to be drawn at least in proportion to the ratio of the area of bounding box of the polygon to the area of the polygon. Next, they return the points found from the bounding box area that fall within the polygon. If too many points are found within the polygon, they thin them. If, on the other hand, they are unlucky, they iterate until they meet or exceed the number needed. Handling holes in the polygon(s) is an added attraction as are multiple polygons in a window (spsample methods do this, as do similar methods in spatstat).

Could you provide an example with a set.seed() value that does what you say, or at least the code you used? The spsample methods for polygons have an iter= argument that can be used to make then "try harder", did you try it (with what values - the help page senctence you quote is from the iter= description)?

Did you try asking for multiple points and then choosing a single point at random? This would be equivalent to increasing iter while asking for a single point.

>
> Thus I tried csr (from splancs) after having extracted the polygon
> coordinates of each shape from the Spatial object, and everything went
> smoothly, with hit success every trial.
>

This is because - see the code csr -> ranpts -> gen - it simply iterates until the number of generated points within the polygon exceeds the number required. If the polygon is easy to "hit", this isn't costly, the alternative is easy to see.

Hope this helps

Roger

PS. Perhaps R-sig-geo is a more appropriate list?

> Has anybody (anybody will probably be Edzer or/and Roger...) an idea why here
> splancs looks like outperforming spsample ?
>
> Patrick
>
>
>
>
>
>

-- 
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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 17 Feb 2008 - 17:36:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 17 Feb 2008 - 20:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive