Skip to content

Commit

Permalink
Merge pull request #4 from sat-utils/fixes
Browse files Browse the repository at this point in the history
version 0.3.0
  • Loading branch information
Scisco committed Jun 8, 2016
2 parents c45dcb1 + 6d029c2 commit a7a8e7d
Show file tree
Hide file tree
Showing 10 changed files with 3,407 additions and 153 deletions.
8 changes: 8 additions & 0 deletions libs/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ var intersects = function (geojson, query) {
query = geojsonQueryBuilder(feature, query);
}
} else {
if (geojson.type !== 'Feature') {
geojson = {
'type': 'Feature',
'properties': {},
'geometry': geojson
};
}

query = geojsonQueryBuilder(geojson, query);
}
return query;
Expand Down
46 changes: 43 additions & 3 deletions libs/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var _ = require('lodash');
var ejs = require('elastic.js');
var area = require('turf-area');
var intersect = require('turf-intersect');
var elasticsearch = require('elasticsearch');

var queries = require('./queries.js');
Expand All @@ -24,6 +26,13 @@ var Search = function (event) {
params = {};
}

this.aoiCoverage = null;

if (_.has(params, 'aoi_coverage_percentage')) {
this.aoiCoverage = params['aoi_coverage_percentage'];
params = _.omit(params, ['aoi_coverage_percentage']);
}

// get page number
var page = parseInt((params.page) ? params.page : 1);

Expand All @@ -39,6 +48,30 @@ var Search = function (event) {
this.page = parseInt((params.skip) ? params.skip : page);
};

Search.prototype.calculateAoiCoverage = function (response) {

var self = this;
if (this.aoiCoverage && _.has(this.params, 'intersects')) {
var coverage = parseFloat(this.aoiCoverage);
var newResponse = [];
var aoiArea = area(self.params.intersects);

response.forEach(function (r) {
var intersectObj = intersect(self.params.intersects, r.data_geometry);
var intersectArea = area(intersectObj);
var percentage = (intersectArea / aoiArea) * 100;

if (percentage >= coverage) {
newResponse.push(r);
}
});

return newResponse;
} else {
return response;
}
};

Search.prototype.buildSearch = function () {
var fields;

Expand Down Expand Up @@ -146,7 +179,7 @@ Search.prototype.legacy = function (callback) {

var r = {
meta: {
author: 'Development Seed',
author: process.env.NAME || 'Development Seed',
results: {
skip: self.frm,
limit: self.size,
Expand Down Expand Up @@ -181,12 +214,19 @@ Search.prototype.simple = function (callback) {
response.push(body.hits.hits[i]._source);
}

response = self.calculateAoiCoverage(response);

// this is needed for cases where calculateAoiCoverage has returned a smaller value
if (response.length < self.size) {
count = response.length;
}

var r = {
meta: {
found: count,
name: 'sat-api',
name: process.env.NAME || 'sat-api',
license: 'CC0-1.0',
website: 'https://api.developmentseed.org/satellites/',
website: process.env.WEBSITE || 'https://api.developmentseed.org/satellites/',
page: self.page,
limit: self.size
},
Expand Down
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "sat-api-lib",
"version": "0.2.0",
"version": "0.3.0",
"description": "A library for creating a search API of public Satellites metadata using Elasticsearch",
"main": "index.js",
"scripts": {
"test": "tap test/*.js"
"test": "ava"
},
"repository": {
"type": "git",
Expand All @@ -20,10 +20,16 @@
"elastic.js": "^1.2.0",
"elasticsearch": "^11.0.1",
"geojson-validation": "^0.1.6",
"lodash": "^4.12.0"
"lodash": "^4.12.0",
"turf-area": "^3.0.1",
"turf-intersect": "^3.0.1"
},
"devDependencies": {
"nock": "^8.0.0",
"tap": "^5.7.1"
"ava": "^0.15.2",
"nock": "^8.0.0"
},
"ava": {
"verbose": true,
"serial": true
}
}
77 changes: 76 additions & 1 deletion test/events/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"date_to": "2016-05-05"
}
},
"postIntersects": {
"postIntersects_coverage_zero": {
"body": {
"aoi_coverage_percentage": 0,
"intersects": {
"type": "Feature",
"properties": {},
Expand Down Expand Up @@ -57,6 +58,80 @@
}
}
},
"postIntersects_coverage_100": {
"body": {
"aoi_coverage_percentage": 100,
"limit": 10,
"intersects": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-77.354736328125,
37.38761749978395
],
[
-77.354736328125,
37.75768707689704
],
[
-76.849365234375,
37.75768707689704
],
[
-76.849365234375,
37.38761749978395
],
[
-77.354736328125,
37.38761749978395
]
]
]
}
}
}
},
"postIntersects_coverage_50": {
"body": {
"aoi_coverage_percentage": 50,
"limit": 10,
"intersects": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-76.640625,
37.983174833513395
],
[
-76.640625,
38.31149091244452
],
[
-76.300048828125,
38.31149091244452
],
[
-76.300048828125,
37.983174833513395
],
[
-76.640625,
37.983174833513395
]
]
]
}
}
}
},
"getIntersects": {
"query": {
"intersects": {
Expand Down
Loading

0 comments on commit a7a8e7d

Please sign in to comment.