Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 21, 2012 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egaumer/1877636 to your computer and use it in GitHub Desktop.
Save egaumer/1877636 to your computer and use it in GitHub Desktop.
Simple Polygon Search in Cloud9
#!/bin/bash
# run this before feeding example documents
# create the collection
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/gis'
# explicitly define the location field to be of type 'geo_point'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/gis/pin/_mapping' -d '{
"pin" : {
"properties" : {
"location" : {
"type" : "geo_point"
}
}
}
}'
<!doctype html>
<head>
<title>Simple Geo Polygon Search</title>
<!-- Example search template using underscore.js -->
<script type="text/template" id="results">
<% _.each(hits.hits, function(hit) { %>
<li><%= hit._source.desc %></li>
<% }); %>
</script>
</head>
<body>
<div class="search"></div>
<script src="js/jquery-1.6.2.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/c9/c9api.min.js"></script>
<!-- change 'yourapp' to the name of your Cloud9 application -->
<script defer src="yourapp/js/polygonSearch.js"></script>
</body>
</html>
/* An example of a searching within a polygon
*
* The polygon filter allows you to restrict search results
* to those which fall within a given set of points.
*/
(function($) {
/* create a query object */
var query = c9.query.QueryString("zoning:commercial");
/* a set of points that define a county or neighborhood */
var filter = c9.filter.GeoPolygonFilter("location").points([
[-122.396480, 37.7819288],
[-122.396181, 37.7817289],
[-122.395681, 37.7813289],
[-122.395381, 37.7810287],
[-122.394981, 37.7806288],
[-122.398881, 37.7803292],
[-122.396480, 37.7819288]
]);
/* compose a filtered query */
var filterQuery = c9.query.FilteredQuery(query, filter);
/* a function to display results */
var resultsCallBack = function(results) {
if (results.hits) {
var template = _.template($("#results").html(), results);
$(".search").empty();
$(".search").append(template);
}
};
/* execute the request */
c9.search.Request()
.collections("gis")
.types("pin")
.query(filterQuery)
.get(resultsCallBack);
})(jQuery);
#!/bin/bash
# some sample test data
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/gis/pin/1' -d '{
"zoning": "commercial",
"city": "San Francisco",
"location": [-122.40560, 37.77280],
"desc": "Commercial property in downtown San Francisco"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/gis/pin/2' -d '{
"zoning": "commercial",
"city": "San Francisco",
"location": [-122.39560, 37.78095],
"desc": "Commercial property in downtown San Francisco"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/gis/pin/3' -d '{
"zoning": "Residential",
"city": "San Francisco",
"location": [-122.39573, 37.78095],
"desc": "Two story condo in downtown San Francisco"
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment