-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
IsSimpleOp.js
165 lines (164 loc) · 5.26 KB
/
IsSimpleOp.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
import hasInterface from '../../../../hasInterface.js'
import MultiPoint from '../geom/MultiPoint.js'
import GeometryGraph from '../geomgraph/GeometryGraph.js'
import GeometryCollection from '../geom/GeometryCollection.js'
import LinearComponentExtracter from '../geom/util/LinearComponentExtracter.js'
import TreeMap from '../../../../java/util/TreeMap.js'
import MultiLineString from '../geom/MultiLineString.js'
import TreeSet from '../../../../java/util/TreeSet.js'
import LineString from '../geom/LineString.js'
import Polygonal from '../geom/Polygonal.js'
import RobustLineIntersector from '../algorithm/RobustLineIntersector.js'
export default class IsSimpleOp {
constructor() {
IsSimpleOp.constructor_.apply(this, arguments)
}
static constructor_() {
this._inputGeom = null
this._isClosedEndpointsInInterior = true
this._nonSimpleLocation = null
if (arguments.length === 1) {
const geom = arguments[0]
this._inputGeom = geom
} else if (arguments.length === 2) {
const geom = arguments[0], boundaryNodeRule = arguments[1]
this._inputGeom = geom
this._isClosedEndpointsInInterior = !boundaryNodeRule.isInBoundary(2)
}
}
static isSimple() {
if (arguments.length === 1) {
const geom = arguments[0]
const op = new IsSimpleOp(geom)
return op.isSimple()
} else if (arguments.length === 2) {
const geom = arguments[0], boundaryNodeRule = arguments[1]
const op = new IsSimpleOp(geom, boundaryNodeRule)
return op.isSimple()
}
}
isSimpleMultiPoint(mp) {
if (mp.isEmpty()) return true
const points = new TreeSet()
for (let i = 0; i < mp.getNumGeometries(); i++) {
const pt = mp.getGeometryN(i)
const p = pt.getCoordinate()
if (points.contains(p)) {
this._nonSimpleLocation = p
return false
}
points.add(p)
}
return true
}
isSimplePolygonal(geom) {
const rings = LinearComponentExtracter.getLines(geom)
for (let i = rings.iterator(); i.hasNext(); ) {
const ring = i.next()
if (!this.isSimpleLinearGeometry(ring)) return false
}
return true
}
hasClosedEndpointIntersection(graph) {
const endPoints = new TreeMap()
for (let i = graph.getEdgeIterator(); i.hasNext(); ) {
const e = i.next()
const isClosed = e.isClosed()
const p0 = e.getCoordinate(0)
this.addEndpoint(endPoints, p0, isClosed)
const p1 = e.getCoordinate(e.getNumPoints() - 1)
this.addEndpoint(endPoints, p1, isClosed)
}
for (let i = endPoints.values().iterator(); i.hasNext(); ) {
const eiInfo = i.next()
if (eiInfo.isClosed && eiInfo.degree !== 2) {
this._nonSimpleLocation = eiInfo.getCoordinate()
return true
}
}
return false
}
getNonSimpleLocation() {
return this._nonSimpleLocation
}
isSimpleLinearGeometry(geom) {
if (geom.isEmpty()) return true
const graph = new GeometryGraph(0, geom)
const li = new RobustLineIntersector()
const si = graph.computeSelfNodes(li, true)
if (!si.hasIntersection()) return true
if (si.hasProperIntersection()) {
this._nonSimpleLocation = si.getProperIntersectionPoint()
return false
}
if (this.hasNonEndpointIntersection(graph)) return false
if (this._isClosedEndpointsInInterior)
if (this.hasClosedEndpointIntersection(graph)) return false
return true
}
hasNonEndpointIntersection(graph) {
for (let i = graph.getEdgeIterator(); i.hasNext(); ) {
const e = i.next()
const maxSegmentIndex = e.getMaximumSegmentIndex()
for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext(); ) {
const ei = eiIt.next()
if (!ei.isEndPoint(maxSegmentIndex)) {
this._nonSimpleLocation = ei.getCoordinate()
return true
}
}
}
return false
}
addEndpoint(endPoints, p, isClosed) {
let eiInfo = endPoints.get(p)
if (eiInfo === null) {
eiInfo = new EndpointInfo(p)
endPoints.put(p, eiInfo)
}
eiInfo.addEndpoint(isClosed)
}
computeSimple(geom) {
this._nonSimpleLocation = null
if (geom.isEmpty()) return true
if (geom instanceof LineString) return this.isSimpleLinearGeometry(geom)
if (geom instanceof MultiLineString) return this.isSimpleLinearGeometry(geom)
if (geom instanceof MultiPoint) return this.isSimpleMultiPoint(geom)
if (hasInterface(geom, Polygonal)) return this.isSimplePolygonal(geom)
if (geom instanceof GeometryCollection) return this.isSimpleGeometryCollection(geom)
return true
}
isSimple() {
this._nonSimpleLocation = null
return this.computeSimple(this._inputGeom)
}
isSimpleGeometryCollection(geom) {
for (let i = 0; i < geom.getNumGeometries(); i++) {
const comp = geom.getGeometryN(i)
if (!this.computeSimple(comp)) return false
}
return true
}
}
class EndpointInfo {
constructor() {
EndpointInfo.constructor_.apply(this, arguments)
}
static constructor_() {
this.pt = null
this.isClosed = null
this.degree = null
const pt = arguments[0]
this.pt = pt
this.isClosed = false
this.degree = 0
}
getCoordinate() {
return this.pt
}
addEndpoint(isClosed) {
this.degree++
this.isClosed |= isClosed
}
}
IsSimpleOp.EndpointInfo = EndpointInfo