Revision 725 org.gvsig.topology/trunk/org.gvsig.topology/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/lib/impl/DefaultTopologyDataSet.java
DefaultTopologyDataSet.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.topology.lib.impl; |
25 | 25 |
|
26 |
import java.util.Iterator; |
|
26 | 27 |
import java.util.Map; |
28 |
import org.apache.commons.collections.IteratorUtils; |
|
27 | 29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.apache.commons.lang3.mutable.MutableObject; |
|
31 |
import org.gvsig.expressionevaluator.Expression; |
|
28 | 32 |
import org.gvsig.fmap.dal.EditingNotification; |
29 | 33 |
import org.gvsig.fmap.dal.EditingNotificationManager; |
30 | 34 |
import org.gvsig.fmap.dal.exception.DataException; |
... | ... | |
33 | 37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 | 38 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
35 | 39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
|
36 | 41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 | 42 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
38 | 43 |
import org.gvsig.fmap.geom.Geometry; |
44 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
45 |
import org.gvsig.fmap.geom.GeometryManager; |
|
46 |
import org.gvsig.fmap.geom.SpatialIndex; |
|
39 | 47 |
import org.gvsig.fmap.geom.type.GeometryType; |
40 | 48 |
import org.gvsig.tools.exception.BaseException; |
41 | 49 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
... | ... | |
48 | 56 |
import org.gvsig.topology.lib.api.TopologyManager; |
49 | 57 |
import org.json.JSONObject; |
50 | 58 |
import org.gvsig.topology.lib.api.TopologyServices; |
59 |
import org.slf4j.Logger; |
|
60 |
import org.slf4j.LoggerFactory; |
|
51 | 61 |
|
52 | 62 |
/** |
53 | 63 |
* |
... | ... | |
56 | 66 |
@SuppressWarnings({"EqualsAndHashcode","UseSpecificCatch"}) |
57 | 67 |
public class DefaultTopologyDataSet implements TopologyDataSet { |
58 | 68 |
|
69 |
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultTopologyDataSet.class); |
|
70 |
|
|
59 | 71 |
private TopologyServices services; |
60 | 72 |
private String name; |
61 | 73 |
private FeatureStore store; |
62 | 74 |
private boolean needFinishEditing; |
63 | 75 |
private String fullName; |
64 | 76 |
private PropertiesSupportHelper propertiesHelper; |
65 |
|
|
77 |
private MutableObject<SpatialIndex> spatialIndex = null; |
|
78 |
|
|
66 | 79 |
public DefaultTopologyDataSet() { |
67 | 80 |
this.services = null; |
68 | 81 |
this.name = null; |
... | ... | |
323 | 336 |
return this.propertiesHelper.getProperties(); |
324 | 337 |
} |
325 | 338 |
|
339 |
public SpatialIndex getSpatialIndex() { |
|
340 |
if( this.spatialIndex == null ) { |
|
341 |
this.spatialIndex = new MutableObject<>(); |
|
342 |
FeatureStore theStore = this.getStore(); |
|
343 |
FeatureStoreProviderFactory storeFactory = (FeatureStoreProviderFactory) theStore.getProviderFactory(); |
|
344 |
if( storeFactory.useLocalIndexesCanImprovePerformance()==FeatureStoreProviderFactory.YES ) { |
|
345 |
try { |
|
346 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
347 |
final SpatialIndex geomIndex = geomManager.createSpatialIndex( |
|
348 |
GeometryManager.SPATIALINDEX_DEFAULT_QUADTREE, |
|
349 |
null |
|
350 |
); |
|
351 |
final SpatialIndex dataIndex = theStore.wrapSpatialIndex(geomIndex); |
|
352 |
try { |
|
353 |
store.accept(new Visitor() { |
|
354 |
@Override |
|
355 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
|
356 |
Feature f = (Feature) o; |
|
357 |
Geometry geom = f.getDefaultGeometry(); |
|
358 |
if (geom != null) { |
|
359 |
dataIndex.insert(geom, f); |
|
360 |
} |
|
361 |
} |
|
362 |
}); |
|
363 |
} catch (VisitCanceledException ex) { |
|
364 |
} |
|
365 |
this.spatialIndex.setValue(dataIndex); |
|
366 |
} catch (Exception ex) { |
|
367 |
LOGGER.warn("Can't create spatial index", ex); |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
return this.spatialIndex.getValue(); |
|
372 |
} |
|
373 |
|
|
374 |
public Iterable<FeatureReference> query(Geometry geom) { |
|
375 |
SpatialIndex index = this.getSpatialIndex(); |
|
376 |
if( index == null ) { |
|
377 |
return (Iterable<FeatureReference>) IteratorUtils.EMPTY_ITERATOR; |
|
378 |
} |
|
379 |
final Iterator it = index.query(geom); |
|
380 |
if( it == null ) { |
|
381 |
return (Iterable<FeatureReference>) IteratorUtils.EMPTY_ITERATOR; |
|
382 |
} |
|
383 |
return new Iterable<FeatureReference>() { |
|
384 |
@Override |
|
385 |
public Iterator<FeatureReference> iterator() { |
|
386 |
return it; |
|
387 |
} |
|
388 |
}; |
|
389 |
} |
|
390 |
|
|
391 |
@Override |
|
392 |
public Feature findFirst(Expression filter) { |
|
393 |
try { |
|
394 |
return this.getStore().findFirst(filter); |
|
395 |
} catch (Exception ex) { |
|
396 |
return null; |
|
397 |
} |
|
398 |
} |
|
399 |
|
|
326 | 400 |
} |
Also available in: Unified diff