Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / grid / roi / VectorialROIsReader.java @ 26004

History | View | Annotate | Download (3.36 KB)

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

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

    
9
import org.cresques.cts.IProjection;
10
import org.gvsig.raster.dataset.FileNotExistsException;
11
import org.gvsig.raster.grid.Grid;
12

    
13
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
14
import com.hardcode.gdbms.engine.values.NumericValue;
15
import com.iver.andami.PluginServices;
16
import com.iver.andami.config.generate.Plugin;
17
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
18
import com.iver.cit.gvsig.fmap.core.IFeature;
19
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
20
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
21
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
22

    
23

    
24
public class VectorialROIsReader {
25
        
26
        private String                         filename                         = null;
27
        private IProjection         projection                         = null;
28
        private FLyrVect                fLyrVect                        = null;
29
        private HashMap                        rois                                = null;
30
        private Grid                        grid                                = null;
31

    
32
        
33
        public VectorialROIsReader(String filename, Grid grid, IProjection projection) throws LoadLayerException, FileNotExistsException {
34
                this.filename = filename;
35
                this.projection = projection;
36
                this.grid = grid;
37
                File file = new File(filename);
38
                if(file.exists()){
39
                        fLyrVect = (FLyrVect)LayerFactory.createLayer("layer1", "gvSIG shp driver", file, projection);
40
                }else
41
                        throw new FileNotExistsException("file not found");
42
        }
43

    
44

    
45
        public ArrayList read(ArrayList existingROIs) throws ReadDriverException, InvalidROIsShpException{
46
                SelectableDataSource dataSource = fLyrVect.getSource().getRecordset();
47
                
48
                // Validaci?n del .shp:
49
                int nameFieldIndex = dataSource.getFieldIndexByName("name");
50
                int rFiledIndex = dataSource.getFieldIndexByName("R");
51
                int gFiledIndex = dataSource.getFieldIndexByName("G");
52
                int bFiledIndex = dataSource.getFieldIndexByName("B");
53
                if (nameFieldIndex < 0 || rFiledIndex < 0 || gFiledIndex < 0 || bFiledIndex < 0){
54
                        throw new InvalidROIsShpException("");
55
                }
56
                if (dataSource.getFieldType(nameFieldIndex) != Types.VARCHAR ||
57
                        dataSource.getFieldType(rFiledIndex) < Types.NUMERIC || dataSource.getFieldType(rFiledIndex) > Types.DOUBLE  ||
58
                        dataSource.getFieldType(gFiledIndex) < Types.NUMERIC || dataSource.getFieldType(gFiledIndex) > Types.DOUBLE  ||
59
                        dataSource.getFieldType(bFiledIndex) < Types.NUMERIC || dataSource.getFieldType(bFiledIndex) > Types.DOUBLE )
60
                        throw new InvalidROIsShpException("");
61
                                
62

    
63
                if (existingROIs != null)
64
                rois = new HashMap();
65
                if (existingROIs != null){
66
                        for (int i = 0; i < existingROIs.size(); i++) {
67
                                VectorialROI roi = (VectorialROI)existingROIs.get(i);
68
                                rois.put(roi.getName(), roi);
69
                        }
70
                }
71
                String roiName;
72
                int r, g, b;
73
                for (int i = 0; i<dataSource.getRowCount(); i++) {
74
                        IFeature feature = fLyrVect.getSource().getFeature(i);
75
                        roiName = feature.getAttribute(nameFieldIndex).toString();
76
                        VectorialROI roi = null;
77
                        if (!rois.containsKey(roiName)){
78
                                roi = new VectorialROI(grid);
79
                                roi.setName(roiName);
80
                                r = ((NumericValue)feature.getAttribute(rFiledIndex)).intValue();
81
                                g = ((NumericValue)feature.getAttribute(gFiledIndex)).intValue();
82
                                b = ((NumericValue)feature.getAttribute(bFiledIndex)).intValue();
83
                                roi.setColor(new Color(r,g,b));
84
                                rois.put(roi.getName(), roi);
85
                        }
86
                        else
87
                                roi = (VectorialROI)rois.get(roiName);
88
                        roi.addGeometry(feature.getGeometry());
89
                }
90
                return new ArrayList(rois.values());
91
        }
92
}