gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / fmap / roi / VectorialROIsReader.java @ 1232
History | View | Annotate | Download (5.55 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 |
package org.gvsig.raster.fmap.roi; |
23 |
|
24 |
import java.awt.Color; |
25 |
import java.io.File; |
26 |
import java.util.ArrayList; |
27 |
import java.util.HashMap; |
28 |
|
29 |
import org.cresques.cts.IProjection; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.coverage.exception.FileNotExistsException; |
33 |
import org.gvsig.fmap.dal.coverage.grid.Grid; |
34 |
import org.gvsig.fmap.dal.coverage.grid.ROI; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
38 |
import org.gvsig.fmap.dal.feature.Feature; |
39 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
42 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
43 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
44 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
45 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
46 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
47 |
import org.gvsig.tools.dataTypes.DataTypes; |
48 |
import org.gvsig.tools.dispose.DisposableIterator; |
49 |
|
50 |
|
51 |
public class VectorialROIsReader { |
52 |
private FLyrVect fLyrVect = null; |
53 |
private HashMap<String, ROI> rois = null; |
54 |
private Grid grid = null; |
55 |
|
56 |
|
57 |
@SuppressWarnings("deprecation") |
58 |
public VectorialROIsReader(String filename, Grid grid, IProjection projection) throws LoadLayerException, FileNotExistsException { |
59 |
this.grid = grid;
|
60 |
File file = new File(filename); |
61 |
if(file.exists()) {
|
62 |
DataManager datamanager = DALLocator.getDataManager(); |
63 |
SHPStoreParameters params = null;
|
64 |
try {
|
65 |
params = (SHPStoreParameters)datamanager.createStoreParameters(SHPStoreProvider.NAME); |
66 |
params.setCRS(projection); |
67 |
} catch (InitializeException e) {
|
68 |
throw new LoadLayerException(file.getName(),e); |
69 |
} catch (ProviderNotRegisteredException e) {
|
70 |
throw new LoadLayerException(file.getName(),e); |
71 |
} |
72 |
params.setFile(file); |
73 |
fLyrVect = (FLyrVect)LayerFactory.getInstance().createLayer("layer1", params);
|
74 |
} else
|
75 |
throw new FileNotExistsException("file not found"); |
76 |
} |
77 |
|
78 |
|
79 |
@SuppressWarnings("deprecation") |
80 |
public ArrayList<ROI> read(ArrayList<ROI> existingROIs) throws InvalidROIsShpException, DataException{ |
81 |
FeatureStore featureStore = fLyrVect.getFeatureStore(); |
82 |
|
83 |
// Validaci?n del .shp:
|
84 |
FeatureType featureType=featureStore.getDefaultFeatureType(); |
85 |
|
86 |
int nameFieldIndex = featureType.getIndex("name"); |
87 |
int rFiledIndex = featureType.getIndex("R"); |
88 |
int gFiledIndex = featureType.getIndex("G"); |
89 |
int bFiledIndex = featureType.getIndex("B"); |
90 |
|
91 |
int typeFieldIndex = featureType.getAttributeDescriptor(nameFieldIndex).getType();
|
92 |
int typerFiledIndex = featureType.getAttributeDescriptor(rFiledIndex).getType();
|
93 |
int typegFiledIndex = featureType.getAttributeDescriptor(gFiledIndex).getType();
|
94 |
int typebFiledIndex = featureType.getAttributeDescriptor(bFiledIndex).getType();
|
95 |
|
96 |
if (nameFieldIndex < 0 || rFiledIndex < 0 || gFiledIndex < 0 || bFiledIndex < 0) |
97 |
throw new InvalidROIsShpException(""); |
98 |
|
99 |
if (typeFieldIndex != DataTypes.STRING ||
|
100 |
(typerFiledIndex != DataTypes.DOUBLE && typerFiledIndex != DataTypes.INT) || |
101 |
(typegFiledIndex != DataTypes.DOUBLE && typegFiledIndex != DataTypes.INT) || |
102 |
(typebFiledIndex != DataTypes.DOUBLE && typebFiledIndex != DataTypes.INT) ) |
103 |
throw new InvalidROIsShpException(""); |
104 |
|
105 |
|
106 |
if (existingROIs != null) |
107 |
rois = new HashMap<String, ROI>(); |
108 |
if (existingROIs != null) |
109 |
for (int i = 0; i < existingROIs.size(); i++) { |
110 |
VectorialROI roi = (VectorialROI)existingROIs.get(i); |
111 |
rois.put(roi.getName(), roi); |
112 |
} |
113 |
String roiName;
|
114 |
int r, g, b;
|
115 |
FeatureSet set = null;
|
116 |
DisposableIterator features = null;
|
117 |
try {
|
118 |
set = featureStore.getFeatureSet(); |
119 |
features = set.iterator(); |
120 |
while (features.hasNext()) {
|
121 |
Feature feature = (Feature) features.next(); |
122 |
|
123 |
// }
|
124 |
// for (int i = 0; i<dataSource.getRowCount(); i++) {
|
125 |
// IFeature feature = fLyrVect.getSource().getFeature(i);
|
126 |
roiName = feature.getString(nameFieldIndex).toString(); |
127 |
VectorialROI roi = null;
|
128 |
if (!rois.containsKey(roiName)) {
|
129 |
roi = new VectorialROI(grid);
|
130 |
roi.setName(roiName); |
131 |
r = ((Number) feature.get(rFiledIndex)).intValue();
|
132 |
g = ((Number) feature.get(gFiledIndex)).intValue();
|
133 |
b = ((Number) feature.get(bFiledIndex)).intValue();
|
134 |
roi.setColor(new Color(r, g, b)); |
135 |
rois.put(roi.getName(), roi); |
136 |
} else
|
137 |
roi = (VectorialROI) rois.get(roiName); |
138 |
roi.addGeometry(feature.getDefaultGeometry()); |
139 |
} |
140 |
} finally {
|
141 |
if (features != null) |
142 |
features.dispose(); |
143 |
if (set != null) |
144 |
set.dispose(); |
145 |
} |
146 |
return new ArrayList<ROI>(rois.values()); |
147 |
} |
148 |
} |