simplify-geojson-visvalingam
Version:
GeoJSON simplification library using the Visvalingam algorithm for efficient point reduction and geometry preservation.
2 lines • 6.57 kB
JavaScript
;var P=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var q=(e,r)=>{for(var t in r)P(e,t,{get:r[t],enumerable:!0})},_=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let l of $(r))!p.call(e,l)&&l!==t&&P(e,l,{get:()=>r[l],enumerable:!(n=k(r,l))||n.enumerable});return e};var B=e=>_(P({},"__esModule",{value:!0}),e);var X={};q(X,{default:()=>z});module.exports=B(X);function A(e){return e!=null&&Number.isFinite(e)&&e>0}function d(e){return typeof e=="object"&&e!==null}function F(e){return d(e)&&e?.type==="Feature"}function h(e){return d(e)&&e?.type==="FeatureCollection"}function U(e){return d(e)&&e?.type==="LineString"}function G(e){return d(e)&&e?.type==="MultiLineString"}function w(e){return d(e)&&e?.type==="Polygon"}function g(e){return d(e)&&e?.type==="MultiPolygon"}function O(e){return d(e)&&e?.type==="GeometryCollection"}function z(e,r={}){if(!d(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 v(e,u),u.coordinates.length&&I(e,V(u,H(u.coordinates),t,n),0),e}function H(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]),n[0]=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 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,b,i;for(;i=t,(u=t<<1)<=e[0]&&S(e[i],e[u],n,l)>0&&(i=u),(b=u+1)<=e[0]&&S(e[i],e[b],n,l)>0&&(i=b),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 M(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),b=Math.round(l*n),i=new Uint32Array(l),o=new Uint8Array(l),a=new Uint32Array(l+1),m=new Float64Array(l),x=new Uint32Array(l);for(let f=0;f<l;f++)e.prevIndexes[f]!==-1&&e.nextIndexes[f]!==-1&&(m[f]=J(f,e),a[0]++,a[a[0]]=f,x[f]=a[0]);R(a,x,m,e.coordinates);let c;for(;a[0];)if(c=Q(a,x,m,e.coordinates),!u[c]){if(m[c]>=t&&b<=0)break;if(o[c]||(o[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?(o[e.prevIndexes[f]]||(o[e.prevIndexes[f]]=1,i[r.groupedFrom[e.prevIndexes[f]]]++),o[e.nextIndexes[f]]||(o[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,b--,u[e.nextIndexes[f]]=1,u[e.prevIndexes[f]]=1,y=r.groupedFrom[c]-1)):(u[f]=1,b--,e.prevIndexes[e.nextIndexes[f]]=e.prevIndexes[f],e.nextIndexes[e.prevIndexes[f]]=e.nextIndexes[f],e.prevIndexes[e.prevIndexes[f]]!==-1&&(m[e.prevIndexes[f]]=J(e.prevIndexes[f],e),M(a,x,e.prevIndexes[f],m,e.coordinates)),e.nextIndexes[e.nextIndexes[f]]!==-1&&(m[e.nextIndexes[f]]=J(e.nextIndexes[f],e),M(a,x,e.nextIndexes[f],m,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=E(e.coordinates,r,t);else if(G(e))for(let n=0;n<e.coordinates.length;n++)t=E(e.coordinates[n],r,t);else if(w(e)){let n=!1;[t,n]=T(e.coordinates,r,t),n&&e.coordinates.splice(0)}else if(g(e)){let n=0;for(let l=!1,u=0;u<e.coordinates.length;u++)[t,l]=T(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 E(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 T(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 v(e,r){if(h(e))for(let t=0;t<e.features.length;t++)v(e.features[t],r);else if(F(e))v(e.geometry,r);else if(O(e))for(let t=0;t<e.geometries.length;t++)v(e.geometries[t],r);else if(U(e))s(e.coordinates,r);else if(G(e))for(let t=0;t<e.coordinates.length;t++)s(e.coordinates[t],r);else if(w(e))for(let t=0;t<e.coordinates.length;t++)N(e.coordinates[t],r);else if(g(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 s(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