Statistics
| Revision:

root / tags / v2_0_0_Build_2049 / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 38488

History | View | Annotate | Download (11.1 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

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {Iver T.I.}   {Task}
26
*/
27
package org.gvsig.fmap.mapcontext.layers.vectorial.impl;
28

    
29
import java.awt.Graphics2D;
30
import java.awt.geom.Point2D;
31
import java.awt.image.BufferedImage;
32
import java.util.Iterator;
33

    
34
import org.cresques.cts.ICoordTrans;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.DataTypes;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
42
import org.gvsig.fmap.dal.feature.EditableFeature;
43
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
44
import org.gvsig.fmap.dal.feature.EditableFeatureType;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureSet;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.Geometry.TYPES;
52
import org.gvsig.fmap.geom.GeometryLocator;
53
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
54
import org.gvsig.fmap.geom.primitive.Envelope;
55
import org.gvsig.fmap.mapcontext.ViewPort;
56
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
57
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
58
import org.gvsig.fmap.mapcontext.exceptions.ReprojectLayerException;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
61
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
62
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
63
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
64
import org.gvsig.tools.dispose.DisposableIterator;
65
import org.gvsig.tools.exception.BaseException;
66
import org.gvsig.tools.task.Cancellable;
67
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
69

    
70
/**
71
 * Default {@link GraphicLayer} implementation. 
72
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
73
 */
74
public class DefaultGraphicLayer extends FLyrVect implements GraphicLayer{
75
        private static final String DEFAULT = "default";
76
        protected static final Logger logger = LoggerFactory.getLogger(DefaultGraphicLayer.class);
77
        private static DataManager dataManager = DALLocator.getDataManager();
78
        private IVectorialUniqueValueLegend legend = null;
79
        
80
        private FeatureStore store = null;
81
        private long ids = 0;
82

    
83
        private int symbolId = -1;
84
        
85
        private int featureIdIndex;
86
        private int groupIdIndex;
87
        private int geomIndex;
88
        private int idsymIndex;
89
        private int labelIndex;
90
        private int tagIndex;
91
        private int priorityIndex;
92
        
93
        public void initialize(IProjection projection) throws ValidateDataParametersException, DataException, LoadLayerException { 
94
                store = dataManager.createMemoryStore(FEATURE_ATTR_PRIORITY);
95
                store.edit();
96

    
97
                EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable();
98

    
99
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GROUPID, DataTypes.STRING);
100

    
101
                EditableFeatureAttributeDescriptor geometryDescriptor = editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GEOMETRY, DataTypes.GEOMETRY);
102
                geometryDescriptor.setGeometryType(TYPES.GEOMETRY);
103
                geometryDescriptor.setGeometrySubType(SUBTYPES.GEOM2D);
104
                geometryDescriptor.setSRS(projection);
105
                editableFeatureType.setDefaultGeometryAttributeName(GraphicLayer.FEATURE_ATTR_GEOMETRY);
106

    
107
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_IDSYMBOL, DataTypes.INT);
108

    
109
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_LABEL, DataTypes.STRING);
110

    
111
                EditableFeatureAttributeDescriptor featureIdDescriptor = 
112
                        editableFeatureType.add(GraphicLayer.FEATURE_ATTR_FEATUREID, DataTypes.LONG);
113

    
114
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_TAG, DataTypes.OBJECT);
115
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_PRIORITY, DataTypes.INT);
116
                
117
                featureIdDescriptor.setIsPrimaryKey(true);
118
                editableFeatureType.setHasOID(true);
119

    
120
                store.update(editableFeatureType);        
121

    
122
                store.finishEditing();
123
                
124
                FeatureType ftype = store.getDefaultFeatureType();
125
                featureIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_FEATUREID);
126
                groupIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GROUPID);
127
                geomIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GEOMETRY);
128
                idsymIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_IDSYMBOL);
129
                labelIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_LABEL);
130
                tagIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_TAG);
131
                priorityIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_PRIORITY);
132

    
133
                this.setDataStore(store);
134
                
135
                setName("Graphic Layer");
136
        }
137
        
138
        public void addGraphic(String groupId, Geometry geom, int idsym) {
139
                addGraphic(groupId, geom, idsym, null, null, DEFAULT_PRIORITY);
140
        }
141

    
142
        public void addGraphic(Geometry geom, int idsym) {
143
                addGraphic(DEFAULT, geom, idsym, null, null, DEFAULT_PRIORITY);
144
        }
145

    
146
        public void addGraphic(Geometry geom, int idsym, String label) {
147
                addGraphic(DEFAULT, geom, idsym, label, null, DEFAULT_PRIORITY);
148
        }
149

    
150
        public void addGraphic(String groupId, Geometry geom, int idsym, String label) {
151
                addGraphic(groupId, geom, idsym, label, null, DEFAULT_PRIORITY);
152
        }
153
        
154
        public void addGraphic(String groupId, Geometry geom, int idsym, String label,
155
                        Object tag, int priority) {
156

    
157
                try{
158
                        store.beginComplexNotification();
159
                // Just in case another thread is going to read from the store
160
                        synchronized (store) {                                
161
                                if (!store.isEditing()){
162
                                        store.edit(FeatureStore.MODE_APPEND);
163
                                }
164
                                insertGeometry(groupId, geom, idsym, label, tag, priority);
165
                                store.finishEditing();
166
                        }
167
                        store.endComplexNotification();
168
                } catch (DataException e) {
169
                        logger.error("Error adding a geometry to the graphic layer", e);
170
                }
171
        }
