Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / grid / roi / VectorialROIsReader.java @ 33331

History | View | Annotate | Download (4.59 KB)

1
package org.gvsig.fmap.raster.grid.roi;
2

    
3
import java.awt.Color;
4
import java.io.File;
5
import java.util.ArrayList;
6
import java.util.HashMap;
7

    
8
import org.cresques.cts.IProjection;
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.DataManager;
11
import org.gvsig.fmap.dal.DataTypes;
12
import org.gvsig.fmap.dal.exception.DataException;
13
import org.gvsig.fmap.dal.exception.InitializeException;
14
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
15
import org.gvsig.tools.dispose.DisposableIterator;
16
import org.gvsig.fmap.dal.feature.Feature;
17
import org.gvsig.fmap.dal.feature.FeatureSet;
18
import org.gvsig.fmap.dal.feature.FeatureStore;
19
import org.gvsig.fmap.dal.feature.FeatureType;
20
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
21
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider;
22
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
23
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
24
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
25
import org.gvsig.raster.dataset.FileNotExistsException;
26
import org.gvsig.raster.grid.Grid;
27

    
28

    
29
public class VectorialROIsReader {
30

    
31
        private String                         filename                         = null;
32
        private IProjection         projection                         = null;
33
        private FLyrVect                fLyrVect                        = null;
34
        private HashMap                        rois                                = null;
35
        private Grid                        grid                                = null;
36

    
37

    
38
        public VectorialROIsReader(String filename, Grid grid, IProjection projection) throws LoadLayerException, FileNotExistsException {
39
                this.filename = filename;
40
                this.projection = projection;
41
                this.grid = grid;
42
                File file = new File(filename);
43
                if(file.exists()){
44
                        DataManager datamanager=DALLocator.getDataManager();
45
                        SHPStoreParameters params=null;
46
                        try {
47
                                params = (SHPStoreParameters)datamanager.createStoreParameters(SHPStoreProvider.NAME);
48
                                params.setCRS(projection);
49
                        } catch (InitializeException e) {
50
                                throw new LoadLayerException(file.getName(),e);
51
                        } catch (ProviderNotRegisteredException e) {
52
                                throw new LoadLayerException(file.getName(),e);
53
                        }
54
                        params.setFile(file);
55
                        fLyrVect = (FLyrVect)LayerFactory.getInstance().createLayer("layer1", params);
56
                }else
57
                        throw new FileNotExistsException("file not found");
58
        }
59

    
60

    
61
        public ArrayList read(ArrayList existingROIs) throws InvalidROIsShpException, DataException{
62
                FeatureStore featureStore = fLyrVect.getFeatureStore();
63

    
64
                // Validaci?n del .shp:
65
                FeatureType featureType=featureStore.getDefaultFeatureType();
66

    
67
                int nameFieldIndex = featureType.getIndex("name");
68
                int rFiledIndex = featureType.getIndex("R");
69
                int gFiledIndex = featureType.getIndex("G");
70
                int bFiledIndex = featureType.getIndex("B");
71

    
72
                int typeFieldIndex = featureType.getAttributeDescriptor(nameFieldIndex).getType();
73
                int typerFiledIndex = featureType.getAttributeDescriptor(rFiledIndex).getType();
74
                int typegFiledIndex = featureType.getAttributeDescriptor(gFiledIndex).getType();
75
                int typebFiledIndex = featureType.getAttributeDescriptor(bFiledIndex).getType();
76

    
77
                if (nameFieldIndex < 0 || rFiledIndex < 0 || gFiledIndex < 0 || bFiledIndex < 0)
78
                        throw new InvalidROIsShpException("");
79

    
80
                if (typeFieldIndex != DataTypes.STRING ||
81
                                typerFiledIndex != DataTypes.DOUBLE || typerFiledIndex != DataTypes.INT  ||
82
                                typegFiledIndex != DataTypes.DOUBLE || typegFiledIndex != DataTypes.INT  ||
83
                                typebFiledIndex != DataTypes.DOUBLE || typebFiledIndex != DataTypes.INT )
84
                        throw new InvalidROIsShpException("");
85

    
86

    
87
                if (existingROIs != null)
88
                rois = new HashMap();
89
                if (existingROIs != null)
90
                        for (int i = 0; i < existingROIs.size(); i++) {
91
                                VectorialROI roi = (VectorialROI)existingROIs.get(i);
92
                                rois.put(roi.getName(), roi);
93
                        }
94
                String roiName;
95
                int r, g, b;
96
                FeatureSet set = null;
97
                DisposableIterator features = null;
98
                try {
99
                        set = featureStore.getFeatureSet();
100
                        features = set.iterator();
101
                        while (features.hasNext()) {
102
                                Feature feature = (Feature) features.next();
103

    
104
                                // }
105
                                // for (int i = 0; i<dataSource.getRowCount(); i++) {
106
                                // IFeature feature = fLyrVect.getSource().getFeature(i);
107
                                roiName = feature.getString(nameFieldIndex).toString();
108
                                VectorialROI roi = null;
109
                                if (!rois.containsKey(roiName)) {
110
                                        roi = new VectorialROI(grid);
111
                                        roi.setName(roiName);
112
                                        r = ((Number) feature.get(rFiledIndex)).intValue();
113
                                        g = ((Number) feature.get(gFiledIndex)).intValue();
114
                                        b = ((Number) feature.get(bFiledIndex)).intValue();
115
                                        roi.setColor(new Color(r, g, b));
116
                                        rois.put(roi.getName(), roi);
117
                                } else
118
                                        roi = (VectorialROI) rois.get(roiName);
119
                                roi.addGeometry(feature.getDefaultGeometry());
120
                        }
121
                } finally {
122
                        if (features != null)
123
                                features.dispose();
124
                        if (set != null)
125
                                set.dispose();
126
                }
127
                return new ArrayList(rois.values());
128
        }
129
}