zenobase/geocluster-facet — pull request #12 by kielni (2013-10-07 20:28:23+00:00)
Title: I've added an optional calc_polygon parameter (default is false).  If tr...

...ue, the GeoCluster class also keeps track of a polygon that includes all of the points; it only stores the vertices. When you add a point, it uses the winding number algorithm (from http://geomalgorithms.com/a03-_inclusion.html) to determine if the new point is inside the polygon. If not, it calculates the convex hull (from from https://code.google.com/p/convex-hull/source/browse/Convex+Hull/src/algorithms/FastConvexHull.java?r=4) for the current polygon plus the new point. When merging clusters, it calculates the convex hull for the set of points in both cluster polygon edges. It returns the vertices as an array of vertex objects.

![image](https://f.cloud.github.com/assets/1649528/1284038/ec6962b8-2f8e-11e3-880c-fa5c755aeb7f.png)

sample request:

"facets": {
"places": {
"geo_cluster": {
"field": "geo.coordinates",
"factor": 0.2,
"calc_polygon": "true"
}
}
}

returns:

facets: {
places: {
_type: geo_cluster
factor: 0.3
calc_polygon: true
clusters: [
{
total: 167
center: {
lat: 36.44304571982402
lon: -98.55528440796905
}
top_left: {
lat: 44.1638895385842
lon: -110.955069065094
}
bottom_right: {
lat: 30.62798
lon: -88.22341181369133
}
vertices: [
{
vertex: {
lat: 30.62798
lon: -96.33441
}
}
{
vertex: {
lat: 30.67822970994149
lon: -88.22341181369133
}
}
{
vertex: {
lat: 44.1638895385842
lon: -93.95074367523193
}
}
{
vertex: {
lat: 41.26569482005358
lon: -110.955069065094
}
}
{
vertex: {
lat: 32.22174
lon: -110.92648
}
}
]
}