In R, I have two shapefiles: Shapefile A represents roads (polylines), Shapefile B some location (points). I want to plot the convex hull enclosing the study region, and it has to be set to the larger of the extents of the two shapefiles.

To produce the convex hull, I use the gConvexHull function (out of the rgeos package) within a function I have written, which preliminary checks which of the two extents is larger using a simple if/else evaluation:

if(extent(points) > extent(polylines)){
    region <- gConvexHull(points)                            
  } else {
    region <- gConvexHull(polylines)                              

It seems that the extent of the polyline shapefile is actually larger:

extent(points) > extent(polylines)

The details about the two extent are as follows:

> extent(points)
class       : Extent 
xmin        : 443314 
xmax        : 459959.1 
ymin        : 3963496 
ymax        : 3976765 
> extent(polylines)
class       : Extent 
xmin        : 439326.4 
xmax        : 458996.4 
ymin        : 3963185 
ymax        : 3983645 

This should imply (at the best of my understanding) that the points should all fall within the polylines' convex hull. Yet, I cannot understand why some points fall well outside the convex hull based on the polyine extent (see image). With teference to the image attached, I am quite happy with the convex hull, but I would like it to be larger toward south-est, in order to include the few points left outside.

enter image description here

  • Could you include the output of extent(points) as well as that of extent(polylines)
    – dmci
    Jan 9 '18 at 16:30
  • I have edited the question, reporting the output of the extent() function.
    – NewAtGis
    Jan 9 '18 at 16:43
  • I dont think you can compare extents like that: extent(c(0, 20, 0, 20)) > extent(c(0,10,0,10)) returns FALSE.
    – Spacedman
    Jan 9 '18 at 17:54
  • 2
    If you want the area of an extent, use this function: earea = function(e){(e@xmax-e@xmin)*(e@ymax-e@ymin)} - you can then compare areas to get the biggest. But I'm not sure that's what you want. I suspect you want the union of the extents...
    – Spacedman
    Jan 9 '18 at 17:59

I found a solution, which is along the lines of what @Spacedman suggested in his comment. I used the union() function out of the raster package.

ch <- union(gConvexHull(points), gConvexHull(polylines))  

where ch is the final convex hull, which is the logical union of the convex hulls enclosing each individual shapefile. As already mentioned, gConvexHull is a function of rgeos package.

  • ch won't be a "convex hull", and could even be a concave shape. Sure that's what you want?
    – Spacedman
    Jan 9 '18 at 21:35
  • 2
    I think you'd need to run the convex hull command one more time to get the actual convex hull of the 'combined' shape. gConvexHull(ch) should do it.
    – Jul
    Jan 11 '18 at 2:07

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.