Revision 23803 branches/v2_0_0_prep/libraries/libFMap_spatialindex/src/org/gvsig/fmap/data/index/spatial/gt2/QuadTreeGt2Factory.java
QuadTreeGt2Factory.java | ||
---|---|---|
30 | 30 |
import java.io.File; |
31 | 31 |
import java.io.IOException; |
32 | 32 |
|
33 |
import org.gvsig.fmap.data.index.Index; |
|
33 | 34 |
import org.gvsig.fmap.data.index.IndexException; |
34 |
import org.gvsig.fmap.data.index.spatial.SpatialIndex; |
|
35 |
import org.gvsig.fmap.data.index.spatial.SpatialIndexFactory; |
|
36 |
import org.gvsig.fmap.data.index.spatial.SpatialIndexParameters; |
|
35 |
import org.gvsig.fmap.data.index.IndexFactory; |
|
36 |
import org.gvsig.fmap.data.index.IndexParameters; |
|
37 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
38 |
import org.gvsig.tools.exception.BaseException; |
|
37 | 39 |
|
38 |
public class QuadTreeGt2Factory extends SpatialIndexFactory {
|
|
40 |
public class QuadTreeGt2Factory extends IndexFactory { |
|
39 | 41 |
|
40 | 42 |
static { |
41 |
registerFactory(Factories.GT2_QUADTREE, "QuadTreeGt2Factory", QuadTreeGt2Factory.class); |
|
43 |
registerFactory(Factories.TYPE_GEOMETRY.GT2_QUADTREE, "QuadTreeGt2Factory", QuadTreeGt2Factory.class);
|
|
42 | 44 |
} |
43 | 45 |
|
44 |
public SpatialIndex createSpatialIndex(SpatialIndexParameters params) throws IndexException { |
|
45 |
if (params == null) { |
|
46 |
params = new SpatialIndexParameters(); |
|
47 |
} |
|
48 |
SpatialIndex index = null; |
|
46 |
public Index createIndex(IndexParameters params) throws IndexException { |
|
47 |
if (params == null) throw new IllegalArgumentException("IndexParameters cannot be null."); |
|
48 |
if (params.getFeatureStore() == null) throw new IllegalArgumentException("FeatureStore cannot be null."); |
|
49 |
if (params.getFeatureType() == null) throw new IllegalArgumentException("FeatureType cannot be null."); |
|
50 |
|
|
51 |
Index index = null; |
|
49 | 52 |
|
50 | 53 |
try { |
51 |
String filename = params.getFilename(); |
|
52 | 54 |
File file = null; |
53 |
|
|
54 |
if (filename != null) { |
|
55 |
file = new File(filename); |
|
55 |
if (params.getName() == null) { |
|
56 |
file = File.createTempFile(params.getFeatureStore().getName(), ".qix"); |
|
56 | 57 |
} else { |
57 |
file = File.createTempFile("gvsig", ".qix");
|
|
58 |
file = new File(params.getFeatureStore().getName() + "-" + params.getName() + ".qix");
|
|
58 | 59 |
} |
59 | 60 |
|
60 |
index = new QuadtreeGt2(file.getAbsolutePath(), "NM", params.getEnvelope(), params.getFeatureCount(), params.isOverwrite()); |
|
61 |
Envelope envelope = (Envelope) params.getFeatureStore().getMetadata().get("extent"); |
|
62 |
int featureCount = params.getFeatureStore().getDataCollection().size(); |
|
61 | 63 |
|
62 |
} catch (IOException ioe) { |
|
63 |
throw new IndexException(ioe); |
|
64 |
index = new QuadtreeGt2(file.getAbsolutePath(), "NM", envelope, featureCount, params.isOverwrite()); |
|
65 |
index.rebuild(); |
|
66 |
} catch (IOException e) { |
|
67 |
throw new IndexException(e); |
|
68 |
} catch (BaseException e) { |
|
69 |
throw new IndexException(e); |
|
64 | 70 |
} |
65 | 71 |
|
66 | 72 |
return index; |
67 | 73 |
} |
68 |
|
|
69 |
/* Metodo extraido de FlyrVect |
|
70 |
|
|
71 |
public SpatialIndex createSpatialIndex(FeatureStore store, File indexFile) throws SpatialIndexException { |
|
72 |
SpatialIndex spatialIndex = null; |
|
73 |
|
|
74 |
String fileName = indexFile.getAbsolutePath(); |
|
75 |
SpatialIndex localCopy = null; |
|
76 |
|
|
77 |
Envelope fullExtent = (Envelope) store.getMetadata().get("extent"); |
|
78 |
int featureCount = store.getMetadata().getInt("featureCount"); |
|
79 |
|
|
80 |
double[] lc = fullExtent.getLowerCorner(); |
|
81 |
double[] uc = fullExtent.getUpperCorner(); |
|
82 |
|
|
83 |
com.vividsolutions.jts.geom.Envelope env = new com.vividsolutions.jts.geom.Envelope(lc[0], uc[0], lc[1], uc[1]); |
|
84 |
|
|
85 |
try { |
|
86 |
localCopy = new QuadtreeGt2(fileName, "NM", env, featureCount, true); |
|
87 |
} catch (SpatialIndexException sie1) { |
|
88 |
// Probably we dont have writing permissions |
|
89 |
String directoryName = System.getProperty("java.io.tmpdir"); |
|
90 |
File newFile = new File(directoryName + File.separator |
|
91 |
+ indexFile.getName()); |
|
92 |
String newFileName = newFile.getName(); |
|
93 |
try { |
|
94 |
localCopy = new QuadtreeGt2(newFileName, "NM", fullExtent, |
|
95 |
featureCount, true); |
|
96 |
} catch (SpatialIndexException sie2) { |
|
97 |
// if we cant build a file based spatial index, we'll build |
|
98 |
// a pure memory spatial index |
|
99 |
localCopy = new QuadtreeJts(); |
|
100 |
} |
|
101 |
} catch (Exception e) { |
|
102 |
e.printStackTrace(); |
|
103 |
}// try |
|
104 |
BoundedShapes shapeBounds = (BoundedShapes) va.getDriver(); |
|
105 |
try { |
|
106 |
for (int i = 0; i < va.getShapeCount(); i++) { |
|
107 |
if (cancelMonitor != null) { |
|
108 |
if (cancelMonitor.isCanceled()) |
|
109 |
return; |
|
110 |
cancelMonitor.reportStep(); |
|
111 |
} |
|
112 |
Rectangle2D r = shapeBounds.getShapeBounds(i); |
|
113 |
if (r != null) |
|
114 |
localCopy.insert(r, i); |
|
115 |
} // for |
|
116 |
va.stop(); |
|
117 |
if (localCopy instanceof PersistentSpatialIndex) |
|
118 |
((PersistentSpatialIndex) localCopy).flush(); |
|
119 |
spatialIndex = localCopy; |
|
120 |
// vectorial adapter needs a reference to the spatial index, to |
|
121 |
// solve |
|
122 |
// request for feature iteration based in spatial queries |
|
123 |
source.setSpatialIndex(spatialIndex); |
|
124 |
} catch (ReadDriverException e) { |
|
125 |
// TODO Auto-generated catch block |
|
126 |
e.printStackTrace(); |
|
127 |
} |
|
128 |
|
|
129 |
return spatialIndex; |
|
130 |
} |
|
131 |
*/ |
|
132 | 74 |
} |
Also available in: Unified diff