forked from google/earthengine-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geometry.js
906 lines (808 loc) · 31.9 KB
/
geometry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/**
* @fileoverview An object representing EE Geometries.
*/
goog.provide('ee.Geometry');
goog.provide('ee.Geometry.LineString');
goog.provide('ee.Geometry.LinearRing');
goog.provide('ee.Geometry.MultiLineString');
goog.provide('ee.Geometry.MultiPoint');
goog.provide('ee.Geometry.MultiPolygon');
goog.provide('ee.Geometry.Point');
goog.provide('ee.Geometry.Polygon');
goog.provide('ee.Geometry.Rectangle');
goog.require('ee.ApiFunction');
goog.require('ee.ComputedObject');
goog.require('ee.Serializer');
goog.require('ee.Types');
goog.require('ee.arguments');
goog.require('goog.array');
goog.require('goog.json.Serializer');
goog.require('goog.object');
goog.forwardDeclare('ee.ErrorMargin');
goog.forwardDeclare('ee.Projection');
/**
* Creates a geometry.
* @param {Object} geoJson The GeoJSON object describing the geometry or
* a ComputedObject to be reinterpreted as a Geometry. Supports
* CRS specifications as per the GeoJSON spec, but only allows named
* (rather than "linked" CRSs). If this includes a 'geodesic' field,
* and opt_geodesic is not specified, it will be used as opt_geodesic.
* @param {ee.Projection=} opt_proj An optional projection specification, either
* as a CRS ID code or as a WKT string. If specified, overrides any CRS
* found in the geoJson parameter. If unspecified and the geoJson does not
* declare a CRS, defaults to "EPSG:4326" (x=longitude, y=latitude).
* @param {boolean=} opt_geodesic Whether line segments should be interpreted
* as spherical geodesics. If false, indicates that line segments should
* be interpreted as planar lines in the specified CRS. If absent, defaults
* to true if the CRS is geographic (including the default EPSG:4326),
* or to false if the CRS is projected.
* @param {boolean=} opt_evenOdd If true, polygon interiors will be determined
* by the even/odd rule, where a point is inside if it crosses an odd number
* of edges to reach a point at infinity. Otherwise polygons use the left-
* inside rule, where interiors are on the left side of the shell's edges
* when walking the vertices in the given order. If unspecified, defaults to
* true.
* @constructor
* @extends {ee.ComputedObject}
* @export
*/
ee.Geometry = function(geoJson, opt_proj, opt_geodesic, opt_evenOdd) {
if (!(this instanceof ee.Geometry)) {
return ee.ComputedObject.construct(ee.Geometry, arguments);
}
// Note: evenOdd is a parameter name and may be a key in the
// first argument, the geoJson object. This means ee.arguments.extract()
// cannot reliably differentiate:
//
// 1) ee.Geometry(myGeoJsonObject)
// from 2) ee.Geometry({geoJson: myGeoJsonObject})
//
// However, we know that the geoJson object MUST contain the "type" key,
// which is not an expected param name. If we see this key in the first
// argument, we know the arguments were passed in sequence. If not, we
// assume the user intended to pass a named argument dictionary and use
// ee.arguments.extract() to validate and extract the keys.
if (!('type' in geoJson)) {
var args = ee.arguments.extract(ee.Geometry, arguments);
geoJson = args['geoJson'];
opt_proj = args['proj'];
opt_geodesic = args['geodesic'];
opt_evenOdd = args['evenOdd'];
}
ee.Geometry.initialize();
var computed = geoJson instanceof ee.ComputedObject &&
!(geoJson instanceof ee.Geometry && geoJson.type_);
var options = (goog.isDefAndNotNull(opt_proj) ||
goog.isDefAndNotNull(opt_geodesic) ||
goog.isDefAndNotNull(opt_evenOdd));
if (computed) {
if (options) {
throw new Error(
'Setting the CRS, geodesic, or evenOdd flag on a computed Geometry ' +
'is not supported. Use Geometry.transform().');
} else {
goog.base(this, geoJson.func, geoJson.args, geoJson.varName);
return;
}
}
// Below here, we're working with a GeoJSON literal.
if (geoJson instanceof ee.Geometry) {
geoJson = /** @type {Object} */(geoJson.encode());
}
if (!ee.Geometry.isValidGeometry_(geoJson)) {
throw Error('Invalid GeoJSON geometry: ' + JSON.stringify(geoJson));
}
goog.base(this, null, null);
/**
* The type of the geometry.
* @type {string}
* @private
*/
this.type_ = geoJson['type'];
/**
* The coordinates of the geometry, up to 4 nested levels with numbers at
* the last level. Null iff type is GeometryCollection.
* @type {Array?}
* @private
*/
this.coordinates_ =
goog.isDefAndNotNull(geoJson['coordinates']) ?
goog.object.unsafeClone(geoJson['coordinates']) :
null;
/**
* The subgeometries, non-null iff type is GeometryCollection.
* @type {Array?}
* @private
*/
this.geometries_ = geoJson['geometries'] || null;
/**
* The projection of the geometry.
* @type {String|undefined}
* @private
*/
this.proj_;
if (goog.isDefAndNotNull(opt_proj)) {
this.proj_ = opt_proj;
} else if ('crs' in geoJson) {
if (goog.isObject(geoJson['crs']) &&
geoJson['crs']['type'] == 'name' &&
goog.isObject(geoJson['crs']['properties']) &&
goog.isString(geoJson['crs']['properties']['name'])) {
this.proj_ = geoJson['crs']['properties']['name'];
} else {
throw Error('Invalid CRS declaration in GeoJSON: ' +
(new goog.json.Serializer()).serialize(geoJson['crs']));
}
}
/**
* Whether the geometry has spherical geodesic edges.
* @type {boolean|undefined}
* @private
*/
this.geodesic_ = opt_geodesic;
if (!goog.isDef(this.geodesic_) && 'geodesic' in geoJson) {
this.geodesic_ = Boolean(geoJson['geodesic']);
}
/**
* Whether polygon interiors are based on the even/odd rule. If false,
* the left-inside rule is used. If unspecified, defaults to true.
* @type {boolean|undefined}
* @private
*/
this.evenOdd_ = opt_evenOdd;
if (!goog.isDef(this.evenOdd_) && 'evenOdd' in geoJson) {
this.evenOdd_ = Boolean(geoJson['evenOdd']);
}
};
goog.inherits(ee.Geometry, ee.ComputedObject);
/**
* Whether the class has been initialized with API functions.
* @type {boolean}
* @private
*/
ee.Geometry.initialized_ = false;
/**
* Imports API functions to this class.
*/
ee.Geometry.initialize = function() {
if (!ee.Geometry.initialized_) {
ee.ApiFunction.importApi(ee.Geometry, 'Geometry', 'Geometry');
ee.Geometry.initialized_ = true;
}
};
/**
* Removes imported API functions from this class.
*/
ee.Geometry.reset = function() {
ee.ApiFunction.clearApi(ee.Geometry);
ee.Geometry.initialized_ = false;
};
////////////////////////////////////////////////////////////////////////////////
// Subclass constructors. //
////////////////////////////////////////////////////////////////////////////////
/**
* Constructs an ee.Geometry describing a point.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating EPSG:4326 points, e.g. ee.Geometry.Point(lng, lat).
*
* @param {!Array<number>} coords A list of two [x,y] coordinates in the given
* projection.
* @param {ee.Projection=} opt_proj The projection of this geometry, or
* EPSG:4326 if unspecified.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.Point = function(coords, opt_proj) {
if (!(this instanceof ee.Geometry.Point)) {
return ee.Geometry.createInstance_(ee.Geometry.Point, arguments);
}
var init = ee.Geometry.construct_(ee.Geometry.Point, 'Point', 1, arguments);
if (!(init instanceof ee.ComputedObject)) {
var xy = init['coordinates'];
if (!goog.isArray(xy) || xy.length != 2) {
throw Error('The Geometry.Point constructor requires 2 coordinates.');
}
}
goog.base(this, init);
};
goog.inherits(ee.Geometry.Point, ee.Geometry);
/**
* Constructs an ee.Geometry describing a MultiPoint.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating EPSG:4326 MultiPoints given an even number of arguments,
* e.g. ee.Geometry.MultiPoint(aLng, aLat, bLng, bLat, ...).
*
* @param {!Array<!Array<number>>|
* Array<!ee.Geometry>|
* Array<number>} coords
* A list of points, each in the GeoJSON 'coordinates' format of a Point, or
* a list of the x,y coordinates in the given projection, or an ee.Geometry
* describing a point.
* @param {ee.Projection=} opt_proj The projection of this geometry. If
* unspecified, the default is the projection of the input ee.Geometry, or
* EPSG:4326 if there are no ee.Geometry inputs.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.MultiPoint = function(coords, opt_proj) {
if (!(this instanceof ee.Geometry.MultiPoint)) {
return ee.Geometry.createInstance_(ee.Geometry.MultiPoint, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.MultiPoint, 'MultiPoint', 2, arguments));
};
goog.inherits(ee.Geometry.MultiPoint, ee.Geometry);
/**
* Constructs an ee.Geometry describing a rectangular polygon.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating EPSG:4326 Polygons given exactly four coordinates,
* e.g. ee.Geometry.Rectangle(minLng, minLat, maxLng, maxLat).
*
* @param {!Array<!Array<number>>|
* Array<!ee.Geometry>|
* Array<number>} coords
* The minimum and maximum corners of the rectangle, as a list of two points
* each in the format of GeoJSON 'Point' coordinates, or a list of two
* ee.Geometry describing a point, or a list of four numbers in the order
* xMin, yMin, xMax, yMax.
* @param {ee.Projection=} opt_proj The projection of this geometry. If
* unspecified, the default is the projection of the input ee.Geometry, or
* EPSG:4326 if there are no ee.Geometry inputs.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {boolean=} opt_evenOdd If true, polygon interiors will be determined
* by the even/odd rule, where a point is inside if it crosses an odd number
* of edges to reach a point at infinity. Otherwise polygons use the left-
* inside rule, where interiors are on the left side of the shell's edges
* when walking the vertices in the given order. If unspecified, defaults to
* true.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.Rectangle = function(coords, opt_proj, opt_geodesic, opt_evenOdd) {
if (!(this instanceof ee.Geometry.Rectangle)) {
return ee.Geometry.createInstance_(ee.Geometry.Rectangle, arguments);
}
var init = ee.Geometry.construct_(
ee.Geometry.Rectangle, 'Rectangle', 2, arguments);
if (!(init instanceof ee.ComputedObject)) {
// GeoJSON does not have a 'Rectangle' type, so expand it into a Polygon.
var xy = init['coordinates'];
if (xy.length != 2) {
throw Error('The Geometry.Rectangle constructor requires 2 points or 4 ' +
'coordinates.');
}
var x1 = xy[0][0];
var y1 = xy[0][1];
var x2 = xy[1][0];
var y2 = xy[1][1];
init['coordinates'] = [[[x1, y2], [x1, y1], [x2, y1], [x2, y2]]];
init['type'] = 'Polygon';
}
goog.base(this, init);
};
goog.inherits(ee.Geometry.Rectangle, ee.Geometry);
/**
* Constructs an ee.Geometry describing a LineString.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating geodesic EPSG:4326 LineStrings given an even number of
* arguments, e.g. ee.Geometry.LineString(aLng, aLat, bLng, bLat, ...).
*
* @param {!Array<!Array<number>>|
* Array<!ee.Geometry>|
* Array<number>} coords
* A list of at least two points. May be a list of coordinates in the
* GeoJSON 'LineString' format, a list of at least two ee.Geometry
* describing a point, or a list of at least four numbers defining the [x,y]
* coordinates of at least two points.
* @param {ee.Projection=} opt_proj The projection of this geometry. If
* unspecified, the default is the projection of the input ee.Geometry, or
* EPSG:4326 if there are no ee.Geometry inputs.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {ee.ErrorMargin=} opt_maxError Max error when input geometry must be
* reprojected to an explicitly requested result projection or geodesic
* state.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.LineString = function(
coords, opt_proj, opt_geodesic, opt_maxError) {
if (!(this instanceof ee.Geometry.LineString)) {
return ee.Geometry.createInstance_(ee.Geometry.LineString, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.LineString, 'LineString', 2, arguments));
};
goog.inherits(ee.Geometry.LineString, ee.Geometry);
/**
* Constructs an ee.Geometry describing a LinearRing. If the last point is not
* equal to the first, a duplicate of the first point will be added at the end.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating geodesic EPSG:4326 LinearRings given an even number of
* arguments, e.g.
* ee.Geometry.LinearRing(aLng, aLat, bLng, bLat, ..., aLng, aLat).
*
* @param {!Array<!Array<number>>|
* Array<!ee.Geometry>|
* Array<number>} coords
* A list of points in the ring. May be a list of coordinates in the GeoJSON
* 'LinearRing' format, a list of at least three ee.Geometry describing a
* point, or a list of at least six numbers defining the [x,y] coordinates
* of at least three points.
* @param {ee.Projection=} opt_proj The projection of this geometry. If
* unspecified, the default is the projection of the input ee.Geometry, or
* EPSG:4326 if there are no ee.Geometry inputs.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {ee.ErrorMargin=} opt_maxError Max error when input geometry must be
* reprojected to an explicitly requested result projection or geodesic
* state.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.LinearRing = function(
coords, opt_proj, opt_geodesic, opt_maxError) {
if (!(this instanceof ee.Geometry.LinearRing)) {
return ee.Geometry.createInstance_(ee.Geometry.LinearRing, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.LinearRing, 'LinearRing', 2, arguments));
};
goog.inherits(ee.Geometry.LinearRing, ee.Geometry);
/**
* Constructs an ee.Geometry describing a MultiLineString.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating geodesic EPSG:4326 MultiLineStrings with a single LineString,
* given an even number of arguments, e.g.
* ee.Geometry.MultiLineString(aLng, aLat, bLng, bLat, ...).
*
* @param {!Array<!Array<!Array<number>>>|
* Array<!ee.Geometry>|
* Array.<number>}
* coords A list of linestrings. May be a list of coordinates in the GeoJSON
* 'MultiLineString' format, a list of at least two ee.Geometry describing a
* LineString, or a list of number defining a single linestring.
* @param {ee.Projection=} opt_proj The projection of this geometry. If
* unspecified, the default is the projection of the input ee.Geometry, or
* EPSG:4326 if there are no ee.Geometry inputs.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {ee.ErrorMargin=} opt_maxError Max error when input geometry must be
* reprojected to an explicitly requested result projection or geodesic
* state.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.MultiLineString = function(
coords, opt_proj, opt_geodesic, opt_maxError) {
if (!(this instanceof ee.Geometry.MultiLineString)) {
return ee.Geometry.createInstance_(ee.Geometry.MultiLineString, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.MultiLineString, 'MultiLineString', 3, arguments));
};
goog.inherits(ee.Geometry.MultiLineString, ee.Geometry);
/**
* Constructs an ee.Geometry describing a polygon.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating geodesic EPSG:4326 Polygons with a single LinearRing
* given an even number of arguments, e.g.
* ee.Geometry.Polygon(aLng, aLat, bLng, bLat, ..., aLng, aLat).
*
* @param {!Array<!Array<!Array<number>>>|
* Array<!ee.Geometry>|
* Array<number>}
* coords A list of rings defining the boundaries of the polygon. May be a
* list of coordinates in the GeoJSON 'Polygon' format, a list of
* ee.Geometry describing a LinearRing, or a list of number defining a
* single polygon boundary.
* @param {ee.Projection=} opt_proj The projection of this geometry. The
* default is the projection of the inputs, where Numbers are assumed to be
* EPSG:4326.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {ee.ErrorMargin=} opt_maxError Max error when input geometry must be
* reprojected to an explicitly requested result projection or geodesic
* state.
* @param {boolean=} opt_evenOdd If true, polygon interiors will be determined
* by the even/odd rule, where a point is inside if it crosses an odd number
* of edges to reach a point at infinity. Otherwise polygons use the left-
* inside rule, where interiors are on the left side of the shell's edges
* when walking the vertices in the given order. If unspecified, defaults to
* true.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.Polygon = function(
coords, opt_proj, opt_geodesic, opt_maxError, opt_evenOdd) {
if (!(this instanceof ee.Geometry.Polygon)) {
return ee.Geometry.createInstance_(ee.Geometry.Polygon, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.Polygon, 'Polygon', 3, arguments));
};
goog.inherits(ee.Geometry.Polygon, ee.Geometry);
/**
* Constructs an ee.Geometry describing a MultiPolygon.
*
* For convenience, varargs may be used when all arguments are numbers. This
* allows creating geodesic EPSG:4326 MultiPolygons with a single Polygon with a
* single LinearRing given an even number of arguments, e.g.
* ee.Geometry.MultiPolygon(aLng, aLat, bLng, bLat, ..., aLng, aLat).
*
* @param {!Array<!Array<!Array<!Array<number>>>>|
* Array<ee.Geometry>|
* Array<number>}
* coords A list of polygons. May be a list of coordinates in the GeoJSON
* 'MultiPolygon' format, a list of ee.Geometry describing a Polygon, or a
* list of number defining a single polygon boundary.
* @param {ee.Projection=} opt_proj The projection of this geometry. The
* default is the projection of the inputs, where Numbers are assumed to be
* EPSG:4326.
* @param {boolean=} opt_geodesic If false, edges are straight in the
* projection. If true, edges are curved to follow the shortest path on the
* surface of the Earth. The default is the geodesic state of the inputs, or
* true if the inputs are numbers.
* @param {ee.ErrorMargin=} opt_maxError Max error when input geometry must be
* reprojected to an explicitly requested result projection or geodesic
* state.
* @param {boolean=} opt_evenOdd If true, polygon interiors will be determined
* by the even/odd rule, where a point is inside if it crosses an odd number
* of edges to reach a point at infinity. Otherwise polygons use the left-
* inside rule, where interiors are on the left side of the shell's edges
* when walking the vertices in the given order. If unspecified, defaults to
* true.
* @constructor
* @extends {ee.Geometry}
* @export
*/
ee.Geometry.MultiPolygon = function(
coords, opt_proj, opt_geodesic, opt_maxError, opt_evenOdd) {
if (!(this instanceof ee.Geometry.MultiPolygon)) {
return ee.Geometry.createInstance_(ee.Geometry.MultiPolygon, arguments);
}
goog.base(this, ee.Geometry.construct_(
ee.Geometry.MultiPolygon, 'MultiPolygon', 4, arguments));
};
goog.inherits(ee.Geometry.MultiPolygon, ee.Geometry);
////////////////////////////////////////////////////////////////////////////////
// Instance methods. //
////////////////////////////////////////////////////////////////////////////////
/**
* @param {function(*): *=} opt_encoder A function that can be called to encode
* the components of an object.
* @return {*} An encoded representation of the geometry.
*/
ee.Geometry.prototype.encode = function(opt_encoder) {
if (!this.type_) {
// This is not a concrete Geometry.
if (!opt_encoder) {
throw Error('Must specify an encode function when encoding a ' +
'computed geometry.');
}
return ee.ComputedObject.prototype.encode.call(this, opt_encoder);
}
var result = {'type': this.type_};
if (this.type_ == 'GeometryCollection') {
result['geometries'] = this.geometries_;
} else {
result['coordinates'] = this.coordinates_;
}
if (goog.isDefAndNotNull(this.proj_)) {
result['crs'] = {
'type': 'name',
'properties': {
'name': this.proj_
}
};
}
if (goog.isDefAndNotNull(this.geodesic_)) {
result['geodesic'] = this.geodesic_;
}
if (goog.isDefAndNotNull(this.evenOdd_)) {
result['evenOdd'] = this.evenOdd_;
}
return /** @type {ee.data.GeoJSONGeometry} */(result);
};
/**
* @return {!ee.data.GeoJSONGeometry} A GeoJSON representation of the geometry.
* @export
*/
ee.Geometry.prototype.toGeoJSON = function() {
if (this.func) {
throw new Error('Can\'t convert a computed Geometry to GeoJSON. ' +
'Use getInfo() instead.');
}
return /** @type {!ee.data.GeoJSONGeometry} */ (this.encode());
};
/**
* @return {string} A GeoJSON string representation of the geometry.
* @export
*/
ee.Geometry.prototype.toGeoJSONString = function() {
if (this.func) {
throw new Error('Can\'t convert a computed Geometry to GeoJSON. ' +
'Use getInfo() instead.');
}
return (new goog.json.Serializer()).serialize(this.toGeoJSON());
};
/**
* @return {string} The serialized representation of this object.
* @export
*/
ee.Geometry.prototype.serialize = function() {
return ee.Serializer.toJSON(this);
};
/** @override */
ee.Geometry.prototype.toString = function() {
return 'ee.Geometry(' + this.toGeoJSONString() + ')';
};
////////////////////////////////////////////////////////////////////////////////
// Implementation. //
////////////////////////////////////////////////////////////////////////////////
// TODO(user): Validation should ensure that a polygon has >2 points.
// Context at cl/158861478.
/**
* Checks if a geometry looks valid.
* @param {Object} geometry The geometry to validate.
* @return {boolean} whether the geometry looks valid.
* @private
*/
ee.Geometry.isValidGeometry_ = function(geometry) {
var type = geometry['type'];
if (type == 'GeometryCollection') {
var geometries = geometry['geometries'];
if (!goog.isArray(geometries)) {
return false;
}
for (var i = 0; i < geometries.length; i++) {
if (!ee.Geometry.isValidGeometry_(geometries[i])) {
return false;
}
}
return true;
} else {
var coords = geometry['coordinates'];
var nesting = ee.Geometry.isValidCoordinates_(coords);
return (type == 'Point' && nesting == 1) ||
(type == 'MultiPoint' && (nesting == 2 || coords.length == 0)) ||
(type == 'LineString' && nesting == 2) ||
(type == 'LinearRing' && nesting == 2) ||
(type == 'MultiLineString' && (nesting == 3 || coords.length == 0)) ||
(type == 'Polygon' && nesting == 3) ||
(type == 'MultiPolygon' && (nesting == 4 || coords.length == 0));
}
};
/**
* Validate the coordinates of a geometry.
* @param {number|!Array.<*>} shape The coordinates to validate.
* @return {number} The number of nested lists or -1 on error.
* @private
*/
ee.Geometry.isValidCoordinates_ = function(shape) {
if (!goog.isArray(shape)) {
return -1;
}
if (goog.isArray(shape[0])) {
var count = ee.Geometry.isValidCoordinates_(shape[0]);
// If more than 1 ring or polygon, they should have the same nesting.
for (var i = 1; i < shape.length; i++) {
if (ee.Geometry.isValidCoordinates_(shape[i]) != count) {
return -1;
}
}
return count + 1;
} else {
// Make sure the coordinates are all numbers.
for (var i = 0; i < shape.length; i++) {
if (!goog.isNumber(shape[i])) {
return -1;
}
}
// Test that we have an even number of coordinates.
return (shape.length % 2 == 0) ? 1 : -1;
}
};
/**
* Create a line from a list of points.
* @param {IArrayLike} coordinates The points to convert. Must be a
* multiple of 2.
* @return {!Array<!Array<number>>} A list of pairs of points.
* @private
*/
ee.Geometry.coordinatesToLine_ = function(coordinates) {
if (!goog.isNumber(coordinates[0])) {
return /** @type {!Array<!Array<number>>} */ (coordinates);
}
if (coordinates.length == 2) {
return /** @type {!Array<!Array<number>>} */ (coordinates);
}
if (coordinates.length % 2 != 0) {
throw Error('Invalid number of coordinates: ' + coordinates.length);
}
var line = [];
for (var i = 0; i < coordinates.length; i += 2) {
var pt = [coordinates[i], coordinates[i + 1]];
line.push(pt);
}
return line;
};
/**
* Constructs either a GeoJSON object or a ComputedObject for a JS geometry
* constructor and its arguments.
* @param {!Function} jsConstructorFn The JS geometry constructor called.
* @param {string} apiConstructorName The name of the server-side geometry
* constructor to use.
* @param {number} depth The nesting depth at which points are found within
* the coordinates array.
* @param {!Arguments} originalArgs The arguments to the JS constructor.
* @return {!Object|ee.ComputedObject} If the arguments are simple,
* a GeoJSON object describing the geometry. Otherwise a
* ComputedObject calling the appropriate server-side constructor.
* @private
*/
ee.Geometry.construct_ = function(
jsConstructorFn, apiConstructorName, depth, originalArgs) {
var eeArgs = ee.Geometry.getEeApiArgs_(jsConstructorFn, originalArgs);
// Standardize the coordinates and test if they are simple enough for
// client-side initialization.
if (ee.Geometry.hasServerValue_(eeArgs['coordinates']) ||
goog.isDefAndNotNull(eeArgs['crs']) ||
goog.isDefAndNotNull(eeArgs['maxError'])) {
// Some arguments cannot be handled in the client, so make a server call.
// Note we don't declare a default evenOdd value, so the server can infer
// a default based on the projection.
var serverName = 'GeometryConstructors.' + apiConstructorName;
return new ee.ApiFunction(serverName).apply(eeArgs);
} else {
// Everything can be handled here, so init a simple GeoJSON object.
var geoJson = eeArgs;
geoJson['type'] = apiConstructorName;
geoJson['coordinates'] = ee.Geometry.fixDepth_(
depth, geoJson['coordinates']);
var isPolygon =
goog.array.contains(
['Polygon', 'Rectangle', 'MultiPolygon'],
apiConstructorName);
if (isPolygon && !goog.isDefAndNotNull(geoJson['evenOdd'])) {
// Default to evenOdd=true for any kind of polygon.
geoJson['evenOdd'] = true;
}
if (isPolygon &&
geoJson['geodesic'] === false &&
geoJson['evenOdd'] === false) {
throw new Error('Planar interiors must be even/odd.');
}
return geoJson;
}
};
/**
* Creates an argument dictionary for a server-side geometry constructor from
* the arguments to a JS geometry constructor. The arguments the JS constructor
* can be passed as either a list of coordinates (as var_args), a sequence of
* parameters, or a dictionary of named parameters.
* @param {!Function} jsConstructorFn The JS constructor to parse arguments for.
* @param {!Arguments} originalArgs The arguments to the JS constructor.
* @return {!Object} The named server-side geometry constructor arguments.
* @private
*/
ee.Geometry.getEeApiArgs_ = function(jsConstructorFn, originalArgs) {
if (goog.array.every(originalArgs, ee.Types.isNumber)) {
// All numbers, so convert them to a true array.
return {'coordinates': goog.array.toArray(originalArgs)};
} else {
var args = ee.arguments.extract(jsConstructorFn, originalArgs);
// Convert the argument dictionary to proper GeoJSON. Some of the parameter
// names intentionally don't map precisely to GeoJSON key names.
// For example, the server expects different CRS values than GeoJSON.
args['coordinates'] = args['coords'];
delete args['coords'];
args['crs'] = args['proj'];
delete args['proj'];
return goog.object.filter(args, goog.isDefAndNotNull);
}
};
/**
* Returns whether any of the coordinates are computed values or geometries;
* these types must be resolved by the server (evaluated in the case of computed
* values, and processed to a single projection and geodesic state in the case
* of geometries.)
*
* @param {!Array} coordinates A nested list of ... of number coordinates.
* @return {boolean} Whether all coordinates are lists or numbers.
* @private
*/
ee.Geometry.hasServerValue_ = function(coordinates) {
if (goog.isArray(coordinates)) {
return goog.array.some(coordinates, ee.Geometry.hasServerValue_);
} else {
return coordinates instanceof ee.ComputedObject;
}
};
/**
* Fixes the depth of the given coordinates, and checks that each element has
* the expected depth as all other elements at that depth.
*
* @private
* @param {number} depth The desired depth.
* @param {!Array} coords The coordinates to fix.
* @return {!Array} The fixed coordinates, with the deepest
* elements at the requested depth.
*/
ee.Geometry.fixDepth_ = function(depth, coords) {
if (depth < 1 || depth > 4) {
throw new Error('Unexpected nesting level.');
}
// Handle a list of numbers.
if (goog.array.every(coords, goog.isNumber)) {
coords = ee.Geometry.coordinatesToLine_(coords);
}
// Make sure the number of nesting levels is correct.
var item = coords;
var count = 0;
while (goog.isArray(item)) {
item = item[0];
count++;
}
while (count < depth) {
coords = [coords];
count++;
}
if (ee.Geometry.isValidCoordinates_(coords) != depth) {
throw Error('Invalid geometry');
}
// Empty arrays should not be wrapped.
item = coords;
while (goog.isArray(item) && item.length == 1) {
item = item[0];
}
if (goog.isArray(item) && item.length == 0) {
return [];
}
return /** @type {!Array} */ (coords);
};
/**
* Creates an instance of an object given a constructor and a set of arguments.
* @param {function(this:T, ...?): T} klass The class constructor.
* @param {Arguments} args The arguments to pass to the constructor.
* @return {T} The new instance.
* @template T
* @private
*/
ee.Geometry.createInstance_ = function(klass, args) {
/** @constructor */
var f = function() {};
f.prototype = klass.prototype;
var instance = new f();
var result = klass.apply(instance, args);
return result !== undefined ? result : instance;
};
/** @override */
ee.Geometry.prototype.name = function() {
return 'Geometry';
};