gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / roi / VectorialROIsReader.java @ 4179
History | View | Annotate | Download (8.87 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.impl.grid.roi; |
23 |
|
24 |
import java.awt.Color; |
25 |
import java.awt.geom.Rectangle2D; |
26 |
import java.io.File; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
|
31 |
import org.cresques.cts.IProjection; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.coverage.exception.FileNotExistsException; |
37 |
import org.gvsig.fmap.dal.coverage.exception.ROIException; |
38 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
41 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
42 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
43 |
import org.gvsig.fmap.dal.feature.Feature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
48 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
49 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
50 |
import org.gvsig.fmap.geom.GeometryLocator; |
51 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
52 |
import org.gvsig.fmap.geom.primitive.Envelope; |
53 |
import org.gvsig.fmap.geom.primitive.NullGeometry; |
54 |
import org.gvsig.raster.roi.ROI; |
55 |
import org.gvsig.raster.roi.ROIReader; |
56 |
import org.gvsig.tools.dispose.DisposableIterator; |
57 |
|
58 |
|
59 |
public class VectorialROIsReader implements ROIReader { |
60 |
private HashMap<String, ROI> rois = null; |
61 |
private RasterDataStore store = null; |
62 |
private Envelope bbox = null; |
63 |
private FeatureStore[] featureStore = null; |
64 |
|
65 |
public VectorialROIsReader(
|
66 |
FeatureStore featureStore, |
67 |
RasterDataStore store, |
68 |
IProjection projection) { |
69 |
Rectangle2D e = store.getExtent().toRectangle2D();
|
70 |
try {
|
71 |
bbox = GeometryLocator.getGeometryManager().createEnvelope( |
72 |
e.getX(), e.getY(), e.getMaxX(), e.getMaxY(), SUBTYPES.GEOM2D); |
73 |
} catch (CreateEnvelopeException e1) {
|
74 |
LoggerFactory.getLogger(VectorialROIsReader.class).error("Error creating the envelope", e);
|
75 |
} |
76 |
|
77 |
this.store = store;
|
78 |
this.featureStore = new FeatureStore[1]; |
79 |
this.featureStore[0] = featureStore; |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Constructor using the envelope of the raster layer
|
84 |
* @param filename
|
85 |
* @param store
|
86 |
* @param projection
|
87 |
* @throws InvalidROIsShpException
|
88 |
* @throws FileNotExistsException
|
89 |
*/
|
90 |
public VectorialROIsReader(
|
91 |
String filename,
|
92 |
RasterDataStore store, |
93 |
IProjection projection) throws ROIException, FileNotExistsException {
|
94 |
Rectangle2D e = store.getExtent().toRectangle2D();
|
95 |
try {
|
96 |
Envelope env = GeometryLocator.getGeometryManager().createEnvelope( |
97 |
e.getX(), e.getY(), e.getMaxX(), e.getMaxY(), SUBTYPES.GEOM2D); |
98 |
init(filename, store, projection, env); |
99 |
} catch (CreateEnvelopeException e1) {
|
100 |
LoggerFactory.getLogger(VectorialROIsReader.class).error("Error creating the envelope", e);
|
101 |
} |
102 |
} |
103 |
|
104 |
public VectorialROIsReader(
|
105 |
String filename,
|
106 |
RasterDataStore store, |
107 |
IProjection projection, |
108 |
Envelope bbox) throws ROIException, FileNotExistsException {
|
109 |
init(filename, store, projection, bbox); |
110 |
} |
111 |
|
112 |
public void init( |
113 |
String filename,
|
114 |
RasterDataStore store, |
115 |
IProjection projection, |
116 |
Envelope bbox) throws ROIException, FileNotExistsException {
|
117 |
this.store = store;
|
118 |
this.bbox = bbox;
|
119 |
File file = new File(filename); |
120 |
if(file.exists()) {
|
121 |
this.featureStore = new FeatureStore[1]; |
122 |
this.featureStore[0] = openOneROIFile(file, projection); |
123 |
} else {
|
124 |
file = new File(filename + ".shp"); |
125 |
if(file.exists()) {
|
126 |
this.featureStore = new FeatureStore[1]; |
127 |
this.featureStore[0] = openOneROIFile(file, projection); |
128 |
} else {
|
129 |
this.featureStore = openThreeROIFiles(filename, projection);
|
130 |
} |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
private FeatureStore[] openThreeROIFiles(String filename, IProjection projection) throws ROIException, FileNotExistsException { |
136 |
FeatureStore[] featureStore = new FeatureStore[3]; |
137 |
if(filename.endsWith(".shp")) { |
138 |
filename = filename.substring(0, filename.lastIndexOf(".shp")); |
139 |
} |
140 |
File file = new File(filename + "_polygons.shp"); |
141 |
boolean existsAtLeastOne = false; |
142 |
if(file.exists()) {
|
143 |
featureStore[0] = openOneROIFile(file, projection);
|
144 |
existsAtLeastOne = true;
|
145 |
} |
146 |
|
147 |
file = new File(filename + "_points.shp"); |
148 |
if(file.exists()) {
|
149 |
featureStore[1] = openOneROIFile(file, projection);
|
150 |
existsAtLeastOne = true;
|
151 |
} |
152 |
|
153 |
file = new File(filename + "_polylines.shp"); |
154 |
if(file.exists()) {
|
155 |
featureStore[2] = openOneROIFile(file, projection);
|
156 |
existsAtLeastOne = true;
|
157 |
} |
158 |
|
159 |
if(!existsAtLeastOne)
|
160 |
throw new FileNotExistsException("file not found"); |
161 |
return featureStore;
|
162 |
} |
163 |
|
164 |
private FeatureStore openOneROIFile(File file, IProjection projection) throws ROIException { |
165 |
DataManager datamanager = DALLocator.getDataManager(); |
166 |
SHPStoreParameters params = null;
|
167 |
try {
|
168 |
params = (SHPStoreParameters)datamanager.createStoreParameters(SHPStoreProvider.NAME); |
169 |
params.setCRS(projection); |
170 |
} catch (InitializeException e) {
|
171 |
throw new ROIException(e.getMessage(), e); |
172 |
} catch (ProviderNotRegisteredException e) {
|
173 |
throw new ROIException(e.getMessage(), e); |
174 |
} |
175 |
params.setFile(file); |
176 |
DataManager dataManager = DALLocator.getDataManager(); |
177 |
try {
|
178 |
return (FeatureStore)dataManager.openStore(params.getDataStoreName(), params);
|
179 |
} catch (ValidateDataParametersException e) {
|
180 |
throw new ROIException(e.getMessage(), e); |
181 |
} catch (InitializeException e) {
|
182 |
throw new ROIException(e.getMessage(), e); |
183 |
} catch (ProviderNotRegisteredException e) {
|
184 |
throw new ROIException(e.getMessage(), e); |
185 |
} |
186 |
} |
187 |
|
188 |
public List<ROI> read(List<ROI> existingROIs) throws ROIException { |
189 |
for (int i = 0; i < this.featureStore.length; i++) { |
190 |
try {
|
191 |
existingROIs = read(existingROIs, this.featureStore[i]);
|
192 |
} catch (InvalidROIsShpException e) {
|
193 |
throw new ROIException("", e); |
194 |
} catch (DataException e) {
|
195 |
throw new ROIException("", e); |
196 |
} |
197 |
} |
198 |
return existingROIs;
|
199 |
} |
200 |
|
201 |
@SuppressWarnings("deprecation") |
202 |
public List<ROI> read(List<ROI> existingROIs, FeatureStore featureStore) throws InvalidROIsShpException, DataException { |
203 |
FeatureType featureType = featureStore.getDefaultFeatureType(); |
204 |
long id = System.currentTimeMillis(); |
205 |
|
206 |
int nameFieldIndex = featureType.getIndex("name"); |
207 |
int rFiledIndex = featureType.getIndex("R"); |
208 |
int gFiledIndex = featureType.getIndex("G"); |
209 |
int bFiledIndex = featureType.getIndex("B"); |
210 |
|
211 |
if (existingROIs != null) { |
212 |
rois = new HashMap<String, ROI>(); |
213 |
for (int i = 0; i < existingROIs.size(); i++) { |
214 |
VectorialROIImpl roi = (VectorialROIImpl)existingROIs.get(i); |
215 |
rois.put(roi.getName(), roi); |
216 |
} |
217 |
} |
218 |
String roiName;
|
219 |
int r = 255, g = 0, b = 0; |
220 |
FeatureSet set = null;
|
221 |
DisposableIterator features = null;
|
222 |
try {
|
223 |
set = featureStore.getFeatureSet(); |
224 |
features = set.fastIterator(); |
225 |
String defaultROIName = "ROI"; |
226 |
while (features.hasNext()) {
|
227 |
Feature feature = (Feature) features.next(); |
228 |
|
229 |
if(feature.getDefaultGeometry() == null || |
230 |
feature.getDefaultGeometry() instanceof NullGeometry)
|
231 |
continue;
|
232 |
if(feature.getDefaultGeometry() != null && |
233 |
!feature.getDefaultGeometry().getEnvelope().intersects(bbox)) |
234 |
continue;
|
235 |
|
236 |
if(nameFieldIndex >= 0) |
237 |
roiName = feature.getString(nameFieldIndex).toString(); |
238 |
else {
|
239 |
roiName = defaultROIName + "-" + (id++);
|
240 |
} |
241 |
VectorialROIImpl roi = null;
|
242 |
if (!rois.containsKey(roiName)) {
|
243 |
roi = new VectorialROIImpl(store);
|
244 |
roi.setName(roiName); |
245 |
if(rFiledIndex >= 0) |
246 |
r = ((Number) feature.get(rFiledIndex)).intValue();
|
247 |
if(gFiledIndex >= 0) |
248 |
g = ((Number) feature.get(gFiledIndex)).intValue();
|
249 |
if(bFiledIndex >= 0) |
250 |
b = ((Number) feature.get(bFiledIndex)).intValue();
|
251 |
roi.setColor(new Color(r, g, b)); |
252 |
rois.put(roi.getName(), roi); |
253 |
} else
|
254 |
roi = (VectorialROIImpl) rois.get(roiName); |
255 |
roi.addGeometry(feature.getDefaultGeometry()); |
256 |
} |
257 |
} finally {
|
258 |
if (features != null) |
259 |
features.dispose(); |
260 |
if (set != null) |
261 |
set.dispose(); |
262 |
} |
263 |
return new ArrayList<ROI>(rois.values()); |
264 |
} |
265 |
} |