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.
Answer
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:
and this is after code modifications presented here:
No comments:
Post a Comment