Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 38206

History | View | Annotate | Download (10.7 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.geom.Point2D;
30
import java.util.Iterator;
31

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

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

    
79
        private int symbolId = -1;
80
        
81
        private int featureIdIndex;
82
        private int groupIdIndex;
83
        private int geomIndex;
84
        private int idsymIndex;
85
        private int labelIndex;
86
        private int tagIndex;
87
        private int priorityIndex;
88

    
89
        public DefaultGraphicLayer() {
90
                super();        
91
        }
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
                        if (!store.isEditing()){
159
                                store.edit(FeatureStore.MODE_APPEND);
160
                        }
161
                        insertGeometry(groupId, geom, idsym, label, tag, priority);
162
                        store.finishEditing();
163
                } catch (DataException e) {
164
                        logger.error("Error adding a geometry to the graphic layer", e);
165
                }
166
        }
167

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

    
179
        ids++;
180

    
181
        store.insert(feature);
182
    }        
183

    
184
    public void addGraphics(String groupId, Iterator geoms, int idsym) {
185
        addGraphics(groupId, geoms, idsym, null, null, DEFAULT_PRIORITY);
186
    }
187

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

    
193
    public void addGraphics(String groupId, Iterator geoms, int idsym,
194
        String label, Object tag, int priority) {
195
        try {
196
            if (!store.isEditing()) {
197
                store.edit(FeatureStore.MODE_APPEND);
198
            }
199
            for (; geoms.hasNext();) {
200
                Geometry geom = (Geometry) geoms.next();
201
                insertGeometry(groupId, geom, idsym, label, tag, priority);
202
            }
203
            store.finishEditing();
204
        } catch (DataException e) {
205
            logger.error("Error adding a geometry to the graphic layer", e);
206
        }
207
    }
208

    
209
        public int addSymbol(ISymbol newSymbol) {
210
                symbolId++;
211
                legend.addSymbol(new Integer(symbolId), newSymbol);
212
                return symbolId;
213
        }
214

    
215
        public ISymbol getSymbol(int symbolPos) {
216
                return legend.getSymbolByValue(new Integer(symbolPos));                
217
        }
218

    
219
        public int getSymbolId(ISymbol symbol) {
220
                Object key = legend.getSymbolKey(symbol);
221
                return key == null ? -1 : ((Number) key).intValue();
222
        }
223

    
224
        public void clearAllGraphics() {
225
                DisposableIterator iterator = null;
226
                FeatureSet featureSet = null;
227
                try{
228
                        if (!store.isEditing()){
229
                                store.edit();
230
                        }
231
                        featureSet = store.getFeatureSet();
232
                        for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
233
                                Feature feature = (Feature) iterator.next();
234
                                featureSet.delete(feature);
235
                        }
236
                        store.finishEditing();                        
237
                } catch (DataException e) {
238
                        logger.error("Error clearing all the geometry of the graphic layer", e);
239
                } finally {
240
                        if (featureSet != null) {
241
                                featureSet.dispose();
242
                        }
243
                        if (iterator != null) {
244
                                iterator.dispose();
245
                        }
246
                }
247
        }
248

    
249
        public int clearAllSymbols() {
250
                legend.clear();
251
                symbolId = -1;
252
                return symbolId;
253
        }
254

    
255
        public void removeGraphics(String groupId) {
256
                DisposableIterator iterator = null;
257
                FeatureSet featureSet = null;
258
                try{
259
                        if (!store.isEditing()){
260
                                store.edit();
261
                        }
262
                        featureSet = store.getFeatureSet();
263
                        for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
264
                                Feature feature = (Feature) iterator.next();
265
                                if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
266
                                        featureSet.delete(feature);
267
                                }
268
                        }
269
                        store.finishEditing();
270
                } catch (DataException e) {
271
                        logger.error("Error clearing all the geometry of the graphic layer", e);
272
                } finally {
273
                        if (featureSet != null) {
274
                                featureSet.dispose();
275
                        }
276
                        if (iterator != null) {
277
                                iterator.dispose();
278
                        }
279
                }
280
        }
281

    
282
        protected void doDispose() throws BaseException {
283
                super.doDispose();
284
                if (store != null) {
285
                        store.dispose();
286
                        store = null;
287
                }
288
        }
289

    
290
        /* (non-Javadoc)
291
         * @see org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect#setLegend(org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend)
292
         */
293
        public void setLegend(IVectorLegend legend) throws LegendLayerException {
294
                if (legend instanceof IVectorialUniqueValueLegend){                
295
                        super.setLegend(legend);
296
                        this.legend = (IVectorialUniqueValueLegend)legend;
297
                        this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL});
298
                }else{
299
                        logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
300
                }
301
        }
302
        
303
    public Envelope getFullEnvelope() throws ReadException {
304
            // Change parent implementation which creates an envelop by default
305
        Envelope rAux;
306
        try {
307
            rAux = getFeatureStore().getEnvelope();
308
        } catch (BaseException e) {
309
            throw new ReadException(getName(), e);
310
        }
311

    
312
        if (rAux == null) {
313
                return null;
314
        }
315
        
316
        ICoordTrans ct = getCoordTrans();
317
        if (ct != null) {
318
                rAux = rAux.convert(ct);
319
        }
320
        return rAux;
321
    }
322
}