-
Notifications
You must be signed in to change notification settings - Fork 261
/
OperatorUnionCursor.java
300 lines (255 loc) · 8.2 KB
/
OperatorUnionCursor.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
/*
Copyright 1995-2015 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: contracts@esri.com
*/
package com.esri.core.geometry;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
final class OperatorUnionCursor extends GeometryCursor {
private GeometryCursor m_inputGeoms;
private ProgressTracker m_progress_tracker;
private SpatialReferenceImpl m_spatial_reference;
private int m_index = -1;
private boolean m_b_done = false;
private boolean [] m_had_geometry = new boolean[4];
private int [] m_dim_geom_counts = new int [4];
private boolean m_b_union_all_dimensions = false;
private int m_max_dimension = -1;
private int m_added_geoms = 0;
private int m_current_dim = -1;
private final static class Geom_pair
{
void init() { geom = null; vertex_count = -1; unioned = false; }
Geometry geom;
int vertex_count;
boolean unioned;//true if geometry is a result of union operation
}
final static class Bin_type //bin array and the total vertex count in the bin
{
int bin_vertex_count = 0;
ArrayList<Geom_pair> geometries = new ArrayList<Geom_pair>();
void add_pair(Geom_pair geom)
{
bin_vertex_count += geom.vertex_count;
geometries.add(geom);
}
void pop_pair()
{
bin_vertex_count -= geometries.get(geometries.size() - 1).vertex_count;
geometries.remove(geometries.size() - 1);
}
Geom_pair back_pair() { return geometries.get(geometries.size() - 1); }
int geom_count() { return geometries.size(); }
}
ArrayList< TreeMap<Integer, Bin_type> > m_union_bins = new ArrayList< TreeMap<Integer, Bin_type> >();//for each dimension there is a list of bins sorted by level
OperatorUnionCursor(GeometryCursor inputGeoms1, SpatialReference sr,
ProgressTracker progress_tracker) {
m_inputGeoms = inputGeoms1;
m_spatial_reference = (SpatialReferenceImpl) (sr);
m_progress_tracker = progress_tracker;
}
private Geometry get_result_geometry(int dim) {
assert (m_dim_geom_counts[dim] > 0);
java.util.TreeMap<Integer, Bin_type> map = m_union_bins.get(dim);
Map.Entry<Integer, Bin_type> e = map.firstEntry();
Bin_type bin = e.getValue();
Geometry resG;
resG = bin.back_pair().geom;
boolean unioned = bin.back_pair().unioned;
map.remove(e.getKey());
if (unioned) {
resG = OperatorSimplify.local().execute(resG, m_spatial_reference,
false, m_progress_tracker);
if (dim == 0 && resG.getType() == Geometry.Type.Point) {// must
// return
// multipoint
// for
// points
MultiPoint mp = new MultiPoint(resG.getDescription());
if (!resG.isEmpty())
mp.add((Point) resG);
resG = mp;
}
}
return resG;
}
@Override
public Geometry next() {
if (m_b_done && m_current_dim == m_max_dimension)
return null;
while (!step_()) {
}
if (m_max_dimension == -1)
return null;// empty input cursor
if (m_b_union_all_dimensions) {
m_current_dim++;
while (true) {
if (m_current_dim > m_max_dimension || m_current_dim < 0)
throw GeometryException.GeometryInternalError();
if (m_had_geometry[m_current_dim])
break;
}
m_index++;
return get_result_geometry(m_current_dim);
} else {
m_index = 0;
assert (m_max_dimension >= 0);
m_current_dim = m_max_dimension;
return get_result_geometry(m_max_dimension);
}
}
@Override
public int getGeometryID() {
return m_index;
}
private boolean step_(){
if (m_b_done)
return true;
Geometry geom = null;
if (m_inputGeoms != null)
{
geom = m_inputGeoms.next();
if (geom == null) {
m_b_done = true;
m_inputGeoms = null;
}
}
ProgressTracker.checkAndThrow(m_progress_tracker);
if (geom != null) {
int dim = geom.getDimension();
m_had_geometry[dim] = true;
if (dim >= m_max_dimension && !m_b_union_all_dimensions)
{
add_geom(dim, false, geom);
if (dim > m_max_dimension && !m_b_union_all_dimensions)
{
//this geometry has higher dimension than the previously processed one
//Therefore we delete all lower dimensions (unless m_b_union_all_dimensions is true).
remove_all_bins_with_lower_dimension(dim);
}
}
else
{
//this geometry is skipped
}
} else {
//geom is null. do nothing
}
if (m_added_geoms > 0) {
for (int dim = 0; dim <= m_max_dimension; dim++) {
while (m_dim_geom_counts[dim] > 1) {
ArrayList<Geometry> batch_to_union = collect_geometries_to_union(dim);
boolean serial_execution = true;
if (serial_execution) {
if (batch_to_union.size() != 0) {
Geometry geomRes = TopologicalOperations
.dissolveDirty(batch_to_union,
m_spatial_reference,
m_progress_tracker);
add_geom(dim, true, geomRes);
} else {
break;
}
}
}
}
}
return m_b_done;
}
ArrayList<Geometry> collect_geometries_to_union(int dim) {
ArrayList<Geometry> batch_to_union = new ArrayList<Geometry>();
ArrayList<Map.Entry<Integer, Bin_type>> entriesToRemove = new ArrayList<Map.Entry<Integer, Bin_type>>();
Set<Map.Entry<Integer, Bin_type>> set = m_union_bins.get(dim)
.entrySet();
for (Map.Entry<Integer, Bin_type> e : set) {
//int level = e.getKey();
Bin_type bin = e.getValue();
final int binVertexThreshold = 10000;
if (m_b_done
|| (bin.bin_vertex_count > binVertexThreshold && bin
.geom_count() > 1)) {
m_dim_geom_counts[dim] -= bin.geom_count();
m_added_geoms -= bin.geom_count();
while (bin.geometries.size() > 0) {
// empty geometries will be unioned too.
batch_to_union.add(bin.back_pair().geom);
bin.pop_pair();
}
entriesToRemove.add(e);
}
}
set.removeAll(entriesToRemove);
return batch_to_union;
}
private void remove_all_bins_with_lower_dimension(int dim) {
// this geometry has higher dimension than the previously processed one
for (int i = 0; i < dim; i++) {
m_union_bins.get(i).clear();
m_added_geoms -= m_dim_geom_counts[i];
m_dim_geom_counts[i] = 0;
}
}
private void add_geom(int dimension, boolean unioned, Geometry geom) {
Geom_pair pair = new Geom_pair();
pair.init();
pair.geom = geom;
int sz = get_vertex_count_(geom);
pair.vertex_count = sz;
int level = get_level_(sz);
if (dimension + 1 > (int) m_union_bins.size()) {
for (int i = 0, n = Math.max(2, dimension + 1); i < n; i++) {
m_union_bins.add(new TreeMap<Integer, Bin_type>());
}
}
Bin_type bin = m_union_bins.get(dimension).get(level);//return null if level is abscent
if (bin == null) {
bin = new Bin_type();
m_union_bins.get(dimension).put(level, bin);
}
pair.unioned = unioned;
bin.add_pair(pair);
// Update global cursor state
m_dim_geom_counts[dimension]++;
m_added_geoms++;
m_max_dimension = Math.max(m_max_dimension, dimension);
}
private static int get_level_(int sz) {// calculates logarithm of sz to base
// 4.
return sz > 0 ? (int) (Math.log((double) sz) / Math.log(4.0) + 0.5)
: (int) 0;
}
private static int get_vertex_count_(Geometry geom) {
int gt = geom.getType().value();
if (Geometry.isMultiVertex(gt)) {
return ((MultiVertexGeometry) geom).getPointCount();
} else if (gt == Geometry.GeometryType.Point) {
return 1;
} else if (gt == Geometry.GeometryType.Envelope) {
return 4;
} else if (Geometry.isSegment(gt)) {
return 2;
} else {
throw GeometryException.GeometryInternalError();
}
}
@Override
public boolean tock() {
return step_();
}
}