Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 36638

History | View | Annotate | Download (9.27 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.util.Iterator;
30

    
31
import org.cresques.cts.IProjection;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.exception.DataException;
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.geom.Geometry;
47
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
48
import org.gvsig.fmap.geom.Geometry.TYPES;
49
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
50
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
51
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
52
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
53
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
54
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
55
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
56
import org.gvsig.tools.dispose.DisposableIterator;
57
import org.gvsig.tools.exception.BaseException;
58

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

    
72
        private int symbolId = -1;
73

    
74
        public DefaultGraphicLayer() {
75
                super();        
76
        }
77

    
78
        public void initialize(IProjection projection) throws ValidateDataParametersException, DataException, LoadLayerException { 
79
                store = dataManager.createMemoryStore(FEATURE_ATTR_PRIORITY);
80
                store.edit();
81

    
82
                EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable();
83

    
84
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GROUPID, DataTypes.STRING);
85

    
86
                EditableFeatureAttributeDescriptor geometryDescriptor = editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GEOMETRY, DataTypes.GEOMETRY);
87
                geometryDescriptor.setGeometryType(TYPES.GEOMETRY);
88
                geometryDescriptor.setGeometrySubType(SUBTYPES.GEOM2D);
89
                geometryDescriptor.setSRS(projection);
90
                editableFeatureType.setDefaultGeometryAttributeName(GraphicLayer.FEATURE_ATTR_GEOMETRY);
91

    
92
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_IDSYMBOL, DataTypes.INT);
93

    
94
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_LABEL, DataTypes.STRING);
95

    
96
                EditableFeatureAttributeDescriptor featureIdDescriptor = 
97
                        editableFeatureType.add(GraphicLayer.FEATURE_ATTR_FEATUREID, DataTypes.LONG);
98

    
99
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_TAG, DataTypes.OBJECT);
100
                editableFeatureType.add(GraphicLayer.FEATURE_ATTR_PRIORITY, DataTypes.INT);
101
                
102
                featureIdDescriptor.setIsPrimaryKey(true);
103
                editableFeatureType.setHasOID(true);
104

    
105
                store.update(editableFeatureType);        
106

    
107
                store.finishEditing();                
108

    
109
                this.setDataStore(store);
110
        }
111

    
112
        public void addGraphic(String groupId, Geometry geom, int idsym) {
113
                addGraphic(groupId, geom, idsym, null, null, DEFAULT_PRIORITY);
114
        }
115

    
116
        public void addGraphic(Geometry geom, int idsym) {
117
                addGraphic(DEFAULT, geom, idsym, null, null, DEFAULT_PRIORITY);
118
        }
119

    
120
        public void addGraphic(Geometry geom, int idsym, String label) {
121
                addGraphic(DEFAULT, geom, idsym, label, null, DEFAULT_PRIORITY);
122
        }
123

    
124
        public void addGraphic(String groupId, Geometry geom, int idsym, String label) {
125
                addGraphic(groupId, geom, idsym, label, null, DEFAULT_PRIORITY);
126
        }
127
        
128
        public void addGraphic(String groupId, Geometry geom, int idsym, String label,
129
                        Object tag, int priority) {
130

    
131
                try{
132
                        if (!store.isEditing()){
133
                                store.edit(FeatureStore.MODE_APPEND);
134
                        }
135
                        insertGeometry(groupId, geom, idsym, label, tag, priority);
136
                        store.finishEditing();
137
                } catch (DataException e) {
138
                        logger.error("Error adding a geometry to the graphic layer", e);
139
                }
140
        }
141

    
142
    private void insertGeometry(String groupId, Geometry geom, int idsym,
143
        String label, Object tag, int priority) throws DataException {
144
        EditableFeature feature = store.createNewFeature().getEditable();
145
        feature.setString(GraphicLayer.FEATURE_ATTR_GROUPID, groupId);
146
        feature.setGeometry(GraphicLayer.FEATURE_ATTR_GEOMETRY, geom);
147
        feature.setInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL, idsym);        
148
        feature.setString(GraphicLayer.FEATURE_ATTR_LABEL, label);        
149
        feature.setLong(GraphicLayer.FEATURE_ATTR_FEATUREID, ids);
150
        feature.set(GraphicLayer.FEATURE_ATTR_TAG, tag);
151
        feature.setInt(GraphicLayer.FEATURE_ATTR_PRIORITY, priority);
152

    
153
        ids++;
154

    
155
        store.insert(feature);
156
    }        
157

    
158
    public void addGraphics(String groupId, Iterator geoms, int idsym) {
159
        addGraphics(groupId, geoms, idsym, null, null, DEFAULT_PRIORITY);
160
    }
161

    
162
    public void addGraphics(String groupId, Iterator geoms, int idsym,
163
        String label) {
164
        addGraphics(groupId, geoms, idsym, label, null, DEFAULT_PRIORITY);
165
    }
166

    
167
    public void addGraphics(String groupId, Iterator geoms, int idsym,
168
        String label, Object tag, int priority) {
169
        try {
170
            if (!store.isEditing()) {
171
                store.edit(FeatureStore.MODE_APPEND);
172
            }
173
            for (; geoms.hasNext();) {
174
                Geometry geom = (Geometry) geoms.next();
175
                insertGeometry(groupId, geom, idsym, label, tag, priority);
176
            }
177
            store.finishEditing();
178
        } catch (DataException e) {
179
            logger.error("Error adding a geometry to the graphic layer", e);
180
        }
181
    }
182

    
183
        public int addSymbol(ISymbol newSymbol) {
184
                symbolId++;
185
                legend.addSymbol(new Integer(symbolId), newSymbol);
186
                return symbolId;
187
        }
188

    
189
        public ISymbol getSymbol(int symbolPos) {
190
                return legend.getSymbolByValue(new Integer(symbolPos));                
191
        }
192

    
193
        public int getSymbolId(ISymbol symbol) {
194
                Object key = legend.getSymbolKey(symbol);
195
                return key == null ? -1 : ((Number) key).intValue();
196
        }
197

    
198
        public void clearAllGraphics() {
199
                DisposableIterator iterator = null;
200
                FeatureSet featureSet = null;
201
                try{
202
                        if (!store.isEditing()){
203
                                store.edit();
204
                        }
205
                        featureSet = store.getFeatureSet();
206
                        for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
207
                                Feature feature = (Feature) iterator.next();
208
                                featureSet.delete(feature);
209
                        }
210
                        store.finishEditing();                        
211
                } catch (DataException e) {
212
                        logger.error("Error clearing all the geometry of the graphic layer", e);
213
                } finally {
214
                        if (featureSet != null) {
215
                                featureSet.dispose();
216
                        }
217
                        if (iterator != null) {
218
                                iterator.dispose();
219
                        }
220
                }
221
        }
222

    
223
        public int clearAllSymbols() {
224
                legend.clear();
225
                symbolId = -1;
226
                return symbolId;
227
        }
228

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

    
256
        protected void doDispose() throws BaseException {
257
                super.doDispose();
258
                if (store != null) {
259
                        store.dispose();
260
                        store = null;
261
                }
262
        }
263

    
264
        /* (non-Javadoc)
265
         * @see org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect#setLegend(org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend)
266
         */
267
        public void setLegend(IVectorLegend legend) throws LegendLayerException {
268
                if (legend instanceof IVectorialUniqueValueLegend){                
269
                        super.setLegend(legend);
270
                        this.legend = (IVectorialUniqueValueLegend)legend;
271
                        this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL});
272
                }else{
273
                        logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
274
                }
275
        }
276
}