Skip to content

Instantly share code, notes, and snippets.

@mick
Created September 14, 2011 23:17
Show Gist options
  • Select an option

  • Save mick/1218084 to your computer and use it in GitHub Desktop.

Select an option

Save mick/1218084 to your computer and use it in GitHub Desktop.
slide gists
{
"_id": "_design/gc-utils",
"_rev": "2-bd8fcdf5509cb9cc99560a39f0c642f0",
"vendor": {
"clustering": {
"KNNCluster": "/**\n * kNN-based Clustering\n */\n(function() {\n\tvar cluster = this.cluster = {};\n\n\tif (typeof module !== 'undefined' && module.exports) {\n\t\tmodule.exports = cluster;\n\t}\n\t\n\t//TODO\n})();",
"ProximityCluster": "/**\n * Proximity based clustering\n */\n(function() {\n\n var proxcluster = this.proxcluster = {};\n\tif (typeof module !== 'undefined' && module.exports) {\n\t\tmodule.exports = proxcluster;\n\t}\n\n var gju = require('vendor/geojson-js-utils/geojson-utils');\n\n proxcluster.PointCluster = function(threshold){\n this.clusters = [];\n this.points = [];\n this.distanceThreshold = threshold;\n\n };\n \n proxcluster.PointCluster.prototype.addToClosestCluster = function(point) {\n var distance = 40000; // Some large number\n var clusterToAddTo = null;\n var pos = point.geometry;\n for (var i = 0, cluster; cluster = this.clusters[i]; i++) {\n var center = cluster.center;\n \n if (center) {\n var d = gju.pointDistance(center, pos)/1000; //convert to km\n if ( d < this.distanceThreshold) {\n distance = d;\n clusterToAddTo = cluster;\n }\n }\n }\n\n if (clusterToAddTo) {\n clusterToAddTo.addPoint(point);\n } else {\n var cluster = new proxcluster.Cluster(this);\n cluster.addPoint(point);\n this.clusters.push(cluster);\n }\n };\n\n proxcluster.PointCluster.prototype.getClusters = function(){\n\n var clusterdata = [];\n for (var i = 0, cluster; cluster = this.clusters[i]; i++) {\n clusterdata.push({\"center\":cluster.center, \"points\":cluster.getPoints(), \"size\": cluster.getSize()});\n }\n return clusterdata;\n }\n\n /**\n * A cluster that contains points.\n *\n * @param {PointCluster} The PointCluster that this\n * cluster is associated with.\n * @constructor\n * @ignore\n */\n proxcluster.Cluster = function (PointCluster) {\n this.pointCluster = PointCluster;\n this.center = null;\n this.points = [];\n }\n\n /**\n * Add a point the cluster.\n *\n * @param {point} The point to add.\n * @return {boolean} True if the point was added.\n */\n proxcluster.Cluster.prototype.addPoint = function(point) {\n if (!this.center) {\n this.center = point.geometry;\n } else {\n var l = this.points.length + 1;\n var lat = (this.center.coordinates[0] * (l-1) + point.geometry.coordinates[0]) / l;\n var lng = (this.center.coordinates[1] * (l-1) + point.geometry.coordinates[1]) / l;\n this.center = {\"type\": \"Point\", \"coordinates\":[lat, lng]};\n }\n\n \n this.points.push(point);\n\n return true;\n };\n\n\n /**\n * Returns points in the cluster\n *\n * @return {Array.<points>} The points\n */\n proxcluster.Cluster.prototype.getPoints = function() {\n return this.points;\n };\n\n /**\n * Returns number of points\n *\n * @return {int} number of points\n */\n proxcluster.Cluster.prototype.getSize = function() {\n return this.points.length;\n };\n\n\n})();"
},
"geojson-js-utils": {
}
},
"views": {
"all": {
"map": "/**\n * A simple map function mocking _all, but allows usage with lists etc.\n * \n */\nfunction(doc) {\n emit(doc.id, doc);\n}"
}
},
"lists": {
"knn-clustering": "/**\n * A clustering algorithm that clusters object based on their proximity, producing k distinct clusters.\n */\nfunction(head, req) {\n\tstart({\"code\": 501,\"headers\":{\"Content-Type\" : \"text/plain\"}});\n\t//TODO: implement kNN clustering list \n}",
"proximity-clustering": "/**\n * A clustering algorithm that clusters object based on their proximity, using a threshold value.\n */\nfunction(head, req) {\t\n\n var g = require('vendor/clustering/ProximityCluster'),\n row,\n threshold =100;\n\n start({\"headers\":{\"Content-Type\" : \"application/json\"}});\n if ('callback' in req.query) send(req.query['callback'] + \"(\");\n \n if('threshold' in req.query){ threshold = req.query.threshold;}\n var pc = new g.PointCluster(parseInt(threshold)); \n \n while (row = getRow()) {\n pc.addToClosestCluster(row.value);\n }\n\n send(JSON.stringify({\"rows\":pc.getClusters()}));\n\n if ('callback' in req.query) send(\")\");\n};",
"kml": "/**\n * A list function that transforms a spatial view result set into a KML feed.\n *\n * @author Benjamin Erb\n */\nfunction(head, req) {\n var row, out, sep = '\\n';\n\n start({\"headers\":{\"Content-Type\" : \"application/vnd.google-earth.kml+xml\"}});\n send('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n');\n send('<kml xmlns=\"http://www.opengis.net/kml/2.2\">\\n');\n send('<Document>\\n');\n send('<name>GeoCouch Result - KML Feed</name>\\n');\n while (row = getRow()) {\n if(row.geometry){\n send('\\t<Placemark>');\n send('<name>'+row.id+'</name>');\n send('<Point><coordinates>'+row.geometry.coordinates[0]+','+row.geometry.coordinates[1]+',0</coordinates></Point>');\n send('</Placemark>\\n');\n \t}\n }\n send('</Document>\\n');\n send('</kml>\\n');\n};",
"radius": "/**\n * This will take the centroid of the bbox parameter and a supplied radius\n * parameter in meters and filter the rectangularly shaped bounding box\n * result set by circular radius.\n *\n * @author Max Ogden\n */\nfunction(head, req) {\n var gju = require('vendor/geojson-js-utils/geojson-utils'),\n row,\n out,\n radius = req.query.radius,\n bbox = JSON.parse(\"[\" + req.query.bbox + \"]\"),\n center = gju.rectangleCentroid({\n \"type\": \"Polygon\",\n \"coordinates\": [[[bbox[0], bbox[1]], [bbox[2], bbox[3]]]]\n }),\n callback = req.query.callback,\n circle = gju.drawCircle(radius, center),\n startedOutput = false;\n\n if (req.headers.Accept.indexOf('application/json') != -1)\n start({\"headers\":{\"Content-Type\" : \"application/json\"}});\n else\n start({\"headers\":{\"Content-Type\" : \"text/plain\"}});\n\n if ('callback' in req.query) send(req.query['callback'] + \"(\");\n send('{\"type\": \"FeatureCollection\", \"features\":[');\n while (row = getRow()) {\n if (gju.pointInPolygon(row.geometry, circle)) {\n if (startedOutput) send(\",\\n\");\n out = '{\"type\": \"Feature\", \"geometry\": ' + JSON.stringify(row.geometry) +\n ', \"properties\": ' + JSON.stringify(row.value) + '}';\n send(out);\n startedOutput = true;\n }\n }\n send(\"\\n]};\");\n if ('callback' in req.query) send(\")\");\n};",
"geojson": "/**\n * This function outputs a GeoJSON FeatureCollection (compatible with\n * OpenLayers). JSONP requests are supported as well.\n *\n * @author Volker Mische\n */\nfunction(head, req) {\n var row, out, sep = '\\n';\n\n // Send the same Content-Type as CouchDB would\n if (req.headers.Accept.indexOf('application/json')!=-1) {\n start({\"headers\":{\"Content-Type\" : \"application/json\"}});\n }\n else {\n start({\"headers\":{\"Content-Type\" : \"text/plain\"}});\n }\n\n if ('callback' in req.query) {\n send(req.query['callback'] + \"(\");\n }\n\n send('{\"type\": \"FeatureCollection\", \"features\":[');\n while (row = getRow()) {\n out = JSON.stringify({type: \"Feature\", geometry: row.geometry,\n properties: row.value});\n send(sep + out);\n sep = ',\\n';\n }\n send(\"]}\");\n\n if ('callback' in req.query) {\n send(\")\");\n }\n};"
},
"spatial": {
"geomsOnly": "/**\n * A simple spatial view that emits only the GeoJSON object without\n * further values.\n */\nfunction(doc){\n if(doc.geometry){\n emit(doc.geometry, null);\n }\n}",
"geomsFull": "/**\n * A simple spatial view that emits the GeoJSON plus the complete document.\n */\nfunction(doc){\n if(doc.geometry){\n emit(doc.geometry, doc);\n }\n}",
"recentFull": "/**\n * A simple spatial view that emits the GeoJSON plus the complete document.\n */\nfunction(doc){\n\tif(doc.geometry){\n startdate = new Date();\n\n //Only in the last hour.\n startdate.setTime(startdate.getTime() - (1000*60*60));\n messagedate = new Date(doc.date);\n if(messagedate > startdate){\n\t\t emit(doc.geometry, doc);\n }\n\t}\n}",
"geoms": "/**\n * A simple spatial view that emits GeoJSON plus the original document id.\n */\nfunction(doc){\n if(doc.geometry){\n emit(doc.geometry, doc._id);\n }\n}"
},
"couchapp": {
"signatures": {
},
"objects": {
},
"manifest": [
"lists/",
"lists/geojson.js",
"lists/kml.js",
"lists/knn-clustering.js",
"lists/proximity-clustering.js",
"lists/radius.js",
"spatial/",
"spatial/geoms.js",
"spatial/geomsFull.js",
"spatial/geomsOnly.js",
"spatial/recentFull.js",
"vendor/",
"vendor/clustering/",
"vendor/clustering/KNNCluster.js",
"vendor/clustering/ProximityCluster.js",
"vendor/geojson-js-utils/",
"views/",
"views/all/",
"views/all/map.js"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment