0

I have found the same in here How to Convert data frame to spatial coordinates. But in my case, I got very large data.

exchange    longitude   latitude
AB  103.3281386 1.594218196
AB  103.3285929 1.593990735
AB  103.3312494 1.591424235
AB  103.3283736 1.594063254
AB  103.3536164 1.622771588
AB  103.3613242 1.627138676
AB  103.3560151 1.619455334
AB  103.3297071 1.593398614
AB  103.3269466 1.596574285
AB  103.3279517 1.593614052
AB  103.3281356 1.593848271
AB  103.3567136 1.620498495
AB  103.3668021 1.63456952
AB  103.359686  1.624821271
AB  103.3308963 1.585290892
AB  103.3319569 1.59104387
AB  103.3307149 1.592006748
AB  103.3283657 1.593675616
AB  103.3314873 1.591186363
AB  103.3319648 1.590585241
AB  103.3321508 1.590422594
AB  103.3318503 1.588685843
AB  103.3324507 1.594547225
AB  103.3442528 1.60909707
AB  103.3292733 1.593461728
AB  103.3288584 1.594312512
AB  103.329041  1.594135083
AB  103.3348961 1.59761749
AB  103.3500524 1.614224612

It is impossible to do like they do:

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", 
"latitude"), class = "data.frame", row.names = c(NA, -8L))

So anyone can help me to change class from data.frame to spatial polygon?

Community
  • 1
  • 1
ahmad fikri
  • 61
  • 1
  • 8
  • 2
    [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem](http://stackoverflow.com/help/mcve) – swiftBoy May 31 '16 at 06:41
  • This is because my data is just the same as the other post. But the solution for small data only. If i got let say 100 data. How can i use structure(list(longitude=C(......))) – ahmad fikri May 31 '16 at 07:04
  • With regards to your edit: obviously your data is not the same as in the other post. Please get used to provide reproducible examples like described in RDC's link. – lukeA May 31 '16 at 09:19

1 Answers1

0

my data is just the same as the other post [...] So anyone can help me to change classfrom data.frame to spatial polygon?

library(sp)
sp <- SpatialPolygons(list(Polygons(list(Polygon(mydf[, -1])), ID=1)))
class(sp)
# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

or, if you want exchange to be the polygon identifier:

sp <- lapply(split(mydf[, -1], mydf[, 1]), Polygon)
sp <- SpatialPolygons(lapply(seq_along(sp), function(i) { 
  Polygons(list(sp[[i]]), ID = row.names(mydf[!duplicated(mydf[, 1]), ])[i] ) 
}))
# class(sp)
# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

Data:

mydf <- read.table(header=T, text="
exchange   longitude   latitude
AB  103.3281386 1.594218196
AB  103.3285929 1.593990735
AB  103.3312494 1.591424235
AB  103.3283736 1.594063254
AB  103.3536164 1.622771588
AB  103.3613242 1.627138676
AB  103.3560151 1.619455334
AB  103.3297071 1.593398614
AB  103.3269466 1.596574285
AB  103.3279517 1.593614052
AB  103.3281356 1.593848271
AB  103.3567136 1.620498495
AB  103.3668021 1.63456952
AB  103.359686  1.624821271
AB  103.3308963 1.585290892
AB  103.3319569 1.59104387
AB  103.3307149 1.592006748
AB  103.3283657 1.593675616
AB  103.3314873 1.591186363
AB  103.3319648 1.590585241
AB  103.3321508 1.590422594
AB  103.3318503 1.588685843
AB  103.3324507 1.594547225
AB  103.3442528 1.60909707
AB  103.3292733 1.593461728
AB  103.3288584 1.594312512
AB  103.329041  1.594135083
AB  103.3348961 1.59761749
AB  103.3500524 1.614224612")
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • i couldnt get it. what is mydf in your answer?data name? – ahmad fikri May 31 '16 at 08:23
  • it says, Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix – ahmad fikri May 31 '16 at 08:24
  • It's from the post you linked. – lukeA May 31 '16 at 08:47
  • my problem is my data is big let say i have 100 points.It is impossible to do like they do, mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, -4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", "latitude"), class = "data.frame", row.names = c(NA, -8L)) – ahmad fikri May 31 '16 at 09:05
  • Yes, you said that. If I feed the above with `mydf`, I get a spatial polygons object. Question answered as good as possible since you did not follow @RDC advice – lukeA May 31 '16 at 09:09
  • Use `Polygon(mydf[, -1])` instead of `Polygon(mydf)` - the first column is not part of the coordinates. – lukeA May 31 '16 at 09:18
  • i didnt get it. It says, Error in lapply(seq_along(lst), function(i) { : object 'lst' not found – ahmad fikri Jun 01 '16 at 01:49
  • Oops, `lst` has to be `sp` here. Please try again – lukeA Jun 01 '16 at 07:20
  • is this spatialpolygon type?if i type `class(sp)` it should be `SpatialPolygon` right?but it return `list` – ahmad fikri Jun 01 '16 at 08:59
  • You got to wrap it in `SpatialPolygons` again - see my edit – lukeA Jun 01 '16 at 09:02
  • yes u got it right. If possible can u add the description because im new to r. did not understand much. appreciate your time – ahmad fikri Jun 01 '16 at 09:16
  • It's a bit tricky. First we split all data frame columns except the first one by the first one. Then we make each split (which is a long/lat data frame) a Polygon object. So the first lapply gets a list of Polygon objects. The next lapply gets a list of Polygons objects. And that's what we can convert to a SpatialPolygons object. Have a look at the help files, e.g. `?Polygons`. – lukeA Jun 01 '16 at 11:13