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