11

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)` ?? – mdsumner Aug 14 '16 at 4:28
  • Yes, corrected it. It still doesnt work though – user134611 Aug 14 '16 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) – mdsumner Aug 14 '16 at 15:09
7

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.

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:

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 Aug 15 '16 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 Aug 15 '16 at 14:27
19

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)
  • 1
    this looks like a better way than my answer. – dof1985 Aug 15 '16 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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