root / branches / dal_time_support / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 36418
History | View | Annotate | Download (8.09 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 org.cresques.cts.IProjection; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataTypes; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 |
import org.gvsig.tools.dispose.DisposableIterator; |
36 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.geom.Geometry; |
43 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
44 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
45 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
46 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
47 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
48 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
49 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
50 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
51 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
52 |
import org.gvsig.tools.exception.BaseException; |
53 |
import org.slf4j.Logger; |
54 |
import org.slf4j.LoggerFactory; |
55 |
|
56 |
/**
|
57 |
* Default {@link GraphicLayer} implementation.
|
58 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
59 |
*/
|
60 |
public class DefaultGraphicLayer extends FLyrVect implements GraphicLayer{ |
61 |
private static final String DEFAULT = "default"; |
62 |
protected static final Logger logger = LoggerFactory.getLogger(DefaultGraphicLayer.class); |
63 |
private static DataManager dataManager = DALLocator.getDataManager(); |
64 |
private IVectorialUniqueValueLegend legend = null; |
65 |
|
66 |
private FeatureStore store = null; |
67 |
private long ids = 0; |
68 |
|
69 |
private int symbolId = -1; |
70 |
|
71 |
public DefaultGraphicLayer() {
|
72 |
super();
|
73 |
} |
74 |
|
75 |
public void initialize(IProjection projection) throws ValidateDataParametersException, DataException, LoadLayerException { |
76 |
store = dataManager.createMemoryStore(FEATURE_ATTR_PRIORITY); |
77 |
store.edit(); |
78 |
|
79 |
EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable(); |
80 |
|
81 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GROUPID, DataTypes.STRING); |
82 |
|
83 |
EditableFeatureAttributeDescriptor geometryDescriptor = editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GEOMETRY, DataTypes.GEOMETRY); |
84 |
geometryDescriptor.setGeometryType(TYPES.GEOMETRY); |
85 |
geometryDescriptor.setGeometrySubType(SUBTYPES.GEOM2D); |
86 |
geometryDescriptor.setSRS(projection); |
87 |
editableFeatureType.setDefaultGeometryAttributeName(GraphicLayer.FEATURE_ATTR_GEOMETRY); |
88 |
|
89 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_IDSYMBOL, DataTypes.INT); |
90 |
|
91 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_LABEL, DataTypes.STRING); |
92 |
|
93 |
EditableFeatureAttributeDescriptor featureIdDescriptor = |
94 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_FEATUREID, DataTypes.LONG); |
95 |
|
96 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_TAG, DataTypes.OBJECT); |
97 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_PRIORITY, DataTypes.INT); |
98 |
|
99 |
featureIdDescriptor.setIsPrimaryKey(true);
|
100 |
editableFeatureType.setHasOID(true);
|
101 |
|
102 |
store.update(editableFeatureType); |
103 |
|
104 |
store.finishEditing(); |
105 |
|
106 |
this.setDataStore(store);
|
107 |
} |
108 |
|
109 |
public void addGraphic(String groupId, Geometry geom, int idsym) { |
110 |
addGraphic(groupId, geom, idsym, null, null, DEFAULT_PRIORITY); |
111 |
} |
112 |
|
113 |
public void addGraphic(Geometry geom, int idsym) { |
114 |
addGraphic(DEFAULT, geom, idsym, null, null, DEFAULT_PRIORITY); |
115 |
} |
116 |
|
117 |
public void addGraphic(Geometry geom, int idsym, String label) { |
118 |
addGraphic(DEFAULT, geom, idsym, label, null, DEFAULT_PRIORITY);
|
119 |
} |
120 |
|
121 |
public void addGraphic(String groupId, Geometry geom, int idsym, String label) { |
122 |
addGraphic(groupId, geom, idsym, label, null, DEFAULT_PRIORITY);
|
123 |
} |
124 |
|
125 |
public void addGraphic(String groupId, Geometry geom, int idsym, String label, |
126 |
Object tag, int priority) { |
127 |
|
128 |
try{
|
129 |
if (!store.isEditing()){
|
130 |
store.edit(FeatureStore.MODE_APPEND); |
131 |
} |
132 |
EditableFeature feature = store.createNewFeature().getEditable(); |
133 |
feature.setString(GraphicLayer.FEATURE_ATTR_GROUPID, groupId); |
134 |
feature.setGeometry(GraphicLayer.FEATURE_ATTR_GEOMETRY, geom); |
135 |
feature.setInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL, idsym); |
136 |
feature.setString(GraphicLayer.FEATURE_ATTR_LABEL, label); |
137 |
feature.setLong(GraphicLayer.FEATURE_ATTR_FEATUREID, ids); |
138 |
feature.set(GraphicLayer.FEATURE_ATTR_TAG, tag); |
139 |
feature.setInt(GraphicLayer.FEATURE_ATTR_PRIORITY, priority); |
140 |
|
141 |
ids++; |
142 |
|
143 |
store.insert(feature); |
144 |
store.finishEditing(); |
145 |
} catch (DataException e) {
|
146 |
logger.error("Error adding a geometry to the graphic layer", e);
|
147 |
} |
148 |
} |
149 |
|
150 |
public int addSymbol(ISymbol newSymbol) { |
151 |
symbolId++; |
152 |
legend.addSymbol(new Integer(symbolId), newSymbol); |
153 |
return symbolId;
|
154 |
} |
155 |
|
156 |
public ISymbol getSymbol(int symbolPos) { |
157 |
return legend.getSymbolByValue(new Integer(symbolPos)); |
158 |
} |
159 |
|
160 |
public int getSymbolId(ISymbol symbol) { |
161 |
Object key = legend.getSymbolKey(symbol);
|
162 |
return key == null ? -1 : ((Number) key).intValue(); |
163 |
} |
164 |
|
165 |
public void clearAllGraphics() { |
166 |
DisposableIterator iterator = null;
|
167 |
FeatureSet featureSet = null;
|
168 |
try{
|
169 |
if (!store.isEditing()){
|
170 |
store.edit(); |
171 |
} |
172 |
featureSet = store.getFeatureSet(); |
173 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
174 |
Feature feature = (Feature) iterator.next(); |
175 |
featureSet.delete(feature); |
176 |
} |
177 |
store.finishEditing(); |
178 |
} catch (DataException e) {
|
179 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
180 |
} finally {
|
181 |
if (featureSet != null) { |
182 |
featureSet.dispose(); |
183 |
} |
184 |
if (iterator != null) { |
185 |
iterator.dispose(); |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
public int clearAllSymbols() { |
191 |
legend.clear(); |
192 |
symbolId = -1;
|
193 |
return symbolId;
|
194 |
} |
195 |
|
196 |
public void removeGraphics(String groupId) { |
197 |
DisposableIterator iterator = null;
|
198 |
FeatureSet featureSet = null;
|
199 |
try{
|
200 |
if (!store.isEditing()){
|
201 |
store.edit(); |
202 |
} |
203 |
featureSet = store.getFeatureSet(); |
204 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
205 |
Feature feature = (Feature) iterator.next(); |
206 |
if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
|
207 |
featureSet.delete(feature); |
208 |
} |
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 |
protected void doDispose() throws BaseException { |
224 |
super.doDispose();
|
225 |
if (store != null) { |
226 |
store.dispose(); |
227 |
store = null;
|
228 |
} |
229 |
} |
230 |
|
231 |
/* (non-Javadoc)
|
232 |
* @see org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect#setLegend(org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend)
|
233 |
*/
|
234 |
public void setLegend(IVectorLegend legend) throws LegendLayerException { |
235 |
if (legend instanceof IVectorialUniqueValueLegend){ |
236 |
super.setLegend(legend);
|
237 |
this.legend = (IVectorialUniqueValueLegend)legend;
|
238 |
this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL}); |
239 |
}else{
|
240 |
logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
|
241 |
} |
242 |
} |
243 |
} |