UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 5.15 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import{equals as e}from"../core/arrayUtils.js";import{deprecatedProperty as r}from"../core/deprecate.js";import{clone as i}from"../core/lang.js";import s from"../core/Logger.js";import{property as n,subclass as o}from"../core/accessorSupport/decorators.js";import a from"./Extent.js";import l from"./Geometry.js";import h from"./Point.js";import c from"./SpatialReference.js";import{polygonCentroidPoint as p}from"./support/centroid.js";import{polygonContainsPoint as u}from"./support/contains.js";import{isClockwise as m}from"./support/coordsUtils.js";import{getPolygonExtent as f}from"./support/extentUtils.js";import{project as g}from"./support/webMercatorUtils.js";import{updateSupportFromPoint as y}from"./support/zmUtils.js";import{writer as R}from"../core/accessorSupport/decorators/writer.js";var d;function w(t){return!Array.isArray(t[0])}function v(t){return"number"==typeof t[0]?.[0]}function x(t){if(!t)return;let{rings:e,hasM:r,hasZ:i,spatialReference:s}=t;switch(e??=[],v(e)&&(e=[e]),e[0]?.[0]?.length){case 4:i??=!0,r??=!0;break;case 3:i??=!0!==r,r??=!i;break;default:i??=!1,r??=!1}return s??=c.WGS84,{...t,hasM:r,hasZ:i,rings:e,spatialReference:s}}let j=d=class extends l{static fromExtent(t){const e=t.clone().normalize(),{spatialReference:r}=t;let i=!1,s=!1;for(const o of e)o.hasZ&&(i=!0),o.hasM&&(s=!0);const n={rings:e.map(t=>{const e=[[t.xmin,t.ymin],[t.xmin,t.ymax],[t.xmax,t.ymax],[t.xmax,t.ymin],[t.xmin,t.ymin]];if(i&&t.hasZ){const r=t.zmin+.5*(t.zmax-t.zmin);for(let t=0;t<e.length;t++)e[t].push(r)}if(s&&t.hasM){const r=t.mmin+.5*(t.mmax-t.mmin);for(let t=0;t<e.length;t++)e[t].push(r)}return e}),spatialReference:r};return i&&(n.hasZ=!0),s&&(n.hasM=!0),new d(n)}constructor(t){super(x(t)),this.curveRings=void 0,this.rings=[],this.type="polygon"}get cache(){return this.commitProperty("curveRings"),this.commitProperty("hasM"),this.commitProperty("hasZ"),this.commitProperty("rings"),this.commitProperty("spatialReference"),{}}get centroid(){r(s.getLogger(this),"centroid",{replacement:"Please use centroidOperator.execute() instead.",version:"4.34",warnOnce:!0});const t=p(this);return t?h.fromJSON(t):null}writeCurveRings(t,e){e.curveRings=i(t)}get extent(){const t=f(this),{spatialReference:e}=this;return t?new a({...t,spatialReference:e}):null}writeRings(t,e){e.rings=i(this.rings)}addRing(t){if(!t)return;const e=this.rings,r=e.length;if(w(t)){const i=[];for(let e=0,r=t.length;e<r;e++)i[e]=t[e].toArray();e[r]=i}else e[r]=t.slice();return this.notifyChange("rings"),this}clone(){const t=new d;return t.spatialReference=this.spatialReference,t.rings=i(this.rings),t.curveRings=i(this.curveRings),t.hasZ=this.hasZ,t.hasM=this.hasM,t}equals(t){if(this===t)return!0;if(null==t)return!1;const r=this.spatialReference,i=t.spatialReference;if(null!=r!=(null!=i))return!1;if(null!=r&&null!=i&&!r.equals(i))return!1;if(this.rings.length!==t.rings.length)return!1;const s=([t,e,r,i],[s,n,o,a])=>t===s&&e===n&&(null==r&&null==o||r===o)&&(null==i&&null==a||i===a);for(let n=0;n<this.rings.length;n++){const r=this.rings[n],i=t.rings[n];if(!e(r,i,s))return!1}return!0}contains(t){if(!t)return!1;const e=g(t,this.spatialReference);return u(this,null!=e?e:t)}isClockwise(t){const e=w(t)?t.map(t=>this.hasZ?this.hasM?[t.x,t.y,t.z,t.m]:[t.x,t.y,t.z]:[t.x,t.y]):t;return m(e)}getPoint(t,e){if(!this._validateInputs(t,e))return null;const r=this.rings[t][e],i=this.hasZ,s=this.hasM;return i&&!s?new h(r[0],r[1],r[2],void 0,this.spatialReference):s&&!i?new h(r[0],r[1],void 0,r[2],this.spatialReference):i&&s?new h(r[0],r[1],r[2],r[3],this.spatialReference):new h(r[0],r[1],this.spatialReference)}insertPoint(t,e,r){return this._validateInputs(t,e,!0)?(y(this,r),Array.isArray(r)||(r=r.toArray()),this.rings[t].splice(e,0,r),this.notifyChange("rings"),this):this}removePoint(t,e){if(!this._validateInputs(t,e))return null;const r=new h(this.rings[t].splice(e,1)[0],this.spatialReference);return this.notifyChange("rings"),r}removeRing(t){if(!this._validateInputs(t,null))return null;const e=this.rings.splice(t,1)[0],r=this.spatialReference,i=e.map(t=>new h(t,r));return this.notifyChange("rings"),i}setPoint(t,e,r){return this._validateInputs(t,e)?(y(this,r),Array.isArray(r)||(r=r.toArray()),this.rings[t][e]=r,this.notifyChange("rings"),this):this}_validateInputs(t,e,r=!1){if(null==t||t<0||t>=this.rings.length)return!1;if(null!=e){const i=this.rings[t];if(r&&(e<0||e>i.length))return!1;if(!r&&(e<0||e>=i.length))return!1}return!0}toJSON(t){return this.write({},t)}};t([n({readOnly:!0})],j.prototype,"cache",null),t([n({readOnly:!0})],j.prototype,"centroid",null),t([n({json:{write:!0,origins:{"portal-item":{write:!1},"web-map":{write:!1},"web-scene":{write:!1}}}})],j.prototype,"curveRings",void 0),t([R("curveRings")],j.prototype,"writeCurveRings",null),t([n({readOnly:!0})],j.prototype,"extent",null),t([n({type:[[[Number]]],json:{write:{isRequired:!0}}})],j.prototype,"rings",void 0),t([R("rings")],j.prototype,"writeRings",null),j=d=t([o("esri.geometry.Polygon")],j),j.prototype.toJSON.isDefaultToJSON=!0;export{j as default};