@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.02 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
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 n}from"../../layers/orientedImagery/transformations/utils.js";import{imageGalleryThumbnailSize as s}from"./constants.js";import{validateTifOrMrfExtension as i,getContentType as m}from"./utils.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]}),n=await t.queryAttachments(o,r),s=n[`${a}`]?.[0],i=s?.url;if(!i)throw new e("NoAttachmentError","no attachments found",{[t.objectIdField]:a,layer:t});return{datasetFormat:s.contentType.split("/")[1].toUpperCase(),url:i}};async function l(t,e){const a="string"==typeof t,r=a?t:t.url,{pathname:n,searchParams:s,origin:i}=new URL(r);let c=a?o(n):t.datasetFormat;c||(c=await m(r));const h=await import("../../layers/support/rasterDatasets/RasterFactory.js").then((t=>t.default)),l=await h.open({ioConfig:{customFetchParameters:Object.fromEntries(s),skipMapInfo:!0},datasetFormat:c?.toUpperCase(),url:`${i}${n}`,signal:e?.signal});return l?u(l,{signal:e?.signal}):null}async function u(t,e){const{level:a,offset:o,size:n}=p(t),{pixelBlock:s}=await t.fetchRawPixels(a,o,n,e);return r(e),s}function p(t){const{storageInfo:e,width:a,height:r}=t.rasterInfo,{maximumPyramidLevel:o,pyramidScalingFactor:n}=e,s=n??2;return{level:o,offset:{x:0,y:0},size:{width:Math.ceil(a/s**o),height:Math.ceil(r/s**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:n,url:m}=await h(a,+r,o);return i(n)?await l({datasetFormat:n,url:m},o):(await t(m,{...o,useRequestQueue:!0,responseType:"image",query:{w:s}})).data}function f(t,e,a=0){const{height:r,width:o}=t;e.width=o,e.height=r;const n=e.getContext("2d");if(!n)return;const s=g(t,e,n);s.getContext("2d")&&(a>0?w(n,s,o,r,a):n.drawImage(s,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 n=a.createImageData(e.width,e.height);n&&(n.data.set(t.getAsRGBA()),r.width=n.width,r.height=n.height,o?.putImageData(n,0,0))}return r}function w(t,e,r,o,s){const i=a(s),m=[n(0,0,i),n(r,0,i),n(r,o,i),n(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};