Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src / org / gvsig / fmap / data / index / spatial / gt2 / QuadTreeGt2Factory.java @ 23290

History | View | Annotate | Download (4.14 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {{Company}}   {{Task}}
26
 */
27

    
28
package org.gvsig.fmap.data.index.spatial.gt2;
29

    
30
import java.io.File;
31
import java.io.IOException;
32

    
33
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;
37

    
38
public class QuadTreeGt2Factory extends SpatialIndexFactory {
39
        
40
        static {
41
                registerFactory(Factories.GT2_QUADTREE, "QuadTreeGt2Factory", QuadTreeGt2Factory.class);
42
        }
43
        
44
        public SpatialIndex createSpatialIndex(SpatialIndexParameters params) throws IndexException {
45
                if (params == null) {
46
                        params = new SpatialIndexParameters();
47
                }
48
                SpatialIndex index = null;
49
                
50
                try {
51
                        String filename = params.getFilename();
52
                        File file = null;
53
                        
54
                        if (filename != null) {
55
                                file = new File(filename);
56
                        } else {
57
                                file = File.createTempFile("gvsig", ".qix");
58
                        }
59
                        
60
                        index = new QuadtreeGt2(file.getAbsolutePath(), "NM", params.getEnvelope(), params.getFeatureCount(), params.isOverwrite());
61
                        
62
                } catch (IOException ioe) {
63
                        throw new IndexException(ioe);
64
                }
65
                
66
                return index;
67
        }
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
}