simplify-geojson-visvalingam
Version:
GeoJSON simplification library using the Visvalingam algorithm for efficient point reduction and geometry preservation.
2 lines • 6.59 kB
JavaScript
;var v=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var _=(e,r)=>{for(var t in r)v(e,t,{get:r[t],enumerable:!0})},B=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let l of $(r))!q.call(e,l)&&l!==t&&v(e,l,{get:()=>r[l],enumerable:!(n=k(r,l))||n.enumerable});return e};var z=e=>B(v({},"__esModule",{value:!0}),e);var X={};_(X,{default:()=>H});module.exports=z(X);function A(e){return e!=null&&Number.isFinite(e)&&e>0}function a(e){return typeof e=="object"&&e!==null}function F(e){return a(e)&&e?.type==="Feature"}function h(e){return a(e)&&e?.type==="FeatureCollection"}function U(e){return a(e)&&e?.type==="LineString"}function G(e){return a(e)&&e?.type==="MultiLineString"}function g(e){return a(e)&&e?.type==="Polygon"}function w(e){return a(e)&&e?.type==="MultiPolygon"}function O(e){return a(e)&&e?.type==="GeometryCollection"}function M(e){let r=new Uint32Array(e.length),t=new Uint32Array(e.length),n=new Uint32Array(e.length);for(let l=0;l<e.length;l++)r[l]=l;r.sort((l,u)=>e[l][0]-e[u][0]||e[l][1]-e[u][1]),e.length&&(n[r[e.length-1]]=n.length-1);for(let l=1,u=e.length-2;l<e.length;l++,u--)e[r[l]][0]!==e[r[l-1]][0]||e[r[l]][1]!==e[r[l-1]][1]?t[r[l]]=l:t[r[l]]=t[r[l-1]],e[r[u]][0]!==e[r[u+1]][0]||e[r[u]][1]!==e[r[u+1]][1]?n[r[u]]=u:n[r[u]]=n[r[u+1]];return{sortIndexes:r,groupedFrom:t,groupedTo:n}}function H(e,r={}){if(!a(e))throw new TypeError(`Expected provided GeoJSON to be an object, but received ${e===null?"null":typeof e}.`);let t=0;if(r.tolerance!==void 0){if(!A(r.tolerance))throw new Error(`Expected provided tolerance to be a finite positive number, but received ${r.tolerance}.`);t=r.tolerance}let n=0;if(r.fraction!==void 0){if(!A(r.fraction))throw new Error(`Expected provided fraction to be a finite positive number, but received ${r.fraction}.`);if(r.fraction>1)throw new Error(`Expected provided fraction to be less or equal to 1, but received ${r.fraction}.`);n=r.fraction}let l=!0;if(r.mutate!==void 0&&(l=!!r.mutate),t===0&&n===0)return e;l||(e=structuredClone(e));let u={coordinates:[],nextIndexes:[],prevIndexes:[]};return P(e,u),u.coordinates.length&&I(e,V(u,M(u.coordinates),t,n),0),e}function J(e,r){return Math.abs(r.coordinates[e][0]*(r.coordinates[r.prevIndexes[e]][1]-r.coordinates[r.nextIndexes[e]][1])+r.coordinates[r.prevIndexes[e]][0]*(r.coordinates[r.nextIndexes[e]][1]-r.coordinates[e][1])+r.coordinates[r.nextIndexes[e]][0]*(r.coordinates[e][1]-r.coordinates[r.prevIndexes[e]][1]))/2}function S(e,r,t,n){return t[e]-t[r]||n[e][0]-n[r][0]||n[e][1]-n[r][1]}function C(e,r,t,n,l){let u,o,i;for(;i=t,(u=t<<1)<=e[0]&&S(e[i],e[u],n,l)>0&&(i=u),(o=u+1)<=e[0]&&S(e[i],e[o],n,l)>0&&(i=o),t!==i;)L(e,r,t,i),t=i;return t}function K(e,r,t,n,l){let u;for(;t>1&&(u=t>>>1,!(t===u||S(e[u],e[t],n,l)<=0));)L(e,r,t,u),t=u;return t}function p(e,r,t,n,l){K(e,r,C(e,r,r[t],n,l),n,l)}function L(e,r,t,n){r[e[t]]=n,r[e[n]]=t,[e[t],e[n]]=[e[n],e[t]]}function Q(e,r,t,n){return e[0]>1?(L(e,r,1,e[0]),e[0]--,C(e,r,1,t,n),e[e[0]+1]):e[0]===1?(e[0]--,e[1]):-1}function R(e,r,t,n){for(let l=e[0]>>>1;l>0;l--)C(e,r,l,t,n)}function V(e,r,t,n){let l=e.coordinates.length,u=new Uint8Array(l),o=Math.round(l*n),i=new Uint32Array(l),b=new Uint8Array(l),m=new Uint32Array(l+1),d=new Float64Array(l),x=new Uint32Array(l);for(let f=0;f<l;f++)e.prevIndexes[f]!==-1&&e.nextIndexes[f]!==-1&&(d[f]=J(f,e),m[0]++,m[m[0]]=f,x[f]=m[0]);R(m,x,d,e.coordinates);let c;for(;m[0];)if(c=Q(m,x,d,e.coordinates),!u[c]){if(d[c]>=t&&o<=0)break;if(b[c]||(b[c]=1,i[r.groupedFrom[c]]++),i[r.groupedFrom[c]]===r.groupedTo[c]-r.groupedFrom[c]+1)for(let f,y=r.groupedFrom[c];y<=r.groupedTo[c];y++)f=r.sortIndexes[y],!u[f]&&(e.prevIndexes[f]!==-1&&e.prevIndexes[e.prevIndexes[f]]!==-1&&e.prevIndexes[e.prevIndexes[e.prevIndexes[f]]]===f?(b[e.prevIndexes[f]]||(b[e.prevIndexes[f]]=1,i[r.groupedFrom[e.prevIndexes[f]]]++),b[e.nextIndexes[f]]||(b[e.nextIndexes[f]]=1,i[r.groupedFrom[e.nextIndexes[f]]]++),i[r.groupedFrom[e.prevIndexes[f]]]===r.groupedTo[e.prevIndexes[f]]-r.groupedFrom[e.prevIndexes[f]]+1&&i[r.groupedFrom[e.nextIndexes[f]]]===r.groupedTo[e.nextIndexes[f]]-r.groupedFrom[e.nextIndexes[f]]+1&&(u[f]=1,u[e.nextIndexes[f]]=1,u[e.prevIndexes[f]]=1,o-=3,y=r.groupedFrom[c]-1)):(u[f]=1,o--,e.prevIndexes[e.nextIndexes[f]]=e.prevIndexes[f],e.nextIndexes[e.prevIndexes[f]]=e.nextIndexes[f],e.prevIndexes[e.prevIndexes[f]]!==-1&&(d[e.prevIndexes[f]]=J(e.prevIndexes[f],e),p(m,x,e.prevIndexes[f],d,e.coordinates)),e.nextIndexes[e.nextIndexes[f]]!==-1&&(d[e.nextIndexes[f]]=J(e.nextIndexes[f],e),p(m,x,e.nextIndexes[f],d,e.coordinates))))}return u}function I(e,r,t){if(h(e))for(let n=0;n<e.features.length;n++)t=I(e.features[n],r,t);else if(F(e))t=I(e.geometry,r,t);else if(O(e))for(let n=0;n<e.geometries.length;n++)t=I(e.geometries[n],r,t);else if(U(e))t=s(e.coordinates,r,t);else if(G(e))for(let n=0;n<e.coordinates.length;n++)t=s(e.coordinates[n],r,t);else if(g(e)){let n=!1;[t,n]=E(e.coordinates,r,t),n&&e.coordinates.splice(0)}else if(w(e)){let n=0;for(let l=!1,u=0;u<e.coordinates.length;u++)[t,l]=E(e.coordinates[u],r,t),l?n++:n&&(e.coordinates[u-n]=e.coordinates[u]);n&&e.coordinates.splice(e.coordinates.length-n)}return t}function s(e,r,t){let n=0;for(let l=0;l<e.length;l++)r[t]?n++:n&&(e[l-n]=e[l]),t++;return n&&e.splice(e.length-n),t}function E(e,r,t){let n=0;for(let l=!1,u=0;u<e.length;u++)if([t,l]=W(e[u],r,t),l){if(u===0){for(u=1;u<e.length;u++)e[u].length&&(t+=e[u].length-1);return[t,!0]}n++}else n&&(e[u-n]=e[u]);return n&&e.splice(e.length-n),[t,!1]}function W(e,r,t){let n=0;for(let l=0;l<e.length-1;l++)r[t]?n++:n&&(e[l-n]=e[l]),t++;if(n){if(e.length-1-n<3)return[t,!0];e.splice(e.length-1-n,n+1,e[0])}return[t,!1]}function P(e,r){if(h(e))for(let t=0;t<e.features.length;t++)P(e.features[t],r);else if(F(e))P(e.geometry,r);else if(O(e))for(let t=0;t<e.geometries.length;t++)P(e.geometries[t],r);else if(U(e))T(e.coordinates,r);else if(G(e))for(let t=0;t<e.coordinates.length;t++)T(e.coordinates[t],r);else if(g(e))for(let t=0;t<e.coordinates.length;t++)N(e.coordinates[t],r);else if(w(e))for(let t,n=0;n<e.coordinates.length;n++)for(t=0;t<e.coordinates[n].length;t++)N(e.coordinates[n][t],r)}function T(e,r){for(let t=r.coordinates.length,n=e.length,l=0;l<n;l++)r.coordinates.push(e[l]),r.prevIndexes.push(l===0?-1:t+l-1),r.nextIndexes.push(l===n-1?-1:t+l+1)}function N(e,r){for(let t=r.coordinates.length,n=e.length-1,l=0;l<n;l++)r.coordinates.push(e[l]),r.prevIndexes.push(l===0?t+n-1:t+l-1),r.nextIndexes.push(l===n-1?t:t+l+1)}
//# sourceMappingURL=index.cjs.map