[R-sig-eco] spatial/mapping question

Julian Burgos julian at hafro.is
Thu May 30 00:16:45 CEST 2013


Hi Matt,

This is easy to do.  I prefer to use objects of class "Spatial" (package sp).

# Create a SpatialPolygon object

# Vertices of the polygon
# Note that the last point is the same as the first (so the polygon is
closed)
lats <- c(38,38,42,45,38)
longs <- c(20,22,23,21,20)

# Create a SpatialPolygons object
pol <- cbind(longs,lats)
pol <- Polygons(list(Polygon(pol)),"ID")
pol <- SpatialPolygons(list(pol),proj4string = CRS("+proj=longlat
+datum=WGS84"))

# Create a grid that extends a little bit beyond the range of the polygon,
that can be extracted using the bbox function

bbox(pol)

# Prepare a SpatialPoints object with the nodes of the grid

lats <- seq(from=37,to=46,by=0.5)
longs <- seq(from=19,to=24,by=0.5)
grid <- expand.grid(longs,lats)
coordinates(grid) <- c("Var1","Var2")
proj4string(grid) = CRS("+proj=longlat +datum=WGS84")

# Visual check
plot(pol,axes=T)
plot(grid,add=T)

# Keep Identify points in polygon
ov <- over(grid,pol)
grid <- grid[!is.na(ov),]

# More checking...
plot(grid,add=T,col="red",pch=19)

# Convert to data frame
grid <- as.data.frame(grid)

# Voilá!

Julian Mariano Burgos, PhD
Hafrannsóknastofnunin/Marine Research Institute
Skúlagata 4, 121 Reykjavík, Iceland
Sími/Telephone : +354-5752037
Bréfsími/Telefax:  +354-5752001
Netfang/Email: julian at hafro.is

> Hi folks,
>
> I need to perform a certain map-based task in R, and I'm hoping that
> someone can point me towards the right package or approach:
>
> I have lat/long coordinates that specify the corners of a polygon, and I
> need to be able to draw a grid within that polygon where the points of the
> grid are regularly spaced at intervals I determine.  Then I want to
> extract
> that list of points from the grid (so that I can send people out to take
> samples at those grid points).
>
> I've been looking around, and the closet thing I've seen is map.grid
> {mapproj}, which seems to be restricted to a shape defined by 4 points
> rather than a many-sided polygon which is what I need....
>
> Any pointers would be very welcome!
> thanks
> Matt
>
>
> --
> Matthew L Forister
> Assistant Professor
> Dept. of Biology / MS 314
> 1664 N. Virginia St.
> University of Nevada, Reno
> Reno, Nevada 89557
> --
> Office 257 Fleischmann Life Sciences
> (775) 784 - 6770
> --
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>



More information about the R-sig-ecology mailing list