I came across R function -alphahull- and believe it is the best option to turn gps points into a polygon (3 or 4 days worth of gps points across residence/neighborhood for each of 300 subjects). However, I am just starting to use R and I would appreciate any help with removing duplicate points, as I am getting the following error: "Error in tri.mesh(X) : duplicate data points" after running:


#define projection
project2<-"+proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
# read point shapefile
data.shape <- readShapePoints("D:/clayera/UWO/msthesistake2/tesisGIS/D1001_GPS_WalkExtSel.shp",proj4string=CRS(project2))
# extract coordinates
x.coords <- coordinates(data.shape) #this way we get duplicate coordinates
# alpha-shape: 100 meter threshold
x.as <- ashape(x.coords[,1], x.coords[,2], alpha=100)
  • For efficiency sake. Your "defined projection" is doing nothing because it is not being assigned to you spatial object. There is no need to create a separate coordinate object. You can access the coordinate slot directly this way: ashape(coordinates(data.shape)[,1], coordinates(data.shape)[,2], alpha=100) – Jeffrey Evans Oct 10 '12 at 18:41
  • You're correct, @Jeff, that the defined projection does nothing to the coordinates, but it is assigned to data.shape by readShapePoints. You can see it in the output of summary, for instance. – whuber Oct 10 '12 at 19:14

If you don't want to modify the SpatialPointsDataFrame object data.shape, just insert unique (an efficient function built into base R for such purposes):

x.coords <- unique(coordinates(data.shape))
  • The first line does remove duplicates in the "coords" slot but does not reapply the slot ids to the row.names in the data slot, thus breaking data. The second line has the potential, if duplicate values occur, of deleting actual data and does not work in the absence of coordinate columns. I tried this approach and whereas it displays using "plot" R throws a length mismatch error if you use "spplot". If you are just interested in a SpatialPoints object it is fine but you will likely break a SpatialPointsDataFrame object this way. – Jeffrey Evans Oct 10 '12 at 18:33
  • Thank you for that evaluation, @Jeffrey. I misunderstood the role of the data slot; the problem is now fixed. (Althought spplot now works on the modified values, I am suspicious of it because it cannot even plot a simple shapefile having repeated points in the first place ("'breaks' are not unique"), at least when it has just one constant attribute.) – whuber Oct 10 '12 at 19:03
  • Sorry, but I still do not think that the code is correct. The coords slot is a matrix and does not have rownames associated. As such the result of your data slot manipulation results in a zero dimension dataframe. The way the slot relationships work is not by an ID link by rather by ordering. So, if you remove coord pairs 1,5,10 then you need to remove the corresponding lines in the data slot dataframe. When reading a shapefile the rownames correspond to the FID and thus start with 0. The zerodist function is indexing line numbers and, as such, can account for the ordering across slots. – Jeffrey Evans Oct 10 '12 at 19:33
  • Perhaps we are using different versions of the library, @Jeffrey. I have tested this solution with point shapefiles. If you would execute the command rownames(data.shape@coords) that might resolve this difference for you: on my system, I get a set of rownames and they correctly identify corresponding elements in the @data slot. – whuber Oct 10 '12 at 19:43
  • I am running R 2.15.1 with sp 1.0-0. Admittedly, package changes can cause considerable headaches. The class of the coords slot is a matrix and rownames are NULL. – Jeffrey Evans Oct 10 '12 at 20:01

I remove duplicate points using the sp zerodist function.

sdata <- sdata[-zerodist(sdata )[,1],]

There is a formalized alphahull function that will return an sp SpatialPolygonDataFrame object suitable for export as a shapefile in this thread: Concave Hull: Definition, Algorithms and Practical Solutions

  • Thanks a lot for all the comments and useful postings. I understand it better now and was able to delete the duplicates and plot the concave hull. – user9914 Oct 11 '12 at 17:28

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.