Polygon.js 5.2 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{_ as t}from"../chunks/tslib.es6.js";import{equals as r}from"../core/arrayUtils.js";import{clone as e}from"../core/lang.js";import{isNone as s,isSome as i}from"../core/maybe.js";import{property as n}from"../core/accessorSupport/decorators/property.js";import"../core/accessorSupport/ensureType.js";import{subclass as o}from"../core/accessorSupport/decorators/subclass.js";import{writer as a}from"../core/accessorSupport/decorators/writer.js";import l from"./Extent.js";import h from"./Geometry.js";import p from"./Point.js";import c from"./SpatialReference.js";import{polygonCentroid as u}from"./support/centroid.js";import{polygonContainsPoint as m}from"./support/contains.js";import{isClockwise as f}from"./support/coordsUtils.js";import{getPolygonExtent as g}from"./support/extentUtils.js";import{isSelfIntersecting as y}from"./support/intersectsBase.js";import{project as d}from"./support/webMercatorUtils.js";import{updateSupportFromPoint as R}from"./support/zmUtils.js";var x;function j(t){return!Array.isArray(t[0])}let w=x=class extends h{constructor(...t){super(...t),this.rings=[],this.type="polygon"}static fromExtent(t){const r=t.clone().normalize(),e=t.spatialReference;let s=!1,i=!1;for(const o of r)o.hasZ&&(s=!0),o.hasM&&(i=!0);const n={rings:r.map((t=>{const r=[[t.xmin,t.ymin],[t.xmin,t.ymax],[t.xmax,t.ymax],[t.xmax,t.ymin],[t.xmin,t.ymin]];if(s&&t.hasZ){const e=t.zmin+.5*(t.zmax-t.zmin);for(let t=0;t<r.length;t++)r[t].push(e)}if(i&&t.hasM){const e=t.mmin+.5*(t.mmax-t.mmin);for(let t=0;t<r.length;t++)r[t].push(e)}return r})),spatialReference:e};return s&&(n.hasZ=!0),i&&(n.hasM=!0),new x(n)}normalizeCtorArgs(t,r){let e,s,i=null,n=null;return t&&!Array.isArray(t)?(i=t.rings?t.rings:null,r||(t.spatialReference?r=t.spatialReference:t.rings||(r=t)),e=t.hasZ,s=t.hasM):i=t,i=i||[],r=r||c.WGS84,i.length&&i[0]&&null!=i[0][0]&&"number"==typeof i[0][0]&&(i=[i]),n=i[0]&&i[0][0],n&&(void 0===e&&void 0===s?(e=n.length>2,s=n.length>3):void 0===e?e=s?n.length>3:n.length>2:void 0===s&&(s=e?n.length>3:n.length>2)),{rings:i,spatialReference:r,hasZ:e,hasM:s}}get cache(){return this.commitProperty("rings"),this.commitProperty("hasZ"),this.commitProperty("hasM"),this.commitProperty("spatialReference"),{}}get centroid(){const t=u(this);if(!t||isNaN(t[0])||isNaN(t[1])||this.hasZ&&isNaN(t[2]))return null;const r=new p;return r.x=t[0],r.y=t[1],r.spatialReference=this.spatialReference,this.hasZ&&(r.z=t[2]),r}get extent(){const{spatialReference:t}=this,r=g(this);if(!r)return null;const e=new l(r);return e.spatialReference=t,e}get isSelfIntersecting(){return y(this.rings)}writeRings(t,r){r.rings=e(this.rings)}addRing(t){if(!t)return;const r=this.rings,e=r.length;if(j(t)){const s=[];for(let r=0,e=t.length;r<e;r++)s[r]=t[r].toArray();r[e]=s}else r[e]=t.concat();return this.notifyChange("rings"),this}clone(){const t=new x;return t.spatialReference=this.spatialReference,t.rings=e(this.rings),t.hasZ=this.hasZ,t.hasM=this.hasM,t}equals(t){if(this===t)return!0;if(s(t))return!1;const e=this.spatialReference,n=t.spatialReference;if(i(e)!==i(n))return!1;if(i(e)&&i(n)&&!e.equals(n))return!1;if(this.rings.length!==t.rings.length)return!1;const o=([t,r,e,s],[i,n,o,a])=>t===i&&r===n&&(null==e&&null==o||e===o)&&(null==s&&null==a||s===a);for(let s=0;s<this.rings.length;s++){const e=this.rings[s],i=t.rings[s];if(!r(e,i,o))return!1}return!0}contains(t){if(!t)return!1;const r=d(t,this.spatialReference);return m(this,i(r)?r:t)}isClockwise(t){let r;return r=j(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,f(r,this.hasM,this.hasZ)}getPoint(t,r){if(!this._validateInputs(t,r))return null;const e=this.rings[t][r],s=this.hasZ,i=this.hasM;return s&&!i?new p(e[0],e[1],e[2],void 0,this.spatialReference):i&&!s?new p(e[0],e[1],void 0,e[2],this.spatialReference):s&&i?new p(e[0],e[1],e[2],e[3],this.spatialReference):new p(e[0],e[1],this.spatialReference)}insertPoint(t,r,e){return this._validateInputs(t,r,!0)?(R(this,e),Array.isArray(e)||(e=e.toArray()),this.rings[t].splice(r,0,e),this.notifyChange("rings"),this):this}removePoint(t,r){if(!this._validateInputs(t,r))return null;const e=new p(this.rings[t].splice(r,1)[0],this.spatialReference);return this.notifyChange("rings"),e}removeRing(t){if(!this._validateInputs(t,null))return null;const r=this.rings.splice(t,1)[0],e=this.spatialReference,s=r.map((t=>new p(t,e)));return this.notifyChange("rings"),s}setPoint(t,r,e){return this._validateInputs(t,r)?(R(this,e),Array.isArray(e)||(e=e.toArray()),this.rings[t][r]=e,this.notifyChange("rings"),this):this}_validateInputs(t,r,e=!1){if(null==t||t<0||t>=this.rings.length)return!1;if(null!=r){const s=this.rings[t];if(e&&(r<0||r>s.length))return!1;if(!e&&(r<0||r>=s.length))return!1}return!0}toJSON(t){return this.write({},t)}};t([n({readOnly:!0})],w.prototype,"cache",null),t([n({readOnly:!0})],w.prototype,"centroid",null),t([n({readOnly:!0})],w.prototype,"extent",null),t([n({readOnly:!0})],w.prototype,"isSelfIntersecting",null),t([n({type:[[[Number]]],json:{write:{isRequired:!0}}})],w.prototype,"rings",void 0),t([a("rings")],w.prototype,"writeRings",null),w=x=t([o("esri.geometry.Polygon")],w),w.prototype.toJSON.isDefaultToJSON=!0;const v=w;export{v as default};