1

Let's say my data is like this:

 daerah longitude   latitude    wilayah
    a   103.0059509 1.736281037   z
    a   103.0055008 1.736822963   z
    a   103.0049973 1.737220049   z
    a   103.0044479 1.737781048   z
    a   103.0041733 1.737781048   z
    a   103.003891  1.738060951   z
    a   103.0022202 1.738055944   z
    a   103.0019455 1.738332033   z
    a   103.0013885 1.738332033   z
    a   103.0011139 1.738610029   z
    a   103.0008316 1.738610029   z
    a   103.0005569 1.738891006   z
    a   103.000267  1.738891006   z
    a   103         1.738610029   z
    b   102.9966965 2.316540003   z
    b   102.9990997 2.315969944   z
    b   103.0125961 2.307929039   z
    b   103.0151978 2.306900978   z
    b   103.0171967 2.305169106   z
    b   103.0181961 2.30298996    z
    b   103.0189972 2.300110102   z
    b   103.0190964 2.29734993    z
    b   103.0169983 2.290781021   z
    b   102.9596024 2.197421074   z
    c   102.82444   2.365111113   z
    c   102.8239212 2.359646082   z
    c   102.8092346 2.338672638   z
    c   102.7966537 2.315601826   z
    c   102.7987518 2.290433407   z
    c   102.7987518 2.252681017   z
    c   102.7777786 2.225415468   z
    c   102.7421188 2.189760447   z
    c   102.7064667 2.183468342   z
    c   102.6708145 2.160397291   z
    c   102.6204758 2.137326479   z

The daerah a,b,c is in wilayah z.
So how can I get the latitude and longitude for wilayah?
The latitude and longitude should form 1 complete shape according to the points daerah.

Any help would be appreciated. Thank you in advance.

epo3
  • 2,991
  • 2
  • 33
  • 60
ahmad fikri
  • 61
  • 1
  • 8

1 Answers1

3

data in the following scripts are your example data with the following structure:

str(data)
# 'data.frame': 35 obs. of  4 variables:
#  $ daerah   : Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 1 1 1 1 ...
#  $ longitude: num  103 103 103 103 103 ...
#  $ latitude : num  1.74 1.74 1.74 1.74 1.74 ...
#  $ wilayah  : Factor w/ 1 level "z": 1 1 1 1 1 1 1 1 1 1 ...

Using package spatstat, you can create a polygon from all data.

# install.packages("spatstat", dependencies = TRUE)
library(spatstat)

poly <- owin(xrange=range(data$longitude), 
     yrange=range(data$latitude), 
     poly=list(x=data$longitude, y=data$latitude))
plot(poly, axes=TRUE, las=1, col="lightblue")
points(data$longitude, data$latitude)

Single polygon from the dataset

Or you can prefer package sp, particularly if the points are not ordered, to obtain the same result (Edit: added based on comments).

create.sp <- function(data, col.name, map.name){
    temp <- data[which(data[,col.name]==map.name),]
    poly <- Polygons(list(Polygon(temp[,c("longitude", "latitude")])), map.name)
    return(list(poly))
}

polys <- lapply(levels(data$wilayah), 
    FUN=function(x) SpatialPolygons(create.sp(data=data, col.name="wilayah", map.name=x)))

plot(polys[[1]], axes=TRUE, las=1, col="lightblue")

To create polygons separately for each daerah and using them to represent a wilayah. (Edit: generalized based on comments).

library(sp)

# creates a single polygon from a character string of one daerah name
create.poly <- function(daerah){
    temp <- data[which(data$daerah==daerah),]
    return(Polygon(temp[,c("longitude", "latitude")]))
 }

# creates named polygons for all daerahs in the data 
# based on character string of their names
all.polys <- function(daerahs){
    polys <- list()
    for(i in 1:length(daerahs)){
       daerah <- levels(data$daerah)[i]
       polys[[i]] <- Polygons(list(create.poly(daerah)), daerah)
    }
    return(polys)
}

polys <- SpatialPolygons(all.polys(levels(data$daerah)), 1:length(levels(data$daerah)))

plot(polys, axes=TRUE, las=1, col="lightblue")

Multiple polygons

Or you can plot a concave hull from your data points with package alphahull.

library(alphahull)

phull <- ahull(data[,c("longitude","latitude")], alpha = 1)
pshap <- ashape(data[,c("longitude","latitude")], alpha = .3)

par(mfrow=c(1,2))
plot(phull, main="alpha hull")
plot(pshap, main="alpha shape")

Concave hulls

The concave hulls do not represent any kind of a spatial object, but the functions are handy for plotting an area around a set of points.

The ideal solution to your problem might be ashape boundary as that will not intersect itself as the earlier options might be prone to do. However, I do not know how to solve this problem.

Community
  • 1
  • 1
nya
  • 2,138
  • 15
  • 29
  • i get an error on your first answer. and based on your second answer,what if i got 20 daerah?is there an effective way to name the a,b,c, and so on? – ahmad fikri May 24 '16 at 08:57
  • What error do you get? I'm working on an edit to generalize the second solution. – nya May 24 '16 at 09:09
  • this is what i get, Error in owin(xrange = range(shape$longitude), yrange = range(shape$latitude), : poly must be either a list(x,y) or a list of list(x,y) – ahmad fikri May 24 '16 at 09:16
  • Maybe your `shape` data have a different structure to what I used in the answer. The data structure for which the scripts were written is at the top of the post. – nya May 24 '16 at 09:31
  • the second answer have a multiple line that connect to each of the daerah since i have many wilayah to get. i dont know how to share u my full data. – ahmad fikri May 24 '16 at 09:35
  • Ok. That can be solved. Please update your question so that we could see what exactly you need to do. How should your result look like? – nya May 24 '16 at 09:38
  • i got around 30 wilayah and i want to set the boundary of each of the wilayah but the boundary is based on the daerah in the wilayah of most outer point and it will connect all the points to point to form a boundary of wilayah. and what i want is to get the points of boudary of wilayah – ahmad fikri May 24 '16 at 09:43
  • the first one is near to what i want – ahmad fikri May 24 '16 at 10:48