# R - Create a boundingbox, convert to Polygon class and Plot

I have the `NE lat lng` and `SW lat lng`. My goal is simple, I am using RStudio and I want to create a bounding box from the above two lats and longs and then generate Polygon and I want to add random points into the square polygon. I have NELat/Lng and SW-Lat/Lng in a csv file that I could import into a data.frame.

I just need a little guidance to get started, Im very new to R language, its really different to what i am used to.

So far I have this:

``````coords = cbind(78.46801, 19.53407)
coordsmax = cbind(78.83157, 19.74557 )
sp = SpatialPoints(coordsmax)
sp2 = SpatialPoints(coords)

r1 = rbind(coords, coordsmax[1, ])  # join
P1 = Polygon(r1)
Ps1 = Polygons(list(P1), ID = "a")
plot(Ps1)
``````

I just saw some examples elsewhere but I am not able to plot a polygon of 4 corners.

• You only have a single duplicated coordinate in r1, is that meant to be 'r1 = rbind(coords, coordsmax)` ?? Aug 14, 2016 at 4:28
• Yes, corrected it. It still doesnt work though Aug 14, 2016 at 11:52
• Working through problems carefully one a time is a good way to get a solution. I consider it a bug that Polygon() does not error when given only two or one coordinates, I've noted this in the sp issues. For you q, library(raster); SPs1 <- as(extent(r1), "SpatialPolygons"); plot(SPs1) Aug 14, 2016 at 15:09
• i am sorry that i have to use the answer function of stackexchange but my reputation is not high enough to comment. @dof1985 you use (x_min, y_min), (x_max, y_min), (x_max, y_max), (x_max, y_min), (x_min, y_min) as coordinates to create the polygon. i have tried the same, but failed. then i looked closer at your example and examined, that you use instead of (x_min, y_min), (x_max, y_min),... following (x_min, y_min), (x_min, y_max),... could this be the reason of my error? please correct your example :) Aug 19, 2019 at 11:43

You can nest the `extent` function, from the raster library, in `as` to create a SpatialPolygons object.

``````library(sp)
e <- as(raster::extent(78.46801, 78.83157, 19.53407, 19.74557), "SpatialPolygons")
proj4string(e) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
plot(e)
``````
• this looks like a better way than my answer. Aug 15, 2016 at 14:29

First, note that I dropped the points creation. You can form a polygon without the use of `SpatialPoints`. Though in case many point are involved it would be better to create a polygon from points.

Second, I wrote 5 couples of coordinates in the matrix below.each coordinate couple stand for one corner of your bounding box, and the fifth repeats the first point. Namely the matrix includes: [(x_min, y_min), (x_max, y_min), (x_max, y_max), (x_max, y_min), (x_min, y_min)]

Finally, I used `SpatialPolygons` with `espg:4326` to form a plot-able object within a geographic context.

``````library(sp)

coords = matrix(c(78.46801, 19.53407,
78.46801, 19.74557,
78.83157, 19.74557,
78.83157, 19.53407,
78.46801, 19.53407),
ncol = 2, byrow = TRUE)

P1 = Polygon(coords)
Ps1 = SpatialPolygons(list(Polygons(list(P1), ID = "a")), proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
plot(Ps1, axes = TRUE)
``````

This is what happens If I plot your code:

and this is after code modifications presented here:

• thats it! Could you comment a little bit on `proj4String` ? are you defining characteristics of a lat and long through it? Aug 15, 2016 at 13:15
• Hit `?is.projected` in R-console to see the `proj4string` help. Yes it is used to define CRS class to a spatial object Aug 15, 2016 at 14:27

Just to add another option, you can just simply use the `bbox2SP` function from the rgeos package - below is an example:

``````library(rgeos)