UNPKG

dufour-peyton-intersection

Version:

Reference Implementation of the Dufour-Peyton Intersection Algorithm. Calculates the Intersections of Arbitrary Polygons with a Geospatial Raster.

2 lines 8.56 kB
(()=>{var t={44:(t,e,r)=>{"use strict";var n;const o=r(813),s=r(727),i=r(130),c=r(288),a=r(568),u=r(534),l=r(682),f=r(560),h=r(496),p=r(714),g=r(973),x=r(942),d=r(908),y=r(389),m=r(426),w=r(963),_=r(628),v=r(233),A={calculate:a,calculateCore:u,carveHoles:w,categorizeIntersection:o,clamp:s,cluster:i,clusterLineSegments:c,couple:l,eachPair:f,eachPolygon:h,mergeRanges:p,mergeConsecutiveRanges:g,partition:x,prepareSnap:d,rangeContains:y,rangeCut:m,rangeOverlap:_,rangeSort:r(224),roundDown:v};void 0===(n=(()=>A).call(e,r,e,t))||(t.exports=n),t.exports=A,"object"==typeof self&&(self.dufour_peyton_intersection=A),"object"==typeof window&&(window.dufour_peyton_intersection=A)},123:t=>{t.exports=function(t){const e=t=>Array.isArray(t)||t instanceof Int8Array||t instanceof Uint8Array||t instanceof Uint8ClampedArray||t instanceof Int16Array||t instanceof Uint16Array||t instanceof Int32Array||t instanceof Uint32Array||t instanceof Float32Array||t instanceof Float64Array||t instanceof BigInt64Array||t instanceof BigUint64Array;let r=0,n=t;for(;e(n);)r++,n=n[0];return r}},130:t=>{"use strict";t.exports=function(t,e){try{const r=t.length,n=[];let o=[];for(let s=0;s<r;s++){const r=t[s];o.push(r),e(r)&&(n.push(o),o=[])}return o.length>0&&n.push(o),n}catch(t){console.error("[cluster]:",t)}}},199:(t,e,r)=>{"use strict";const n=r(534),o=r(924),s=r(973),i=r(224);t.exports=function({debug_level:t=0,raster_bbox:e,raster_height:r,raster_width:c,pixel_height:a,pixel_width:u,ring:l}){const[f,h,p,g]=e;if(e.every((t=>"string"==typeof t)))throw new Error("[dufour-peyton-intersection] raster_bbox should be all numbers, not strings");null==a&&(a=(g-h)/r),null==u&&(u=(p-f)/c);const x=[];for(let t=0;t<r;t++)x.push([]);n({debug_level:t,raster_bbox:e,raster_height:r,raster_width:c,pixel_height:a,pixel_width:u,ring:l,per_row_segment:({row:t,columns:e})=>{x[t].push(e)}});for(let t=0;t<x.length;t++){const e=x[t];e&&(i(e),x[t]=s(e))}return t>=2&&o(x),x}},224:(t,e,r)=>{const n=r(341);t.exports=function(t){return t.sort(n)}},233:t=>{"use strict";t.exports=function(t){return-1*Math.round(-1*t)}},288:(t,e,r)=>{"use strict";const n=r(130);t.exports=function(t,e,r=!1){try{const e=n(t,(t=>t.endsOffLine));if(e.length>=2){const t=e[0],r=t[0],n=e[e.length-1],o=n[n.length-1];o.last_edge_in_ring&&0===r.index&&o.endsOnLine&&(e[0]=e.pop().concat(t))}return e}catch(t){console.error("[clusterLineSegments]",t)}}},341:t=>{t.exports=function(t,e){return Math.sign(t[0]-e[0])||Math.sign(t[1]-e[1])}},389:t=>{t.exports=function(t,e){return e[0]>=t[0]&&e[1]<=t[1]}},426:(t,e,r)=>{const n=r(628);t.exports=function(t,e){if(!Array.isArray(t))throw new Error("[cut] range is not an array: "+JSON.stringify(t));if(void 0===e)return[t];if(!Array.isArray(e))throw new Error("[cut] hole is not an array:"+JSON.stringify(e));if(!n(t,e))return[t];const r=[];return t[0]<e[0]&&r.push([t[0],e[0]-1]),t[1]>e[1]&&r.push([e[1]+1,t[1]]),r}},496:(t,e,r)=>{"use strict";const n=r(123);t.exports=function t(e,r){if("FeatureCollection"===e.type)e.features.forEach((e=>t(e,r)));else if("GeometryCollection"===e.type)e.geometries.forEach((e=>t(e,r)));else if("Feature"===e.type)t(e.geometry,r);else if("Polygon"===e.type)t(e.coordinates,r);else if("MultiPolygon"===e.type)e.coordinates.forEach((t=>{r(t)}));else if(Array.isArray(e)){const t=n(e);4===t?e.forEach((t=>{r(t)})):3===t?r(e):2===t&&r([e])}}},534:(t,e,r)=>{"use strict";const n=r(813),o=r(727),s=r(682),i=r(288),c=r(560),a=r(714),u=r(942),l=r(908);t.exports=function({debug_level:t=0,raster_bbox:e,raster_height:r,raster_width:f,pixel_height:h,pixel_width:p,ring:g,per_pixel:x,per_row_segment:d}){const[y,m,w,_]=e,v=[];for(let t=0;t<r;t++)v.push([]);c(g,((t,n)=>{const[s,i]=t,[c,a]=s,[u,l]=i,f=Math.sign(l-a),p=a===l,x=c===u,d=(l-a)/(u-c),y=a,m=Math.min(a,l),w=Math.max(a,l);let A,b,E,M;if(c<u?([A,b]=s,[M,E]=i):([A,b]=i,[M,E]=s),void 0===A)throw Error("startLng is "+A);const L=Math.round((e[3]-.5*h-b)/h),O=Math.round((e[3]-.5*h-E)/h);let I,C;L<O?(I=L,C=O):(I=O,C=L),I=o(I,0,r-1),C=o(C,0,r-1);for(let e=I;e<C+1;e++){const r=_-h*e-h/2,o=a===r,s=l===r,i=!s;let b,E;if(p){if(y!==r)continue;b=A,E=M}else if(x)r>=m&&r<=w&&(b=A,E=M);else if(o)b=E=c;else if(s)b=E=u;else try{b=E=c+(r-a)/d}catch(t){throw t}void 0!==b&&void 0!==E&&(p||b>=A&&E<=M&&r<=w&&r>=m)&&v[e].push({direction:f,index:n,edge:t,last_edge_in_ring:n===g.length-2,endsOnLine:s,endsOffLine:i,horizontal:p,startsOnLine:o,vertical:x,xmin:b,xmax:E,imageLineY:r})}}));const A=p/2,b=l(y,p);t>=1&&console.log("[dufour-peyton-intersection] intersectionsByRow:",v),v.forEach(((e,r)=>{if(e.length>0){const o=i(e).map(n),[c,l]=u(o,(t=>t.through));if(c.length%2==1)throw t>=1&&console.error("[dufour-peyton-intersection] throughs:",JSON.stringify(c)),Error("throughs.length for "+r+" is odd with "+c.length);let h=l.map((t=>[t.xmin,t.xmax]));c.sort(((t,e)=>t.xmin-e.xmin));const p=s(c).map((t=>{const[e,r]=t;return[e.xmin,r.xmax]}));h=h.concat(p),h=a(h),h.forEach((t=>{const[e,n]=t;if(n-e<A)return;const[o,s]=b(t);if(o===s)return;if(o>f)return;if(s<=0)return;const i=Math.max(o,0),c=Math.min(s-1,f-1);if(d&&d({row:r,columns:[i,c]}),x)for(let t=i;t<=c;t++)x({row:r,column:t})}))}}))}},560:t=>{"use strict";t.exports=function(t,e){for(let r=1;r<t.length;r++)e([t[r-1],t[r]],r-1)}},568:(t,e,r)=>{"use strict";const n=r(990),o=r(973),s=r(224),i=r(699);t.exports=function({geometry:t,raster_height:e,per_pixel:r,per_row_segment:c,debug_level:a=0,...u}){const l=n(t).map((t=>i({polygon:t,raster_height:e,debug_level:a,...u})));a>=1&&console.log("[dufour-peyton-intersection] inside_rows_by_polygon:",l);const f=new Array(e);for(let t=0;t<e;t++){const e=l.map((e=>e[t])).filter((t=>void 0!==t&&t.length>0)).flat();if(e.length>0){const r=s(e),n=o(r);f[t]=n}}return(c||r)&&f.forEach(((t,e)=>{t&&t.forEach(((t,n)=>{if(c&&c({row:e,columns:t}),r){const[n,o]=t;for(let t=n;t<=o;t++)r({row:e,column:t})}}))})),{rows:f}}},628:t=>{t.exports=function(t,e){if(!Array.isArray(t))throw new Error("[overlaps] a is not an array");if(!Array.isArray(e))throw new Error("[overlaps] b is not an array");return t[0]<=e[1]&&e[0]<=t[1]}},682:t=>{"use strict";t.exports=function(t){const e=[],r=t.length;for(let n=0;n<r;n+=2)e.push([t[n],t[n+1]]);return e}},699:(t,e,r)=>{"use strict";const n=r(963),o=r(199);t.exports=function({polygon:t,...e}){const{debug_level:r=0}=e,[s,...i]=t.map((t=>o({ring:t,...e})));return r>=2&&console.log("[dufour-peyton-intersection] exterior:",s),s.map(((t,e)=>{const r=i.map((t=>t[e])).flat();return n(t,r)}))}},714:t=>{t.exports=function(t){const e=t.length;if(e>0){t.sort(((t,e)=>t[0]-e[0]||t[1]-e[1]));const r=[];let[n,o]=t[0];for(let s=1;s<e;s++){const[e,i]=t[s];e<=o?o=Math.max(o,i):(r.push([n,o]),n=e,o=i)}return r.push([n,o]),r}}},727:t=>{"use strict";t.exports=function(t,e,r){return t<e?e:t>r?r:t}},813:t=>{"use strict";t.exports=function(t){try{let e,r,n;const o=t.length,s=t[0];if(1===o)e=!0,r=s.xmin,n=s.xmax;else{const i=t[o-1];e=s.direction===i.direction,r=Math.min(s.xmin,i.xmin),n=Math.max(s.xmax,i.xmax)}if(void 0===r||void 0===n||void 0===e||isNaN(r)||isNaN(n))throw Error("categorizeIntersection failed with xmin",r,"and xmax",n);return{xmin:r,xmax:n,through:e}}catch(e){throw console.error("[categorizeIntersection] segments:",t),console.error("[categorizeIntersection]",e),e}}},908:(t,e,r)=>{"use strict";const n=r(233);t.exports=function(t,e){return([r,o])=>(-0===(r=n((r-t)/e))&&(r=0),-0===(o=Math.round((o-t)/e))&&(o=0),[r,o])}},924:t=>{"use strict";t.exports=function(t){t.forEach(((t,e)=>{for(let r=0;r<t.length;r++){const n=t[r],[o,s]=n;o>s&&console.warn("[dufour-peyton-intersection] uh oh, encountered invalid range",n,"at row index",e,"with ranges",t);for(let n=r+1;n<t.length;n++)n[0]<=s&&console.warn("[dufour-peyton-intersection] encountered range problem on row index",e,":",t)}}))}},942:t=>{"use strict";t.exports=function(t,e){const r=[],n=[],o=t.length;for(let s=0;s<o;s++){const o=t[s];e(o)?r.push(o):n.push(o)}return[r,n]}},963:(t,e,r)=>{const n=r(426);t.exports=function(t,e){let r=t;return e.forEach((t=>{r=r.map((e=>n(e,t))).flat()})),r}},973:t=>{t.exports=function(t){const e=t.length;if(0===e)return[];const r=t[0];let n=r[1];const o=[r];for(let r=1;r<e;r++){const e=t[r],[s,i]=e;s<=n+1?o[o.length-1][1]=Math.max(n,i):o.push(e),n=i}return o}},990:(t,e,r)=>{"use strict";const n=r(496);t.exports=function(t){const e=[];return n(t,(t=>e.push(t))),e}}},e={};!function r(n){var o=e[n];if(void 0!==o)return o.exports;var s=e[n]={exports:{}};return t[n](s,s.exports,r),s.exports}(44)})(); //# sourceMappingURL=dufour-peyton-intersection.min.js.map