@opencage/ol-opencage-geosearch
Version:
An OpenLayers geosearch control that uses OpenCage's geosearch API.
2 lines (1 loc) • 7.28 kB
JavaScript
"use strict";const gt=require("ol/control/Control"),dt=require("@algolia/autocomplete-js"),Et=require("@opencage/geosearch-core");function K(n){return n*180/Math.PI}function U(n){return n*Math.PI/180}function p(n,t,e){if(n>=t&&n<e)return n;const o=e-t;return((n-t)%o+o)%o+t}const Pt={radians:6370997/(2*Math.PI),degrees:2*Math.PI*6370997/360,ft:.3048,m:1,"us-ft":1200/3937};class z{constructor(t){this.code_=t.code,this.units_=t.units,this.extent_=t.extent!==void 0?t.extent:null,this.worldExtent_=t.worldExtent!==void 0?t.worldExtent:null,this.axisOrientation_=t.axisOrientation!==void 0?t.axisOrientation:"enu",this.global_=t.global!==void 0?t.global:!1,this.canWrapX_=!!(this.global_&&this.extent_),this.getPointResolutionFunc_=t.getPointResolution,this.defaultTileGrid_=null,this.metersPerUnit_=t.metersPerUnit}canWrapX(){return this.canWrapX_}getCode(){return this.code_}getExtent(){return this.extent_}getUnits(){return this.units_}getMetersPerUnit(){return this.metersPerUnit_||Pt[this.units_]}getWorldExtent(){return this.worldExtent_}getAxisOrientation(){return this.axisOrientation_}isGlobal(){return this.global_}setGlobal(t){this.global_=t,this.canWrapX_=!!(t&&this.extent_)}getDefaultTileGrid(){return this.defaultTileGrid_}setDefaultTileGrid(t){this.defaultTileGrid_=t}setExtent(t){this.extent_=t,this.canWrapX_=!!(this.global_&&t)}setWorldExtent(t){this.worldExtent_=t}setGetPointResolution(t){this.getPointResolutionFunc_=t}getPointResolutionFunc(){return this.getPointResolutionFunc_}}const R=6378137,_=Math.PI*R,wt=[-_,-_,_,_],mt=[-180,-85,180,85],F=R*Math.log(Math.tan(Math.PI/2));class M extends z{constructor(t){super({code:t,units:"m",extent:wt,global:!0,worldExtent:mt,getPointResolution:function(e,o){return e/Math.cosh(o[1]/R)}})}}const Q=[new M("EPSG:3857"),new M("EPSG:102100"),new M("EPSG:102113"),new M("EPSG:900913"),new M("http://www.opengis.net/def/crs/EPSG/0/3857"),new M("http://www.opengis.net/gml/srs/epsg.xml#3857")];function Mt(n,t,e,o){const r=n.length;e=e>1?e:2,o=o??e,t===void 0&&(e>2?t=n.slice():t=new Array(r));for(let s=0;s<r;s+=o){t[s]=_*n[s]/180;let c=R*Math.log(Math.tan(Math.PI*(+n[s+1]+90)/360));c>F?c=F:c<-F&&(c=-F),t[s+1]=c}return t}function _t(n,t,e,o){const r=n.length;e=e>1?e:2,o=o??e,t===void 0&&(e>2?t=n.slice():t=new Array(r));for(let s=0;s<r;s+=o)t[s]=180*n[s]/_,t[s+1]=360*Math.atan(Math.exp(n[s+1]/R))/Math.PI-90;return t}const St=6378137,Y=[-180,-90,180,90],Tt=Math.PI*St/180;class P extends z{constructor(t,e){super({code:t,units:"degrees",extent:Y,axisOrientation:e,global:!0,metersPerUnit:Tt,worldExtent:Y})}}const B=[new P("CRS:84"),new P("EPSG:4326","neu"),new P("urn:ogc:def:crs:OGC:1.3:CRS84"),new P("urn:ogc:def:crs:OGC:2:84"),new P("http://www.opengis.net/def/crs/OGC/1.3/CRS84"),new P("http://www.opengis.net/gml/srs/epsg.xml#4326","neu"),new P("http://www.opengis.net/def/crs/EPSG/0/4326","neu")];let j={};function xt(n){return j[n]||j[n.replace(/urn:(x-)?ogc:def:crs:EPSG:(.*:)?(\w+)$/,"EPSG:$3")]||null}function Ct(n,t){j[n]=t}let S={};function I(n,t,e){const o=n.getCode(),r=t.getCode();o in S||(S[o]={}),S[o][r]=e}function N(n,t){return n in S&&t in S[n]?S[n][t]:null}const $=.9996,l=.00669438,L=l*l,q=L*l,w=l/(1-l),tt=Math.sqrt(1-l),T=(1-tt)/(1+tt),rt=T*T,J=rt*T,V=J*T,ct=V*T,it=1-l/4-3*L/64-5*q/256,Gt=3*l/8+3*L/32+45*q/1024,It=15*L/256+45*q/1024,Rt=35*q/3072,bt=3/2*T-27/32*J+269/512*ct,Ot=21/16*rt-55/32*V,yt=151/96*J-417/128*ct,At=1097/512*V,D=6378137;function Ft(n,t,e){const o=n-5e5,c=(e.north?t:t-1e7)/$/(D*it),i=c+bt*Math.sin(2*c)+Ot*Math.sin(4*c)+yt*Math.sin(6*c)+At*Math.sin(8*c),a=Math.sin(i),m=a*a,d=Math.cos(i),x=a/d,f=x*x,E=f*f,h=1-l*m,b=Math.sqrt(1-l*m),O=D/b,y=(1-l)/h,u=w*d**2,C=u*u,g=o/(O*$),A=g*g,G=A*g,Z=G*g,H=Z*g,ht=H*g,ut=i-x/y*(A/2-Z/24*(5+3*f+10*u-4*C-9*w))+ht/720*(61+90*f+298*u+45*E-252*w-3*C);let v=(g-G/6*(1+2*f+u)+H/120*(5-2*u+28*f-3*C+8*w+24*E))/d;return v=p(v+U(at(e.number)),-Math.PI,Math.PI),[K(v),K(ut)]}const nt=-80,et=84,Ut=-180,$t=180;function Dt(n,t,e){n=p(n,Ut,$t),t<nt?t=nt:t>et&&(t=et);const o=U(t),r=Math.sin(o),s=Math.cos(o),c=r/s,i=c*c,a=i*i,m=U(n),d=at(e.number),x=U(d),f=D/Math.sqrt(1-l*r**2),E=w*s**2,h=s*p(m-x,-Math.PI,Math.PI),b=h*h,O=b*h,y=O*h,u=y*h,C=u*h,g=D*(it*o-Gt*Math.sin(2*o)+It*Math.sin(4*o)-Rt*Math.sin(6*o)),A=$*f*(h+O/6*(1-i+E)+u/120*(5-18*i+a+72*E-58*w))+5e5;let G=$*(g+f*c*(b/2+y/24*(5-i+9*E+4*E**2)+C/720*(61-58*i+a+600*E-330*w)));return e.north||(G+=1e7),[A,G]}function at(n){return(n-1)*6-180+3}const Xt=[/^EPSG:(\d+)$/,/^urn:ogc:def:crs:EPSG::(\d+)$/,/^http:\/\/www\.opengis\.net\/def\/crs\/EPSG\/0\/(\d+)$/];function lt(n){let t=0;for(const r of Xt){const s=n.match(r);if(s){t=parseInt(s[1]);break}}if(!t)return null;let e=0,o=!1;return t>32700&&t<32761?e=t-32700:t>32600&&t<32661&&(o=!0,e=t-32600),e?{number:e,north:o}:null}function ot(n,t){return function(e,o,r,s){const c=e.length;r=r>1?r:2,s=s??r,o||(r>2?o=e.slice():o=new Array(c));for(let i=0;i<c;i+=s){const a=e[i],m=e[i+1],d=n(a,m,t);o[i]=d[0],o[i+1]=d[1]}return o}}function Lt(n){return lt(n)?new z({code:n,units:"m"}):null}function qt(n){const t=lt(n.getCode());return t?{forward:ot(Dt,t),inverse:ot(Ft,t)}:null}const vt=[qt],Nt=[Lt];function ft(n,t){if(t!==void 0){for(let e=0,o=n.length;e<o;++e)t[e]=n[e];t=t}else t=n.slice();return t}function k(n){Ct(n.getCode(),n),I(n,n,ft)}function Wt(n){n.forEach(k)}function X(n){if(typeof n!="string")return n;const t=xt(n);if(t)return t;for(const e of Nt){const o=e(n);if(o)return o}return null}function st(n){Wt(n),n.forEach(function(t){n.forEach(function(e){t!==e&&I(t,e,ft)})})}function pt(n,t,e,o){n.forEach(function(r){t.forEach(function(s){I(r,s,e),I(s,r,o)})})}function jt(n,t){const e=n.getCode(),o=t.getCode();let r=N(e,o);if(r)return r;let s=null,c=null;for(const a of vt)s||(s=a(n)),c||(c=a(t));if(!s&&!c)return null;const i="EPSG:4326";if(c)if(s)r=W(s.inverse,c.forward);else{const a=N(e,i);a&&(r=W(a,c.forward))}else{const a=N(i,o);a&&(r=W(s.inverse,a))}return r&&(k(n),k(t),I(n,t,r)),r}function W(n,t){return function(e,o,r,s){return o=n(e,o,r,s),t(o,o,r,s)}}function kt(n,t){const e=X(n),o=X(t);return jt(e,o)}function zt(n,t,e){const o=kt(t,e);if(!o){const r=X(t).getCode(),s=X(e).getCode();throw new Error(`No transform available between ${r} and ${s}`)}return o(n,void 0,n.length)}function Jt(){st(Q),st(B),pt(B,Q,Mt,_t)}Jt();class Vt extends gt{constructor(t){const e=t||{},o=document.createElement("div");o.className="ol-opencage-geosearch ol-control ol-unselectable",super({element:o,target:t.target}),this.options=e,this.element=o,this.setCssPosition(this.options.position),dt.autocomplete({container:this.element,plugins:[Et.OpenCageGeoSearchPlugin(this.options,{onSelect:this.handleSelect.bind(this),onActive:this.options.onActive,onSubmit:this.options.onSubmit})]})}setMap(t){super.setMap(t),this.map=t}setCssPosition(t){t&&(t.match("top")&&(this.element.style.top=0),t.match("bottom")&&(this.element.style.bottom=0),t.match("left")&&(this.element.style.left=0),t.match("right")&&(this.element.style.right=0))}handleSelect(t){if(typeof this.options.onSelect=="function"){this.options.onSelect(t);return}const{item:e}=t,o=zt([e.geometry.lng,e.geometry.lat],"EPSG:4326",this.getMap().getView().getProjection());this.getMap().getView().setCenter(o),this.getMap().getView().setZoom(13)}}module.exports=Vt;