how to get coordinates from SpatialPolygon

774 views Asked by At

i'm getting a polygon from the drawpoly using:

x<- ogrListLayers(file_map)
shape=readOGR(file_map, layer=x) #will load the shapefile to your dataset.
plot(shape)
polygon<- drawPoly()

but, I'm trying to get the coordinates from polygon, but i can't:

poligon@polygons [[1]] An object of class "Polygons" Slot "Polygons": [[1]] An object of class "Polygon" Slot "labpt": [1] -46.37327 -23.91955

Slot "area":
[1] 0.09747001

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
           [,1]      [,2]
 [1,] -46.20830 -23.73089
 [2,] -46.20089 -23.73691
 [3,] -46.19842 -23.74595
 [4,] -46.20336 -23.74670
 [5,] -46.20995 -23.76853
 [6,] -46.22313 -23.79338
 [7,] -46.20665 -23.79488
 [8,] -46.20171 -23.80467
 [9,] -46.20254 -23.82801
[10,] -46.21489 -23.84683
[11,] -46.19348 -23.84608
[12,] -46.17618 -23.87469
[13,] -46.11853 -23.84834
[14,] -46.12182 -23.86942
[15,] -46.14406 -23.90481

thanks in advance

2

There are 2 answers

0
augusto carillo ferrari On BEST ANSWER

well, i found out that the function clickpoly and used:

polygon <- clickpoly(add = TRUE, col = 2, lwd = 2)

and used:

poligon$bdry[[1]][1]
poligon$bdry[[1]][2]

to get the x,y points in the polygon

0
Brian D On

It helps if you add a MWE. Here is one:

polygon = "POLYGON((186.547856450189 -46.4485209972227,182.523886136587 -87.3045772259105,170.606614118262 -126.59055812607,151.254014475596 -162.79672548354,125.209796759794 -194.531696591426,93.4748256519081 -220.575914307228,65.3272038227373 -235.621131625452,36.2687622716732 -244.435913500765,-9.05803921416017 -248.900213788796,-54.3848406999935 -244.435913500765,-97.9697586242894 -231.214573193132,-138.137849033921 -209.744281559306,-173.34547474097 -180.850129866096,-202.239626434179 -145.642504159047,-221.510885247941 -109.588514788964,-228.270643805406 -87.3045772259111,-232.294614119009 -46.4485209972234,-228.270643805406 -5.59246476853568,-216.353371787081 33.6935161316239,-197.000772144416 69.8996834890942,-170.956554428614 101.63465459698,-139.221583320728 127.678872312782,-103.015415963258 147.031471955448,-63.7294350630982 158.948743973773,-22.8733788344104 162.972714287376,17.9826773942774 158.948743973774,57.2686582944371 147.031471955449,93.4748256519074 127.678872312783,125.209796759793 101.634654596981,151.254014475596 69.8996834890953,170.606614118262 33.6935161316251,182.523886136587 -5.59246476853451,186.547856450189 -46.4485209972227))"

I don't know why this is so difficult, but here ya go:

polygon <- lapply(polygon, FUN = function(x) readWKT(x))

poly1 <- sapply(polygon, FUN=function(x) x@polygons)
poly2 <- sapply(poly1, FUN=function(x) x@Polygons)
poly3 <- lapply(poly2, FUN=function(x) x@coords)
plot(poly3[[1]])