root / branches / v2_0_0_prep / libraries / org.gvsig.annotation / org.gvsig.annotation.lib / org.gvsig.annotation.lib.impl / src / main / java / org / gvsig / annotation / impl / DefaultAnnotationCreationService.java @ 37492
History | View | Annotate | Download (15.6 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.annotation.impl; |
23 |
|
24 |
import java.io.File; |
25 |
import java.io.FileOutputStream; |
26 |
import java.io.IOException; |
27 |
import java.io.InputStream; |
28 |
import java.io.OutputStream; |
29 |
|
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
import org.gvsig.annotation.AnnotationCreationException; |
34 |
import org.gvsig.annotation.AnnotationCreationFinishAction; |
35 |
import org.gvsig.annotation.AnnotationCreationService; |
36 |
import org.gvsig.annotation.AnnotationDataTypes; |
37 |
import org.gvsig.annotation.AnnotationManager; |
38 |
import org.gvsig.annotation.calculator.AnnotationPositionCalculationException; |
39 |
import org.gvsig.annotation.calculator.AnnotationPositionCalculator; |
40 |
import org.gvsig.annotation.calculator.AnnotationPositionCalculatorCreationException; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.DataTypes; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.InitializeException; |
46 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
47 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
48 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
49 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
51 |
import org.gvsig.fmap.dal.feature.Feature; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
55 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
56 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
57 |
import org.gvsig.fmap.geom.Geometry; |
58 |
import org.gvsig.tools.dispose.DisposableIterator; |
59 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
60 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
61 |
|
62 |
|
63 |
/**
|
64 |
* Default {@link AnnotationCreationService} implementation.
|
65 |
*
|
66 |
* @author gvSIG Team
|
67 |
* @version $Id$
|
68 |
*/
|
69 |
public class DefaultAnnotationCreationService extends AbstractMonitorableTask implements AnnotationCreationService { |
70 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultAnnotationCreationService.class); |
71 |
private static final String TEMPLATE_NAME = "template.gvslab"; |
72 |
private static DataManager dataManager = DALLocator.getDataManager(); |
73 |
|
74 |
private AnnotationManager manager;
|
75 |
private FeatureStore sourceStore;
|
76 |
|
77 |
private int sourceFontTypeAttribute = -1; |
78 |
private int sourceFontStyleAttribute = -1; |
79 |
private int sourceFontColorAttribute = -1; |
80 |
private int sourceRotationAttribute = -1; |
81 |
private int sourceHeigthAttribute = -1; |
82 |
private AnnotationPositionCalculator annotationPositionCalculator = null; |
83 |
|
84 |
private String destinationGeometryAttributeName = null; |
85 |
|
86 |
private AnnotationCreationFinishAction annotationCreationFinishAction = null; |
87 |
|
88 |
/**
|
89 |
* {@link DefaultAnnotationCreationService} constructor with a
|
90 |
* {@link AnnotationManager}.
|
91 |
*
|
92 |
* @param manager
|
93 |
* to use in the service
|
94 |
* @throws DataException
|
95 |
*/
|
96 |
public DefaultAnnotationCreationService(FeatureStore featureStore, AnnotationManager manager) throws DataException { |
97 |
super("annotation"); |
98 |
this.sourceStore = featureStore;
|
99 |
this.manager = manager;
|
100 |
destinationGeometryAttributeName = featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
101 |
} |
102 |
|
103 |
public AnnotationManager getManager() {
|
104 |
return this.manager; |
105 |
} |
106 |
|
107 |
public FeatureStore createAnnotationStore(String destinationShapeFile, int textValueAttribute) |
108 |
throws AnnotationCreationException {
|
109 |
try {
|
110 |
if (destinationShapeFile == null){ |
111 |
throw new AnnotationCreationException("File can not be null"); |
112 |
} |
113 |
|
114 |
String destinationShapeFileWithoutExtension = destinationShapeFile.toLowerCase().replaceAll("\\.shp", ""); |
115 |
if (destinationShapeFileWithoutExtension.length() == destinationShapeFile.length()-4){ |
116 |
destinationShapeFileWithoutExtension = destinationShapeFile.substring(0, destinationShapeFile.length()-4); |
117 |
}else{
|
118 |
destinationShapeFileWithoutExtension = destinationShapeFile; |
119 |
} |
120 |
|
121 |
NewFeatureStoreParameters newFeatureStoreParameters = (NewFeatureStoreParameters)dataManager.createNewStoreParameters("FilesystemExplorer", "Shape"); |
122 |
newFeatureStoreParameters.setDynValue("shpfile", destinationShapeFileWithoutExtension + ".shp"); |
123 |
newFeatureStoreParameters.setDynValue("dbffile", destinationShapeFileWithoutExtension + ".dbf"); |
124 |
newFeatureStoreParameters.setDynValue("shxfile", destinationShapeFileWithoutExtension + ".shx"); |
125 |
newFeatureStoreParameters.setDynValue("crs", sourceStore.getDefaultFeatureType().getDefaultSRS());
|
126 |
|
127 |
EditableFeatureType editableFeatureType = sourceStore.getDefaultFeatureType().getEditable();//newFeatureStoreParameters.getDefaultFeatureType().getEditable();
|
128 |
FeatureAttributeDescriptor[] featureAttributeDescriptors = editableFeatureType.getAttributeDescriptors();
|
129 |
for (int i=featureAttributeDescriptors.length-1 ; i>=0 ; i--){ |
130 |
editableFeatureType.remove(i); |
131 |
} |
132 |
initializeFeatureType(editableFeatureType); |
133 |
newFeatureStoreParameters.setDefaultFeatureType(editableFeatureType); |
134 |
|
135 |
dataManager.newStore("FilesystemExplorer", "Shape", newFeatureStoreParameters, true); |
136 |
|
137 |
//If there is not an annotationPositionCalculator it gets the default
|
138 |
if (annotationPositionCalculator == null){ |
139 |
annotationPositionCalculator = manager.getDefaultAnnotationPositionCalculator(); |
140 |
} |
141 |
|
142 |
FeatureStore destinationStore = (FeatureStore) dataManager.openStore("Shape", newFeatureStoreParameters);
|
143 |
|
144 |
copyFeatureStore(sourceStore, |
145 |
destinationStore, |
146 |
textValueAttribute); |
147 |
|
148 |
try {
|
149 |
copyLegend(destinationStore); |
150 |
} catch (IOException e) { |
151 |
LOG.error("Error copying the legend");
|
152 |
} |
153 |
|
154 |
if (annotationCreationFinishAction != null){ |
155 |
annotationCreationFinishAction.finished(destinationStore); |
156 |
} |
157 |
|
158 |
return destinationStore;
|
159 |
} catch (InitializeException e) {
|
160 |
throw new AnnotationCreationException(e); |
161 |
} catch (ProviderNotRegisteredException e) {
|
162 |
throw new AnnotationCreationException(e); |
163 |
} catch (ValidateDataParametersException e) {
|
164 |
throw new AnnotationCreationException(e); |
165 |
} catch (DynFieldNotFoundException e) {
|
166 |
throw new AnnotationCreationException(e); |
167 |
} catch (DataException e) {
|
168 |
throw new AnnotationCreationException(e); |
169 |
} catch (AnnotationPositionCalculatorCreationException e) {
|
170 |
throw new AnnotationCreationException(e); |
171 |
} |
172 |
} |
173 |
|
174 |
public FeatureStore createAnnotationStore(String destinationShapeFile, |
175 |
String textValueAttributeName) throws AnnotationCreationException { |
176 |
try {
|
177 |
return createAnnotationStore(destinationShapeFile, getIndex(textValueAttributeName));
|
178 |
} catch (DataException e) {
|
179 |
throw new AnnotationCreationException(e); |
180 |
} |
181 |
} |
182 |
|
183 |
private void initializeFeatureType(EditableFeatureType editableFeatureType) throws DataException |
184 |
{ |
185 |
EditableFeatureAttributeDescriptor geometryType = editableFeatureType.add(destinationGeometryAttributeName, DataTypes.GEOMETRY); |
186 |
geometryType.setAllowNull(false);
|
187 |
geometryType.setGeometryType(Geometry.TYPES.POINT).setGeometrySubType(Geometry.SUBTYPES.GEOM2D); |
188 |
geometryType.setSRS(sourceStore.getDefaultFeatureType().getDefaultSRS()); |
189 |
geometryType.setObjectClass(Geometry.class); |
190 |
|
191 |
editableFeatureType.add(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME, AnnotationDataTypes.TEXT, 256).setAllowNull(true); |
192 |
editableFeatureType.add(AnnotationManager.FONTTYPE_ATTRIBUTE_NAME, AnnotationDataTypes.FONTTYPE, 30).setAllowNull(true); |
193 |
editableFeatureType.add(AnnotationManager.FONTSTYLE_ATTRIBUTE_NAME, AnnotationDataTypes.FONTSTYLE, 30).setAllowNull(true); |
194 |
editableFeatureType.add(AnnotationManager.FONTCOLOR_ATTRIBUTE_NAME, AnnotationDataTypes.FONTCOLOR, 20).setAllowNull(true); |
195 |
editableFeatureType.add(AnnotationManager.FONTROTATION_ATTRIBUTE_NAME, AnnotationDataTypes.FONTROTATION, 5).setAllowNull(true); |
196 |
editableFeatureType.add(AnnotationManager.FONTHEGTH_ATTRIBUTE_NAME, AnnotationDataTypes.FONTHEIGHT, 5).setAllowNull(true); |
197 |
} |
198 |
|
199 |
private AttributeInserter createInserter(int attributePosition, Object defaultValue) |
200 |
{ |
201 |
if (attributePosition > -1){ |
202 |
return new StoreAttributeInserter(attributePosition); |
203 |
}else{
|
204 |
return new DefaultAttributeInserter(defaultValue); |
205 |
} |
206 |
} |
207 |
|
208 |
private void copyFeatureStore(FeatureStore sourceStore, |
209 |
FeatureStore destinationStore, int textAttribute)
|
210 |
throws AnnotationCreationException, DataException {
|
211 |
|
212 |
//Start the edition
|
213 |
destinationStore.edit(); |
214 |
|
215 |
//Copy data
|
216 |
FeatureSet featureSet = sourceStore.getFeatureSet(); |
217 |
DisposableIterator iterator = featureSet.iterator(); |
218 |
|
219 |
taskStatus.setRangeOfValues(0, featureSet.getSize());
|
220 |
|
221 |
//Create the attribute inserter's
|
222 |
AttributeInserter fontTypeAttributeInserter = createInserter(sourceFontTypeAttribute, manager.getDefaultFontType()); |
223 |
AttributeInserter fontStyleAttributeInserter = createInserter(sourceFontStyleAttribute, manager.getDefaultFontStyle()); |
224 |
AttributeInserter fontColorAttributeInserter = createInserter(sourceFontColorAttribute, manager.getDefaultFontColor()); |
225 |
AttributeInserter fontRotationAttributeInserter = createInserter(sourceRotationAttribute, manager.getDefaultFontRotation()); |
226 |
AttributeInserter fontHeigthAttributeInserter = createInserter(sourceHeigthAttribute, manager.getDefaultFontHeight()); |
227 |
|
228 |
Feature sourceFeature; |
229 |
long featureCount = 0; |
230 |
while (iterator.hasNext()) {
|
231 |
sourceFeature = (Feature) iterator.next(); |
232 |
|
233 |
EditableFeature destinationFeature = destinationStore.createNewFeature().getEditable(); |
234 |
try {
|
235 |
destinationFeature.set(destinationGeometryAttributeName, annotationPositionCalculator.getAnnotationPosition(sourceFeature)); |
236 |
} catch (AnnotationPositionCalculationException e) {
|
237 |
LOG.error("Not possible to get the point for the geometry", e);
|
238 |
} |
239 |
destinationFeature.set(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME, sourceFeature.get(textAttribute)); |
240 |
destinationFeature.set(AnnotationManager.FONTTYPE_ATTRIBUTE_NAME, fontTypeAttributeInserter.getValue(sourceFeature)); |
241 |
destinationFeature.set(AnnotationManager.FONTSTYLE_ATTRIBUTE_NAME, fontStyleAttributeInserter.getValue(sourceFeature)); |
242 |
destinationFeature.set(AnnotationManager.FONTCOLOR_ATTRIBUTE_NAME, fontColorAttributeInserter.getValue(sourceFeature)); |
243 |
destinationFeature.set(AnnotationManager.FONTROTATION_ATTRIBUTE_NAME, fontRotationAttributeInserter.getValue(sourceFeature)); |
244 |
destinationFeature.set(AnnotationManager.FONTHEGTH_ATTRIBUTE_NAME, fontHeigthAttributeInserter.getValue(sourceFeature)); |
245 |
|
246 |
destinationStore.insert(destinationFeature); |
247 |
featureCount++; |
248 |
this.taskStatus.setCurValue(featureCount);
|
249 |
|
250 |
if (this.taskStatus.isCancellationRequested()) { |
251 |
return;
|
252 |
} |
253 |
} |
254 |
|
255 |
//Finish the edition
|
256 |
destinationStore.finishEditing(); |
257 |
|
258 |
//Dispose resources
|
259 |
iterator.dispose(); |
260 |
featureSet.dispose(); |
261 |
|
262 |
this.taskStatus.terminate();
|
263 |
this.taskStatus.remove();
|
264 |
} |
265 |
|
266 |
private void copyLegend(FeatureStore destinationStore) throws ValidateDataParametersException, DataException, IOException { |
267 |
FilesystemServerExplorer filesystemServerExplorer = (FilesystemServerExplorer)destinationStore.getExplorer(); |
268 |
File target = filesystemServerExplorer.getResourcePath(destinationStore, "gvslab"); |
269 |
|
270 |
//Copy the template
|
271 |
InputStream in = getClass().getClassLoader().getResourceAsStream(TEMPLATE_NAME);
|
272 |
OutputStream out = null; |
273 |
|
274 |
out = new FileOutputStream(target); |
275 |
|
276 |
byte[] buf = new byte[1024]; |
277 |
int len;
|
278 |
while ((len = in.read(buf)) > 0) { |
279 |
out.write(buf, 0, len);
|
280 |
} |
281 |
in.close(); |
282 |
out.close(); |
283 |
} |
284 |
|
285 |
private void checkAttribute(int index, int type) throws DataException{ |
286 |
if (index >= sourceStore.getDefaultFeatureType().size()){
|
287 |
throw new IllegalArgumentException("Attribute not found"); |
288 |
} |
289 |
if (sourceStore.getDefaultFeatureType().getAttributeDescriptor(index).getDataType().getType() != type){
|
290 |
throw new IllegalArgumentException("The Attribute has not have the fine type"); |
291 |
} |
292 |
} |
293 |
|
294 |
|
295 |
public void setFontTypetAttribute(int index) throws DataException { |
296 |
checkAttribute(index, AnnotationDataTypes.FONTTYPE); |
297 |
this.sourceFontTypeAttribute = index;
|
298 |
} |
299 |
|
300 |
public void setFontStyleAttribute(int index) throws DataException { |
301 |
checkAttribute(index, AnnotationDataTypes.FONTSTYLE); |
302 |
this.sourceFontStyleAttribute = index;
|
303 |
} |
304 |
|
305 |
public void setFontColorAttribute(int index) throws DataException { |
306 |
checkAttribute(index, AnnotationDataTypes.FONTCOLOR); |
307 |
this.sourceFontColorAttribute = index;
|
308 |
} |
309 |
|
310 |
public void setFontHeigthAttribute(int index) throws DataException { |
311 |
checkAttribute(index, AnnotationDataTypes.FONTHEIGHT); |
312 |
this.sourceHeigthAttribute = index;
|
313 |
} |
314 |
|
315 |
public void setFontRotationAttribute(int index) throws DataException { |
316 |
checkAttribute(index, AnnotationDataTypes.FONTROTATION); |
317 |
this.sourceRotationAttribute = index;
|
318 |
} |
319 |
|
320 |
public void setAnnotationPositionCalculator( |
321 |
AnnotationPositionCalculator annotationPositionCalculator) { |
322 |
this.annotationPositionCalculator = annotationPositionCalculator;
|
323 |
} |
324 |
|
325 |
public int getIndex(String attributeName) throws DataException{ |
326 |
FeatureAttributeDescriptor featureAttributeDescriptor = sourceStore.getDefaultFeatureType().getAttributeDescriptor(attributeName); |
327 |
if (featureAttributeDescriptor != null){ |
328 |
return featureAttributeDescriptor.getIndex();
|
329 |
} |
330 |
return -1; |
331 |
} |
332 |
|
333 |
public void setFontColorAttribute(String attributeName) throws DataException { |
334 |
setFontColorAttribute(getIndex(attributeName)); |
335 |
} |
336 |
|
337 |
public void setFontHeigthAttribute(String attributeName) throws DataException { |
338 |
setFontHeigthAttribute(getIndex(attributeName)); |
339 |
} |
340 |
|
341 |
public void setFontRotationAttribute(String attributeName) throws DataException { |
342 |
setFontRotationAttribute(getIndex(attributeName)); |
343 |
} |
344 |
|
345 |
public void setFontStyleAttribute(String attributeName) throws DataException { |
346 |
setFontStyleAttribute(getIndex(attributeName)); |
347 |
} |
348 |
|
349 |
public void setFontTypetAttribute(String attributeName) throws DataException { |
350 |
setFontTypetAttribute(getIndex(attributeName)); |
351 |
} |
352 |
|
353 |
|
354 |
public FeatureStore getFeatureStore() {
|
355 |
return sourceStore;
|
356 |
} |
357 |
|
358 |
private interface AttributeInserter{ |
359 |
public Object getValue(Feature feature); |
360 |
} |
361 |
|
362 |
|
363 |
private class StoreAttributeInserter implements AttributeInserter{ |
364 |
private int attributePosition = -1; |
365 |
|
366 |
public StoreAttributeInserter(int attributePosition) { |
367 |
super();
|
368 |
this.attributePosition = attributePosition;
|
369 |
} |
370 |
|
371 |
public Object getValue(Feature feature){ |
372 |
return feature.get(attributePosition);
|
373 |
} |
374 |
} |
375 |
|
376 |
private class DefaultAttributeInserter implements AttributeInserter{ |
377 |
private Object defaultValue = null; |
378 |
|
379 |
public DefaultAttributeInserter(Object defaultValue) { |
380 |
super();
|
381 |
this.defaultValue = defaultValue;
|
382 |
} |
383 |
|
384 |
public Object getValue(Feature feature){ |
385 |
return defaultValue;
|
386 |
} |
387 |
} |
388 |
|
389 |
public AnnotationCreationFinishAction getAnnotationCreationFinishAction() {
|
390 |
return annotationCreationFinishAction;
|
391 |
} |
392 |
|
393 |
|
394 |
public void setAnnotationCreationFinishAction( |
395 |
AnnotationCreationFinishAction annotationCreationFinishAction) { |
396 |
this.annotationCreationFinishAction = annotationCreationFinishAction;
|
397 |
} |
398 |
} |