Skip to content

Commit

Permalink
Merge pull request #1 from sat-utils/v0.2.0
Browse files Browse the repository at this point in the history
V0.2.0
  • Loading branch information
Scisco committed May 24, 2016
2 parents 1da48a0 + aba2cd0 commit fc55207
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 56 deletions.
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": ["standard"],
"env": {
"es6": true,
"browser": true
},
"rules": {
"semi": [2, "always"],
"no-extra-semi": 2,
"semi-spacing": [2, { "before": false, "after": true }]
}
}
39 changes: 24 additions & 15 deletions libs/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _ = require('lodash');
var ejs = require('elastic.js');
var gjv = require('geojson-validation');

var geojsonError = new Error('Invalid Geojson');

/**
* @apiDefine search
* @apiParam {string} [search] Supports Lucene search syntax for all available fields
Expand All @@ -16,7 +18,7 @@ var legacyParams = function (params, q) {

var geojsonQueryBuilder = function (feature, query) {
var shape = ejs.Shape(feature.geometry.type, feature.geometry.coordinates);
query = query.should(ejs.GeoShapeQuery()
query = query.must(ejs.GeoShapeQuery()
.field('data_geometry')
.shape(shape));
return query;
Expand All @@ -31,17 +33,17 @@ var geojsonQueryBuilder = function (feature, query) {
* with no spaces in between. Example: `contains=23,21`
**/
var contains = function (params, query) {
var correct_query = new RegExp('^[0-9\.\,\-]+$');
if (correct_query.test(params)) {
var correctQuery = new RegExp('^[0-9\.\,\-]+$');
if (correctQuery.test(params)) {
var coordinates = params.split(',');
coordinates = coordinates.map(parseFloat);

if (coordinates[0] < -180 || coordinates[0] > 180) {
return err.incorrectCoordinatesError(params);
throw 'Invalid coordinates';
}

if (coordinates[1] < -90 || coordinates[1] > 90) {
return err.incorrectCoordinatesError(params);
throw 'Invalid coordinates';
}

var shape = ejs.Shape('circle', coordinates).radius('1km');
Expand All @@ -51,7 +53,7 @@ var contains = function (params, query) {
.shape(shape));
return query;
} else {
err.incorrectCoordinatesError(params);
throw 'Invalid coordinates';
}
};

Expand All @@ -64,11 +66,19 @@ var contains = function (params, query) {
**/
var intersects = function (geojson, query) {
// if we receive an object, assume it's GeoJSON, if not, try and parse
if (typeof geojson === 'string') {
try {
geojson = JSON.parse(geojson);
} catch (e) {
throw geojsonError;
}
}

if (gjv.valid(geojson)) {
// If it is smaller than Nigeria use geohash
// if (tools.areaNotLarge(geojson)) {
if (geojson.type === 'FeatureCollection') {
for (var i=0; i < geojson.features.length; i++) {
for (var i = 0; i < geojson.features.length; i++) {
var feature = geojson.features[i];
query = geojsonQueryBuilder(feature, query);
}
Expand All @@ -77,7 +87,7 @@ var intersects = function (geojson, query) {
}
return query;
} else {
err.invalidGeoJsonError();
throw geojsonError;
}
};

Expand Down Expand Up @@ -130,7 +140,7 @@ module.exports = function (params, q) {
// Do legacy search
if (params.search) {
return legacyParams(params, q);
};
}

// contain search
if (params.contains) {
Expand All @@ -145,7 +155,7 @@ module.exports = function (params, q) {
}

// select parameters that have _from or _to
_.forEach(params, function(value, key) {
_.forEach(params, function (value, key) {
var field = _.replace(key, '_from', '');
field = _.replace(field, '_to', '');

Expand All @@ -155,15 +165,14 @@ module.exports = function (params, q) {
to: 'cloud_to',
field: 'cloud_coverage'
};
}
else if (_.endsWith(key, '_from')) {
} else if (_.endsWith(key, '_from')) {
if (_.isUndefined(rangeFields[field])) {
rangeFields[field] = {};
}

rangeFields[field]['from'] = key;
rangeFields[field]['field'] = field;
} else if ( _.endsWith(key, '_to')) {
} else if (_.endsWith(key, '_to')) {
if (_.isUndefined(rangeFields[field])) {
rangeFields[field] = {};
}
Expand All @@ -176,7 +185,7 @@ module.exports = function (params, q) {
});

// Range search
_.forEach(rangeFields, function(value, key) {
_.forEach(rangeFields, function (value, key) {
query = rangeQuery(
_.get(params, _.get(value, 'from')),
_.get(params, _.get(value, 'to')),
Expand All @@ -195,7 +204,7 @@ module.exports = function (params, q) {
}

// For all items that were not matched pass the key to the term query
_.forEach(params, function(value, key) {
_.forEach(params, function (value, key) {
query = matchQuery(key, value, query);
});

Expand Down
54 changes: 35 additions & 19 deletions libs/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ var client = new elasticsearch.Client({
requestTimeout: 50000 // milliseconds
});


var Search = function (event) {
var params;

if (_.has(event, 'query')) {
if (_.has(event, 'query') && !_.isEmpty(event.query)) {
params = event.query;
} else if (_.has(event, 'body')) {
} else if (_.has(event, 'body') && !_.isEmpty(event.body)) {
params = event.body;
} else {
throw('Event must either have query or body');
params = {};
}

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

// Build Elastic Search Query
this.q = ejs.Request();
Expand All @@ -41,7 +40,6 @@ var Search = function (event) {
};

Search.prototype.buildSearch = function () {

var fields;

// if fields are included remove it from params
Expand Down Expand Up @@ -95,7 +93,7 @@ Search.prototype.buildAggregation = function () {
if (_.has(this.params, 'fields')) {
var fields = this.params.fields.split(',');

_.forEach(fields, function(field) {
_.forEach(fields, function (field) {
if (_.has(aggr, field)) {
self.q.agg(aggr[field](field).field(field));
}
Expand Down Expand Up @@ -130,10 +128,14 @@ Search.prototype.legacy = function (callback) {
self.params.search = sat;
}

var search_params = this.buildSearch();
try {
var searchParams = this.buildSearch();
} catch (e) {
return callback(e, null);
}

// limit search to only landsat
client.search(search_params).then(function (body) {
client.search(searchParams).then(function (body) {
var response = [];
var count = 0;

Expand All @@ -149,7 +151,7 @@ Search.prototype.legacy = function (callback) {
skip: self.frm,
limit: self.size,
total: count
},
}
},
results: response
};
Expand All @@ -162,9 +164,15 @@ Search.prototype.legacy = function (callback) {

Search.prototype.simple = function (callback) {
var self = this;
var search_params = this.buildSearch();
var searchParams;

try {
searchParams = this.buildSearch();
} catch (e) {
return callback(e, null);
}

client.search(search_params).then(function (body) {
client.search(searchParams).then(function (body) {
var response = [];
var count = 0;

Expand Down Expand Up @@ -193,10 +201,15 @@ Search.prototype.simple = function (callback) {

Search.prototype.geojson = function (callback) {
var self = this;
var search_params = this.buildSearch();
var searchParams;

client.search(search_params).then(function (body) {
try {
searchParams = this.buildSearch();
} catch (e) {
return callback(e, null);
}

client.search(searchParams).then(function (body) {
var count = body.hits.total;

var response = {
Expand Down Expand Up @@ -230,10 +243,15 @@ Search.prototype.geojson = function (callback) {
};

Search.prototype.count = function (callback) {
var search_params = this.buildAggregation();
var searchParams;

client.search(search_params).then(function (body) {
try {
searchParams = this.buildAggregation();
} catch (e) {
return callback(e, null);
}

client.search(searchParams).then(function (body) {
var count = 0;

count = body.hits.total;
Expand All @@ -243,7 +261,7 @@ Search.prototype.count = function (callback) {
found: count,
name: process.env.NAME || 'sat-api',
license: 'CC0-1.0',
website: process.env.WEBSITE || 'https://api.developmentseed.org/satellites/',
website: process.env.WEBSITE || 'https://api.developmentseed.org/satellites/'
},
counts: body.aggregations
};
Expand All @@ -252,8 +270,6 @@ Search.prototype.count = function (callback) {
}, function (err) {
return callback(err);
});

};


module.exports = Search;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sat-api-lib",
"version": "0.1.0",
"version": "0.2.0",
"description": "A library for creating a search API of public Satellites metadata using Elasticsearch",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions test/events/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,21 @@
]
}
}
},
"getIntersectsInvalid": {
"query": {
"intersects": "{"
}
},
"getIntersectsString": {
"query": {
"intersects": "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-41.41845703125,35.209721645221386],[-41.41845703125,35.746512259918504],[-40.71533203125,35.746512259918504],[-40.71533203125,35.209721645221386],[-41.41845703125,35.209721645221386]]]}}]}"
}
},
"getIntersectsWithSatellineName": {
"query": {
"intersects": "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-76.5032958984375,36.7520891569463],[-76.5032958984375,37.081475648860525],[-75.926513671875,37.081475648860525],[-75.926513671875,36.7520891569463],[-76.5032958984375,36.7520891569463]]]}}",
"satellite_name": "sentinel"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/count-getIntersects.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"body": {
"query": {
"bool": {
"should": [
"must": [
{
"geo_shape": {
"data_geometry": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/count-postIntersects.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"body": {
"query": {
"bool": {
"should": [
"must": [
{
"geo_shape": {
"data_geometry": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/geojson-getIntersects.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"body": {
"query": {
"bool": {
"should": [
"must": [
{
"geo_shape": {
"data_geometry": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/geojson-postIntersects.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"body": {
"query": {
"bool": {
"should": [
"must": [
{
"geo_shape": {
"data_geometry": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/simple-getIntersects.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"body": {
"query": {
"bool": {
"should": [
"must": [
{
"geo_shape": {
"data_geometry": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/simple-getIntersectsInvalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit fc55207

Please sign in to comment.