[R] points with-in boundaries of a map

Frede Aakmann Tøgersen frtog at vestas.com
Mon Mar 24 08:09:26 CET 2014


Hi Eliza

I think this comes close to what you want (escaping manual work).


library("maps")
library("sp") ## has a point.in.polygon function

ireland <- map("world", "ireland")
map.axes()

xr <- range(ireland$x, na.rm = TRUE)
yr <- range(ireland$y, na.rm = TRUE)
xseq <- seq(xr[1], xr[2], by = 0.1)
yseq <- seq(yr[1], yr[2], by = 0.1)
xy <- expand.grid(longitude = xseq, latitude = yseq)

## grid lines
abline(v = xseq, col = "grey80", lty = 3)
abline(h = yseq, col = "grey80", lty = 3)

## grid points
ireland <- map("world", "ireland")
map.axes()
points(xy, pch = ".")

## get ireland map as polygon, only for main island
## map.poly not exported so use maps:::map.poly
## use exact to leave out Achill Island and only get main island
ie.polygon <- maps:::map.poly("world", "Ireland", exact = TRUE, as.polygon = TRUE)
## x[1] and y[1] added to close polygon
index <- point.in.polygon(xy$longitude, xy$latitude, c(ie.polygon$x, ie.polygon$x[1]), c(ie.polygon$y, ie.polygon$y[1]))
table(index)

ireland <- map("world", "Ireland", exact = TRUE) # leaving out Achill
points(xy, pch = ".", cex = 1)
map.axes()
## index: 0: outside, 1: border, 3: inside
points(xy[index > 0,], pch = 1, col = "red", cex = 0.9)

## If you need Achill Island as well then do the same for this region also
print(regionNames <- map("world", "ireland", plot = FALSE, namesonly = TRUE))

ireland <- map("world", "Ireland:Achill Island", exact = TRUE, xlim = xr, ylim = yr)

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of eliza botto
> Sent: 23. marts 2014 03:02
> To: r-help at r-project.org
> Subject: [R] points with-in boundaries of a map
> 
> Dear UseRs,
> I have a question regarding reading the coordinates within a country' map. I
> drew map of ireland by using the following commands
> library("maps")
> library("mapproj")
> map("world", "ireland")
> map.axes()
> You can clearly see the axis labelled. What is want to do is to draw lines (both
> vertically and horizontally ) at an interval of 0.5. Which means if x-axis starts
> from -10 then the first be on -10 and second on -9.5 and so on. Similar has to
> be the case with y-axis.
> Afterwards I only want to read the coordinates of the point falling with in the
> boundary of ireland.  I spent quite sometime while going through the
> previous posts but couldnt find the answer.
> Thankyou very much in advance,
> Elisa
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.




More information about the R-help mailing list