[R-sig-Geo] Warning message: points were rejected as lying outside the specified window

Hossain, Md Md.Hossain at cchmc.org
Fri May 23 19:55:34 CEST 2014


With sincere appreciation for all the suggestions so far I got, and thought it's better to keep you all updated.
To my datasets (coded as "points"), I did the point-in-polygon test with the R code:

library("maps")
library ("sp")
library("spatstat")
library(SDMTools)
usmap <- map("usa", fill=TRUE) 
polypnts = cbind(x=usmap$x, y=usmap$y)
points = cbind(x=my_usdat$x, y=my_usdat$y)
plot(rbind(polypnts, points))
out = pnt.in.poly(points,polypnts)
sum(out==0)
> [1] 1746
sum(out==1)
> [1] 0

It shows that in my dataset there is no points outside of USA map. So the problem I was getting, mainly because of not using the correct coordinate reference system (CRS). It prompted me to apply another strategy that does not require to specify any CRS, posted by Rolf Turner as:

us.df <- with(usmap,data.frame(x=rev(x),y=rev(y)))
us.df <- unique(us.df)
B <- list(list(x=us.df$x, y=us.df$y))
owin(poly=B)

But, got the error message as:
Error in owin(poly = B) : 
  poly must be either a list(x,y) or a list of list(x,y)

After removing all the "NA" cases by the code:

us.df <- us.df[is.na(us.df$x)==FALSE, ]
B <- list(list(x=us.df$x, y=us.df$y))
owin(poly=B)

Got another error message as:
Error in owin(poly = B) : Area of window is negative;
 check that all polygons were traversed in the right direction

It seems I am very close, any idea please! 
Thanks,

Monir
 
-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: Sunday, May 18, 2014 11:39 AM
To: Hossain, Md
Cc: Barry Rowlingson; Rolf Turner; r-sig-geo at r-project.org; Adrian Baddeley
Subject: Re: [R-sig-Geo] Warning message: points were rejected as lying outside the specified window

On Sun, 18 May 2014, Hossain, Md wrote:

> Thanks, Berry.
> The data do not have any obvious errors as you have mentioned. Almost 
> all the points those are outside of the window are along the 
> coastline, and rest are near the Great Lakes area. It looks like these 
> 42 points are ending up on the wrong side of the approximated boundary.

So either you can choose a different approximation to the shoreline you are interested in from another source. Or perhaps buffer it out the maximum distance from the excluded points to the original polygon (see rgeos::gDistance and rgeos::gBuffer)? This may not be adequate, as your polygon and points appear to be in geographical rather than planar coordinates, which will affect the naive distances obtained when using geographical coordinates as if they are planar.

Roger


> Regards,
>
> Monir
> ________________________________________
> From: b.rowlingson at gmail.com [b.rowlingson at gmail.com] on behalf of Barry Rowlingson [b.rowlingson at lancaster.ac.uk]
> Sent: Sunday, May 18, 2014 4:50 AM
> To: Rolf Turner
> Cc: Hossain, Md; r-sig-geo at r-project.org; Adrian Baddeley
> Subject: Re: [R-sig-Geo] Warning message: points were rejected as lying outside the specified window
>
> On Sun, May 18, 2014 at 9:29 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
>> On 18/05/14 14:35, Hossain, Md wrote:
>>> Very grateful to Rolf and Adrian for your kind suggestions. Obviously I
>>> am going to follow, but it seems there is no quick fix.
>>> In the mean time, just wondering, is there anything that I can do
>>> with the "map2SpatialPolygons" function. The data came with the
>>> long-lat, but no information about how these long-lats are created,
>>> i.e., whether using "WGS84" is apropriate or should I try for other
>>> options, e.g., for GCS_North_American_1983_HARN. My knowledge in
>>> geography is very poor, please help.
>
> Have you done whats been suggested and plotted the points to see *why*
> the points are thought to be outside your map? Various things can
> happen with data:
>
> x and y coordinates get swapped
> x and y coordinates are set to 0,0 or  -999,-999 to make missing data
> numbers get typed in wrong
> minus signs in western or southern hemisphere coordinates get left off
> locations are rounded to the nearest 100m, then converted to lat-long
> etc
>
> the other problem is that the boundary is an APPROXIMATION to the
> border. To keep the dataset size small enough, a lot of the wiggly
> coastline is approximated by a straight line. If you have points near
> the coast then some may end up on the wrong side of the approximated
> line. Is that what's happening? Have you plotted the points?
>
> don't go messing around with projections and datums (which might only
> move points by a few metres) until you've had a look at why the points
> are outside your polygon.
>
> Barry
>
> _______________________________________________
> 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



More information about the R-sig-Geo mailing list