Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.annotation / src / main / java / org / gvsig / annotation / impl / DefaultAnnotationManager.java @ 31676

History | View | Annotate | Download (14.6 KB)

1
package org.gvsig.annotation.impl;
2

    
3
import java.awt.Font;
4
import java.awt.Rectangle;
5
import java.io.IOException;
6
import java.util.HashMap;
7
import java.util.Iterator;
8
import java.util.Map;
9
import java.util.Set;
10

    
11
import org.gvsig.andami.messages.NotificationManager;
12
import org.gvsig.annotation.AnnotationLocator;
13
import org.gvsig.annotation.AnnotationManager;
14
import org.gvsig.annotation.AnnotationPreferences;
15
import org.gvsig.fmap.dal.DALLocator;
16
import org.gvsig.fmap.dal.DataManager;
17
import org.gvsig.fmap.dal.DataServerExplorer;
18
import org.gvsig.fmap.dal.exception.DataException;
19
import org.gvsig.fmap.dal.exception.ReadException;
20
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
21
import org.gvsig.fmap.dal.feature.DisposableIterator;
22
import org.gvsig.fmap.dal.feature.EditableFeature;
23
import org.gvsig.fmap.dal.feature.EditableFeatureType;
24
import org.gvsig.fmap.dal.feature.Feature;
25
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.dal.feature.FeatureSelection;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.fmap.dal.feature.FeatureStore;
29
import org.gvsig.fmap.dal.feature.FeatureType;
30
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.GeometryManager;
34
import org.gvsig.fmap.geom.exception.CreateGeometryException;
35
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
36
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
37
import org.gvsig.tools.dataTypes.DataTypes;
38

    
39
public class DefaultAnnotationManager implements AnnotationManager{
40
        private DataManager dataManager = DALLocator.getDataManager();
41
        private AnnotationPreferences preferences=null;
42
        private FeatureAttributeDescriptor[] fieldsNames;
43
        private long rowCount;
44
        private int posText;
45
        private int posTypeFont;
46
        private int posStyleFont;
47
        private int posColor;
48
        private int posHeight;
49
        private int posRotate;
50
        private boolean rightType;
51
                
52
        /**
53
         * Initialize set preference values
54
         */
55
        private void InitializePreferences(){
56
                //logger initialized
57
                //Default mapped columns
58
                posText = getPreferences().getMappedNumColumnText();
59
        posTypeFont = getPreferences().getMappedColumnNumHeight();
60
        posStyleFont = getPreferences().getMappedColumnNumStyleFont();
61
        posColor = getPreferences().getMappedNumColumnColor();
62
        posHeight = getPreferences().getMappedColumnNumHeight();
63
        posRotate = getPreferences().getMappedColumnNumRotate();        
64
        }
65
        
66
        /***************************
67
         * CREATE METHODS        *
68
         ***************************/
69
        /**
70
         * Create Feature annotation Type
71
         * @param params 
72
         */
73
        private EditableFeatureType createFeatureType(NewFeatureStoreParameters params){
74
                EditableFeatureType featureType = (EditableFeatureType)params.getDefaultFeatureType();
75
                
76
                // ANNOTATION TEXT
77
                  if (posText == -1)
78
                          featureType.add("TEXT",DataTypes.STRING, 100); //Default text size
79
                  else{
80
                          featureType.add("TEXT",DataTypes.STRING, fieldsNames[posText].getSize()); 
81
                  }
82
                  featureType.add("TYPEFONT",DataTypes.STRING,50);
83
                  featureType.add("STYLEFONT",DataTypes.INT,10);
84
                  featureType.add("COLOR",DataTypes.INT,10);
85
                  featureType.add("HEIGHT",DataTypes.DOUBLE,10);
86
                  featureType.add("ROTATE",DataTypes.DOUBLE,10);
87
                  featureType.add("GEOMETRY", DataTypes.GEOMETRY).setGeometryType(
88
                                Geometry.TYPES.POINT).setGeometrySubType(
89
                                Geometry.SUBTYPES.GEOM2D);
90
                  return featureType;
91
        }
92
        
93
        /**
94
         * Create new annotation store.
95
         */
96
        private FeatureStore createEmptyAnnotationStore(FeatureStore origStore, NewFeatureStoreParameters params, DataServerExplorer explorer) throws ValidateDataParametersException, DataException, IOException{
97
                FeatureSet origFeatures = origStore.getFeatureSet();
98
                FeatureType type = origFeatures.getDefaultFeatureType();
99
                fieldsNames = type.getAttributeDescriptors();
100
                EditableFeatureType featureType = createFeatureType(params);
101
            params.setDefaultFeatureType(featureType);
102
                 //Adding new basic annotation feature type.
103
            explorer.add(params, true);
104
            FeatureStore annotationStore = (FeatureStore) dataManager.createStore(params);
105
            annotationStore.edit(FeatureStore.MODE_APPEND);
106
            annotationStore.finishEditing();
107
            return annotationStore;
108
        }
109
        
110
        /**
111
         * Fill the annotation store with the original store.
112
         * @throws DataException
113
         * @throws CreateGeometryException 
114
         */
115
        private void createFillAnnotationStore(FeatureStore origStore,FeatureStore annotationStore, String duplicate) throws DataException, CreateGeometryException{
116
                if (posText == -1){
117
                          //throws new DataException();
118
                        return;
119
                }else{
120
                        try {
121
                                FeatureSelection selection = origStore.getFeatureSelection();
122
                                rowCount = selection.getSize();
123
                                if(rowCount==0){
124
                                        rowCount=origStore.getFeatureCount();
125
                                }
126
                        } catch (ReadException e) {
127
                                NotificationManager.addError(e);
128
                        }catch (DataException e) {
129
                                NotificationManager.addError(e);
130
                        }
131
                        annotationStore.edit(FeatureStore.MODE_APPEND);
132
                        if (duplicate.equals("duplicate.none")) 
133
                                createNormal(origStore,annotationStore);
134
                        else if (duplicate.equals("centered")) 
135
                                createCentered(origStore,annotationStore);
136
                      annotationStore.finishEditing();
137
                }
138
        }
139
                
140
        /**
141
         * CREATE NORMAL: Take all distinct and equals values form the original layer and generates a
142
         *  new tag for each one. 
143
         * @throws DataException 
144
         * @throws CreateGeometryException 
145
         */
146
        private void createNormal(FeatureStore origStore, FeatureStore annotationStore) throws DataException, CreateGeometryException{
147
                //Take the original attribute names and types from features
148
                FeatureSet origFeatures = origStore.getFeatureSet();
149
                DisposableIterator it = origFeatures.iterator();
150
                FeatureType type = origFeatures.getDefaultFeatureType();
151
                fieldsNames = type.getAttributeDescriptors();
152
                while(it.hasNext()){
153
                        Feature origFeature = (Feature)it.next();
154
                        EditableFeature feature = annotationStore.createNewFeature();
155
                        feature=fillNewFeature(feature,origFeature);                                   
156
                        annotationStore.insert(feature);        
157
                }
158
        }
159
        
160
        /**
161
         * CREATE CENTERED: Take the not repeated values from the selected original layer chosen attribute. 
162
         * Generates a layer with the distinct value selection. 
163
         * TODO Take a look to use less memory.
164
         * @throws DataException 
165
         * @throws CreateGeometryException 
166
         */
167
        @SuppressWarnings("unchecked")
168
        private void createCentered(FeatureStore origStore, FeatureStore annotationStore) throws DataException, CreateGeometryException{
169
                Map <String,EditableFeature>namesMapped=new HashMap<String,EditableFeature>();
170
                FeatureSet origFeatures = origStore.getFeatureSet();
171
                DisposableIterator it = origFeatures.iterator();
172
                FeatureType type = origFeatures.getDefaultFeatureType();
173
                fieldsNames = type.getAttributeDescriptors();
174
                for (int i = 0; i < rowCount; i++) {
175
                                Feature origFeature = (Feature)it.next();
176
                                String annotationText = (String) origFeature.get(fieldsNames[posText].getName()).toString();
177
                        if (!namesMapped.containsKey(annotationText)) {
178
                                EditableFeature feature = annotationStore.createNewFeature();
179
                                        feature=fillNewFeature(feature,origFeature);
180
                                        namesMapped.put(annotationText,feature);
181
                        }else{
182
                                EditableFeature oldFeature = namesMapped.get(annotationText);
183
                                Geometry oldGeometry=oldFeature.getDefaultGeometry();
184
                                Geometry origGeometry = origFeature.getDefaultGeometry();
185
                                Rectangle oldRect = oldGeometry.getBounds();
186
                                  Rectangle origRect = origGeometry.getBounds();
187
                                  Rectangle resul = (Rectangle) origRect.createUnion(oldRect);
188
                                  GeometryManager gf = GeometryLocator.getGeometryManager();
189
                                  Geometry geom = gf.createPoint(resul.getCenterX(),resul.getCenterY(), Geometry.SUBTYPES.GEOM2D);
190
                                //GEOMETRY INTERNAL POINT
191
                               oldFeature.set("GEOMETRY", geom); 
192
                               //remove previous duplicated features from mapped attributes list
193
                               namesMapped.remove(annotationText);
194
                               namesMapped.put(annotationText, oldFeature);
195
                        }
196
                }
197
                //Inserting to the store the selected Features only...
198
                Set<?> entries = namesMapped.entrySet();
199
                Iterator<?> mapIterator = entries.iterator();
200
                while (mapIterator.hasNext()) {
201
                     Map.Entry entry = (Map.Entry) mapIterator.next();
202
                     annotationStore.insert((EditableFeature) entry.getValue());        
203
                }                        
204
                /**
205
                 * Write the file annotation features launching a new Thread TASK 
206
                 */
207
            //PluginServices.cancelableBackgroundExecution((IMonitorableTask) new AnnotationCreateTask(origStore, annotationStore, duplicate));    
208
        }
209

    
210
        /**
211
         * Create annotation Layer from Empty annotation Store and original Store
212
         * @throws IOException 
213
         */
214
        public void create(FeatureStore origStore, DataServerExplorer explorer, NewFeatureStoreParameters parameters, String duplicate) throws DataException, ValidateDataParametersException, CreateGeometryException, IOException {
215
                InitializePreferences();
216
                FeatureStore annotationStore = createEmptyAnnotationStore(origStore,parameters, explorer);
217
                createFillAnnotationStore(origStore, annotationStore, duplicate);
218
        }
219

    
220
        /**
221
         * Fill a new feature with values from original file feature. 
222
         * @param feature
223
         * @param origFeature
224
         * @return feature edited
225
         * @throws CreateGeometryException
226
         */
227
        private EditableFeature fillNewFeature(EditableFeature feature, Feature origFeature) throws CreateGeometryException{
228
                // ANNOTATION TEXT
229
                  if (posText == -1)
230
                          feature.setString("TEXT",getPreferences().getText());
231
                  else
232
                          feature.setString("TEXT", origFeature.getString(fieldsNames[posText].getName()));
233
                  
234
                  // TYPE FONT
235
                  if (posTypeFont == -1)
236
                          feature.setString("TYPEFONT",getPreferences().getTypeFont());
237
                  else
238
                          feature.setString("TYPEFONT",origFeature.getString(fieldsNames[posTypeFont].getName()));
239
        
240
                  // STYLE FONT
241
                  if (posStyleFont == -1)
242
                          feature.setInt("STYLEFONT",getPreferences().getStyleFont());
243
                  else
244
                          feature.setInt("STYLEFONT",origFeature.getInt(fieldsNames[posStyleFont].getName()));
245
                  
246
                  // COLOR
247
                  if (posColor==-1) 
248
                          feature.setInt("COLOR", getPreferences().getColor());
249
                  else
250
                          feature.setInt("COLOR",origFeature.getInt(fieldsNames[posColor].getName()));
251
                  
252
                  // HEIGHT
253
                  if (posHeight==-1)
254
                          feature.setDouble("HEIGHT", getPreferences().getHeight());
255
                  else
256
                          feature.setDouble("HEIGHT",origFeature.getDouble(fieldsNames[posHeight].getName()));
257
                  
258
                  // ROTATE
259
                  if (posRotate == -1)
260
                          feature.setDouble("ROTATE", getPreferences().getRotate());
261
                  else
262
                          feature.setDouble("ROTATE",origFeature.getDouble(fieldsNames[posRotate].getName()));
263
                  
264
                  Geometry origGeometry = origFeature.getDefaultGeometry();
265
                  Rectangle rect = origGeometry.getBounds();
266
                  GeometryManager gf = GeometryLocator.getGeometryManager();
267
                  Geometry geom = gf.createPoint(rect.getCenterX(),rect.getCenterY(), Geometry.SUBTYPES.GEOM2D);
268
                                              
269
                  //GEOMETRY INTERNAL POINT
270
               feature.set("GEOMETRY", geom); 
271
               return feature;
272
        }
273
        
274
        
275
        private AnnotationPreferences getPreferences() {
276
                if ( preferences==null ) {
277
                        preferences = AnnotationLocator.getAnnotationPreferences();
278
                }
279
                return preferences;
280
        }
281
        
282
        /**
283
         * OPEN ANNOTATION: Opens an store to generate new Labeling Strategy.
284
         */
285
        public ILabelingStrategy open(FeatureStore annotationStore,int cartographicSupport) throws DataException {
286
                //Creating a new Strategy.
287
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
288
                
289
                FeatureSet annotationFeatures = annotationStore.getFeatureSet();
290
                DisposableIterator it = annotationFeatures.iterator();
291
                FeatureType type = annotationFeatures.getDefaultFeatureType();
292
                fieldsNames = type.getAttributeDescriptors();
293
                if (it.hasNext()){
294
                        Feature feature = (Feature) it.next();
295
                        int fontsize=0;
296
                        try{
297
                                fontsize=new Double(feature.getDouble("HEIGHT")).intValue();
298
                        }catch (ClassCastException e){
299
                                //In order to maintain the compatibility with 1.1.2 annotations.
300
                                fontsize=feature.getInt("HEIGHT");
301
                        }
302
                        int style = feature.getInt("STYLEFONT");
303
                        Font font = new Font(feature.getString("TYPEFONT"),style,fontsize);
304
                
305
                        strategy.setTextField("TEXT");
306
                        strategy.setColorField("COLOR");
307
                        strategy.setHeightField("HEIGHT");
308
                        strategy.setRotationField("ROTATE");
309
                        strategy.setFont(font);        
310
                        strategy.setReferenceSystem(cartographicSupport);
311
                        //TODO set data unit
312
                }                
313
                return strategy;
314
        }
315

    
316
        public ILabelingStrategy activate(FeatureStore annotationStore,int cartographicSupport) throws DataException {
317
                boolean resul = checkIsAnnotation(annotationStore);
318
                if (resul==true){
319
                        if (rightType==false){
320
                                //TODO EXPORTAR A FORMATO CORRECTO ANNOTATION gvSIG 2.0
321
                                //CREATE SHAPE NEW PARAMETERS AND DATA SERVER EXPLORER
322
                                //Create new Shape file with the right fields
323
                     }        
324
                        //Open AnnotationLayer
325
                        return open(annotationStore,cartographicSupport);
326
                }
327
                return null;
328
        }
329

    
330
        public boolean checkIsAnnotation(FeatureStore annotationStore) throws DataException {
331
                int fieldsIncluded=0;
332
                rightType=true;
333
                
334
                //Checking that the store has the right fields
335
                FeatureSet annotationFeatures = annotationStore.getFeatureSet();
336
                FeatureType type = annotationFeatures.getDefaultFeatureType();
337
                fieldsNames = type.getAttributeDescriptors();
338
                for(int i=0;i<fieldsNames.length;i++){
339
                        if(fieldsNames[i].getName().compareToIgnoreCase("TEXT")==0){
340
                                getPreferences().setMappedNumColumnText(i);
341
                                if(fieldsNames[i].getDataType()==DataTypes.STRING)
342
                                        rightType=true;
343
                                else
344
                                        rightType=false;
345
                                fieldsIncluded++;
346
                        }else if(fieldsNames[i].getName().compareToIgnoreCase("TYPEFONT")==0){
347
                                getPreferences().setMappedColumnNumTypeFont(i);
348
                                if(fieldsNames[i].getDataType()==DataTypes.STRING)
349
                                        rightType=true;
350
                                else 
351
                                        rightType=false;
352
                                fieldsIncluded++;
353
                        }else if(fieldsNames[i].getName().compareToIgnoreCase("STYLEFONT")==0){
354
                                getPreferences().setMappedColumnNumStyleFont(i);
355
                                if(fieldsNames[i].getDataType()==DataTypes.INT)
356
                                        rightType=true;
357
                                else
358
                                        rightType=false;
359
                                fieldsIncluded++;
360
                        }else if(fieldsNames[i].getName().compareToIgnoreCase("COLOR")==0){
361
                                getPreferences().setMappedNumColumnColor(i);
362
                                if(fieldsNames[i].getDataType()==DataTypes.INT)
363
                                        rightType=true;
364
                                else
365
                                        rightType=false;
366
                                fieldsIncluded++;
367
                        }else if(fieldsNames[i].getName().compareToIgnoreCase("ROTATE")==0){
368
                                getPreferences().setMappedColumnNumRotate(i);
369
                                if(fieldsNames[i].getDataType()==DataTypes.DOUBLE)
370
                                        rightType=true;
371
                                else
372
                                        rightType=false;
373
                                fieldsIncluded++;
374
                        }else if(fieldsNames[i].getName().compareToIgnoreCase("HEIGHT")==0){
375
                                getPreferences().setMappedNumColumnText(i);
376
                                if((fieldsNames[i].getDataType()==DataTypes.DOUBLE)||(fieldsNames[i].getDataType()==DataTypes.INT))        
377
                                        rightType=true;
378
                                else
379
                                        rightType=false;
380
                                fieldsIncluded++;
381
                        }
382
                }
383
                if (fieldsIncluded==6){
384
                        return true;
385
                }
386
                return false;
387
        }
388
        
389
}