Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created January 14, 2015 17:02
Show Gist options
  • Select an option

  • Save morganherlocker/d662d4f87468c9a67b20 to your computer and use it in GitHub Desktop.

Select an option

Save morganherlocker/d662d4f87468c9a67b20 to your computer and use it in GitHub Desktop.
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.simplify=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"/Users/morgan/Documents/projects/turfjs/turf/turf_modules/turf-simplify/index.js":[function(require,module,exports){
var simplify = require('simplify-js');
/**
* Simplifies a {@link Feature} containing a {@link LineString} or
* {@link Polygon} geometry. Internally uses [simplify-js](http://mourner.github.io/simplify-js/)
* to perform simplification.
*
* @module turf/simplify
* @param {Feature} feature - a feature to be simplified
* @param {number} tolerance - simplification tolerance
* @param {boolean} highQuality - whether or not to spend more time to create
* a higher-quality simplification with a different algorithm
* @return {Feature} output
* @example
* var quantization = 50;
* var minimumArea = 0;
* var simplified = turf.simplify(polys, quantization, minimumArea);
*/
module.exports = function(feature, tolerance, highQuality){
if(feature.geometry.type === 'LineString') {
var line = {
type: 'LineString',
coordinates: []
};
var pts = feature.geometry.coordinates.map(function(coord) {
return {x: coord[0], y: coord[1]};
});
line.coordinates = simplify(pts, tolerance, highQuality).map(function(coords){
return [coords.x, coords.y];
});
return simpleFeature(line, feature.properties);
} else if(feature.geometry.type === 'Polygon') {
var poly = {
type: 'Polygon',
coordinates: []
};
feature.geometry.coordinates.forEach(function(ring){
var pts = ring.map(function(coord) {
return {x: coord[0], y: coord[1]};
});
var simpleRing = simplify(pts, tolerance, highQuality).map(function(coords){
return [coords.x, coords.y];
});
poly.coordinates.push(simpleRing);
});
return simpleFeature(poly, feature.properties)
}
}
function simpleFeature (geom, properties) {
return {
type: 'Feature',
geometry: geom,
properties: properties
};
}
},{"simplify-js":"/Users/morgan/Documents/projects/turfjs/turf/turf_modules/turf-simplify/node_modules/simplify-js/simplify.js"}],"/Users/morgan/Documents/projects/turfjs/turf/turf_modules/turf-simplify/node_modules/simplify-js/simplify.js":[function(require,module,exports){
/*
(c) 2013, Vladimir Agafonkin
Simplify.js, a high-performance JS polyline simplification library
mourner.github.io/simplify-js
*/
(function () { 'use strict';
// to suit your point format, run search/replace for '.x' and '.y';
// for 3D version, see 3d branch (configurability would draw significant performance overhead)
// square distance between 2 points
function getSqDist(p1, p2) {
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
return dx * dx + dy * dy;
}
// square distance from a point to a segment
function getSqSegDist(p, p1, p2) {
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
if (dx !== 0 || dy !== 0) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = p2.x;
y = p2.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.x - x;
dy = p.y - y;
return dx * dx + dy * dy;
}
// rest of the code doesn't care about point format
// basic distance-based simplification
function simplifyRadialDist(points, sqTolerance) {
var prevPoint = points[0],
newPoints = [prevPoint],
point;
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
}
if (prevPoint !== point) newPoints.push(point);
return newPoints;
}
// simplification using optimized Douglas-Peucker algorithm with recursion elimination
function simplifyDouglasPeucker(points, sqTolerance) {
var len = points.length,
MarkerArray = typeof Uint8Array !== 'undefined' ? Uint8Array : Array,
markers = new MarkerArray(len),
first = 0,
last = len - 1,
stack = [],
newPoints = [],
i, maxSqDist, sqDist, index;
markers[first] = markers[last] = 1;
while (last) {
maxSqDist = 0;
for (i = first + 1; i < last; i++) {
sqDist = getSqSegDist(points[i], points[first], points[last]);
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
if (maxSqDist > sqTolerance) {
markers[index] = 1;
stack.push(first, index, index, last);
}
last = stack.pop();
first = stack.pop();
}
for (i = 0; i < len; i++) {
if (markers[i]) newPoints.push(points[i]);
}
return newPoints;
}
// both algorithms combined for awesome performance
function simplify(points, tolerance, highestQuality) {
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
return points;
}
// export as AMD module / Node module / browser or worker variable
if (typeof define === 'function' && define.amd) define(function() { return simplify; });
else if (typeof module !== 'undefined') module.exports = simplify;
else if (typeof self !== 'undefined') self.simplify = simplify;
else window.simplify = simplify;
})();
},{}]},{},["/Users/morgan/Documents/projects/turfjs/turf/turf_modules/turf-simplify/index.js"])("/Users/morgan/Documents/projects/turfjs/turf/turf_modules/turf-simplify/index.js")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment