UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 3.93 kB
import e from"../../../Graphic.js";import{unwrap as o,isSome as t}from"../../../core/maybe.js";import r from"../../../geometry/Polygon.js";import{addressToLocations as s}from"../../../rest/locator/addressToLocations.js";import{locationToAddress as n}from"../../../rest/locator/locationToAddress.js";import{suggestLocations as i}from"../../../rest/locator/suggestLocations.js";import c from"../../../rest/support/AddressToLocationsParameters.js";import a from"../../../rest/support/LocationToAddressParameters.js";import u from"../../../rest/support/SuggestLocationsParameters.js";import{createExtentFromGeometry as l,scaleExtent as m}from"./geometryUtils.js";const f="Single Line Input",d=3e5;function g(e,o){return e.location?h(e,o):I(e,o)}function p(e,o){if(o.localSearchDisabled)return null;const t=e?.scale;return"number"==typeof t&&t<=d?e.get("extent.center"):null}function y(e,t){const{source:r,spatialReference:s,view:n,suggestTerm:c,maxSuggestions:a,sourceIndex:l}=e,m=new u,{apiKey:f,url:d}=r,g=j(r,n),y=t&&t.signal;if(!d)return Promise.resolve(null);f&&(m.apiKey=f),r.categories&&(m.categories=r.categories),s&&(m.outSpatialReference=s);const x=p(n,r);x&&(m.location=x);if(!c.trim())return Promise.resolve(null);const{prefix:v="",suffix:S=""}=r,w=`${v}${c}${S}`;return m.text=w,g&&(m.searchExtent=o(g)),m.maxSuggestions=a,r.countryCode&&(m.countryCode=r.countryCode),i(d,m,{signal:y}).then((e=>b(e,l)))}function x(e){return!!e&&/(?:geocode\-api\.arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}function v(e){return!!e&&/(?:\/servers\/[\da-z\.-]+\/rest\/services\/world\/geocodeserver).*/i.test(e)}function S(e){return!!e&&/(?:arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}const w="https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";function h(e,o){const{source:t,spatialReference:r,location:s,sourceIndex:i,view:c}=e,{apiKey:u,url:l,zoomScale:m,defaultZoomScale:f}=t,d=c&&c.scale,g=o&&o.signal,p=new a;return p.location=s,u&&(p.apiKey=u),r&&(p.outSpatialReference=r),n(l,p,{signal:g}).then((e=>L([e],{sourceIndex:i,scale:d,view:c,zoomScale:m,defaultZoomScale:f}))).catch((()=>[]))}function j(e,o){const{filter:t,withinViewEnabled:r}=e,s=o&&o.scale,n=o&&o.extent,i=t&&t.geometry;return l(i,o,s)||(r&&n?n:void 0)}function I(e,t){const{source:r,suggestResult:n,spatialReference:i,view:a,maxResults:u,sourceIndex:l}=e,m=r&&r.zoomScale,d=r&&r.defaultZoomScale;if(!n.text.trim())return Promise.resolve(null);const g=!n.key&&r.prefix?r.prefix:"",y=!n.key&&r.suffix?r.suffix:"",x=`${g}${n.text}${y}`,v=new c,{apiKey:S,url:w}=r,h=a&&a.scale,I=j(r,a),$=t&&t.signal;if(S&&(v.apiKey=S),!w)return Promise.resolve(null);r.categories&&(v.categories=r.categories),r.locationType&&(v.locationType=r.locationType),i&&(v.outSpatialReference=i);const b=p(a,r);b&&(v.location=b),v.maxLocations=u,I&&(v.searchExtent=o(I)),r.countryCode&&(v.countryCode=r.countryCode);const{key:K}=n,P=`${K}`;return K&&(v.magicKey=P),v.address={},v.address[r.singleLineFieldName||f]=x,r.outFields&&(v.outFields=r.outFields),s(w,v,{signal:$}).then((e=>L(e,{key:P,scale:h,sourceIndex:l,view:a,zoomScale:m,defaultZoomScale:d})))}function $(e,o){return{text:e.text,key:e.magicKey,sourceIndex:o}}function b(e,o){return e.map((e=>$(e,o)))}function K(o,s){const{key:n,scale:i,sourceIndex:c,view:a,zoomScale:u,defaultZoomScale:f}=s,{attributes:d,extent:g,location:p,address:y}=o,x=new e({geometry:p,attributes:d}),v=g||p,S=l(v,a,i),w="number"==typeof u?m(S,a,u):"number"==typeof f&&"point"===v.type?m(S,a,f):S,h=p?`${p.x},${p.y}`:"",j=y||h,I=x.clone();return t(w)&&(I.geometry=r.fromExtent(w)),{extent:w,feature:x,target:I,key:n,name:j,sourceIndex:c}}function L(e,o){return e.filter(Boolean).map((e=>K(e,o)))}export{p as getLocation,g as getResults,y as getSuggestions,S as isArcGISWorldGeocoder,x as isMeteredArcGISWorldGeocoder,v as isProxiedArcGISWorldGeocoder,w as meteredArcGISLocatorUrl};