Creating Convex Hulls for geospatial data processing and display in OpenLayers Using Quickhull

Creating Convex Hulls for geospatial data processing and display in OpenLayers Using Quickhull

convex_hull_300A while back I was working on a web mapping application that displayed vector features on an OpenLayers map as clusters. To give the user a sense of the cluster extent, I needed to display a convex hull polygon around the vector locations when the user moused over a cluster. However since we were clustering the features in the browser I needed a way to perform this kind of geospatial data processing on the client using OpenLayers. I was unable to find such a thing on the web, however I was able to find a JavaScript implementation of the quickhull algorithm (http://en.literateprograms.org/Quickhull_(Javascript)).

I took this code and adapted it for OpenLayers for use in my project; I’ve since spent a little time refactoring this code and posted it to GitHub as a Gist (https://gist.github.com/robgaston/8855489). Feel free to reuse this code as you see fit. I will continue to improve the code inside the Gist as I have time; there may still be some flaws in this implementation, so suggested improvements are welcomed. While imperfect, it has proven to be stable and functional for the use case in my project.

Creating Convex Hulls for geospatial data processing and display in OpenLayers Using Quickhull

A few things to note:

  • The method takes in an array of features of any geometry type and returns a single feature with a polygon geometry representing the convex hull.
  • Presently, the code simply uses the centroid for any features that do not have point geometries, as this was satisfactory for my use case. It could fairly easily be enhanced to truly support non-point geometries.
  • The code could be refactored further; for example input feature geometries are currently converted to arrays representing points and then back to actual OpenLayers.Geometry.Point instances.

Related articles

FARL_Divider_Graphic-cropped
Deploying ArcGIS Portal and Your First Web Applications with the LGIM
Use your mobile phone to find the best fishing hotspots near you
Port of San Francisco Uses Enterprise GIS to Prepare for Sea Level Rise Caused by Climate Change