Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Once you have the couchapp utility working, <code>git clone</code> this repo and

### [Spatial Views](https://github.com/couchbase/geocouch)

#### points.js
#### basic.js

A spatial view that additionally emits the original GeoJSON value (doc.geometry)

Example:

$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/points?bbox=80,88,90,90'
$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/basic?bbox=80,88,90,90'
{
"update_seq":203,
"rows":[
Expand All @@ -56,13 +56,13 @@ Example:
]
}

#### pointsFull.js
#### full.js

A spatial view that emits both GeoJSON and the full document (as value).
A spatial view that emits both GeoJSON and the full document (as value).

Example:

$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/pointsFull?bbox=80,88,90,90'
$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/full?bbox=80,88,90,90'
{
"update_seq":203,
"rows":[
Expand All @@ -89,13 +89,13 @@ Example:
]
}

#### pointsOnly.js
#### minimal.js

A spatial view that only emits GeoJSON and no additional value.

Example:

$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/pointsOnly?bbox=80,88,90,90'
$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/minimal?bbox=80,88,90,90'
{
"update_seq":203,
"rows":[
Expand Down Expand Up @@ -178,7 +178,7 @@ This list function generates a simple KML feed

Example:

$ curl http://localhost:5984/yourdb/_design/geo/_spatiallist/kml/points?bbox=0,0,45,45
$ curl http://localhost:5984/yourdb/_design/geo/_spatial/_list/kml/basic?bbox=0,0,45,45

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
Expand All @@ -198,7 +198,7 @@ This function outputs a GeoJSON FeatureCollection (compatible with OpenLayers).

Example:

$curl -X GET 'http://localhost:5984/yourdb/_design/geo/_spatiallist/geojson/points?bbox=80,88,90,90'
$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/_list/geojson/basic?bbox=80,88,90,90'
{
"type":"FeatureCollection",
"features":[
Expand All @@ -223,11 +223,9 @@ Example:

This will take the centroid of the bbox parameter and a supplied radius parameter in meters and filter the rectangularly shaped bounding box result set by circular radius.

**WARNING** This only works with on points, not lines or polygons yet

Example:

$ curl -X GET http://localhost:5984/yourdb/_design/geo/_spatiallist/radius/points?bbox=-122.67,45.52,-122.67,45.52&radius=50
$ curl 'http://localhost:5984/yourdb/_design/geo/_spatial/_list/radius/basic?bbox=-122.67,45.52,-122.67,45.52&radius=50'
{
"type":"FeatureCollection",
"features":[
Expand Down
24 changes: 24 additions & 0 deletions couchapp/_attachments/script/geojson-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@
return { 'type': 'Point', 'coordinates': [y/f, x/f] };
},

// checks if geometry lies entirely within a circle
// works with Point, LineString, Polygon
gju.geometryWithinRadius = function (geometry, center, radius) {
if (geometry.type == 'Point') {
return gju.pointDistance(geometry, center) <= radius;
} else if (geometry.type == 'LineString' || geometry.type == 'Polygon') {
var point = {};
var coordinates;
if (geometry.type == 'Polygon') {
// it's enough to check the exterior ring of the Polygon
coordinates = geometry.coordinates[0];
} else {
coordinates = geometry.coordinates;
}
for (var i in coordinates) {
point.coordinates = coordinates[i];
if (gju.pointDistance(point, center) > radius) {
return false;
}
}
}
return true;
}

gju.simplify = function (source, kink) {
/* source[] array of geojson points */
/* kink in metres, kinks above this depth kept */
Expand Down
3 changes: 1 addition & 2 deletions couchapp/lists/radius.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function(head, req) {
"coordinates": [[[bbox[0], bbox[1]], [bbox[2], bbox[3]]]]
}),
callback = req.query.callback,
circle = gju.drawCircle(radius, center),
startedOutput = false;

if (req.headers.Accept.indexOf('application/json') != -1)
Expand All @@ -27,7 +26,7 @@ function(head, req) {
if ('callback' in req.query) send(req.query['callback'] + "(");
send('{"type": "FeatureCollection", "features":[');
while (row = getRow()) {
if (gju.pointInPolygon(row.value.geometry, circle)) {
if (gju.geometryWithinRadius(row.value.geometry, center, radius)) {
if (startedOutput) send(",\n");
out = '{"type": "Feature", "geometry": ' + JSON.stringify(row.value.geometry);
delete row.value.geometry;
Expand Down
2 changes: 1 addition & 1 deletion couchapp/rewrites.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"from": ""
},
{
"to": "/_spatiallist/geojson/full",
"to": "/_spatial/_list/geojson/full",
"from": "/data"
},
{
Expand Down
24 changes: 24 additions & 0 deletions couchapp/vendor/geojson-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@
return { 'type': 'Point', 'coordinates': [y/f, x/f] };
},

// checks if geometry lies entirely within a circle
// works with Point, LineString, Polygon
gju.geometryWithinRadius = function (geometry, center, radius) {
if (geometry.type == 'Point') {
return gju.pointDistance(geometry, center) <= radius;
} else if (geometry.type == 'LineString' || geometry.type == 'Polygon') {
var point = {};
var coordinates;
if (geometry.type == 'Polygon') {
// it's enough to check the exterior ring of the Polygon
coordinates = geometry.coordinates[0];
} else {
coordinates = geometry.coordinates;
}
for (var i in coordinates) {
point.coordinates = coordinates[i];
if (gju.pointDistance(point, center) > radius) {
return false;
}
}
}
return true;
}

gju.simplify = function (source, kink) {
/* source[] array of geojson points */
/* kink in metres, kinks above this depth kept */
Expand Down
80 changes: 39 additions & 41 deletions misc/geocouch-filler-js/geocouch-filler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,69 @@
var http = require('http');
var url = require('url');
var path = require('path');

require.paths.unshift(path.join(__dirname, 'lib'));
var Barrier = require('barrier').Barrier;

var httpClientPoolSize = 16;
var host,port,db;
var host, port, db;

if(process.argv.length < 5){
console.log("\n\tUsage: node geocouch-filler.js <database-URI> <[bbox]> <count>\n\n")
process.exit(1);
if (process.argv.length < 5) {
console.log("\n\tUsage: node geocouch-filler.js <database-URI> <[bbox]> <count>\n\n")
process.exit(1);
};
var uri = url.parse(process.argv[2] || "http://localhost:5984/gc-utils");
var bbox = JSON.parse(process.argv[3] || "[-180,-90,180,90]") ;
var bbox = JSON.parse(process.argv[3] || "[-180,-90,180,90]");
var documentCount = parseInt(process.argv[4]) || 10;


if(uri && bbox && bbox.length === 4 && documentCount){
host = uri.hostname;
port = uri.port || 5984;
db = uri.pathname.split("/")[1] || "gc-utils";
if (uri && bbox && bbox.length === 4 && documentCount) {
host = uri.hostname;
port = uri.port || 5984;
db = uri.pathname.split("/")[1] || "gc-utils";
}

var randomArbitrary = function(min, max) {
var randomArbitrary = function (min, max) {
return Math.random() * (max - min) + min;
};
};


//create client pool to speed
var clients = [];
for (var i = 0; i < httpClientPoolSize; i++) {
clients.push(http.createClient(port, host));
clients.push(http.createClient(port, host));
}

//barrier allows to exit when queries have been executed
var b = new Barrier(documentCount, function() {
console.log("Insertion completed");
process.exit(0);
var b = new Barrier(documentCount, function () {
console.log("Insertion completed");
process.exit(0);
});

var ptr = 0;

for (var i = 0; i < documentCount; i++) {
var client = clients[ptr++ % httpClientPoolSize];
var client = clients[ptr++ % httpClientPoolSize];

var entity = {
"geometry" : {
"type" : "Point",
"coordinates": [randomArbitrary(bbox[0],bbox[2]),randomArbitrary(bbox[1],bbox[3])]
}
};
var entity = {
"geometry": {
"type": "Point",
"coordinates": [randomArbitrary(bbox[0], bbox[2]), randomArbitrary(bbox[1], bbox[3])]
}
};

var request = client.request('POST', "/" + db, {
"Content-Type": "application/json",
"Connection": "keep-alive"
});
request.write(JSON.stringify(entity));
request.end();
request.once('response', function (response) {
if(response.statusCode === 201){
console.log("Document "+response.headers['location']+" created");
b.submit();
}
else{
console.log("POST caused "+response.statusCode+"!");
b.submit();
}
});
}
var request = client.request('POST', "/" + db, {
"Content-Type": "application/json",
"Connection": "keep-alive"
});
request.write(JSON.stringify(entity));
request.end();
request.once('response', function (response) {

if (response.statusCode === 201) {
console.log("Document " + response.headers['location'] + " created");
b.submit();
} else {
console.log("POST caused " + response.statusCode + "!");
b.submit();
}
});
}
57 changes: 0 additions & 57 deletions misc/geocouch-filler-js/lib/barrier.js

This file was deleted.

41 changes: 41 additions & 0 deletions misc/geocouch-filler-js/node_modules/barrier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.