1 |
|
|
2 |
|
package org.gvsig.app.extension;
|
3 |
|
|
4 |
|
import java.io.File;
|
5 |
|
import java.util.logging.Level;
|
6 |
|
import java.util.logging.Logger;
|
7 |
|
import org.apache.commons.io.FilenameUtils;
|
8 |
|
import org.apache.commons.lang3.ArrayUtils;
|
9 |
|
import org.gvsig.andami.plugins.Extension;
|
10 |
|
import org.gvsig.app.ApplicationLocator;
|
11 |
|
import org.gvsig.app.ApplicationManager;
|
12 |
|
import org.gvsig.app.project.documents.Document;
|
13 |
|
import org.gvsig.app.project.documents.view.ViewDocument;
|
14 |
|
import org.gvsig.app.project.documents.view.ViewManager;
|
15 |
|
import org.gvsig.fmap.dal.DALLocator;
|
16 |
|
import org.gvsig.fmap.dal.DataManager;
|
17 |
|
import org.gvsig.fmap.dal.DataStoreParameters;
|
18 |
|
import org.gvsig.fmap.dal.feature.Feature;
|
19 |
|
import org.gvsig.fmap.dal.feature.FeatureStore;
|
20 |
|
import org.gvsig.fmap.dal.feature.FeatureType;
|
21 |
|
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
|
22 |
|
import org.gvsig.fmap.geom.Geometry;
|
23 |
|
import org.gvsig.fmap.geom.GeometryLocator;
|
24 |
|
import org.gvsig.fmap.geom.GeometryManager;
|
25 |
|
import org.gvsig.fmap.geom.SpatialIndex;
|
26 |
|
import org.gvsig.fmap.geom.SpatialIndexFactory;
|
27 |
|
import org.gvsig.fmap.mapcontext.layers.FLayer;
|
28 |
|
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
|
29 |
|
import org.gvsig.tools.dataTypes.DataTypes;
|
30 |
|
import org.gvsig.tools.dynobject.DynObject;
|
31 |
|
import org.gvsig.tools.exception.BaseException;
|
32 |
|
import org.gvsig.tools.service.Service;
|
33 |
|
import org.gvsig.tools.service.ServiceException;
|
34 |
|
import org.gvsig.tools.util.HasAFile;
|
35 |
|
import org.gvsig.tools.visitor.VisitCanceledException;
|
36 |
|
import org.gvsig.tools.visitor.Visitor;
|
37 |
|
|
38 |
|
|
39 |
|
public class CreateSpatialIndexExtension extends Extension {
|
40 |
|
|
41 |
|
@Override
|
42 |
|
public void initialize() {
|
43 |
|
}
|
44 |
|
|
45 |
|
@Override
|
46 |
|
public void execute(String actionCommand) {
|
47 |
|
if( "layer-create-spatialindex".equalsIgnoreCase(actionCommand) ) {
|
48 |
|
ApplicationManager application = ApplicationLocator.getManager();
|
49 |
|
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
50 |
|
if( view ==null ) {
|
51 |
|
return;
|
52 |
|
}
|
53 |
|
FLayer[] layers = view.getMapContext().getLayers().getActives();
|
54 |
|
if( !ArrayUtils.isEmpty(layers) ) {
|
55 |
|
for( FLayer layer : layers ) {
|
56 |
|
if( layer instanceof FLyrVect ) {
|
57 |
|
FeatureStore store = ((FLyrVect) layer).getFeatureStore();
|
58 |
|
DataStoreParameters params = store.getParameters();
|
59 |
|
if( params instanceof HasAFile) {
|
60 |
|
createSpatialIndex(store);
|
61 |
|
}
|
62 |
|
}
|
63 |
|
}
|
64 |
|
}
|
65 |
|
}
|
66 |
|
}
|
67 |
|
|
68 |
|
@Override
|
69 |
|
public boolean isEnabled() {
|
70 |
|
ApplicationManager application = ApplicationLocator.getManager();
|
71 |
|
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
72 |
|
if( view ==null ) {
|
73 |
|
return false;
|
74 |
|
}
|
75 |
|
FLayer[] layers = view.getMapContext().getLayers().getActives();
|
76 |
|
if( !ArrayUtils.isEmpty(layers) ) {
|
77 |
|
for( FLayer layer : layers ) {
|
78 |
|
if( layer.isVisible() && layer instanceof FLyrVect ) {
|
79 |
|
DataStoreParameters params = ((FLyrVect) layer).getFeatureStore().getParameters();
|
80 |
|
if( params instanceof HasAFile) {
|
81 |
|
return true;
|
82 |
|
}
|
83 |
|
}
|
84 |
|
}
|
85 |
|
}
|
86 |
|
return false;
|
87 |
|
}
|
88 |
|
|
89 |
|
@Override
|
90 |
|
public boolean isVisible() {
|
91 |
|
ApplicationManager application = ApplicationLocator.getManager();
|
92 |
|
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
93 |
|
if( view ==null ) {
|
94 |
|
return false;
|
95 |
|
}
|
96 |
|
return view.getMapContext().hasVectorLayers();
|
97 |
|
}
|
98 |
|
|
99 |
|
private void createSpatialIndex(FeatureStore store) {
|
100 |
|
|
101 |
|
try {
|
102 |
|
HasAFile storeParams = (HasAFile) store.getParameters();
|
103 |
|
File f = new File(FilenameUtils.removeExtension(storeParams.getFile().getAbsolutePath()) + ".jqt");
|
104 |
|
|
105 |
|
GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
106 |
|
DynObject indexParams = geomManager.createServiceParameters(geomManager.SPATIALINDEX_DEFAULT_QUADTREE);
|
107 |
|
((HasAFile)indexParams).setFile(f);
|
108 |
|
final SpatialIndex index = store.wrapSpatialIndex(geomManager.createSpatialIndex(geomManager.SPATIALINDEX_DEFAULT_QUADTREE, indexParams));
|
109 |
|
|
110 |
|
store.accept(new Visitor() {
|
111 |
|
@Override
|
112 |
|
public void visit(Object o) throws VisitCanceledException, BaseException {
|
113 |
|
Feature f = (Feature) o;
|
114 |
|
Geometry g = f.getDefaultGeometry();
|
115 |
|
index.insert(g,f);
|
116 |
|
}
|
117 |
|
});
|
118 |
|
|
119 |
|
} catch (Exception ex) {
|
120 |
|
}
|
121 |
|
|
122 |
|
|
123 |
|
}
|
124 |
|
|
125 |
|
}
|