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