Skip to content

Commit

Permalink
adding extra function layer to give room for custom turf functions, #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mapsam committed Sep 18, 2015
1 parent 07dbab1 commit 3d654d2
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/js/ops.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ var dropchop = (function(dc) {
geoContainer.append(geoBtn);
}

// when layers are selected or unselected, lets check our geo operations
$(dc.ops).on('layer:selected', dc.ops.geoCheck);
$(dc.ops).on('layer:unselected', dc.ops.geoCheck);
// $(dc.ops).on('layer:selected', dc.ops.geoCheck);
// $(dc.ops).on('layer:unselected', dc.ops.geoCheck);
$('.operation-geo').removeClass('operation-inactive');
$('.operation-geo').prop('disabled', false);
$(dc.ops).on('operation:geo', dc.ops.geoExecute);
$(dc.ops).on('operation:file:load-gist', dc.ops.file['load-gist'].get);
$(dc.ops).on('operation:file:load-url', dc.ops.file['load-url'].get);
Expand Down Expand Up @@ -85,10 +86,11 @@ var dropchop = (function(dc) {
*/
dc.ops.geoExecute = function(event, operation, parameters) {
var prep = dc.ops.prepareTurfParams(operation, parameters);
console.log(prep);
var result = null;
try {
result = turf[operation].apply(null, prep.options, 5000);
// result = turf[operation].apply(null, prep.options);
result = dc.ops.geo[operation].execute(prep.options);
console.log(result);
} catch(err) {
dc.notify('That operation isn\'t possible. Try changing the order of your selection.');
throw err;
Expand Down
128 changes: 123 additions & 5 deletions src/js/ops.geo.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ var dropchop = (function(dc) {
default: 'miles'
}
],
execute: function(params) {
var result = {};
var newParams = params;

// if there is a feature collection, do it recursively
if (params[0].type === 'FeatureCollection') {
result.type = 'FeatureCollection';
result.features = [];
var features = params[0].features;
var len = features.length;
for (var f = 0; f < len; f++) {
newParams[0] = features[f];
var feature = turf.along.apply(null, newParams);
result.features.push(feature);
}
// otherwise it's a single
} else {
result = turf.along.apply(null, params);
}
return result;
}
},

bezier: {
Expand Down Expand Up @@ -60,6 +81,27 @@ var dropchop = (function(dc) {

}
],
execute: function(params) {
var result = {};
var newParams = params;

// if there is a feature collection, do it recursively
if (params[0].type === 'FeatureCollection') {
result.type = 'FeatureCollection';
result.features = [];
var features = params[0].features;
var len = features.length;
for (var f = 0; f < len; f++) {
newParams[0] = features[f];
var feature = turf.bezier.apply(null, newParams);
result.features.push(feature);
}
// otherwise it's a single
} else {
result = turf.bezier.apply(null, params);
}
return result;
}
},

buffer: {
Expand Down Expand Up @@ -87,6 +129,10 @@ var dropchop = (function(dc) {
default: 'miles'
}
],
execute: function(params) {
var result = turf.buffer.apply(null, params);
return result;
}
},

center: {
Expand All @@ -95,10 +141,11 @@ var dropchop = (function(dc) {
requirements: {
type: ['FeatureCollection']
},
reqs: [
['FeatureCollection<>']
],
description: 'Creates a point in the center of the feature.',
execute: function(params) {
var result = turf.center.apply(null, params);
return result;
}
},

centroid: {
Expand All @@ -111,6 +158,11 @@ var dropchop = (function(dc) {
['Feature<>', 'FeatureCollection<>']
],
description: 'Creates a point in the centroid of the features.',
execute: function(params) {
console.log(params);
var result = turf.centroid.apply(null, params);
return result;
}
},

destination: {
Expand Down Expand Up @@ -146,6 +198,27 @@ var dropchop = (function(dc) {
default: 'miles'
}
],
execute: function(params) {
var result = {};
var newParams = params;

// if there is a feature collection, do it recursively
if (params[0].type === 'FeatureCollection') {
result.type = 'FeatureCollection';
result.features = [];
var features = params[0].features;
var len = features.length;
for (var f = 0; f < len; f++) {
newParams[0] = features[f];
var feature = turf.destination.apply(null, newParams);
result.features.push(feature);
}
// otherwise it's a single
} else {
result = turf.destination.apply(null, params);
}
return result;
}
},

envelope: {
Expand All @@ -158,6 +231,10 @@ var dropchop = (function(dc) {
['FeatureCollection<>']
],
description: 'Takes any number of features and returns a rectangular Polygon that encompasses all vertices.',
execute: function(params) {
var result = turf.envelope.apply(null, params);
return result;
}
},

explode: {
Expand All @@ -170,6 +247,10 @@ var dropchop = (function(dc) {
['Feature<>', 'FeatureCollection<>']
],
description: 'Takes a feature or set of features and returns all positions as points.',
execute: function(params) {
var result = turf.explode.apply(null, params);
return result;
}
},

midpoint: {
Expand All @@ -183,6 +264,10 @@ var dropchop = (function(dc) {
['Feature<Point>']
],
description: 'Takes two points and returns a point midway between them.',
execute: function(params) {
var result = turf.midpoint.apply(null, params);
return result;
}
},

simplify: {
Expand All @@ -209,7 +294,28 @@ var dropchop = (function(dc) {
description: 'whether or not to spend more time to create a higher-quality simplification with a different algorithm',
default: false
}
]
],
execute: function(params) {
var result = {};
var newParams = params;

// if there is a feature collection, do it recursively
if (params[0].type === 'FeatureCollection') {
result.type = 'FeatureCollection';
result.features = [];
var features = params[0].features;
var len = features.length;
for (var f = 0; f < len; f++) {
newParams[0] = features[f];
var feature = turf.simplify.apply(null, newParams);
result.features.push(feature);
}
// otherwise it's a single
} else {
result = turf.simplify.apply(null, params);
}
return result;
}
},

tin: {
Expand All @@ -223,6 +329,10 @@ var dropchop = (function(dc) {
['FeatureCollection<>']
],
description: 'Triangulated irregular network, interpolation method',
execute: function(params) {
var result = turf.tin.apply(null, params);
return result;
}
},

union: {
Expand All @@ -236,6 +346,10 @@ var dropchop = (function(dc) {
['Feature<Polygon>']
],
description: 'Takes two polygons and returns a combined polygon. If the input polygons are not contiguous, this function returns a MultiPolygon feature.',
execute: function(params) {
var result = turf.union.apply(null, params);
return result;
}
},

within: {
Expand All @@ -255,7 +369,11 @@ var dropchop = (function(dc) {
type: 'switch'
// default: dc.selection.list[0]
}
]
],
execute: function(params) {
var result = turf.within.apply(null, params);
return result;
}
}
};

Expand Down

0 comments on commit 3d654d2

Please sign in to comment.