@rr0/ufoathome 0.108.1 → 0.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import{I as E,S as A,O}from"./rr0-sighting-editor.mjs";class M{constructor(t,n,o,e){this.heights=t,this.building=n,this.width=o,this.height=e}static ROOF_PERCENTILE=.9;static STEP_M=.9;static RECTANGLE_FILL=.72;static MIN_DEPTH_M=2.5;static MAX_PARTS=24;static MAX_CELLS=640;buildings(t){const n=t.metresPerPixel,o=t.minAreaM2??12,e=new Uint8Array(this.width*this.height),r=[];for(let a=0;a<this.building.length;a++){if(!this.building[a]||e[a])continue;const s=this.patchFrom(a,e);if(!(s.length*n*n<o))for(const i of this.buildingsOf(s,n,o))Math.hypot(i.eastM,i.northM)<=t.radiusM&&r.push(i)}return r.sort((a,s)=>Math.hypot(a.eastM,a.northM)-Math.hypot(s.eastM,s.northM)),r.slice(0,t.maxBuildings??60)}patchFrom(t,n){const o=[],e=[t];for(n[t]=1;e.length>0;){const r=e.pop();o.push(r);const a=r%this.width,s=Math.floor(r/this.width);for(const[i,h]of[[1,0],[-1,0],[0,1],[0,-1]]){const c=a+i,u=s+h;if(c<0||u<0||c>=this.width||u>=this.height)continue;const l=u*this.width+c;this.building[l]&&!n[l]&&Math.abs(this.heights[l]-this.heights[r])<=M.STEP_M&&(n[l]=1,e.push(l))}}return o}buildingsOf(t,n,o){const e=this.width/2,r=this.height/2,a=[],s=[],i=[];for(const h of t){const c=h%this.width,u=Math.floor(h/this.width);a.push((c+.5-e)*n),s.push((r-u-.5)*n),i.push(this.heights[h])}return M.ofPoints(a,s,i,n,o)}static ofPoints(t,n,o,e,r,a=M.RECTANGLE_FILL,s=[]){const i=[];for(let g=0;g<t.length;g++)for(const[v,w]of[[-.5,-.5],[.5,-.5],[-.5,.5],[.5,.5]])i.push([t[g]+v*e,n[g]+w*e]);const h=M.hull(i),{angle:c,rectangle:u}=M.smallestRectangle(h),l=t.length*e*e,f=(u.maxU-u.minU)*(u.maxV-u.minV);if(l/f>=a||l<4*r)return[M.building(u,c,M.roof(o))];const d=h.map((g,v)=>({from:{east:g[0],north:g[1]},to:{east:h[(v+1)%h.length][0],north:h[(v+1)%h.length][1]}})),m=M.dominantAngles([...s,...d]);return M.parts(t,n,o,m.length>0?m:[c],e,r)}static dominantAngles(t,n=3){const o=new Map;for(const e of t){const r=Math.hypot(e.to.east-e.from.east,e.to.north-e.from.north);if(r<1e-6)continue;const a=(Math.atan2(e.to.north-e.from.north,e.to.east-e.from.east)%(Math.PI/2)+Math.PI/2)%(Math.PI/2),s=Math.round(a/(Math.PI/60));o.set(s,(o.get(s)??0)+r)}return[...o.entries()].sort((e,r)=>r[1]-e[1]).slice(0,n).map(([e])=>e*Math.PI/60)}static parts(t,n,o,e,r,a){let s=t.map((h,c)=>c);const i=[];for(let h=0;h<M.MAX_PARTS&&s.length>0;h++){let c;for(const f of e){const d=M.largestIn(t,n,s,f,r);d&&(c===void 0||d.area>c.area)&&(c=d)}if(c===void 0||c.area<a)break;const u=c.covered;Math.min(c.rectangle.maxU-c.rectangle.minU,c.rectangle.maxV-c.rectangle.minV)>=M.MIN_DEPTH_M&&i.push(M.building(c.rectangle,c.angle,M.roof([...u].map(f=>o[f])))),s=s.filter(f=>!u.has(f))}return i}static largestIn(t,n,o,e,r){const a=Math.cos(e),s=Math.sin(e);let i=1/0,h=-1/0,c=1/0,u=-1/0;const l=[],f=[];for(const b of o){const P=t[b]*a+n[b]*s,x=-t[b]*s+n[b]*a;l.push(P),f.push(x),i=Math.min(i,P),h=Math.max(h,P),c=Math.min(c,x),u=Math.max(u,x)}const d=Math.max(r*Math.SQRT2,Math.max(h-i,u-c)/M.MAX_CELLS),m=Math.max(1,Math.ceil((h-i)/d+1e-9)),g=Math.max(1,Math.ceil((u-c)/d+1e-9)),v=new Uint8Array(m*g),w=o.map((b,P)=>{const x=Math.min(m-1,Math.max(0,Math.floor((l[P]-i)/d))),R=Math.min(g-1,Math.max(0,Math.floor((f[P]-c)/d)));return v[R*m+x]=1,R*m+x}),I=M.largestRectangle(v,m,g);if(I===void 0)return;const U=new Set;return o.forEach((b,P)=>{const x=w[P]%m,R=Math.floor(w[P]/m);x>=I.column&&x<I.column+I.columns&&R>=I.row&&R<I.row+I.rows&&U.add(b)}),{area:I.area*d*d,angle:e,covered:U,rectangle:{minU:i+I.column*d,maxU:i+(I.column+I.columns)*d,minV:c+I.row*d,maxV:c+(I.row+I.rows)*d}}}static largestRectangle(t,n,o){const e=new Array(n).fill(0);let r;for(let a=0;a<o;a++){for(let i=0;i<n;i++)e[i]=t[a*n+i]?e[i]+1:0;const s=[];for(let i=0;i<=n;i++){const h=i===n?0:e[i];for(;s.length>0&&e[s[s.length-1]]>=h;){const c=s.pop(),u=s.length===0?i:i-s[s.length-1]-1,l=e[c]*u;(r===void 0||l>r.area)&&(r={column:i-u,row:a-e[c]+1,columns:u,rows:e[c],area:l})}s.push(i)}}return r}static roof(t){const n=[...t].sort((o,e)=>o-e);return Math.max(2,n[Math.min(n.length-1,Math.floor(n.length*M.ROOF_PERCENTILE))])}static building(t,n,o){const e=Math.cos(n),r=Math.sin(n),a=t.maxU-t.minU,s=t.maxV-t.minV,i=(t.minU+t.maxU)/2,h=(t.minV+t.maxV)/2,u=((90-(a<s?n:n+Math.PI/2)*180/Math.PI)%180+180)%180;return{eastM:M.tenth(i*e-h*r),northM:M.tenth(i*r+h*e),widthM:M.tenth(Math.max(a,s)),lengthM:M.tenth(Math.min(a,s)),heightM:M.tenth(o),headingDeg:Math.round(u*10)/10}}static smallestRectangle(t){let n={area:1/0,angle:0,rectangle:{minU:0,maxU:0,minV:0,maxV:0}};for(let o=0;o<Math.max(1,t.length);o++){const[e,r]=t[o]??[0,0],[a,s]=t[(o+1)%t.length]??[1,0],i=Math.atan2(s-r,a-e),h=Math.cos(i),c=Math.sin(i);let u=1/0,l=-1/0,f=1/0,d=-1/0;for(const[g,v]of t){const w=g*h+v*c,I=-g*c+v*h;u=Math.min(u,w),l=Math.max(l,w),f=Math.min(f,I),d=Math.max(d,I)}const m=(l-u)*(d-f);m<n.area&&(n={area:m,angle:i,rectangle:{minU:u,maxU:l,minV:f,maxV:d}})}return n}static hull(t){const n=[...t].sort((s,i)=>s[0]-i[0]||s[1]-i[1]),o=n.filter((s,i)=>i===0||s[0]!==n[i-1][0]||s[1]!==n[i-1][1]);if(o.length<3)return o;const e=(s,i,h)=>(i[0]-s[0])*(h[1]-s[1])-(i[1]-s[1])*(h[0]-s[0]),r=[];for(const s of o){for(;r.length>=2&&e(r[r.length-2],r[r.length-1],s)<=0;)r.pop();r.push(s)}const a=[];for(const s of[...o].reverse()){for(;a.length>=2&&e(a[a.length-2],a[a.length-1],s)<=0;)a.pop();a.push(s)}return[...r.slice(0,-1),...a.slice(0,-1)]}static tenth(t){return Math.round(t*10)/10}}class y{constructor(t,n,o,e){this.heights=t,this.vegetation=n,this.width=o,this.height=e}static SUMMIT_RADIUS_M=2.5;static CROWN_REACH_M=8;static MAX_WIDTH_M=16;static MIN_WIDTH_M=2;static tenth(t){return Math.round(t*10)/10}trees(t){const n=t.metresPerPixel,o=t.minHeightM??4,e=t.eyeM??1.6,r=t.minElevationDeg??2,a=Math.max(1,Math.round(y.SUMMIT_RADIUS_M/n)),s=Math.round(y.CROWN_REACH_M/n),i=this.width/2,h=this.height/2,c=[];for(let u=0;u<this.height;u++)for(let l=0;l<this.width;l++){const f=this.canopy(l,u);if(f<o)continue;const d=(l+.5-i)*n,m=(h-u-.5)*n,g=Math.hypot(d,m);if(g>t.radiusM||!this.isSummit(l,u,f,a))continue;const v=Math.atan2(f-e,Math.max(g,1))*180/Math.PI;v<r||c.push({tree:{eastM:y.tenth(d),northM:y.tenth(m),heightM:y.tenth(f),widthM:y.tenth(this.crownWidth(l,u,f,n,s))},elevation:v})}return c.sort((u,l)=>l.elevation-u.elevation),c.slice(0,t.maxTrees??150).map(u=>u.tree)}canopy(t,n){if(t<0||n<0||t>=this.width||n>=this.height)return 0;const o=n*this.width+t;return this.vegetation[o]?this.heights[o]:0}isSummit(t,n,o,e){for(let r=-e;r<=e;r++)for(let a=-e;a<=e;a++){if(a===0&&r===0)continue;const s=this.canopy(t+a,n+r);if(s>o||s===o&&(r<0||r===0&&a<0))return!1}return!0}crownWidth(t,n,o,e,r){let a=0;for(let i=-r;i<=r;i++)for(let h=-r;h<=r;h++)Math.hypot(h,i)*e<=y.CROWN_REACH_M&&this.canopy(t+h,n+i)>=o*.5&&a++;const s=2*Math.sqrt(a*e*e/Math.PI);return Math.max(y.MIN_WIDTH_M,Math.min(s,y.MAX_WIDTH_M))}}class S{id="ign-lidar-hd";name="IGN LiDAR HD";attribution="Canopy and roofs © IGN, LiDAR HD (Licence Ouverte 2.0), as surveyed on a day the service does not give";creditUrl="https://geoservices.ign.fr/lidarhd";contemporary=!0;platform;constructor(t={}){this.platform=new E(t)}covers(t,n){return E.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","The LiDAR HD survey covers France only");const o=E.lidarRadius(n),e=await this.platform.lidar(t,o);return{trees:new y(e.heights,e.vegetation,e.size,e.size).trees({metresPerPixel:e.metresPerPixel,radiusM:o}),buildings:new M(e.heights,e.building,e.size,e.size).buildings({metresPerPixel:e.metresPerPixel,radiusM:o,maxBuildings:300}),bridges:[],lines:[]}}}class p{static METRES_PER_DEGREE=111320;static DEFAULT_HEIGHT_M=6;static FLOOR_HEIGHT_M=3;static FINE_M=.5;static COARSE_M=1;static MAX_CELLS=4e4;static RECTANGLE_FILL=.9;static buildings(t,n,o,e=12,r=300){const a=[];for(const s of t){const i=p.rings(s,n);if(i.length===0)continue;const h=Number(s.properties.nombre_d_etages),c=Number(s.properties.hauteur),u=Number.isFinite(c)&&c>0?c:Number.isFinite(h)&&h>0?h*p.FLOOR_HEIGHT_M:p.DEFAULT_HEIGHT_M;for(const l of p.rectanglesOf(i,u,e))Math.hypot(l.eastM,l.northM)>o||a.push(Number.isFinite(h)&&h>0?{...l,floors:h}:l)}return a.sort((s,i)=>Math.hypot(s.eastM,s.northM)-Math.hypot(i.eastM,i.northM)),a.slice(0,r)}static named(t,n,o){const e=n.map(r=>({name:typeof r.properties.toponyme=="string"?r.properties.toponyme:"",rings:p.rings(r,o)})).filter(r=>r.name.length>0&&r.rings.length>0);return e.length===0?t:t.map(r=>{const a=e.find(s=>p.inside(r.eastM,r.northM,s.rings));return a?{...r,name:a.name}:r})}static inside(t,n,o){let e=0;for(const r of o)for(let a=0;a+1<r.length;a++){const s=r[a],i=r[a+1];s.north<=n!=i.north<=n&&s.east+(n-s.north)/(i.north-s.north)*(i.east-s.east)>t&&e++}return e%2===1}static rings(t,n){const o=t.geometry,e=o.type==="Polygon"?[o.coordinates]:o.type==="MultiPolygon"?o.coordinates:[],r=Math.cos(n.lat*Math.PI/180),a=[];for(const s of e)for(const i of s)a.push(i.map(([h,c])=>({east:(h-n.lng)*p.METRES_PER_DEGREE*r,north:(c-n.lat)*p.METRES_PER_DEGREE})));return a}static rectanglesOf(t,n,o){let e=1/0,r=-1/0,a=1/0,s=-1/0;for(const l of t)for(const f of l)e=Math.min(e,f.east),r=Math.max(r,f.east),a=Math.min(a,f.north),s=Math.max(s,f.north);let i=p.FINE_M;(r-e)*(s-a)/(i*i)>p.MAX_CELLS&&(i=p.COARSE_M);const h=[],c=[];for(let l=a+i/2;l<s;l+=i){const f=[];for(const d of t)for(let m=0;m+1<d.length;m++){const g=d[m],v=d[m+1];g.north<=l!=v.north<=l&&f.push(g.east+(l-g.north)/(v.north-g.north)*(v.east-g.east))}f.sort((d,m)=>d-m);for(let d=0;d+1<f.length;d+=2)for(let m=Math.ceil((f[d]-i/2)/i)*i+i/2;m<f[d+1];m+=i)h.push(m),c.push(l)}if(h.length*i*i<o)return[];const u=t.flatMap(l=>l.slice(0,-1).map((f,d)=>({from:f,to:l[d+1]})));return M.ofPoints(h,c,new Array(h.length).fill(n),i,o,p.RECTANGLE_FILL,u)}}class T{id="ign-lidar-topo";name="IGN LiDAR HD + BD TOPO";attribution="Canopy © IGN, LiDAR HD; buildings, bridges and lines © IGN, BD TOPO (Licence Ouverte 2.0), as surveyed on days the services do not give";creditUrl="https://geoservices.ign.fr/lidarhd";contemporary=!0;platform;constructor(t={}){this.platform=new E(t)}covers(t,n){return E.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","The IGN's surveys cover France only");const o=E.radius(n),[e,r]=await Promise.allSettled([this.platform.lidar(t,o),this.platform.vectors(t,o,!0)]),a=E.lidarRadius(o);if(e.status==="rejected"&&r.status==="rejected")throw e.reason;const s=e.status==="fulfilled"?e.value:void 0,i=r.status==="fulfilled"?r.value:void 0;return{trees:s?new y(s.heights,s.vegetation,s.size,s.size).trees({metresPerPixel:s.metresPerPixel,radiusM:a}):[],buildings:this.buildings(s,i,t,o),bridges:i?O.bridges(i,t,o,s?.structures):[],lines:i?O.lines(i,t,o,s?.structures):[]}}buildings(t,n,o,e){const r=n?p.buildings(n.buildings,o,e):[],a=r.length>0||t===void 0?r:new M(t.heights,t.building,t.size,t.size).buildings({metresPerPixel:t.metresPerPixel,radiusM:E.lidarRadius(e),maxBuildings:300});return n?p.named(a,n.zones,o):a}}class H{id="ign-bd-topo";name="IGN BD TOPO";attribution="Buildings, bridges and lines © IGN, BD TOPO (Licence Ouverte 2.0), as drawn on a day the service does not give";creditUrl="https://geoservices.ign.fr/bdtopo";contemporary=!0;platform;constructor(t={}){this.platform=new E(t)}covers(t,n){return E.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","BD TOPO covers France only");const o=E.radius(n),e=await this.platform.vectors(t,o,!0);return{trees:[],buildings:p.named(p.buildings(e.buildings,t,o),e.zones,t),bridges:O.bridges(e,t,o,void 0),lines:O.lines(e,t,o,void 0)}}}const D=[{id:"ign-lidar-topo",name:"IGN LiDAR HD + BD TOPO",credit:"© IGN, LiDAR HD and BD TOPO, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/lidarhd",create:()=>new T},{id:"ign-lidar-hd",name:"IGN LiDAR HD",credit:"© IGN, LiDAR HD, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/lidarhd",create:()=>new S},{id:"ign-bd-topo",name:"IGN BD TOPO",credit:"© IGN, BD TOPO, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/bdtopo",create:()=>new H}];class L{static DEFAULT_ID=D[0].id;static NONE="none";static byId(t){return t===L.NONE?void 0:D.find(n=>n.id===t)??D[0]}static providerFor(t,n,o){const e=L.byId(t)?.create();return e?.covers(n,o)?e:void 0}}export{D as SURROUNDINGS_SOURCES,L as SurroundingsSources};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rr0/ufoathome",
3
3
  "type": "module",
4
- "version": "0.108.1",
4
+ "version": "0.109.0",
5
5
  "description": "UFO@home — record and replay a UFO sighting's shape, movement and appearance",
6
6
  "author": "Jérôme Beau <rr0@rr0.org> (https://rr0.org)",
7
7
  "license": "MIT",
@@ -1 +0,0 @@
1
- import{I as w,S as A,O}from"./rr0-sighting-editor.mjs";class M{constructor(t,n,a,e){this.heights=t,this.building=n,this.width=a,this.height=e}static ROOF_PERCENTILE=.9;static STEP_M=.9;static RECTANGLE_FILL=.72;static MIN_DEPTH_M=2.5;static MAX_PARTS=24;static MAX_CELLS=640;buildings(t){const n=t.metresPerPixel,a=t.minAreaM2??12,e=new Uint8Array(this.width*this.height),r=[];for(let o=0;o<this.building.length;o++){if(!this.building[o]||e[o])continue;const s=this.patchFrom(o,e);if(!(s.length*n*n<a))for(const i of this.buildingsOf(s,n,a))Math.hypot(i.eastM,i.northM)<=t.radiusM&&r.push(i)}return r.sort((o,s)=>Math.hypot(o.eastM,o.northM)-Math.hypot(s.eastM,s.northM)),r.slice(0,t.maxBuildings??60)}patchFrom(t,n){const a=[],e=[t];for(n[t]=1;e.length>0;){const r=e.pop();a.push(r);const o=r%this.width,s=Math.floor(r/this.width);for(const[i,h]of[[1,0],[-1,0],[0,1],[0,-1]]){const c=o+i,u=s+h;if(c<0||u<0||c>=this.width||u>=this.height)continue;const l=u*this.width+c;this.building[l]&&!n[l]&&Math.abs(this.heights[l]-this.heights[r])<=M.STEP_M&&(n[l]=1,e.push(l))}}return a}buildingsOf(t,n,a){const e=this.width/2,r=this.height/2,o=[],s=[],i=[];for(const h of t){const c=h%this.width,u=Math.floor(h/this.width);o.push((c+.5-e)*n),s.push((r-u-.5)*n),i.push(this.heights[h])}return M.ofPoints(o,s,i,n,a)}static ofPoints(t,n,a,e,r,o=M.RECTANGLE_FILL,s=[]){const i=[];for(let g=0;g<t.length;g++)for(const[v,x]of[[-.5,-.5],[.5,-.5],[-.5,.5],[.5,.5]])i.push([t[g]+v*e,n[g]+x*e]);const h=M.hull(i),{angle:c,rectangle:u}=M.smallestRectangle(h),l=t.length*e*e,d=(u.maxU-u.minU)*(u.maxV-u.minV);if(l/d>=o||l<4*r)return[M.building(u,c,M.roof(a))];const f=h.map((g,v)=>({from:{east:g[0],north:g[1]},to:{east:h[(v+1)%h.length][0],north:h[(v+1)%h.length][1]}})),m=M.dominantAngles([...s,...f]);return M.parts(t,n,a,m.length>0?m:[c],e,r)}static dominantAngles(t,n=3){const a=new Map;for(const e of t){const r=Math.hypot(e.to.east-e.from.east,e.to.north-e.from.north);if(r<1e-6)continue;const o=(Math.atan2(e.to.north-e.from.north,e.to.east-e.from.east)%(Math.PI/2)+Math.PI/2)%(Math.PI/2),s=Math.round(o/(Math.PI/60));a.set(s,(a.get(s)??0)+r)}return[...a.entries()].sort((e,r)=>r[1]-e[1]).slice(0,n).map(([e])=>e*Math.PI/60)}static parts(t,n,a,e,r,o){let s=t.map((h,c)=>c);const i=[];for(let h=0;h<M.MAX_PARTS&&s.length>0;h++){let c;for(const d of e){const f=M.largestIn(t,n,s,d,r);f&&(c===void 0||f.area>c.area)&&(c=f)}if(c===void 0||c.area<o)break;const u=c.covered;Math.min(c.rectangle.maxU-c.rectangle.minU,c.rectangle.maxV-c.rectangle.minV)>=M.MIN_DEPTH_M&&i.push(M.building(c.rectangle,c.angle,M.roof([...u].map(d=>a[d])))),s=s.filter(d=>!u.has(d))}return i}static largestIn(t,n,a,e,r){const o=Math.cos(e),s=Math.sin(e);let i=1/0,h=-1/0,c=1/0,u=-1/0;const l=[],d=[];for(const b of a){const E=t[b]*o+n[b]*s,P=-t[b]*s+n[b]*o;l.push(E),d.push(P),i=Math.min(i,E),h=Math.max(h,E),c=Math.min(c,P),u=Math.max(u,P)}const f=Math.max(r*Math.SQRT2,Math.max(h-i,u-c)/M.MAX_CELLS),m=Math.max(1,Math.ceil((h-i)/f+1e-9)),g=Math.max(1,Math.ceil((u-c)/f+1e-9)),v=new Uint8Array(m*g),x=a.map((b,E)=>{const P=Math.min(m-1,Math.max(0,Math.floor((l[E]-i)/f))),R=Math.min(g-1,Math.max(0,Math.floor((d[E]-c)/f)));return v[R*m+P]=1,R*m+P}),I=M.largestRectangle(v,m,g);if(I===void 0)return;const U=new Set;return a.forEach((b,E)=>{const P=x[E]%m,R=Math.floor(x[E]/m);P>=I.column&&P<I.column+I.columns&&R>=I.row&&R<I.row+I.rows&&U.add(b)}),{area:I.area*f*f,angle:e,covered:U,rectangle:{minU:i+I.column*f,maxU:i+(I.column+I.columns)*f,minV:c+I.row*f,maxV:c+(I.row+I.rows)*f}}}static largestRectangle(t,n,a){const e=new Array(n).fill(0);let r;for(let o=0;o<a;o++){for(let i=0;i<n;i++)e[i]=t[o*n+i]?e[i]+1:0;const s=[];for(let i=0;i<=n;i++){const h=i===n?0:e[i];for(;s.length>0&&e[s[s.length-1]]>=h;){const c=s.pop(),u=s.length===0?i:i-s[s.length-1]-1,l=e[c]*u;(r===void 0||l>r.area)&&(r={column:i-u,row:o-e[c]+1,columns:u,rows:e[c],area:l})}s.push(i)}}return r}static roof(t){const n=[...t].sort((a,e)=>a-e);return Math.max(2,n[Math.min(n.length-1,Math.floor(n.length*M.ROOF_PERCENTILE))])}static building(t,n,a){const e=Math.cos(n),r=Math.sin(n),o=t.maxU-t.minU,s=t.maxV-t.minV,i=(t.minU+t.maxU)/2,h=(t.minV+t.maxV)/2,u=((90-(o<s?n:n+Math.PI/2)*180/Math.PI)%180+180)%180;return{eastM:M.tenth(i*e-h*r),northM:M.tenth(i*r+h*e),widthM:M.tenth(Math.max(o,s)),lengthM:M.tenth(Math.min(o,s)),heightM:M.tenth(a),headingDeg:Math.round(u*10)/10}}static smallestRectangle(t){let n={area:1/0,angle:0,rectangle:{minU:0,maxU:0,minV:0,maxV:0}};for(let a=0;a<Math.max(1,t.length);a++){const[e,r]=t[a]??[0,0],[o,s]=t[(a+1)%t.length]??[1,0],i=Math.atan2(s-r,o-e),h=Math.cos(i),c=Math.sin(i);let u=1/0,l=-1/0,d=1/0,f=-1/0;for(const[g,v]of t){const x=g*h+v*c,I=-g*c+v*h;u=Math.min(u,x),l=Math.max(l,x),d=Math.min(d,I),f=Math.max(f,I)}const m=(l-u)*(f-d);m<n.area&&(n={area:m,angle:i,rectangle:{minU:u,maxU:l,minV:d,maxV:f}})}return n}static hull(t){const n=[...t].sort((s,i)=>s[0]-i[0]||s[1]-i[1]),a=n.filter((s,i)=>i===0||s[0]!==n[i-1][0]||s[1]!==n[i-1][1]);if(a.length<3)return a;const e=(s,i,h)=>(i[0]-s[0])*(h[1]-s[1])-(i[1]-s[1])*(h[0]-s[0]),r=[];for(const s of a){for(;r.length>=2&&e(r[r.length-2],r[r.length-1],s)<=0;)r.pop();r.push(s)}const o=[];for(const s of[...a].reverse()){for(;o.length>=2&&e(o[o.length-2],o[o.length-1],s)<=0;)o.pop();o.push(s)}return[...r.slice(0,-1),...o.slice(0,-1)]}static tenth(t){return Math.round(t*10)/10}}class y{constructor(t,n,a,e){this.heights=t,this.vegetation=n,this.width=a,this.height=e}static SUMMIT_RADIUS_M=2.5;static CROWN_REACH_M=8;static MAX_WIDTH_M=16;static MIN_WIDTH_M=2;static tenth(t){return Math.round(t*10)/10}trees(t){const n=t.metresPerPixel,a=t.minHeightM??4,e=t.eyeM??1.6,r=t.minElevationDeg??2,o=Math.max(1,Math.round(y.SUMMIT_RADIUS_M/n)),s=Math.round(y.CROWN_REACH_M/n),i=this.width/2,h=this.height/2,c=[];for(let u=0;u<this.height;u++)for(let l=0;l<this.width;l++){const d=this.canopy(l,u);if(d<a)continue;const f=(l+.5-i)*n,m=(h-u-.5)*n,g=Math.hypot(f,m);if(g>t.radiusM||!this.isSummit(l,u,d,o))continue;const v=Math.atan2(d-e,Math.max(g,1))*180/Math.PI;v<r||c.push({tree:{eastM:y.tenth(f),northM:y.tenth(m),heightM:y.tenth(d),widthM:y.tenth(this.crownWidth(l,u,d,n,s))},elevation:v})}return c.sort((u,l)=>l.elevation-u.elevation),c.slice(0,t.maxTrees??150).map(u=>u.tree)}canopy(t,n){if(t<0||n<0||t>=this.width||n>=this.height)return 0;const a=n*this.width+t;return this.vegetation[a]?this.heights[a]:0}isSummit(t,n,a,e){for(let r=-e;r<=e;r++)for(let o=-e;o<=e;o++){if(o===0&&r===0)continue;const s=this.canopy(t+o,n+r);if(s>a||s===a&&(r<0||r===0&&o<0))return!1}return!0}crownWidth(t,n,a,e,r){let o=0;for(let i=-r;i<=r;i++)for(let h=-r;h<=r;h++)Math.hypot(h,i)*e<=y.CROWN_REACH_M&&this.canopy(t+h,n+i)>=a*.5&&o++;const s=2*Math.sqrt(o*e*e/Math.PI);return Math.max(y.MIN_WIDTH_M,Math.min(s,y.MAX_WIDTH_M))}}class S{id="ign-lidar-hd";name="IGN LiDAR HD";attribution="Canopy and roofs © IGN, LiDAR HD (Licence Ouverte 2.0), as surveyed on a day the service does not give";creditUrl="https://geoservices.ign.fr/lidarhd";contemporary=!0;platform;constructor(t={}){this.platform=new w(t)}covers(t,n){return w.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","The LiDAR HD survey covers France only");const a=w.radius(n),e=await this.platform.lidar(t,a);return{trees:new y(e.heights,e.vegetation,e.size,e.size).trees({metresPerPixel:e.metresPerPixel,radiusM:a}),buildings:new M(e.heights,e.building,e.size,e.size).buildings({metresPerPixel:e.metresPerPixel,radiusM:a}),bridges:[],lines:[]}}}class p{static METRES_PER_DEGREE=111320;static DEFAULT_HEIGHT_M=6;static FLOOR_HEIGHT_M=3;static FINE_M=.5;static COARSE_M=1;static MAX_CELLS=4e4;static RECTANGLE_FILL=.9;static buildings(t,n,a,e=12,r=60){const o=[];for(const s of t){const i=p.rings(s,n);if(i.length===0)continue;const h=Number(s.properties.nombre_d_etages),c=Number(s.properties.hauteur),u=Number.isFinite(c)&&c>0?c:Number.isFinite(h)&&h>0?h*p.FLOOR_HEIGHT_M:p.DEFAULT_HEIGHT_M;for(const l of p.rectanglesOf(i,u,e))Math.hypot(l.eastM,l.northM)>a||o.push(Number.isFinite(h)&&h>0?{...l,floors:h}:l)}return o.sort((s,i)=>Math.hypot(s.eastM,s.northM)-Math.hypot(i.eastM,i.northM)),o.slice(0,r)}static named(t,n,a){const e=n.map(r=>({name:typeof r.properties.toponyme=="string"?r.properties.toponyme:"",rings:p.rings(r,a)})).filter(r=>r.name.length>0&&r.rings.length>0);return e.length===0?t:t.map(r=>{const o=e.find(s=>p.inside(r.eastM,r.northM,s.rings));return o?{...r,name:o.name}:r})}static inside(t,n,a){let e=0;for(const r of a)for(let o=0;o+1<r.length;o++){const s=r[o],i=r[o+1];s.north<=n!=i.north<=n&&s.east+(n-s.north)/(i.north-s.north)*(i.east-s.east)>t&&e++}return e%2===1}static rings(t,n){const a=t.geometry,e=a.type==="Polygon"?[a.coordinates]:a.type==="MultiPolygon"?a.coordinates:[],r=Math.cos(n.lat*Math.PI/180),o=[];for(const s of e)for(const i of s)o.push(i.map(([h,c])=>({east:(h-n.lng)*p.METRES_PER_DEGREE*r,north:(c-n.lat)*p.METRES_PER_DEGREE})));return o}static rectanglesOf(t,n,a){let e=1/0,r=-1/0,o=1/0,s=-1/0;for(const l of t)for(const d of l)e=Math.min(e,d.east),r=Math.max(r,d.east),o=Math.min(o,d.north),s=Math.max(s,d.north);let i=p.FINE_M;(r-e)*(s-o)/(i*i)>p.MAX_CELLS&&(i=p.COARSE_M);const h=[],c=[];for(let l=o+i/2;l<s;l+=i){const d=[];for(const f of t)for(let m=0;m+1<f.length;m++){const g=f[m],v=f[m+1];g.north<=l!=v.north<=l&&d.push(g.east+(l-g.north)/(v.north-g.north)*(v.east-g.east))}d.sort((f,m)=>f-m);for(let f=0;f+1<d.length;f+=2)for(let m=Math.ceil((d[f]-i/2)/i)*i+i/2;m<d[f+1];m+=i)h.push(m),c.push(l)}if(h.length*i*i<a)return[];const u=t.flatMap(l=>l.slice(0,-1).map((d,f)=>({from:d,to:l[f+1]})));return M.ofPoints(h,c,new Array(h.length).fill(n),i,a,p.RECTANGLE_FILL,u)}}class T{id="ign-lidar-topo";name="IGN LiDAR HD + BD TOPO";attribution="Canopy © IGN, LiDAR HD; buildings, bridges and lines © IGN, BD TOPO (Licence Ouverte 2.0), as surveyed on days the services do not give";creditUrl="https://geoservices.ign.fr/lidarhd";contemporary=!0;platform;constructor(t={}){this.platform=new w(t)}covers(t,n){return w.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","The IGN's surveys cover France only");const a=w.radius(n),[e,r]=await Promise.allSettled([this.platform.lidar(t,a),this.platform.vectors(t,a,!0)]);if(e.status==="rejected"&&r.status==="rejected")throw e.reason;const o=e.status==="fulfilled"?e.value:void 0,s=r.status==="fulfilled"?r.value:void 0;return{trees:o?new y(o.heights,o.vegetation,o.size,o.size).trees({metresPerPixel:o.metresPerPixel,radiusM:a}):[],buildings:this.buildings(o,s,t,a),bridges:s?O.bridges(s,t,a,o?.structures):[],lines:s?O.lines(s,t,a,o?.structures):[]}}buildings(t,n,a,e){const r=n?p.buildings(n.buildings,a,e):[],o=r.length>0||t===void 0?r:new M(t.heights,t.building,t.size,t.size).buildings({metresPerPixel:t.metresPerPixel,radiusM:e});return n?p.named(o,n.zones,a):o}}class H{id="ign-bd-topo";name="IGN BD TOPO";attribution="Buildings, bridges and lines © IGN, BD TOPO (Licence Ouverte 2.0), as drawn on a day the service does not give";creditUrl="https://geoservices.ign.fr/bdtopo";contemporary=!0;platform;constructor(t={}){this.platform=new w(t)}covers(t,n){return w.covers(t,n)}async getSurroundings(t,n){if(!this.covers(t.lat,t.lng))throw new A("no-coverage","BD TOPO covers France only");const a=w.radius(n),e=await this.platform.vectors(t,a,!0);return{trees:[],buildings:p.named(p.buildings(e.buildings,t,a),e.zones,t),bridges:O.bridges(e,t,a,void 0),lines:O.lines(e,t,a,void 0)}}}const D=[{id:"ign-lidar-topo",name:"IGN LiDAR HD + BD TOPO",credit:"© IGN, LiDAR HD and BD TOPO, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/lidarhd",create:()=>new T},{id:"ign-lidar-hd",name:"IGN LiDAR HD",credit:"© IGN, LiDAR HD, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/lidarhd",create:()=>new S},{id:"ign-bd-topo",name:"IGN BD TOPO",credit:"© IGN, BD TOPO, Licence Ouverte 2.0",creditUrl:"https://geoservices.ign.fr/bdtopo",create:()=>new H}];class L{static DEFAULT_ID=D[0].id;static NONE="none";static byId(t){return t===L.NONE?void 0:D.find(n=>n.id===t)??D[0]}static providerFor(t,n,a){const e=L.byId(t)?.create();return e?.covers(n,a)?e:void 0}}export{D as SURROUNDINGS_SOURCES,L as SurroundingsSources};