1+ import { extend , Zondy , notNULL } from "../common" ;
2+ import { ArcGisGeometry } from "./Geometry" ;
3+ import { ArcGisPoint } from "./Point" ;
4+ import * as T from '@turf/turf'
5+ import * as H from '@turf/helpers'
6+
7+ /**
8+ * @class module:ArcGis.ArcGisExtent
9+ * @description ArcGisExtent对象
10+ * @author 基础平台-杨琨
11+ * @param options - {Object} 必选项,构造Extent对象参数。
12+ * @param {String } [query.xmin] 可选项,x轴最小坐标。
13+ * @param {String } [query.ymin] 可选项,x轴最大坐标。
14+ * @param {String } [query.ymin] 可选项,y轴最小坐标。
15+ * @param {String } [query.ymax] 可选项,y轴最小坐标。
16+ * @param {String } [query.zmin] 可选项,z轴最小坐标。
17+ * @param {String } [query.zmax] 可选项,z轴最小坐标。
18+ * @param {String } [query.mmin] 可选项,m轴最小坐标。
19+ * @param {String } [query.mmax] 可选项,m轴最小坐标。
20+ */
21+
22+ class ArcGisExtent extends ArcGisGeometry {
23+ constructor ( options ) {
24+ super ( options ) ;
25+ this . center = undefined ;
26+ this . hasM = false ;
27+ this . hasZ = false ;
28+ this . height = 0 ;
29+ this . mmax = undefined ;
30+ this . mmin = undefined ;
31+ this . type = "extent" ;
32+ this . width = 0 ;
33+ this . xmax = 0 ;
34+ this . xmin = 0 ;
35+ this . ymax = 0 ;
36+ this . ymin = 0 ;
37+ this . zmax = undefined ;
38+ this . zmin = undefined ;
39+ this . extent = this ;
40+
41+ //确保私有变量不能被options修改
42+ this . private = [ "center" , "hasM" , "hasZ" , "height" , "width" , "type" , "extent" ] ;
43+ for ( let key in this . private ) {
44+ if ( notNULL ( options [ this . private [ key ] ] ) ) {
45+ throw new Error ( "[accessor] cannot assign to read-only property '" + this . private [ key ] + "' of ArcGisExtent" ) ;
46+ }
47+ }
48+
49+ extend ( this , options ) ;
50+
51+ //如果z、m有值,hasZ、hasM采薇true
52+ if ( this . zmax || this . zmin ) {
53+ this . hasZ = true ;
54+ }
55+ if ( this . mmax || this . mmin ) {
56+ this . hasM = true ;
57+ }
58+
59+ //生成bbox
60+ this . _extentPolygon = T . polygon ( [ [
61+ [ this . xmin , this . ymin ] ,
62+ [ this . xmin , this . ymax ] ,
63+ [ this . xmax , this . ymax ] ,
64+ [ this . ymax , this . ymin ] ,
65+ [ this . xmin , this . ymin ]
66+ ] ] , { name :"_extentPlygon" } ) ;
67+
68+ //生成中心点
69+ this . center = initCenter ( this ) ;
70+
71+ //计算width,height
72+ this . width = this . xmax - this . xmin ;
73+ this . height = this . ymax - this . ymin ;
74+ }
75+ }
76+
77+ function initCenter ( me ) {
78+ let points = [
79+ H . point ( [ me . xmin , me . ymin ] ) ,
80+ H . point ( [ me . xmin , me . ymax ] ) ,
81+ H . point ( [ me . xmax , me . ymax ] ) ,
82+ H . point ( [ me . ymax , me . ymin ] ) ,
83+ H . point ( [ me . xmin , me . ymin ] )
84+ ] ;
85+ let featureCollection = H . featureCollection ( points ) ;
86+ let coordinates = T . center ( featureCollection ) . geometry . coordinates ;
87+ return new ArcGisPoint ( {
88+ longitude : coordinates [ 0 ] ,
89+ latitude : coordinates [ 1 ]
90+ } ) ;
91+ }
92+
93+ /**
94+ * @function module:ArcGis.ArcGisExtent.prototype.equals
95+ * @description 比较两个Extent对象是否相等
96+ * @param extent - {ArcGisExtent} 必选项,要比较的ArcGisExtent对象。
97+ * @returns Boolean,对象是否相等
98+ */
99+ ArcGisExtent . prototype . equals = function ( extent ) {
100+ return this . mmax === extent . mmax &&
101+ this . mmin === extent . mmin &&
102+ this . xmax === extent . xmax &&
103+ this . xmin === extent . xmin &&
104+ this . ymax === extent . ymax &&
105+ this . ymin === extent . ymin &&
106+ this . zmax === extent . zmax &&
107+ this . zmin === extent . zmin ;
108+ }
109+
110+ /**
111+ * @function module:ArcGis.ArcGisExtent.prototype.contains
112+ * @description 判断是否包含一个点或者一个ArcGisExtent对象
113+ * @param geometry - {Geometry} 必选项,要比较的ArcGisExtent对象或者ArcGisPoint对象。
114+ * @returns Boolean,是否包含
115+ */
116+ ArcGisExtent . prototype . contains = function ( geometry ) {
117+ if ( geometry . type === "point" ) {
118+ let point = geometry . toArray ( ) ;
119+ point = T . point ( [ point [ 0 ] , point [ 1 ] ] ) ;
120+ return T . booleanContains ( this . _extentPolygon , point )
121+ }
122+ if ( geometry . type === "extent" ) {
123+ return T . booleanContains ( this . _extentPolygon , geometry . _extentPolygon )
124+ }
125+ return false ;
126+ }
127+
128+ /**
129+ * @function module:ArcGis.ArcGisExtent.prototype.expand
130+ * @description 根据输入的值,扩大或缩小一个ArcGisExtent
131+ * @param factor - {Number} 必选项,放大或缩小系数。
132+ * @returns ArcGisExtent,缩放后的ArcGisExtent
133+ */
134+ ArcGisExtent . prototype . expand = function ( factor ) {
135+ if ( factor instanceof Number ) {
136+ factor = Math . abs ( factor ) ;
137+ this . width = this . width * factor ;
138+ this . height = this . height * factor ;
139+ this . xmin = this . center . x - this . width / 2 ;
140+ this . xmax = this . center . x + this . width / 2 ;
141+ this . ymin = this . center . y - this . height / 2 ;
142+ this . ymax = this . center . y + this . height / 2 ;
143+ return this ;
144+ } else {
145+ throw new Error ( "require is not defined" ) ;
146+ }
147+ }
148+
149+ /**
150+ * @function module:ArcGis.ArcGisExtent.prototype.intersects
151+ * @description 比较点、多点、线、多边形、extent是否与当前extent相交
152+ * @param geometry - {Geometry} 必选项,要比较的几何对象。
153+ * @returns Boolean,是否相交
154+ */
155+ ArcGisExtent . prototype . intersects = function ( geometry ) {
156+ if ( ! geometry . type ) {
157+ return false ;
158+ }
159+ let geom ;
160+ if ( geometry . type === "polyline" ) {
161+ geom = H . multiLineString ( geometry . paths ) ;
162+ } else if ( geometry . type === "point" ) {
163+ geom = H . point ( geometry . toArray ( ) ) ;
164+ } else if ( geometry . type === "multipoint" ) {
165+ geom = H . multiPoint ( geometry . points ) ;
166+ } else if ( geometry . type === "extent" ) {
167+ geom = geometry . _extentPolygon ;
168+ } else if ( geometry . type === "polygon" ) {
169+ geom = H . polygon ( geometry . rings ) ;
170+ }
171+ return ! T . booleanDisjoint ( geom , this . _extentPolygon ) ;
172+ }
173+
174+ /**
175+ * @function module:ArcGis.ArcGisExtent.prototype.offset
176+ * @description 根据输入的dx, dy, dz值,平移extend
177+ * @param dx - {Number} 必选项,要平移的x值。
178+ * @param dx - {Number} 必选项,要平移的y值。
179+ * @param dx - {Number} 必选项,要平移的z值。
180+ * @returns ArcGisExtent,平移后的ArcGisExtent对象
181+ */
182+ ArcGisExtent . prototype . offset = function ( dx , dy , dz ) {
183+ this . xmax += dx ;
184+ this . xmin += dx ;
185+ this . ymax += dy ;
186+ this . ymin += dy ;
187+ if ( this . hasZ ) {
188+ this . zmax += dz ;
189+ this . zmin += dz ;
190+ } else {
191+ this . hasZ = true ;
192+ this . zmax = 0 ;
193+ this . zmin = 0 ;
194+ }
195+ return this ;
196+ }
197+
198+ /**
199+ * @function module:ArcGis.ArcGisExtent.prototype.centerAt
200+ * @description 根据输入的ArcGisPoint对象,生成衣蛾新的中心点
201+ * @param point - {ArcGisPoint} 必选项,新的中心点。
202+ * @returns ArcGisExtent
203+ */
204+ ArcGisExtent . prototype . centerAt = function ( point ) {
205+ if ( point instanceof ArcGisPoint ) {
206+ this . center = new ArcGisPoint ( {
207+ longitude : point . x ,
208+ latitude : point . y
209+ } ) ;
210+ this . xmin = this . center . x - this . width / 2 ;
211+ this . xmax = this . center . x + this . width / 2 ;
212+ this . ymin = this . center . y - this . height / 2 ;
213+ this . ymax = this . center . y + this . height / 2 ;
214+ return this ;
215+ }
216+ }
217+
218+ ArcGisExtent . prototype . normalize = function ( ) {
219+ return [ this ] ;
220+ }
221+
222+ /**
223+ * @function module:ArcGis.ArcGisExtent.prototype.union
224+ * @description 输入一个ArcGisExtent对象,与原extent对象合并,生成一个新的extent
225+ * @param extent - {ArcGisExtent} 必选项,要合并的ArcGisExtent对象。
226+ * @returns ArcGisExtent,新的Extent对象
227+ */
228+ ArcGisExtent . prototype . union = function ( extent ) {
229+ let cur = this . center ;
230+ let nex = extent . center ;
231+ if ( ( ( nex . x - cur . x ) > 0 && ( nex . y - cur . y ) > 0 ) || ( ( nex . x - cur . x ) === 0 && ( nex . y - cur . y ) > 0 )
232+ || ( ( nex . x - cur . x ) === 0 && ( nex . y - cur . y ) === 0 )
233+ || ( ( nex . x - cur . x ) > 0 && ( nex . y - cur . y ) === 0 ) ) {
234+ this . xmax = extent . xmax ;
235+ this . ymax = extent . ymax ;
236+ } else if ( ( nex . x - cur . x ) > 0 && ( nex . y - cur . y ) < 0 || ( ( nex . x - cur . x ) === 0 && ( nex . y - cur . y ) < 0 ) ) {
237+ this . xmax = extent . xmax ;
238+ this . ymin = extent . ymin ;
239+ } else if ( ( nex . x - cur . x ) < 0 && ( nex . y - cur . y ) < 0 ) {
240+ this . xmin = extent . xmin ;
241+ this . ymin = extent . ymin ;
242+ } else if ( ( nex . x - cur . x ) < 0 && ( nex . y - cur . y || ( ( nex . x - cur . x ) < 0 && ( nex . y - cur . y ) < 0 ) ) === 0 ) {
243+ this . xmin = extent . xmin ;
244+ this . ymax = extent . ymax ;
245+ }
246+ this . center = initCenter ( this ) ;
247+ //计算width,height
248+ this . width = this . xmax - this . xmin ;
249+ this . height = this . ymax - this . ymin ;
250+ return this ;
251+ }
252+
253+ /**
254+ * @function module:ArcGis.ArcGisExtent.prototype.toString
255+ * @description 返回如下格式的字符串:"xmin,ymin,xmax,ymax"
256+ * @returns Sting
257+ */
258+ ArcGisExtent . prototype . toString = function ( ) {
259+ return this . xmin + "," + this . ymin + "," + this . xmax + "," + this . ymax ;
260+ }
261+
262+ export { ArcGisExtent } ;
263+ Zondy . Service . ArcGisExtent = ArcGisExtent ;
0 commit comments