Revision 2470
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyReportLine.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.topology.lib.impl; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
27 |
import org.gvsig.fmap.geom.Geometry; |
|
28 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
|
29 |
import org.gvsig.topology.lib.api.TopologyReport; |
|
30 |
import org.gvsig.topology.lib.api.TopologyReportLine; |
|
31 |
import org.gvsig.topology.lib.api.TopologyRule; |
|
32 |
|
|
33 |
/** |
|
34 |
* |
|
35 |
* @author jjdelcerro |
|
36 |
*/ |
|
37 |
public class DefaultTopologyReportLine implements TopologyReportLine { |
|
38 |
|
|
39 |
private final TopologyReport report; |
|
40 |
private final TopologyRule rule; |
|
41 |
private final TopologyDataSet dataSet1; |
|
42 |
private final TopologyDataSet dataSet2; |
|
43 |
private final Geometry geometry; |
|
44 |
private final FeatureReference feature1; |
|
45 |
private final FeatureReference feature2; |
|
46 |
private final int primitive1; |
|
47 |
private final int primitive2; |
|
48 |
private boolean exception; |
|
49 |
private final String description; |
|
50 |
private final String data; |
|
51 |
private final Geometry errorGeometry; |
|
52 |
|
|
53 |
DefaultTopologyReportLine(DefaultTopologyReport report, |
|
54 |
TopologyRule rule, |
|
55 |
TopologyDataSet dataSet1, |
|
56 |
TopologyDataSet dataSet2, |
|
57 |
Geometry geometry, |
|
58 |
Geometry error, |
|
59 |
FeatureReference feature1, |
|
60 |
FeatureReference feature2, |
|
61 |
int primitive1, |
|
62 |
int primitive2, |
|
63 |
boolean exception, |
|
64 |
String description, |
|
65 |
String data |
|
66 |
) { |
|
67 |
this.report = report; |
|
68 |
this.rule = rule; |
|
69 |
this.dataSet1 = dataSet1; |
|
70 |
this.dataSet2 = dataSet2; |
|
71 |
this.geometry = geometry; |
|
72 |
this.feature1 = feature1; |
|
73 |
this.feature2 = feature2; |
|
74 |
this.primitive1 = primitive1; |
|
75 |
this.primitive2 = primitive2; |
|
76 |
this.exception = exception; |
|
77 |
this.description = description; |
|
78 |
this.errorGeometry = error; |
|
79 |
this.data = data; |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public TopologyRule getRule() { |
|
84 |
return this.rule; |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public TopologyDataSet getDataSet1() { |
|
89 |
return this.dataSet1; |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public TopologyDataSet getDataSet2() { |
|
94 |
return this.dataSet2; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public Geometry getGeometry() { |
|
99 |
return this.geometry; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public FeatureReference getFeature1() { |
|
104 |
return this.feature1; |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public FeatureReference getFeature2() { |
|
109 |
return this.feature2; |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public boolean isException() { |
|
114 |
return this.exception; |
|
115 |
} |
|
116 |
|
|
117 |
public void setException(boolean exception) { |
|
118 |
this.exception = exception; |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
public String getDescription() { |
|
123 |
return description; |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public Geometry getError() { |
|
128 |
return this.errorGeometry; |
|
129 |
} |
|
130 |
|
|
131 |
@Override |
|
132 |
public int getPrimitiveNumber1() { |
|
133 |
return this.primitive1; |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public int getPrimitiveNumber2() { |
|
138 |
return this.primitive2; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public String getData() { |
|
143 |
return this.data; |
|
144 |
} |
|
145 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyDataSet.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.topology.lib.impl; |
|
25 |
|
|
26 |
import java.util.Iterator; |
|
27 |
import java.util.Map; |
|
28 |
import org.apache.commons.collections.IteratorUtils; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
30 |
import org.apache.commons.lang3.mutable.MutableObject; |
|
31 |
import org.gvsig.expressionevaluator.Expression; |
|
32 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
33 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
34 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
35 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
36 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
37 |
import org.gvsig.expressionevaluator.GeometryExpressionUtils; |
|
38 |
import org.gvsig.fmap.dal.DataStore; |
|
39 |
import org.gvsig.fmap.dal.EditingNotification; |
|
40 |
import org.gvsig.fmap.dal.EditingNotificationManager; |
|
41 |
import org.gvsig.fmap.dal.exception.DataException; |
|
42 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
43 |
import org.gvsig.fmap.dal.feature.Feature; |
|
44 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
45 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
46 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
47 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
48 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
|
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
50 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
|
51 |
import org.gvsig.fmap.geom.Geometry; |
|
52 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
53 |
import org.gvsig.fmap.geom.GeometryManager; |
|
54 |
import org.gvsig.fmap.geom.GeometryUtils; |
|
55 |
import org.gvsig.fmap.geom.SpatialIndex; |
|
56 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
57 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
58 |
import org.gvsig.tools.exception.BaseException; |
|
59 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
|
60 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
61 |
import org.gvsig.tools.visitor.Visitor; |
|
62 |
import org.gvsig.topology.lib.api.CancelOperationException; |
|
63 |
import org.gvsig.topology.lib.api.PerformOperationException; |
|
64 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
|
65 |
import org.gvsig.topology.lib.api.TopologyLocator; |
|
66 |
import org.gvsig.topology.lib.api.TopologyManager; |
|
67 |
import org.json.JSONObject; |
|
68 |
import org.gvsig.topology.lib.api.TopologyServices; |
|
69 |
import org.slf4j.Logger; |
|
70 |
import org.slf4j.LoggerFactory; |
|
71 |
|
|
72 |
/** |
|
73 |
* |
|
74 |
* @author jjdelcerro |
|
75 |
*/ |
|
76 |
@SuppressWarnings({"EqualsAndHashcode","UseSpecificCatch"}) |
|
77 |
public class DefaultTopologyDataSet implements TopologyDataSet { |
|
78 |
|
|
79 |
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultTopologyDataSet.class); |
|
80 |
|
|
81 |
private TopologyServices services; |
|
82 |
private String name; |
|
83 |
private DataStore store; |
|
84 |
private boolean needFinishEditing; |
|
85 |
private String fullName; |
|
86 |
private PropertiesSupportHelper propertiesHelper; |
|
87 |
private MutableObject<SpatialIndex> spatialIndex = null; |
|
88 |
|
|
89 |
public DefaultTopologyDataSet() { |
|
90 |
this.services = null; |
|
91 |
this.name = null; |
|
92 |
this.store = null; |
|
93 |
this.needFinishEditing = false; |
|
94 |
this.fullName = null; |
|
95 |
this.propertiesHelper = new PropertiesSupportHelper(); |
|
96 |
} |
|
97 |
|
|
98 |
public DefaultTopologyDataSet(TopologyServices services, String name, DataStore store) { |
|
99 |
this.services = services; |
|
100 |
this.name = name; |
|
101 |
this.store = store; |
|
102 |
this.needFinishEditing = false; |
|
103 |
if( store!=null ) { |
|
104 |
this.fullName = store.getFullName(); |
|
105 |
} |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void restart() { |
|
110 |
this.store = null; |
|
111 |
this.spatialIndex = null; |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public boolean equals(Object obj) { |
|
116 |
if( !(obj instanceof DefaultTopologyDataSet) ) { |
|
117 |
return false; |
|
118 |
} |
|
119 |
DefaultTopologyDataSet other = (DefaultTopologyDataSet)obj; |
|
120 |
if( this.store != other.store ) { |
|
121 |
return false; |
|
122 |
} |
|
123 |
if( !StringUtils.equals(this.getName(), other.getName()) ) { |
|
124 |
return false; |
|
125 |
} |
|
126 |
return true; |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public String getName() { |
|
131 |
return this.name; |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public void setName(String name) { |
|
136 |
this.name = name; |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public String toString() { |
|
141 |
try { |
|
142 |
FeatureAttributeDescriptor attr = this.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttribute(); |
|
143 |
String geomType = attr.getGeomType().getName(); |
|
144 |
return this.name + " ("+ geomType + ")"; |
|
145 |
} catch(Exception ex) { |
|
146 |
return this.name ; |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public DataStore getStore() { |
|
152 |
if (this.store == null) { |
|
153 |
this.store = this.services.getFeatureStore(this); |
|
154 |
} |
|
155 |
return this.store; |
|
156 |
} |
|
157 |
|
|
158 |
@Override |
|
159 |
public FeatureStore getFeatureStore() { |
|
160 |
if (this.store == null) { |
|
161 |
this.store = this.services.getFeatureStore(this); |
|
162 |
} |
|
163 |
return (FeatureStore) this.store; |
|
164 |
} |
|
165 |
|
|
166 |
@Override |
|
167 |
public long getSize() { |
|
168 |
try { |
|
169 |
long size = this.getFeatureStore().getFeatureCount(); |
|
170 |
return size; |
|
171 |
} catch (DataException ex) { |
|
172 |
// TODO: mensage al log |
|
173 |
return 0; |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public boolean isThisStore(FeatureStore store) { |
|
179 |
if( store == null ) { |
|
180 |
return false; |
|
181 |
} |
|
182 |
return StringUtils.equals(this.fullName, store.getFullName()); |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public int getGeometryType() { |
|
187 |
try { |
|
188 |
FeatureStore theStore = this.getFeatureStore(); |
|
189 |
FeatureType featureType = theStore.getDefaultFeatureType(); |
|
190 |
FeatureAttributeDescriptor attr = featureType.getDefaultGeometryAttribute(); |
|
191 |
GeometryType geomType = attr.getGeomType(); |
|
192 |
return geomType.getType(); |
|
193 |
} catch (Exception ex) { |
|
194 |
return Geometry.TYPES.GEOMETRY; |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
@Override |
|
199 |
public void accept(Visitor visitor) throws VisitCanceledException { |
|
200 |
FeatureStore st = this.getFeatureStore(); |
|
201 |
try { |
|
202 |
st.accept(visitor); |
|
203 |
} catch(VisitCanceledException ex) { |
|
204 |
throw ex; |
|
205 |
} catch(BaseException ex) { |
|
206 |
throw new RuntimeException(ex); |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 |
@Override |
|
211 |
public void edit() throws DataException { |
|
212 |
FeatureStore theStore = this.getFeatureStore(); |
|
213 |
if (!theStore.isEditing()) { |
|
214 |
theStore.edit(); |
|
215 |
this.needFinishEditing = true; |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
@Override |
|
220 |
public void finishEditing() throws DataException { |
|
221 |
if (this.needFinishEditing) { |
|
222 |
this.getFeatureStore().finishEditing(); |
|
223 |
} |
|
224 |
} |
|
225 |
|
|
226 |
@Override |
|
227 |
public EditableFeature createNewFeature() throws DataException { |
|
228 |
EditableFeature f = this.getFeatureStore().createNewFeature(); |
|
229 |
return f; |
|
230 |
} |
|
231 |
|
|
232 |
public void perform( |
|
233 |
String operation, |
|
234 |
Feature feature |
|
235 |
) throws DataException { |
|
236 |
this.edit(); |
|
237 |
|
|
238 |
EditingNotificationManager editingNotificationManager |
|
239 |
= DALSwingLocator.getEditingNotificationManager(); |
|
240 |
FeatureStore theStore = this.getFeatureStore(); |
|
241 |
|
|
242 |
EditingNotification notification |
|
243 |
= editingNotificationManager.notifyObservers(this, // source |
|
244 |
operation, // type |
|
245 |
null,// document |
|
246 |
null,// layer |
|
247 |
theStore,// store |
|
248 |
feature// feature |
|
249 |
); |
|
250 |
|
|
251 |
if (notification.isCanceled()) { |
|
252 |
String msg = String.format( |
|
253 |
"Can't insert feature into %1$s, canceled by some observer.", |
|
254 |
this.getName()); |
|
255 |
throw new CancelOperationException(msg); |
|
256 |
} |
|
257 |
|
|
258 |
String after = null; |
|
259 |
if( operation.equalsIgnoreCase(EditingNotification.BEFORE_REMOVE_FEATURE) ) { |
|
260 |
theStore.delete(feature); |
|
261 |
after = EditingNotification.AFTER_REMOVE_FEATURE; |
|
262 |
|
|
263 |
} else { |
|
264 |
if (notification.shouldValidateTheFeature()) { |
|
265 |
if (!editingNotificationManager.validateFeature(feature)) { |
|
266 |
String msg = String.format("%1$s is not valid", feature.toString()); |
|
267 |
throw new PerformOperationException(msg); |
|
268 |
} |
|
269 |
} |
|
270 |
switch(operation) { |
|
271 |
case EditingNotification.BEFORE_UPDATE_FEATURE: |
|
272 |
theStore.update((EditableFeature) feature); |
|
273 |
after = EditingNotification.AFTER_UPDATE_FEATURE; |
|
274 |
break; |
|
275 |
|
|
276 |
case EditingNotification.BEFORE_INSERT_FEATURE: |
|
277 |
theStore.insert((EditableFeature) feature); |
|
278 |
after = EditingNotification.AFTER_INSERT_FEATURE; |
|
279 |
break; |
|
280 |
} |
|
281 |
} |
|
282 |
|
|
283 |
editingNotificationManager.notifyObservers(this, |
|
284 |
after, null, null, |
|
285 |
theStore, feature); |
|
286 |
|
|
287 |
} |
|
288 |
|
|
289 |
@Override |
|
290 |
public void insert(final EditableFeature feature) throws DataException { |
|
291 |
perform( |
|
292 |
EditingNotification.BEFORE_INSERT_FEATURE, |
|
293 |
feature |
|
294 |
); |
|
295 |
} |
|
296 |
|
|
297 |
@Override |
|
298 |
public void update(final EditableFeature feature) throws DataException { |
|
299 |
perform( |
|
300 |
EditingNotification.BEFORE_UPDATE_FEATURE, |
|
301 |
feature |
|
302 |
); |
|
303 |
} |
|
304 |
|
|
305 |
@Override |
|
306 |
public void delete(final Feature feature) throws DataException { |
|
307 |
perform( |
|
308 |
EditingNotification.BEFORE_REMOVE_FEATURE, |
|
309 |
feature |
|
310 |
); |
|
311 |
} |
|
312 |
|
|
313 |
@Override |
|
314 |
public void delete(final FeatureReference feature) throws DataException { |
|
315 |
perform( |
|
316 |
EditingNotification.BEFORE_REMOVE_FEATURE, |
|
317 |
feature.getFeature() |
|
318 |
); |
|
319 |
} |
|
320 |
|
|
321 |
@Override |
|
322 |
public JSONObject toJSON() { |
|
323 |
JSONObject jsonDataSet = new JSONObject(); |
|
324 |
jsonDataSet.put("name", this.name); |
|
325 |
jsonDataSet.put("fullName", this.fullName); |
|
326 |
|
|
327 |
return jsonDataSet; |
|
328 |
} |
|
329 |
|
|
330 |
@Override |
|
331 |
public void fromJSON(String json) { |
|
332 |
this.fromJSON(new JSONObject(json)); |
|
333 |
} |
|
334 |
|
|
335 |
@Override |
|
336 |
public void fromJSON(JSONObject json) { |
|
337 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
|
338 |
this.name = json.getString("name"); |
|
339 |
this.fullName = null; |
|
340 |
if( json.has("fullName") ) { |
|
341 |
this.fullName = json.getString("fullName"); |
|
342 |
} |
|
343 |
this.store = null; |
|
344 |
this.needFinishEditing = false; |
|
345 |
this.services = manager.getDefaultServices(); |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
public Object getProperty(String string) { |
|
350 |
return this.propertiesHelper.getProperty(name); |
|
351 |
} |
|
352 |
|
|
353 |
@Override |
|
354 |
public void setProperty(String string, Object o) { |
|
355 |
this.propertiesHelper.setProperty(name, o); |
|
356 |
} |
|
357 |
|
|
358 |
@Override |
|
359 |
public Map<String, Object> getProperties() { |
|
360 |
return this.propertiesHelper.getProperties(); |
|
361 |
} |
|
362 |
|
|
363 |
@Override |
|
364 |
public SpatialIndex getSpatialIndex() { |
|
365 |
if( this.spatialIndex == null ) { |
|
366 |
this.spatialIndex = new MutableObject<>(); |
|
367 |
FeatureStore theStore = this.getFeatureStore(); |
|
368 |
FeatureStoreProviderFactory storeFactory = (FeatureStoreProviderFactory) theStore.getProviderFactory(); |
|
369 |
if( storeFactory.useLocalIndexesCanImprovePerformance()==FeatureStoreProviderFactory.YES ) { |
|
370 |
try { |
|
371 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
372 |
final SpatialIndex geomIndex = geomManager.createSpatialIndex( |
|
373 |
GeometryManager.SPATIALINDEX_DEFAULT_QUADTREE, |
|
374 |
null |
|
375 |
); |
|
376 |
final SpatialIndex dataIndex = theStore.wrapSpatialIndex(geomIndex); |
|
377 |
try { |
|
378 |
store.accept(new Visitor() { |
|
379 |
@Override |
|
380 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
|
381 |
Feature f = (Feature) o; |
|
382 |
Geometry geom = f.getDefaultGeometry(); |
|
383 |
if (geom != null) { |
|
384 |
dataIndex.insert(geom, f); |
|
385 |
} |
|
386 |
} |
|
387 |
}); |
|
388 |
} catch (VisitCanceledException ex) { |
|
389 |
} |
|
390 |
this.spatialIndex.setValue(dataIndex); |
|
391 |
} catch (Exception ex) { |
|
392 |
LOGGER.warn("Can't create spatial index", ex); |
|
393 |
} |
|
394 |
} |
|
395 |
} |
|
396 |
return this.spatialIndex.getValue(); |
|
397 |
} |
|
398 |
|
|
399 |
@Override |
|
400 |
public Iterable<FeatureReference> query(Geometry geom) { |
|
401 |
return this.queryReferences(geom); |
|
402 |
} |
|
403 |
|
|
404 |
@Override |
|
405 |
public Iterable<FeatureReference> queryReferences(Geometry geom) { |
|
406 |
SpatialIndex index = this.getSpatialIndex(); |
|
407 |
if( index == null ) { |
|
408 |
try { |
|
409 |
FeatureStore theStore = (FeatureStore) this.getStore(); |
|
410 |
Expression expression = ExpressionUtils.createExpression(); |
|
411 |
GeometryExpressionBuilder expressionBuilder = GeometryExpressionUtils.createExpressionBuilder(); |
|
412 |
String geomName = theStore.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
|
413 |
if( GeometryUtils.isSubtype(Geometry.TYPES.POINT, geom.getType()) ) { |
|
414 |
expression.setPhrase( |
|
415 |
expressionBuilder.ifnull( |
|
416 |
expressionBuilder.column(geomName), |
|
417 |
expressionBuilder.constant(false), |
|
418 |
expressionBuilder.ST_Intersects( |
|
419 |
expressionBuilder.column(geomName), |
|
420 |
expressionBuilder.geometry(geom) |
|
421 |
) |
|
422 |
).toString() |
|
423 |
); |
|
424 |
} else { |
|
425 |
expression.setPhrase( |
|
426 |
expressionBuilder.ifnull( |
|
427 |
expressionBuilder.column(geomName), |
|
428 |
expressionBuilder.constant(false), |
|
429 |
expressionBuilder.ST_Overlaps( |
|
430 |
expressionBuilder.column(geomName), |
|
431 |
expressionBuilder.envelope(geom.getEnvelope()) |
|
432 |
) |
|
433 |
).toString() |
|
434 |
); |
|
435 |
} |
|
436 |
FeatureSet set = theStore.getFeatureSet(expression); |
|
437 |
final DisposableIterator it = set.fastIterator(); |
|
438 |
return new Iterable<FeatureReference>() { |
|
439 |
@Override |
|
440 |
public Iterator<FeatureReference> iterator() { |
|
441 |
return new Iterator<FeatureReference>() { |
|
442 |
@Override |
|
443 |
public boolean hasNext() { |
|
444 |
return it.hasNext(); |
|
445 |
} |
|
446 |
|
|
447 |
@Override |
|
448 |
public FeatureReference next() { |
|
449 |
Feature f = (Feature) it.next(); |
|
450 |
return f.getReference(); |
|
451 |
} |
|
452 |
}; |
|
453 |
} |
|
454 |
}; |
|
455 |
|
|
456 |
} catch(Exception ex) { |
|
457 |
return (Iterable<FeatureReference>) IteratorUtils.EMPTY_ITERATOR; |
|
458 |
} |
|
459 |
} |
|
460 |
final Iterator it = index.query(geom); |
|
461 |
if( it == null ) { |
|
462 |
return (Iterable<FeatureReference>) IteratorUtils.EMPTY_ITERATOR; |
|
463 |
} |
|
464 |
return new Iterable<FeatureReference>() { |
|
465 |
@Override |
|
466 |
public Iterator<FeatureReference> iterator() { |
|
467 |
return it; |
|
468 |
} |
|
469 |
}; |
|
470 |
} |
|
471 |
|
|
472 |
@Override |
|
473 |
public Iterable<Feature> queryFeatures(Geometry geom) { |
|
474 |
SpatialIndex index = this.getSpatialIndex(); |
|
475 |
if( index == null ) { |
|
476 |
try { |
|
477 |
FeatureStore theStore = (FeatureStore) this.getStore(); |
|
478 |
Expression expression = ExpressionUtils.createExpression(); |
|
479 |
GeometryExpressionBuilder expressionBuilder = GeometryExpressionUtils.createExpressionBuilder(); |
|
480 |
String geomName = theStore.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
|
481 |
if( GeometryUtils.isSubtype(Geometry.TYPES.POINT, geom.getType()) ) { |
|
482 |
expression.setPhrase( |
|
483 |
expressionBuilder.ifnull( |
|
484 |
expressionBuilder.column(geomName), |
|
485 |
expressionBuilder.constant(false), |
|
486 |
expressionBuilder.ST_Intersects( |
|
487 |
expressionBuilder.column(geomName), |
|
488 |
expressionBuilder.geometry(geom) |
|
489 |
) |
|
490 |
).toString() |
|
491 |
); |
|
492 |
} else { |
|
493 |
expression.setPhrase( |
|
494 |
expressionBuilder.ifnull( |
|
495 |
expressionBuilder.column(geomName), |
|
496 |
expressionBuilder.constant(false), |
|
497 |
expressionBuilder.ST_Overlaps( |
|
498 |
expressionBuilder.column(geomName), |
|
499 |
expressionBuilder.envelope(geom.getEnvelope()) |
|
500 |
) |
|
501 |
).toString() |
|
502 |
); |
|
503 |
} |
|
504 |
FeatureSet set = theStore.getFeatureSet(expression); |
|
505 |
final DisposableIterator it = set.fastIterator(); |
|
506 |
return new Iterable<Feature>() { |
|
507 |
@Override |
|
508 |
public Iterator<Feature> iterator() { |
|
509 |
return it; |
|
510 |
} |
|
511 |
}; |
|
512 |
|
|
513 |
} catch(Exception ex) { |
|
514 |
return (Iterable<Feature>) IteratorUtils.EMPTY_ITERATOR; |
|
515 |
} |
|
516 |
} |
|
517 |
final Iterator it = index.query(geom); |
|
518 |
if( it == null ) { |
|
519 |
return (Iterable<Feature>) IteratorUtils.EMPTY_ITERATOR; |
|
520 |
} |
|
521 |
return new Iterable<Feature>() { |
|
522 |
@Override |
|
523 |
public Iterator<Feature> iterator() { |
|
524 |
return new Iterator<Feature>() { |
|
525 |
@Override |
|
526 |
public boolean hasNext() { |
|
527 |
return it.hasNext(); |
|
528 |
} |
|
529 |
|
|
530 |
@Override |
|
531 |
public Feature next() { |
|
532 |
FeatureReference ref = (FeatureReference) it.next(); |
|
533 |
try { |
|
534 |
return ref.getFeature(); |
|
535 |
} catch (DataException ex) { |
|
536 |
return null; |
|
537 |
} |
|
538 |
} |
|
539 |
}; |
|
540 |
} |
|
541 |
}; |
|
542 |
} |
|
543 |
|
|
544 |
@Override |
|
545 |
public Feature findFirst(Expression filter) { |
|
546 |
try { |
|
547 |
return this.getFeatureStore().findFirst(filter); |
|
548 |
} catch (Exception ex) { |
|
549 |
return null; |
|
550 |
} |
|
551 |
} |
|
552 |
|
|
553 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.topology.lib.impl; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Collections; |
|
28 |
import java.util.HashMap; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Map; |
|
31 |
import org.gvsig.fmap.dal.DataStore; |
|
32 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
|
33 |
import org.gvsig.topology.lib.api.TopologyManager; |
|
34 |
import org.gvsig.topology.lib.api.TopologyPlan; |
|
35 |
import org.gvsig.topology.lib.api.TopologyRuleFactory; |
|
36 |
import org.gvsig.topology.lib.api.TopologyServices; |
|
37 |
|
|
38 |
/** |
|
39 |
* |
|
40 |
* @author jjdelcerro |
|
41 |
*/ |
|
42 |
public class DefaultTopologyManager implements TopologyManager { |
|
43 |
|
|
44 |
private final Map<String,TopologyRuleFactory> factories; |
|
45 |
private TopologyServices services; |
|
46 |
|
|
47 |
public DefaultTopologyManager() { |
|
48 |
this.factories = new HashMap<>(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public TopologyPlan createTopologyPlan() { |
|
53 |
TopologyPlan plan = new DefaultTopologyPlan(this, this.services); |
|
54 |
return plan; |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public List<TopologyRuleFactory> getRuleFactories() { |
|
59 |
List<TopologyRuleFactory> l = new ArrayList<>(this.factories.values()); |
|
60 |
return Collections.unmodifiableList(l); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public List<TopologyRuleFactory> getRuleFactories(TopologyDataSet dataSet) { |
|
65 |
List<TopologyRuleFactory> f = new ArrayList<>(); |
|
66 |
for (TopologyRuleFactory factory : this.factories.values()) { |
|
67 |
if( factory.canApplyToDataSet(dataSet) ) { |
|
68 |
f.add(factory); |
|
69 |
} |
|
70 |
} |
|
71 |
return f; |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public TopologyRuleFactory getRulefactory(String id) { |
|
76 |
if( id==null ) { |
|
77 |
return null; |
|
78 |
} |
|
79 |
TopologyRuleFactory factory = this.factories.get(id.toLowerCase()); |
|
80 |
return factory; |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public void addRuleFactories(TopologyRuleFactory factory) { |
|
85 |
if( factory==null ) { |
|
86 |
throw new IllegalArgumentException("Invalid null value."); |
|
87 |
} |
|
88 |
this.factories.put(factory.getId().toLowerCase(), factory); |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public TopologyDataSet createDataSet(String name, DataStore store) { |
|
93 |
TopologyDataSet dataSet = new DefaultTopologyDataSet(this.services, name, store); |
|
94 |
return dataSet; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public void setDefaultServices(TopologyServices services) { |
|
99 |
this.services = services; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public TopologyServices getDefaultServices() { |
|
104 |
return this.services; |
|
105 |
} |
|
106 |
|
|
107 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyPlan.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.topology.lib.impl; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Collection; |
|
28 |
import java.util.Collections; |
|
29 |
import java.util.Date; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Map; |
|
33 |
import org.gvsig.fmap.dal.DataStore; |
|
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
36 |
import org.gvsig.fmap.geom.GeometryManager; |
|
37 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
38 |
import org.gvsig.tools.ToolsLocator; |
|
39 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
40 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
|
41 |
import org.gvsig.topology.lib.api.TopologyManager; |
|
42 |
import org.gvsig.topology.lib.api.TopologyPlan; |
|
43 |
import org.gvsig.topology.lib.api.TopologyRule; |
|
44 |
import org.gvsig.topology.lib.api.TopologyRuleFactory; |
|
45 |
import org.gvsig.topology.lib.api.TopologyServices; |
|
46 |
import org.json.JSONArray; |
|
47 |
import org.json.JSONObject; |
|
48 |
import org.slf4j.Logger; |
|
49 |
import org.slf4j.LoggerFactory; |
|
50 |
|
|
51 |
/** |
|
52 |
* |
|
53 |
* @author jjdelcerro |
|
54 |
*/ |
|
55 |
@SuppressWarnings("UseSpecificCatch") |
|
56 |
public class DefaultTopologyPlan implements TopologyPlan { |
|
57 |
|
|
58 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTopologyPlan.class); |
|
59 |
|
|
60 |
private final TopologyManager manager; |
|
61 |
private final Map<String,TopologyDataSet> dataSets; |
|
62 |
private final List<TopologyRule> rules; |
|
63 |
|
|
64 |
private String name; |
|
65 |
private DefaultTopologyReport report; |
|
66 |
private TopologyServices services; |
|
67 |
private double tolerance; |
|
68 |
private SimpleTaskStatus taskStatus; |
|
69 |
|
|
70 |
public DefaultTopologyPlan(TopologyManager manager, TopologyServices services) { |
|
71 |
this.manager = manager; |
|
72 |
this.services = services; |
|
73 |
this.dataSets = new HashMap<>(); |
|
74 |
this.rules = new ArrayList<>(); |
|
75 |
this.report = null; |
|
76 |
this.name = "TopologyPlan-" + String.format("%08X", new Date().getTime()); |
|
77 |
this.tolerance = 0; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public void setName(String name) { |
|
82 |
this.name = name; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public String getName() { |
|
87 |
return this.name; |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public void clear() { |
|
92 |
this.name = ""; |
|
93 |
this.tolerance = 0; |
|
94 |
this.services = null; |
|
95 |
this.report = null; |
|
96 |
this.dataSets.clear(); |
|
97 |
this.rules.clear(); |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public double getTolerance() { |
|
102 |
return tolerance; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public void setTolerance(double tolerance) { |
|
107 |
this.tolerance = tolerance; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public void setTopologyServices(TopologyServices services) { |
|
112 |
this.services = services; |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public TopologyServices getTopologyServices() { |
|
117 |
return this.services; |
|
118 |
} |
|
119 |
|
|
120 |
public SimpleTaskStatus getTaskStatus() { |
|
121 |
if( this.taskStatus == null ) { |
|
122 |
this.taskStatus = ToolsLocator.getTaskStatusManager() |
|
123 |
.createDefaultSimpleTaskStatus(this.getName()); |
|
124 |
} |
|
125 |
return this.taskStatus; |
|
126 |
} |
|
127 |
|
|
128 |
@Override |
|
129 |
public void execute() { |
|
130 |
SimpleTaskStatus theTaskStatus = this.getTaskStatus(); |
|
131 |
try { |
|
132 |
theTaskStatus.restart(); |
|
133 |
theTaskStatus.message("Preparing the execution of the plan"); |
|
134 |
theTaskStatus.setAutoremove(true); |
|
135 |
theTaskStatus.setIndeterminate(); |
|
136 |
for (TopologyDataSet dataSet : this.dataSets.values()) { |
|
137 |
dataSet.restart(); |
|
138 |
} |
|
139 |
this.getReport().removeAllLines(); |
|
140 |
long steps = 0; |
|
141 |
for (TopologyRule rule : this.rules) { |
|
142 |
steps += rule.getSteps(); |
|
143 |
steps++; |
|
144 |
} |
|
145 |
|
|
146 |
theTaskStatus.setRangeOfValues(0, steps); |
|
147 |
theTaskStatus.setCurValue(0); |
|
148 |
for (TopologyRule rule : this.rules) { |
|
149 |
if( theTaskStatus.isCancellationRequested() ) { |
|
150 |
theTaskStatus.cancel(); |
|
151 |
break; |
|
152 |
} |
|
153 |
theTaskStatus.message(rule.getName()); |
|
154 |
rule.execute(theTaskStatus, this.getReport()); |
|
155 |
theTaskStatus.incrementCurrentValue(); |
|
156 |
} |
|
157 |
} catch(Exception ex) { |
|
158 |
LOGGER.warn("Problems executing topology plan '"+this.getName()+"'.", ex); |
|
159 |
theTaskStatus.abort(); |
|
160 |
} finally { |
|
161 |
if( theTaskStatus.isRunning() ) { |
|
162 |
theTaskStatus.terminate(); |
|
163 |
} |
|
164 |
this.getReport().setCompleted(true); |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
@Override |
|
169 |
public TopologyDataSet addDataSet(String name, DataStore store) { |
|
170 |
TopologyDataSet dataSet = manager.createDataSet(name, store); |
|
171 |
return this.addDataSet(dataSet); |
|
172 |
} |
|
173 |
|
|
174 |
@Override |
|
175 |
public TopologyDataSet addDataSet(TopologyDataSet dataSet) { |
|
176 |
if( this.dataSets.containsKey(dataSet.getName()) ) { |
|
177 |
throw new IllegalArgumentException("Already exists a dataSet with this name ("+dataSet.getName()+")."); |
|
178 |
} |
|
179 |
this.dataSets.put(dataSet.getName(), dataSet); |
|
180 |
return dataSet; |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public TopologyDataSet getDataSet(String name) { |
|
185 |
return this.dataSets.get(name); |
|
186 |
} |
|
187 |
|
|
188 |
@Override |
|
189 |
public boolean containsDataSet(String name) { |
|
190 |
return this.dataSets.containsKey(name); |
|
191 |
} |
|
192 |
|
|
193 |
@Override |
|
194 |
public Collection<TopologyDataSet> getDataSets() { |
|
195 |
Collection<TopologyDataSet> x = dataSets.values(); |
|
196 |
return Collections.unmodifiableCollection(x); |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public Collection<TopologyDataSet> getSecondaryDataSets(TopologyRuleFactory ruleFactory) { |
|
201 |
List<TopologyDataSet> secondaryDataSets = new ArrayList<>(); |
|
202 |
for (TopologyDataSet dataSet : dataSets.values()) { |
|
203 |
if( ruleFactory.canApplyToSecondaryDataSet(dataSet) ) { |
|
204 |
secondaryDataSets.add(dataSet); |
|
205 |
} |
|
206 |
} |
|
207 |
return secondaryDataSets; |
|
208 |
} |
|
209 |
|
|
210 |
@Override |
|
211 |
public TopologyRule addRule(String id, String dataSet1, String dataSet2, double tolerance) { |
|
212 |
TopologyRuleFactory factory = this.manager.getRulefactory(id); |
|
213 |
if( factory == null ) { |
|
214 |
throw new IllegalArgumentException("Can't locate factory for rule '"+id+"'."); |
|
215 |
} |
|
216 |
if( ! this.canApplyRule(factory, dataSet1, dataSet2) ) { |
|
217 |
throw new IllegalArgumentException( |
|
218 |
"Can't apply rule '"+factory.getName()+"' to the datasets '"+dataSet1+"/"+dataSet2+"'." |
|
219 |
); |
|
220 |
} |
|
221 |
TopologyRule rule = factory.createRule(this, dataSet1, dataSet2, tolerance); |
|
222 |
return this.addRule(rule); |
|
223 |
} |
|
224 |
|
|
225 |
@Override |
|
226 |
public TopologyRule addRule(TopologyRule rule) { |
|
227 |
this.rules.add(rule); |
|
228 |
return rule; |
|
229 |
} |
|
230 |
|
|
231 |
private boolean canApplyRule(TopologyRuleFactory factory, String dataSet1, String dataSet2) { |
|
232 |
try { |
|
233 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
234 |
TopologyDataSet dataset = this.getDataSet(dataSet1); |
|
235 |
FeatureStore store = (FeatureStore) dataset.getStore(); |
|
236 |
GeometryType gt = store.getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType(); |
|
237 |
for (Integer geometryType1 : factory.getGeometryTypeDataSet1()) { |
|
238 |
if( gt.isTypeOf(geometryType1) ) { |
|
239 |
if( factory.getGeometryTypeDataSet2()==null ) { |
|
240 |
return true; |
|
241 |
} |
|
242 |
dataset = this.getDataSet(dataSet2); |
|
243 |
store = (FeatureStore) dataset.getStore(); |
|
244 |
gt = store.getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType(); |
|
245 |
for (Integer geometryType2 : factory.getGeometryTypeDataSet2()) { |
|
246 |
if( gt.isTypeOf(geometryType2) ) { |
|
247 |
return true; |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
return false; |
|
253 |
} catch(Exception ex) { |
|
254 |
return false; |
|
255 |
} |
|
256 |
} |
|
257 |
|
|
258 |
@Override |
|
259 |
public Collection<TopologyRule> getRules() { |
|
260 |
return Collections.unmodifiableList(rules); |
|
261 |
} |
|
262 |
|
|
263 |
@Override |
|
264 |
public DefaultTopologyReport getReport() { |
|
265 |
if( this.report == null ) { |
|
266 |
this.report = new DefaultTopologyReport(this); |
|
267 |
} |
|
268 |
return this.report; |
|
269 |
} |
|
270 |
|
|
271 |
@Override |
|
272 |
public JSONObject toJSON() { |
|
273 |
JSONObject me = new JSONObject(); |
|
274 |
|
|
275 |
me.put("name", this.name); |
|
276 |
me.put("tolerance", this.tolerance); |
|
277 |
|
|
278 |
JSONArray jsonDataSets= new JSONArray(); |
|
279 |
for (TopologyDataSet dataSet : this.dataSets.values()) { |
|
280 |
jsonDataSets.put(((DefaultTopologyDataSet)dataSet).toJSON()); |
|
281 |
} |
|
282 |
me.put("dataSets", jsonDataSets); |
|
283 |
|
|
284 |
JSONArray jsonRules = new JSONArray(); |
|
285 |
for (TopologyRule rule : this.rules) { |
|
286 |
JSONObject jsonRule = rule.toJSON(); |
|
287 |
jsonRule.put("__factoryId", rule.getFactory().getId()); |
|
288 |
jsonRules.put(jsonRule); |
|
289 |
} |
|
290 |
me.put("rules", jsonRules); |
|
291 |
|
|
292 |
return me; |
|
293 |
} |
|
294 |
|
|
295 |
@Override |
|
296 |
public void fromJSON(String json) { |
|
297 |
this.fromJSON(new JSONObject(json)); |
|
298 |
} |
|
299 |
|
|
300 |
@Override |
|
301 |
public void fromJSON(JSONObject jsonPlan) { |
|
302 |
|
|
303 |
this.name = jsonPlan.getString("name"); |
|
304 |
this.tolerance = jsonPlan.getDouble("tolerance"); |
|
305 |
|
|
306 |
JSONArray jsonDataSets = jsonPlan.getJSONArray("dataSets"); |
|
307 |
for (Object o : jsonDataSets) { |
|
308 |
TopologyDataSet dataSet = new DefaultTopologyDataSet(); |
|
309 |
dataSet.fromJSON((JSONObject) o); |
|
310 |
this.dataSets.put(dataSet.getName(),dataSet); |
|
311 |
} |
|
312 |
|
|
313 |
JSONArray jsonRules = jsonPlan.getJSONArray("rules"); |
|
314 |
for (Object o : jsonRules) { |
|
315 |
JSONObject jsonRule = (JSONObject) o; |
|
316 |
TopologyRuleFactory factory = this.manager.getRulefactory(jsonRule.getString("__factoryId")); |
|
317 |
TopologyRule rule = factory.createRule(this,null, null, -1); |
|
318 |
rule.fromJSON(jsonRule); |
|
319 |
this.addRule(rule); |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
@Override |
|
324 |
public void removeDataSet(TopologyDataSet dataSet) { |
|
325 |
this.dataSets.remove(dataSet.getName()); |
|
326 |
} |
|
327 |
|
|
328 |
@Override |
|
329 |
public void removeRule(TopologyRule rule) { |
|
330 |
this.rules.remove(rule); |
|
331 |
} |
|
332 |
|
|
333 |
|
|
334 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyReport.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.topology.lib.impl; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Collections; |
|
28 |
import java.util.List; |
|
29 |
import org.gvsig.expressionevaluator.Code; |
|
30 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
31 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
32 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
34 |
import org.gvsig.fmap.geom.Geometry; |
|
35 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
36 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
|
37 |
import org.gvsig.topology.lib.api.TopologyPlan; |
|
38 |
import org.gvsig.topology.lib.api.TopologyReport; |
|
39 |
import org.gvsig.topology.lib.api.TopologyReportLine; |
|
40 |
import org.gvsig.topology.lib.api.TopologyReportLineSet; |
|
41 |
import org.gvsig.topology.lib.api.TopologyRule; |
|
42 |
import org.slf4j.Logger; |
|
43 |
import org.slf4j.LoggerFactory; |
|
44 |
|
|
45 |
/** |
|
46 |
* |
|
47 |
* @author jjdelcerro |
|
48 |
*/ |
|
49 |
@SuppressWarnings("UseSpecificCatch") |
|
50 |
public class DefaultTopologyReport |
|
51 |
extends AbstractTopologyReportLineSet |
|
52 |
implements TopologyReport |
|
53 |
{ |
|
54 |
|
|
55 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTopologyReport.class); |
|
56 |
|
|
57 |
private class DefaultTopologyReportLineSet extends AbstractTopologyReportLineSet { |
|
58 |
|
|
59 |
public DefaultTopologyReportLineSet() { |
|
60 |
super(); |
|
61 |
} |
|
62 |
|
|
63 |
public void polulate(List<TopologyReportLine> allLines, String filter) { |
|
64 |
this.lines.clear(); |
|
65 |
if (filter == null) { |
|
66 |
this.lines.addAll(allLines); |
|
67 |
this.completed = true; |
|
68 |
return; |
|
69 |
} |
|
70 |
SimpleTaskStatus theTaskStatus = plan.getTaskStatus(); |
|
71 |
try { |
|
72 |
theTaskStatus.push(); |
|
73 |
theTaskStatus.restart(); |
|
74 |
theTaskStatus.message("Preparing filter"); |
|
75 |
theTaskStatus.setAutoremove(true); |
|
76 |
theTaskStatus.setIndeterminate(); |
|
77 |
|
|
78 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
79 |
|
|
80 |
TopologyReportLineSymbolTable lineSymbolTable = new TopologyReportLineSymbolTable(); |
|
81 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
|
82 |
symbolTable.addSymbolTable(lineSymbolTable); |
|
83 |
|
|
84 |
Code code = manager.compile(filter); |
|
85 |
code = manager.optimize(symbolTable, code); |
|
86 |
|
|
87 |
theTaskStatus.setRangeOfValues(0, allLines.size()); |
|
88 |
theTaskStatus.setCurValue(0); |
|
89 |
for (TopologyReportLine line : allLines) { |
|
90 |
lineSymbolTable.setLine(line); |
|
91 |
Object value = manager.evaluate(symbolTable, code); |
|
92 |
if (value instanceof Boolean && ((Boolean) value)) { |
|
93 |
this.lines.add(line); |
|
94 |
this.changeListenerHelper.fireEvent(); |
|
95 |
} |
|
96 |
theTaskStatus.incrementCurrentValue(); |
|
97 |
} |
|
98 |
|
|
99 |
} catch (Exception ex) { |
|
100 |
LOGGER.warn("Problems filtering.", ex); |
|
101 |
theTaskStatus.abort(); |
|
102 |
} finally { |
|
103 |
if (theTaskStatus.isRunning()) { |
|
104 |
theTaskStatus.terminate(); |
|
105 |
} |
|
106 |
theTaskStatus.pop(); |
|
107 |
this.completed = true; |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 |
// TODO: Habria que meter las lineas del report en disco |
|
115 |
private final TopologyPlan plan; |
|
116 |
|
|
117 |
public DefaultTopologyReport(TopologyPlan plan) { |
|
118 |
this.plan = plan; |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
public TopologyReportLine addLine(TopologyRule rule, TopologyDataSet dataSet1, |
|
123 |
TopologyDataSet dataSet2, Geometry geometry, Geometry error, |
|
124 |
FeatureReference feature1, FeatureReference feature2, |
|
125 |
boolean exception, String description |
|
126 |
) { |
|
127 |
TopologyReportLine line = new DefaultTopologyReportLine( |
|
128 |
this, rule, dataSet1, dataSet2, geometry, error, feature1, |
|
129 |
feature2, -1, -1, exception, description, null |
|
130 |
); |
|
131 |
this.lines.add(line); |
|
132 |
this.fireChangeEvent(); |
|
133 |
return line; |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public TopologyReportLine addLine(TopologyRule rule, |
|
138 |
TopologyDataSet dataSet1,TopologyDataSet dataSet2, |
|
139 |
Geometry geometry, Geometry error, |
|
140 |
FeatureReference feature1, FeatureReference feature2, |
|
141 |
int primitive1, int primitive2, |
|
142 |
boolean exception, |
|
143 |
String description, |
|
144 |
String data |
|
145 |
) { |
|
146 |
TopologyReportLine line = new DefaultTopologyReportLine( |
|
147 |
this, rule, dataSet1, dataSet2, geometry, error, feature1, |
|
148 |
feature2, primitive1, primitive2, exception, description, data |
|
149 |
); |
|
150 |
this.lines.add(line); |
|
151 |
this.fireChangeEvent(); |
|
152 |
return line; |
|
153 |
} |
|
154 |
|
|
155 |
@Override |
|
156 |
public void removeAllLines() { |
|
157 |
this.lines.clear(); |
|
158 |
this.fireChangeEvent(); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public TopologyReportLineSet getLineSet(final String filter) { |
|
163 |
final DefaultTopologyReportLineSet set = new DefaultTopologyReportLineSet(); |
|
164 |
Thread th = new Thread(new Runnable() { |
|
165 |
@Override |
|
166 |
public void run() { |
|
167 |
set.polulate(lines, filter); |
|
168 |
} |
|
169 |
}); |
|
170 |
th.start(); |
|
171 |
return set; |
|
172 |
} |
|
173 |
|
|
174 |
@Override |
|
175 |
public List<TopologyReportLine> getLines(String filter) { |
|
176 |
if (filter == null) { |
|
177 |
return Collections.unmodifiableList(this.lines); |
|
178 |
} |
|
179 |
List<TopologyReportLine> ll = new ArrayList<>(); |
|
180 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
181 |
|
|
182 |
TopologyReportLineSymbolTable lineSymbolTable = new TopologyReportLineSymbolTable(); |
|
183 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
|
184 |
symbolTable.addSymbolTable(lineSymbolTable); |
|
185 |
|
|
186 |
Code code = manager.compile(filter); |
|
187 |
code = manager.optimize(symbolTable, code); |
|
188 |
|
|
189 |
for (TopologyReportLine line : this.lines) { |
|
190 |
lineSymbolTable.setLine(line); |
|
191 |
Object value = manager.evaluate(symbolTable, code); |
|
192 |
if (value instanceof Boolean && ((Boolean) value)) { |
|
193 |
ll.add(line); |
|
194 |
} |
|
195 |
} |
|
196 |
return Collections.unmodifiableList(ll); |
|
197 |
} |
|
198 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/TopologyReportLineSymbolTable.java | ||
---|---|---|
1 |
package org.gvsig.topology.lib.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
|
6 |
import org.gvsig.topology.lib.api.TopologyReport; |
|
7 |
import org.gvsig.topology.lib.api.TopologyReportLine; |
|
8 |
|
|
9 |
/** |
|
10 |
* |
|
11 |
* @author jjdelcerro |
|
12 |
*/ |
|
13 |
class TopologyReportLineSymbolTable extends AbstractSymbolTable { |
|
14 |
|
|
15 |
private TopologyReportLine line = null; |
|
16 |
private final List<String> fieldNames; |
|
17 |
|
|
18 |
public TopologyReportLineSymbolTable() { |
|
19 |
this.fieldNames = new ArrayList<>(); |
|
20 |
this.fieldNames.add(TopologyReport.RULE_ID); |
|
21 |
this.fieldNames.add(TopologyReport.IS_ERROR); |
|
22 |
this.fieldNames.add(TopologyReport.IS_EXCEPTION); |
|
23 |
this.fieldNames.add(TopologyReport.GEOMETRY); |
|
24 |
} |
|
25 |
|
|
26 |
public void setLine(TopologyReportLine line) { |
|
27 |
this.line = line; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public boolean exists(String name) { |
|
32 |
if (name == null) { |
|
33 |
return false; |
|
34 |
} |
|
35 |
return this.fieldNames.contains(name.toUpperCase()); |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public Object value(String name) { |
|
40 |
switch (name.toUpperCase()) { |
|
41 |
case TopologyReport.RULE_ID: |
|
42 |
return this.line.getRule().getId(); |
|
43 |
case TopologyReport.IS_ERROR: |
|
44 |
return !this.line.isException(); |
|
45 |
case TopologyReport.IS_EXCEPTION: |
|
46 |
return this.line.isException(); |
|
47 |
case TopologyReport.GEOMETRY: |
|
48 |
return this.line.getGeometry(); |
|
49 |
default: |
|
50 |
return null; |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
} |
org.gvsig.topology/tags/org.gvsig.topology-1.0.41/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/AbstractTopologyReportLineSet.java | ||
---|---|---|
1 |
package org.gvsig.topology.lib.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Collections; |
|
5 |
import java.util.List; |
|
6 |
import javax.swing.event.ChangeListener; |
|
7 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
|
8 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
9 |
import org.gvsig.topology.lib.api.TopologyReportLine; |
|
10 |
import org.gvsig.topology.lib.api.TopologyReportLineSet; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
public abstract class AbstractTopologyReportLineSet implements TopologyReportLineSet { |
|
17 |
|
|
18 |
protected final List<TopologyReportLine> lines; |
|
19 |
protected final ChangeListenerHelper changeListenerHelper; |
|
20 |
protected boolean completed = false; |
|
21 |
|
|
22 |
public AbstractTopologyReportLineSet() { |
|
23 |
this.lines = new ArrayList<>(); |
|
24 |
this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper(); |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public List<TopologyReportLine> getLines() { |
|
29 |
return Collections.unmodifiableList(this.lines); |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public void addChangeListener(ChangeListener cl) { |
|
34 |
this.changeListenerHelper.addChangeListener(cl); |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public ChangeListener[] getChangeListeners() { |
|
39 |
return this.changeListenerHelper.getChangeListeners(); |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public void removeChangeListener(ChangeListener cl) { |
|
44 |
this.changeListenerHelper.removeChangeListener(cl); |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public void removeAllChangeListener() { |
|
49 |
this.changeListenerHelper.removeAllChangeListener(); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public boolean hasChangeListeners() { |
|
54 |
return this.changeListenerHelper.hasChangeListeners(); |
|
55 |
} |
|
56 |
|
|
57 |
protected void fireChangeEvent() { |
|
58 |
this.changeListenerHelper.fireEvent(); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public boolean isComplete() { |
|
63 |
return this.completed; |
|
64 |
} |
|
65 |
|
|
66 |
@Override |
|
67 |
public int size() { |
|
68 |
return this.lines.size(); |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public TopologyReportLine get(int index) { |
|
73 |
return this.lines.get(index); |
|
74 |
} |
|
75 |
|
Also available in: Unified diff