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")

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)` ??
    – mdsumner
    Commented Aug 14, 2016 at 4:28
  • Yes, corrected it. It still doesnt work though
    – user134611
    Commented Aug 14, 2016 at 11:52
  • 1
    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)
    – mdsumner
    Commented 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 :)
    – ExploreR
    Commented Aug 19, 2019 at 11:43

3 Answers 3


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

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

A few changes have been made to your code:

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.


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:

A polygon that looks like a line

and this is after code modifications presented here:

Bonding Box

  • thats it! Could you comment a little bit on proj4String ? are you defining characteristics of a lat and long through it?
    – user134611
    Commented 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
    – dof1985
    Commented 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:

#> Loading required package: sp
#> rgeos version: 0.5-2, (SVN revision 621)
#>  GEOS runtime version: 3.6.1-CAPI-1.10.1 
#>  Linking to sp version: 1.4-1 
#>  Polygon checking: TRUE

my_box <- rgeos::bbox2SP(n = 19.74557, s = 19.53407, w = 78.83157, e = 78.46801,
                         proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))

Created on 2020-05-16 by the reprex package (v0.3.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.