-
Notifications
You must be signed in to change notification settings - Fork 439
/
Geometry.java
1889 lines (1783 loc) · 68.5 KB
/
Geometry.java
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.geom;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import org.locationtech.jts.algorithm.Centroid;
import org.locationtech.jts.algorithm.ConvexHull;
import org.locationtech.jts.algorithm.InteriorPoint;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.locationtech.jts.operation.buffer.BufferParameters;
import org.locationtech.jts.operation.distance.DistanceOp;
import org.locationtech.jts.operation.linemerge.LineMerger;
import org.locationtech.jts.operation.predicate.RectangleContains;
import org.locationtech.jts.operation.predicate.RectangleIntersects;
import org.locationtech.jts.operation.relate.RelateOp;
import org.locationtech.jts.operation.union.UnaryUnionOp;
import org.locationtech.jts.operation.valid.IsSimpleOp;
import org.locationtech.jts.operation.valid.IsValidOp;
import org.locationtech.jts.util.Assert;
/**
* A representation of a planar, linear vector geometry.
* <P>
*
* <H3>Binary Predicates</H3>
* Because it is not clear at this time
* what semantics for spatial
* analysis methods involving <code>GeometryCollection</code>s would be useful,
* <code>GeometryCollection</code>s are not supported as arguments to binary
* predicates or the <code>relate</code>
* method.
*
* <H3>Overlay Methods</H3>
*
* The overlay methods
* return the most specific class possible to represent the result. If the
* result is homogeneous, a <code>Point</code>, <code>LineString</code>, or
* <code>Polygon</code> will be returned if the result contains a single
* element; otherwise, a <code>MultiPoint</code>, <code>MultiLineString</code>,
* or <code>MultiPolygon</code> will be returned. If the result is
* heterogeneous a <code>GeometryCollection</code> will be returned. <P>
*
* Because it is not clear at this time what semantics for set-theoretic
* methods involving <code>GeometryCollection</code>s would be useful,
* <code>GeometryCollections</code>
* are not supported as arguments to the set-theoretic methods.
*
* <H4>Representation of Computed Geometries </H4>
*
* The SFS states that the result
* of a set-theoretic method is the "point-set" result of the usual
* set-theoretic definition of the operation (SFS 3.2.21.1). However, there are
* sometimes many ways of representing a point set as a <code>Geometry</code>.
* <P>
*
* The SFS does not specify an unambiguous representation of a given point set
* returned from a spatial analysis method. One goal of JTS is to make this
* specification precise and unambiguous. JTS uses a canonical form for
* <code>Geometry</code>s returned from overlay methods. The canonical
* form is a <code>Geometry</code> which is simple and noded:
* <UL>
* <LI> Simple means that the Geometry returned will be simple according to
* the JTS definition of <code>isSimple</code>.
* <LI> Noded applies only to overlays involving <code>LineString</code>s. It
* means that all intersection points on <code>LineString</code>s will be
* present as endpoints of <code>LineString</code>s in the result.
* </UL>
* This definition implies that non-simple geometries which are arguments to
* spatial analysis methods must be subjected to a line-dissolve process to
* ensure that the results are simple.
*
* <H4> Constructed Points And The Precision Model </H4>
*
* The results computed by the set-theoretic methods may
* contain constructed points which are not present in the input <code>Geometry</code>
* s. These new points arise from intersections between line segments in the
* edges of the input <code>Geometry</code>s. In the general case it is not
* possible to represent constructed points exactly. This is due to the fact
* that the coordinates of an intersection point may contain twice as many bits
* of precision as the coordinates of the input line segments. In order to
* represent these constructed points explicitly, JTS must truncate them to fit
* the <code>PrecisionModel</code>. <P>
*
* Unfortunately, truncating coordinates moves them slightly. Line segments
* which would not be coincident in the exact result may become coincident in
* the truncated representation. This in turn leads to "topology collapses" --
* situations where a computed element has a lower dimension than it would in
* the exact result. <P>
*
* When JTS detects topology collapses during the computation of spatial
* analysis methods, it will throw an exception. If possible the exception will
* report the location of the collapse. <P>
*
* <h3>Geometry Equality</h3>
*
* There are two ways of comparing geometries for equality:
* <b>structural equality</b> and <b>topological equality</b>.
*
* <h4>Structural Equality</h4>
*
* Structural Equality is provided by the
* {@link #equalsExact(Geometry)} method.
* This implements a comparison based on exact, structural pointwise
* equality.
* The {@link #equals(Object)} is a synonym for this method,
* to provide structural equality semantics for
* use in Java collections.
* It is important to note that structural pointwise equality
* is easily affected by things like
* ring order and component order. In many situations
* it will be desirable to normalize geometries before
* comparing them (using the {@link #norm()}
* or {@link #normalize()} methods).
* {@link #equalsNorm(Geometry)} is provided
* as a convenience method to compute equality over
* normalized geometries, but it is expensive to use.
* Finally, {@link #equalsExact(Geometry, double)}
* allows using a tolerance value for point comparison.
*
*
* <h4>Topological Equality</h4>
*
* Topological Equality is provided by the
* {@link #equalsTopo(Geometry)} method.
* It implements the SFS definition of point-set equality
* defined in terms of the DE-9IM matrix.
* To support the SFS naming convention, the method
* {@link #equals(Geometry)} is also provided as a synonym.
* However, due to the potential for confusion with {@link #equals(Object)}
* its use is discouraged.
* <p>
* Since {@link #equals(Object)} and {@link #hashCode()} are overridden,
* Geometries can be used effectively in Java collections.
*
*@version 1.7
*/
public abstract class Geometry
implements Cloneable, Comparable, Serializable
{
private static final long serialVersionUID = 8763622679187376702L;
protected static final int TYPECODE_POINT = 0;
protected static final int TYPECODE_MULTIPOINT = 1;
protected static final int TYPECODE_LINESTRING = 2;
protected static final int TYPECODE_LINEARRING = 3;
protected static final int TYPECODE_MULTILINESTRING = 4;
protected static final int TYPECODE_POLYGON = 5;
protected static final int TYPECODE_MULTIPOLYGON = 6;
protected static final int TYPECODE_GEOMETRYCOLLECTION = 7;
public static final String TYPENAME_POINT = "Point";
public static final String TYPENAME_MULTIPOINT = "MultiPoint";
public static final String TYPENAME_LINESTRING = "LineString";
public static final String TYPENAME_LINEARRING = "LinearRing";
public static final String TYPENAME_MULTILINESTRING = "MultiLineString";
public static final String TYPENAME_POLYGON = "Polygon";
public static final String TYPENAME_MULTIPOLYGON = "MultiPolygon";
public static final String TYPENAME_GEOMETRYCOLLECTION = "GeometryCollection";
private final static GeometryComponentFilter geometryChangedFilter = new GeometryComponentFilter() {
public void filter(Geometry geom) {
geom.geometryChangedAction();
}
};
/**
* The bounding box of this <code>Geometry</code>.
*/
protected Envelope envelope;
/**
* The {@link GeometryFactory} used to create this Geometry
*/
protected final GeometryFactory factory;
/**
* The ID of the Spatial Reference System used by this <code>Geometry</code>
*/
protected int SRID;
/**
* An object reference which can be used to carry ancillary data defined
* by the client.
*/
private Object userData = null;
/**
* Creates a new <code>Geometry</code> via the specified GeometryFactory.
*
* @param factory
*/
public Geometry(GeometryFactory factory) {
this.factory = factory;
this.SRID = factory.getSRID();
}
/**
* Returns the name of this Geometry's actual class.
*
*@return the name of this <code>Geometry</code>s actual class
*/
public abstract String getGeometryType();
/**
* Returns true if the array contains any non-empty <code>Geometry</code>s.
*
*@param geometries an array of <code>Geometry</code>s; no elements may be
* <code>null</code>
*@return <code>true</code> if any of the <code>Geometry</code>s
* <code>isEmpty</code> methods return <code>false</code>
*/
protected static boolean hasNonEmptyElements(Geometry[] geometries) {
for (int i = 0; i < geometries.length; i++) {
if (!geometries[i].isEmpty()) {
return true;
}
}
return false;
}
/**
* Returns true if the array contains any <code>null</code> elements.
*
*@param array an array to validate
*@return <code>true</code> if any of <code>array</code>s elements are
* <code>null</code>
*/
protected static boolean hasNullElements(Object[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
return true;
}
}
return false;
}
/**
* Returns the ID of the Spatial Reference System used by the <code>Geometry</code>.
* <P>
*
* JTS supports Spatial Reference System information in the simple way
* defined in the SFS. A Spatial Reference System ID (SRID) is present in
* each <code>Geometry</code> object. <code>Geometry</code> provides basic
* accessor operations for this field, but no others. The SRID is represented
* as an integer.
*
*@return the ID of the coordinate space in which the <code>Geometry</code>
* is defined.
*
*/
public int getSRID() {
return SRID;
}
/**
* Sets the ID of the Spatial Reference System used by the <code>Geometry</code>.
* <p>
* <b>NOTE:</b> This method should only be used for exceptional circumstances or
* for backwards compatibility. Normally the SRID should be set on the
* {@link GeometryFactory} used to create the geometry.
* SRIDs set using this method will <i>not</i> be propagated to
* geometries returned by constructive methods.
*
* @see GeometryFactory
*/
public void setSRID(int SRID) {
this.SRID = SRID;
}
/**
* Gets the factory which contains the context in which this geometry was created.
*
* @return the factory for this geometry
*/
public GeometryFactory getFactory() {
return factory;
}
/**
* Gets the user data object for this geometry, if any.
*
* @return the user data object, or <code>null</code> if none set
*/
public Object getUserData() {
return userData;
}
/**
* Returns the number of {@link Geometry}s in a {@link GeometryCollection}
* (or 1, if the geometry is not a collection).
*
* @return the number of geometries contained in this geometry
*/
public int getNumGeometries() {
return 1;
}
/**
* Returns an element {@link Geometry} from a {@link GeometryCollection}
* (or <code>this</code>, if the geometry is not a collection).
*
* @param n the index of the geometry element
* @return the n'th geometry contained in this geometry
*/
public Geometry getGeometryN(int n) {
return this;
}
/**
* A simple scheme for applications to add their own custom data to a Geometry.
* An example use might be to add an object representing a Coordinate Reference System.
* <p>
* Note that user data objects are not present in geometries created by
* construction methods.
*
* @param userData an object, the semantics for which are defined by the
* application using this Geometry
*/
public void setUserData(Object userData) {
this.userData = userData;
}
/**
* Returns the <code>PrecisionModel</code> used by the <code>Geometry</code>.
*
*@return the specification of the grid of allowable points, for this
* <code>Geometry</code> and all other <code>Geometry</code>s
*/
public PrecisionModel getPrecisionModel() {
return factory.getPrecisionModel();
}
/**
* Returns a vertex of this geometry
* (usually, but not necessarily, the first one),
* or <code>null</code> if the geometry is empty.
* The returned coordinate should not be assumed
* to be an actual <code>Coordinate</code> object used in
* the internal representation.
*
*@return a coordinate which is a vertex of this <code>Geometry</code>.
*@return null if this Geometry is empty
*/
public abstract Coordinate getCoordinate();
/**
* Returns an array containing the values of all the vertices for
* this geometry.
* If the geometry is a composite, the array will contain all the vertices
* for the components, in the order in which the components occur in the geometry.
* <p>
* In general, the array cannot be assumed to be the actual internal
* storage for the vertices. Thus modifying the array
* may not modify the geometry itself.
* Use the {@link CoordinateSequence#setOrdinate} method
* (possibly on the components) to modify the underlying data.
* If the coordinates are modified,
* {@link #geometryChanged} must be called afterwards.
*
*@return the vertices of this <code>Geometry</code>
*@see #geometryChanged
*@see CoordinateSequence#setOrdinate
*/
public abstract Coordinate[] getCoordinates();
/**
* Returns the count of this <code>Geometry</code>s vertices. The <code>Geometry</code>
* s contained by composite <code>Geometry</code>s must be
* Geometry's; that is, they must implement <code>getNumPoints</code>
*
*@return the number of vertices in this <code>Geometry</code>
*/
public abstract int getNumPoints();
/**
* Tests whether this {@link Geometry} is simple.
* The SFS definition of simplicity
* follows the general rule that a Geometry is simple if it has no points of
* self-tangency, self-intersection or other anomalous points.
* <p>
* Simplicity is defined for each {@link Geometry} subclass as follows:
* <ul>
* <li>Valid polygonal geometries are simple, since their rings
* must not self-intersect. <code>isSimple</code>
* tests for this condition and reports <code>false</code> if it is not met.
* (This is a looser test than checking for validity).
* <li>Linear rings have the same semantics.
* <li>Linear geometries are simple if they do not self-intersect at points
* other than boundary points.
* <li>Zero-dimensional geometries (points) are simple if they have no
* repeated points.
* <li>Empty <code>Geometry</code>s are always simple.
* </ul>
*
* @return <code>true</code> if this <code>Geometry</code> is simple
* @see #isValid
*/
public boolean isSimple()
{
IsSimpleOp op = new IsSimpleOp(this);
return op.isSimple();
}
/**
* Tests whether this <code>Geometry</code>
* is topologically valid, according to the OGC SFS specification.
* <p>
* For validity rules see the Javadoc for the specific Geometry subclass.
*
*@return <code>true</code> if this <code>Geometry</code> is valid
*
* @see IsValidOp
*/
public boolean isValid()
{
return IsValidOp.isValid(this);
}
/**
* Tests whether the set of points covered by this <code>Geometry</code> is
* empty.
* <p>
* Note this test is for topological emptiness,
* not structural emptiness.
* A collection containing only empty elements is reported as empty.
* To check structural emptiness use {@link #getNumGeometries()}.
*
*@return <code>true</code> if this <code>Geometry</code> does not cover any points
*/
public abstract boolean isEmpty();
/**
* Returns the minimum distance between this <code>Geometry</code>
* and another <code>Geometry</code>.
*
* @param g the <code>Geometry</code> from which to compute the distance
* @return the distance between the geometries
* @return 0 if either input geometry is empty
* @throws IllegalArgumentException if g is null
*/
public double distance(Geometry g)
{
return DistanceOp.distance(this, g);
}
/**
* Tests whether the distance from this <code>Geometry</code>
* to another is less than or equal to a specified value.
*
* @param geom the Geometry to check the distance to
* @param distance the distance value to compare
* @return <code>true</code> if the geometries are less than <code>distance</code> apart.
*/
public boolean isWithinDistance(Geometry geom, double distance)
{
return DistanceOp.isWithinDistance(this, geom, distance);
}
/**
* Tests whether this is a rectangular {@link Polygon}.
*
* @return true if the geometry is a rectangle.
*/
public boolean isRectangle()
{
// Polygon overrides to check for actual rectangle
return false;
}
/**
* Returns the area of this <code>Geometry</code>.
* Areal Geometries have a non-zero area.
* They override this function to compute the area.
* Others return 0.0
*
*@return the area of the Geometry
*/
public double getArea()
{
return 0.0;
}
/**
* Returns the length of this <code>Geometry</code>.
* Linear geometries return their length.
* Areal geometries return their perimeter.
* They override this function to compute the area.
* Others return 0.0
*
*@return the length of the Geometry
*/
public double getLength()
{
return 0.0;
}
/**
* Computes the centroid of this <code>Geometry</code>.
* The centroid
* is equal to the centroid of the set of component Geometries of highest
* dimension (since the lower-dimension geometries contribute zero
* "weight" to the centroid).
* <p>
* The centroid of an empty geometry is <code>POINT EMPTY</code>.
*
* @return a {@link Point} which is the centroid of this Geometry
*/
public Point getCentroid()
{
if (isEmpty())
return factory.createPoint();
Coordinate centPt = Centroid.getCentroid(this);
return createPointFromInternalCoord(centPt, this);
}
/**
* Computes an interior point of this <code>Geometry</code>.
* An interior point is guaranteed to lie in the interior of the Geometry,
* if it possible to calculate such a point exactly. Otherwise,
* the point may lie on the boundary of the geometry.
* <p>
* The interior point of an empty geometry is <code>POINT EMPTY</code>.
*
* @return a {@link Point} which is in the interior of this Geometry
*/
public Point getInteriorPoint()
{
if (isEmpty()) return factory.createPoint();
Coordinate pt = InteriorPoint.getInteriorPoint(this);
return createPointFromInternalCoord(pt, this);
}
/**
* Returns the dimension of this geometry.
* The dimension of a geometry is is the topological
* dimension of its embedding in the 2-D Euclidean plane.
* In the JTS spatial model, dimension values are in the set {0,1,2}.
* <p>
* Note that this is a different concept to the dimension of
* the vertex {@link Coordinate}s.
* The geometry dimension can never be greater than the coordinate dimension.
* For example, a 0-dimensional geometry (e.g. a Point)
* may have a coordinate dimension of 3 (X,Y,Z).
*
* @return the topological dimension of this geometry.
*
* @see #hasDimension(int)
*/
public abstract int getDimension();
/**
* Tests whether an atomic geometry or any element of a collection
* has the specified dimension.
* In particular, this can be used with mixed-dimension {@link GeometryCollection}s
* to test if they contain an element of the specified dimension.
*
* @param dim the dimension to test
* @return true if the geometry has or contains an element with the dimension
*
* @see #getDimension()
*/
public boolean hasDimension(int dim) {
return dim == getDimension();
}
/**
* Returns the boundary, or an empty geometry of appropriate dimension
* if this <code>Geometry</code> is empty.
* (In the case of zero-dimensional geometries, '
* an empty GeometryCollection is returned.)
* For a discussion of this function, see the OpenGIS Simple
* Features Specification. As stated in SFS Section 2.1.13.1, "the boundary
* of a Geometry is a set of Geometries of the next lower dimension."
*
*@return the closure of the combinatorial boundary of this <code>Geometry</code>
*/
public abstract Geometry getBoundary();
/**
* Returns the dimension of this <code>Geometry</code>s inherent boundary.
*
*@return the dimension of the boundary of the class implementing this
* interface, whether or not this object is the empty geometry. Returns
* <code>Dimension.FALSE</code> if the boundary is the empty geometry.
*/
public abstract int getBoundaryDimension();
/**
* Gets a Geometry representing the envelope (bounding box) of
* this <code>Geometry</code>.
* <p>
* If this <code>Geometry</code> is:
* <ul>
* <li>empty, returns an empty <code>Point</code>.
* <li>a point, returns a <code>Point</code>.
* <li>a line parallel to an axis, a two-vertex <code>LineString</code>
* <li>otherwise, returns a
* <code>Polygon</code> whose vertices are (minx miny, minx maxy,
* maxx maxy, maxx miny, minx miny).
* </ul>
*
*@return a Geometry representing the envelope of this Geometry
*
* @see GeometryFactory#toGeometry(Envelope)
*/
public Geometry getEnvelope() {
return getFactory().toGeometry(getEnvelopeInternal());
}
/**
* Gets an {@link Envelope} containing
* the minimum and maximum x and y values in this <code>Geometry</code>.
* If the geometry is empty, an empty <code>Envelope</code>
* is returned.
* <p>
* The returned object is a copy of the one maintained internally,
* to avoid aliasing issues.
* For best performance, clients which access this
* envelope frequently should cache the return value.
*
*@return the envelope of this <code>Geometry</code>.
*@return an empty Envelope if this Geometry is empty
*/
public Envelope getEnvelopeInternal() {
if (envelope == null) {
envelope = computeEnvelopeInternal();
}
return new Envelope(envelope);
}
/**
* Notifies this geometry that its coordinates have been changed by an external
* party (for example, via a {@link CoordinateFilter}).
* When this method is called the geometry will flush
* and/or update any derived information it has cached (such as its {@link Envelope} ).
* The operation is applied to all component Geometries.
*/
public void geometryChanged() {
apply(geometryChangedFilter);
}
/**
* Notifies this Geometry that its Coordinates have been changed by an external
* party. When #geometryChanged is called, this method will be called for
* this Geometry and its component Geometries.
*
* @see #apply(GeometryComponentFilter)
*/
protected void geometryChangedAction() {
envelope = null;
}
/**
* Tests whether this geometry is disjoint from the argument geometry.
* <p>
* The <code>disjoint</code> predicate has the following equivalent definitions:
* <ul>
* <li>The two geometries have no point in common
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* <code>[FF*FF****]</code>
* <li><code>! g.intersects(this) = true</code>
* <br>(<code>disjoint</code> is the inverse of <code>intersects</code>)
* </ul>
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if the two <code>Geometry</code>s are
* disjoint
*
* @see Geometry#intersects
*/
public boolean disjoint(Geometry g) {
return ! intersects(g);
}
/**
* Tests whether this geometry touches the
* argument geometry.
* <p>
* The <code>touches</code> predicate has the following equivalent definitions:
* <ul>
* <li>The geometries have at least one point in common,
* but their interiors do not intersect.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* at least one of the following patterns
* <ul>
* <li><code>[FT*******]</code>
* <li><code>[F**T*****]</code>
* <li><code>[F***T****]</code>
* </ul>
* </ul>
* If both geometries have dimension 0, the predicate returns <code>false</code>,
* since points have only interiors.
* This predicate is symmetric.
*
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if the two <code>Geometry</code>s touch;
* Returns <code>false</code> if both <code>Geometry</code>s are points
*/
public boolean touches(Geometry g) {
// short-circuit test
if (! getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
return false;
return relate(g).isTouches(getDimension(), g.getDimension());
}
/**
* Tests whether this geometry intersects the argument geometry.
* <p>
* The <code>intersects</code> predicate has the following equivalent definitions:
* <ul>
* <li>The two geometries have at least one point in common
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* at least one of the patterns
* <ul>
* <li><code>[T********]</code>
* <li><code>[*T*******]</code>
* <li><code>[***T*****]</code>
* <li><code>[****T****]</code>
* </ul>
* <li><code>! g.disjoint(this) = true</code>
* <br>(<code>intersects</code> is the inverse of <code>disjoint</code>)
* </ul>
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if the two <code>Geometry</code>s intersect
*
* @see Geometry#disjoint
*/
public boolean intersects(Geometry g) {
// short-circuit envelope test
if (! getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
return false;
/**
* TODO: (MD) Add optimizations:
*
* - for P-A case:
* If P is in env(A), test for point-in-poly
*
* - for A-A case:
* If env(A1).overlaps(env(A2))
* test for overlaps via point-in-poly first (both ways)
* Possibly optimize selection of point to test by finding point of A1
* closest to centre of env(A2).
* (Is there a test where we shouldn't bother - e.g. if env A
* is much smaller than env B, maybe there's no point in testing
* pt(B) in env(A)?
*/
// optimization for rectangle arguments
if (isRectangle()) {
return RectangleIntersects.intersects((Polygon) this, g);
}
if (g.isRectangle()) {
return RectangleIntersects.intersects((Polygon) g, this);
}
if (isGeometryCollection() || g.isGeometryCollection()) {
for (int i = 0 ; i < getNumGeometries() ; i++) {
for (int j = 0 ; j < g.getNumGeometries() ; j++) {
if (getGeometryN(i).intersects(g.getGeometryN(j))) {
return true;
}
}
}
return false;
}
// general case
return relate(g).isIntersects();
}
/**
* Tests whether this geometry crosses the
* argument geometry.
* <p>
* The <code>crosses</code> predicate has the following equivalent definitions:
* <ul>
* <li>The geometries have some but not all interior points in common.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* one of the following patterns:
* <ul>
* <li><code>[T*T******]</code> (for P/L, P/A, and L/A situations)
* <li><code>[T*****T**]</code> (for L/P, A/P, and A/L situations)
* <li><code>[0********]</code> (for L/L situations)
* </ul>
* </ul>
* For the A/A and P/P situations this predicate returns <code>false</code>.
* <p>
* The SFS defined this predicate only for P/L, P/A, L/L, and L/A situations.
* To make the relation symmetric
* JTS extends the definition to apply to L/P, A/P and A/L situations as well.
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if the two <code>Geometry</code>s cross.
*/
public boolean crosses(Geometry g) {
// short-circuit test
if (! getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
return false;
return relate(g).isCrosses(getDimension(), g.getDimension());
}
/**
* Tests whether this geometry is within the
* specified geometry.
* <p>
* The <code>within</code> predicate has the following equivalent definitions:
* <ul>
* <li>Every point of this geometry is a point of the other geometry,
* and the interiors of the two geometries have at least one point in common.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* <code>[T*F**F***]</code>
* <li><code>g.contains(this) = true</code>
* <br>(<code>within</code> is the converse of {@link #contains})
* </ul>
* An implication of the definition is that
* "The boundary of a Geometry is not within the Geometry".
* In other words, if a geometry A is a subset of
* the points in the boundary of a geometry B, <code>A.within(B) = false</code>
* (As a concrete example, take A to be a LineString which lies in the boundary of a Polygon B.)
* For a predicate with similar behaviour but avoiding
* this subtle limitation, see {@link #coveredBy}.
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if this <code>Geometry</code> is within
* <code>g</code>
*
* @see Geometry#contains
* @see Geometry#coveredBy
*/
public boolean within(Geometry g) {
return g.contains(this);
}
/**
* Tests whether this geometry contains the
* argument geometry.
* <p>
* The <code>contains</code> predicate has the following equivalent definitions:
* <ul>
* <li>Every point of the other geometry is a point of this geometry,
* and the interiors of the two geometries have at least one point in common.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* the pattern
* <code>[T*****FF*]</code>
* <li><code>g.within(this) = true</code>
* <br>(<code>contains</code> is the converse of {@link #within} )
* </ul>
* An implication of the definition is that "Geometries do not
* contain their boundary". In other words, if a geometry A is a subset of
* the points in the boundary of a geometry B, <code>B.contains(A) = false</code>.
* (As a concrete example, take A to be a LineString which lies in the boundary of a Polygon B.)
* For a predicate with similar behaviour but avoiding
* this subtle limitation, see {@link #covers}.
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if this <code>Geometry</code> contains <code>g</code>
*
* @see Geometry#within
* @see Geometry#covers
*/
public boolean contains(Geometry g) {
// optimization - lower dimension cannot contain areas
if (g.getDimension() == 2 && getDimension() < 2) {
return false;
}
// optimization - P cannot contain a non-zero-length L
// Note that a point can contain a zero-length lineal geometry,
// since the line has no boundary due to Mod-2 Boundary Rule
if (g.getDimension() == 1 && getDimension() < 1 && g.getLength() > 0.0) {
return false;
}
// optimization - envelope test
if (! getEnvelopeInternal().contains(g.getEnvelopeInternal()))
return false;
// optimization for rectangle arguments
if (isRectangle()) {
return RectangleContains.contains((Polygon) this, g);
}
// general case
return relate(g).isContains();
}
/**
* Tests whether this geometry overlaps the
* specified geometry.
* <p>
* The <code>overlaps</code> predicate has the following equivalent definitions:
* <ul>
* <li>The geometries have at least one point each not shared by the other
* (or equivalently neither covers the other),
* they have the same dimension,
* and the intersection of the interiors of the two geometries has
* the same dimension as the geometries themselves.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* <code>[T*T***T**]</code> (for two points or two surfaces)
* or <code>[1*T***T**]</code> (for two curves)
* </ul>
* If the geometries are of different dimension this predicate returns <code>false</code>.
* This predicate is symmetric.
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if the two <code>Geometry</code>s overlap.
*/
public boolean overlaps(Geometry g) {
// short-circuit test
if (! getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
return false;
return relate(g).isOverlaps(getDimension(), g.getDimension());
}
/**
* Tests whether this geometry covers the
* argument geometry.
* <p>
* The <code>covers</code> predicate has the following equivalent definitions:
* <ul>
* <li>Every point of the other geometry is a point of this geometry.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* at least one of the following patterns:
* <ul>
* <li><code>[T*****FF*]</code>
* <li><code>[*T****FF*]</code>
* <li><code>[***T**FF*]</code>
* <li><code>[****T*FF*]</code>
* </ul>
* <li><code>g.coveredBy(this) = true</code>
* <br>(<code>covers</code> is the converse of {@link #coveredBy})
* </ul>
* If either geometry is empty, the value of this predicate is <code>false</code>.
* <p>
* This predicate is similar to {@link #contains},
* but is more inclusive (i.e. returns <code>true</code> for more cases).
* In particular, unlike <code>contains</code> it does not distinguish between
* points in the boundary and in the interior of geometries.
* For most situations, <code>covers</code> should be used in preference to <code>contains</code>.
* As an added benefit, <code>covers</code> is more amenable to optimization,
* and hence should be more performant.
*
*@param g the <code>Geometry</code> with which to compare this <code>Geometry</code>
*@return <code>true</code> if this <code>Geometry</code> covers <code>g</code>
*
* @see Geometry#contains
* @see Geometry#coveredBy
*/
public boolean covers(Geometry g) {
// optimization - lower dimension cannot cover areas
if (g.getDimension() == 2 && getDimension() < 2) {
return false;
}
// optimization - P cannot cover a non-zero-length L
// Note that a point can cover a zero-length lineal geometry
if (g.getDimension() == 1 && getDimension() < 1 && g.getLength() > 0.0) {
return false;
}
// optimization - envelope test
if (! getEnvelopeInternal().covers(g.getEnvelopeInternal()))
return false;
// optimization for rectangle arguments
if (isRectangle()) {
// since we have already tested that the test envelope is covered
return true;
}
return relate(g).isCovers();
}
/**
* Tests whether this geometry is covered by the
* argument geometry.
* <p>
* The <code>coveredBy</code> predicate has the following equivalent definitions:
* <ul>
* <li>Every point of this geometry is a point of the other geometry.
* <li>The DE-9IM Intersection Matrix for the two geometries matches
* at least one of the following patterns:
* <ul>
* <li><code>[T*F**F***]</code>
* <li><code>[*TF**F***]</code>
* <li><code>[**FT*F***]</code>
* <li><code>[**F*TF***]</code>
* </ul>
* <li><code>g.covers(this) = true</code>
* <br>(<code>coveredBy</code> is the converse of {@link #covers})
* </ul>