[R] masking a regular lat/lon grid to extract map boundaries

Duncan Murdoch murdoch at stats.uwo.ca
Wed Oct 8 00:36:29 CEST 2008


On 07/10/2008 6:08 PM, claudia tebaldi wrote:
> Dear R-helpers,
> 
> I have lat/lon coordinates of regularly spaced grid points, about 4Km
> apart, covering the entire US continental region.
> I would like to mask this rectangular grid in order to extract all and
> only the grid points within a specific region.  Today I want to
> extract Montana, say, from this grid, and I am hoping to somehow use
> the returned value of the function map("state",region="montana").
> I'm not espoused to using the "maps" library database, it's just the
> way I thought one could go about it (one smarter than I am...) and it
> would certainly be useful to exploit the many political boundaries
> provided by that library, when I move on from Montana to something
> else...
> 
> Any suggestion for an algorithm to do this?
> 
> Of course if someone out there has a high-res mask for the US states
> I'll happily forget about using map(): I'll take it and interpolate
> (or knn1) away to my grid!


I believe this should work:

montana <- map("state", "montana", fill=TRUE, plot=FALSE)
inmontana <- !is.na(map.where(montana, x, y))

(where x and y are the longitude and latitude coordinates of your points).

Duncan Murdoch



More information about the R-help mailing list