172

    
173
    private void insertGeometry(String groupId, Geometry geom, int idsym,
174
        String label, Object tag, int priority) throws DataException {
175
        EditableFeature feature = store.createNewFeature().getEditable();
176
        feature.setString(groupIdIndex, groupId);
177
        feature.setGeometry(geomIndex, geom);
178
        feature.setInt(idsymIndex, idsym);        
179
        feature.setString(labelIndex, label);        
180
        feature.setLong(featureIdIndex, ids);
181
        feature.set(tagIndex, tag);
182
        feature.setInt(priorityIndex, priority);
183

    
184
        ids++;
185

    
186
        store.insert(feature);
187
    }        
188

    
189
    public void addGraphics(String groupId, Iterator geoms, int idsym) {
190
        addGraphics(groupId, geoms, idsym, null, null, DEFAULT_PRIORITY);
191
    }
192

    
193
    public void addGraphics(String groupId, Iterator geoms, int idsym,
194
        String label) {
195
        addGraphics(groupId, geoms, idsym, label, null, DEFAULT_PRIORITY);
196
    }
197

    
198
    public void addGraphics(String groupId, Iterator geoms, int idsym,
199
        String label, Object tag, int priority) {
200
        try {
201
                // Just in case another thread is going to read from the store
202
                synchronized (store) {                                
203
                        if (!store.isEditing()) {
204
                                store.edit(FeatureStore.MODE_APPEND);
205
                        }
206
                        store.disableNotifications();
207
                        for (; geoms.hasNext();) {
208
                                Geometry geom = (Geometry) geoms.next();
209
                                insertGeometry(groupId, geom, idsym, label, tag, priority);
210
                        }
211
                        store.enableNotifications();
212
                        store.finishEditing();
213
                        }
214
        } catch (DataException e) {
215
            logger.error("Error adding a geometry to the graphic layer", e);
216
        }
217
    }
218

    
219
        public int addSymbol(ISymbol newSymbol) {
220
                symbolId++;
221
                legend.addSymbol(new Integer(symbolId), newSymbol);
222
                return symbolId;
223
        }
224

    
225
        public ISymbol getSymbol(int symbolPos) {
226
                return legend.getSymbolByValue(new Integer(symbolPos));                
227
        }
228

    
229
        public int getSymbolId(ISymbol symbol) {
230
                Object key = legend.getSymbolKey(symbol);
231
                return key == null ? -1 : ((Number) key).intValue();
232
        }
233

    
234
        public void clearAllGraphics() {
235
                DisposableIterator iterator = null;
236
                FeatureSet featureSet = null;
237
                try{
238
                        if (!store.isEditing()){
239
                                store.edit();
240
                        }
241
                        featureSet = store.getFeatureSet();
242
                        for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
243
                                Feature feature = (Feature) iterator.next();
244
                                featureSet.delete(feature);
245
                        }
246
                        store.finishEditing();                        
247
                } catch (DataException e) {
248
                        logger.error("Error clearing all the geometry of the graphic layer", e);
249
                } finally {
250
                        if (featureSet != null) {
251
                                featureSet.dispose();
252
                        }
253
                        if (iterator != null) {
254
                                iterator.dispose();
255
                        }
256
                }
257
        }
258

    
259
        public int clearAllSymbols() {
260
                legend.clear();
261
                symbolId = -1;
262
                return symbolId;
263
        }
264

    
265
        public void removeGraphics(String groupId) {
266
                DisposableIterator iterator = null;
267
                FeatureSet featureSet = null;
268
                try{
269
                        store.beginComplexNotification();
270
                        if (!store.isEditing()){
271
                                store.edit();
272
                        }
273
                        featureSet = store.getFeatureSet();
274
                        store.beginEditingGroup(groupId);
275
                        for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
276
                                Feature feature = (Feature) iterator.next();
277
                                if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
278
                                        featureSet.delete(feature);
279
                                }
280
                        }
281
                        store.endEditingGroup();
282
                        store.finishEditing();
283
                        store.endComplexNotification();
284
                } catch (DataException e) {
285
                        logger.error("Error clearing all the geometry of the graphic layer", e);
286
                } finally {
287
                        if (featureSet != null) {
288
                                featureSet.dispose();
289
                        }
290
                        if (iterator != null) {
291
                                iterator.dispose();
292
                        }
293
                }
294
        }
295

    
296
        protected void doDispose() throws BaseException {
297
                super.doDispose();
298
                if (store != null) {
299
                        store.dispose();
300
                        store = null;
301
                }
302
        }
303

    
304
        public void setLegend(IVectorLegend legend) throws LegendLayerException {
305
                if (legend instanceof IVectorialUniqueValueLegend){                
306
                        super.setLegend(legend);
307
                        this.legend = (IVectorialUniqueValueLegend)legend;
308
                        this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL});
309
                }else{
310
                        logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
311
                }
312
        }
313
        
314
    public Envelope getFullEnvelope() throws ReadException {
315
        Envelope rAux;
316
        try {
317
            rAux = getFeatureStore().getEnvelope();
318
        } catch (BaseException e) {
319
            throw new ReadException(getName(), e);
320
        }
321

    
322
        if (rAux == null) {
323
                return null;
324
        }
325
        
326
        ICoordTrans ct = getCoordTrans();
327
        if (ct != null) {
328
                rAux = rAux.convert(ct);
329
        }
330
        return rAux;
331
    }
332
}