UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 2.95 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import t from"../../request.js";import e from"../../core/Error.js";import{deg2rad as a}from"../../core/mathUtils.js";import{throwIfAborted as r}from"../../core/promiseUtils.js";import{getPathExtension as o}from"../../core/urlUtils.js";import{rotatePixel as s}from"../../layers/orientedImagery/transformations/utils.js";import{imageGalleryThumbnailSize as n}from"./constants.js";import{validateTifOrMrfExtension as i}from"./utils.js";import{getDatasetFormat as m}from"./support/urlUtils.js";const c=t=>"FA"===t,h=async(t,a,r)=>{const o=new(await import("../../rest/support/AttachmentQuery.js").then(t=>t.default))({objectIds:[a]}),s=await t.queryAttachments(o,r),n=s[`${a}`]?.[0],i=n?.url;if(!i)throw new e("NoAttachmentError","no attachments found",{[t.objectIdField]:a,layer:t});return{datasetFormat:n.contentType.split("/")[1].toUpperCase(),url:i}};async function l(t,e){const a="string"==typeof t,r=a?t:t.url,{pathname:s,searchParams:n,origin:i}=new URL(r);let c=a?o(s):t.datasetFormat;c||(c=await m(r));const h=await import("../../layers/raster/datasets/RasterFactory.js").then(t=>t.default),l=await h.open({ioConfig:{customFetchParameters:Object.fromEntries(n),skipMapInfo:!0},datasetFormat:c?.toUpperCase(),url:`${i}${s}`,signal:e?.signal});return l?u(l,{signal:e?.signal}):null}async function u(t,e){const{level:a,offset:o,size:s}=p(t),{pixelBlock:n}=await t.fetchRawPixels(a,o,s,e);return r(e),n}function p(t){const{storageInfo:e,width:a,height:r}=t.rasterInfo,{maximumPyramidLevel:o,pyramidScalingFactor:s}=e,n=s??2;return{level:o,offset:{x:0,y:0},size:{width:Math.ceil(a/n**o),height:Math.ceil(r/n**o)}}}async function d(a,r,o){if(!a)throw new e("NoLayerError","No layer provided, cannot load image from attachment",{layer:a,objectId:r});const{datasetFormat:s,url:m}=await h(a,+r,o);return i(s)?await l({datasetFormat:s,url:m},o):(await t(m,{...o,useQueue:!0,responseType:"image",query:{w:n}})).data}function f(t,e,a=0){const{height:r,width:o}=t;e.width=o,e.height=r;const s=e.getContext("2d");if(!s)return;const n=g(t,e,s);n.getContext("2d")&&(a>0?w(s,n,o,r,a):s.drawImage(n,0,0))}function g(t,e,a){const r=document.createElement("canvas"),o=r.getContext("2d");if(t instanceof HTMLImageElement)r.width=t.width,r.height=t.height,o?.drawImage(t,0,0);else{const s=a.createImageData(e.width,e.height);s&&(s.data.set(t.getAsRGBA()),r.width=s.width,r.height=s.height,o?.putImageData(s,0,0))}return r}function w(t,e,r,o,n){const i=a(n),m=[s(0,0,i),s(r,0,i),s(r,o,i),s(0,o,i)],c=Math.max(...m.map(t=>t[0]))-Math.min(...m.map(t=>t[0])),h=Math.max(...m.map(t=>t[1]))-Math.min(...m.map(t=>t[1])),l=Math.min(r/c,o/h);t.save(),t.clearRect(0,0,r,o),t.translate(r/2,o/2),t.rotate(i),t.scale(l,l),t.drawImage(e,-r/2,-o/2,r,o),t.restore()}export{h as getImageSourceFromAttachment,l as getThumbnailPixelBlock,c as isFeatureAttachment,d as loadImageForAttachment,f as renderImageWithRotation};