svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 31631
History | View | Annotate | Download (8.06 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.DataStoreParameters; |
33 |
import org.gvsig.fmap.dal.DataTypes; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
36 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.dal.store.memory.MemoryStoreProvider; |
44 |
import org.gvsig.fmap.geom.Geometry; |
45 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
46 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
47 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
48 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
49 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
51 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
52 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
53 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
54 |
import org.gvsig.tools.exception.BaseException; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
/**
|
59 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
60 |
*/
|
61 |
public class DefaultGraphicLayer extends FLyrVect implements GraphicLayer{ |
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 |
DataStoreParameters parameters = dataManager |
77 |
.createStoreParameters(MemoryStoreProvider.NAME); |
78 |
|
79 |
store = (FeatureStore) dataManager.createStore(parameters); |
80 |
store.edit(); |
81 |
|
82 |
EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable(); |
83 |
|
84 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_ID, 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 |
featureIdDescriptor.setIsPrimaryKey(true);
|
99 |
editableFeatureType.setHasOID(true);
|
100 |
|
101 |
store.update(editableFeatureType); |
102 |
|
103 |
store.finishEditing(); |
104 |
|
105 |
this.setDataStore(store);
|
106 |
} |
107 |
|
108 |
public void addGraphic(String id, Geometry geom, int idsym) { |
109 |
EditableFeature feature; |
110 |
try {
|
111 |
if (!store.isEditing()){
|
112 |
store.edit(); |
113 |
} |
114 |
feature = store.createNewFeature().getEditable(); |
115 |
feature.set(GraphicLayer.FEATURE_ATTR_ID, id); |
116 |
feature.set(GraphicLayer.FEATURE_ATTR_GEOMETRY, geom); |
117 |
feature.set(GraphicLayer.FEATURE_ATTR_IDSYMBOL, new Integer(idsym)); |
118 |
feature.set(GraphicLayer.FEATURE_ATTR_FEATUREID, new Long(ids)); |
119 |
|
120 |
ids++; |
121 |
|
122 |
store.insert(feature); |
123 |
store.finishEditing(); |
124 |
} catch (DataException e) {
|
125 |
logger.error("Error adding a geometry to the graphic layer", e);
|
126 |
} |
127 |
|
128 |
} |
129 |
|
130 |
public void addGraphic(Geometry geom, int idsym) { |
131 |
addGraphic("0", geom, idsym);
|
132 |
} |
133 |
|
134 |
public void addGraphic(Geometry geom, int idsym, String label) { |
135 |
addGraphic("0", geom, idsym, label);
|
136 |
} |
137 |
|
138 |
public void addGraphic(String id, Geometry geom, int idsym, String label) { |
139 |
/*
|
140 |
* El id, puede ser duplicado. Una aplicacion/extension e?adira todos sus
|
141 |
* graficos con el mismo id para poder acceder a ellos de una tacada.
|
142 |
*/
|
143 |
try{
|
144 |
if (!store.isEditing()){
|
145 |
store.edit(); |
146 |
} |
147 |
EditableFeature feature = store.createNewFeature().getEditable(); |
148 |
feature.set(GraphicLayer.FEATURE_ATTR_ID, id); |
149 |
feature.set(GraphicLayer.FEATURE_ATTR_GEOMETRY, geom); |
150 |
feature.set(GraphicLayer.FEATURE_ATTR_IDSYMBOL, new Integer(idsym)); |
151 |
feature.set(GraphicLayer.FEATURE_ATTR_LABEL, label); |
152 |
feature.set(GraphicLayer.FEATURE_ATTR_FEATUREID, new Long(ids)); |
153 |
|
154 |
ids++; |
155 |
|
156 |
store.insert(feature); |
157 |
store.finishEditing(); |
158 |
} catch (DataException e) {
|
159 |
logger.error("Error adding a geometry to the graphic layer", e);
|
160 |
} |
161 |
} |
162 |
|
163 |
public int addSymbol(ISymbol newSymbol) { |
164 |
symbolId++; |
165 |
legend.addSymbol(new Integer(symbolId), newSymbol); |
166 |
return symbolId;
|
167 |
} |
168 |
|
169 |
public ISymbol getSymbol(int symbolPos) { |
170 |
return legend.getSymbolByValue(new Integer(symbolPos)); |
171 |
} |
172 |
|
173 |
public void clearAllGraphics() { |
174 |
DisposableIterator iterator = null;
|
175 |
FeatureSet featureSet = null;
|
176 |
try{
|
177 |
if (!store.isEditing()){
|
178 |
store.edit(); |
179 |
} |
180 |
featureSet = store.getFeatureSet(); |
181 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
182 |
Feature feature = (Feature) iterator.next(); |
183 |
featureSet.delete(feature); |
184 |
} |
185 |
store.finishEditing(); |
186 |
} catch (DataException e) {
|
187 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
188 |
} finally {
|
189 |
if (featureSet != null) { |
190 |
featureSet.dispose(); |
191 |
} |
192 |
if (iterator != null) { |
193 |
iterator.dispose(); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
public int clearAllSymbols() { |
199 |
legend.clear(); |
200 |
symbolId = -1;
|
201 |
return symbolId;
|
202 |
} |
203 |
|
204 |
public void removeGraphics(String id) { |
205 |
DisposableIterator iterator = null;
|
206 |
FeatureSet featureSet = null;
|
207 |
try{
|
208 |
if (!store.isEditing()){
|
209 |
store.edit(); |
210 |
} |
211 |
featureSet = store.getFeatureSet(); |
212 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
213 |
Feature feature = (Feature) iterator.next(); |
214 |
if (feature.get(FEATURE_ATTR_ID).equals(id)){
|
215 |
featureSet.delete(feature); |
216 |
} |
217 |
} |
218 |
store.finishEditing(); |
219 |
} catch (DataException e) {
|
220 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
221 |
} finally {
|
222 |
if (featureSet != null) { |
223 |
featureSet.dispose(); |
224 |
} |
225 |
if (iterator != null) { |
226 |
iterator.dispose(); |
227 |
} |
228 |
} |
229 |
} |
230 |
|
231 |
protected void doDispose() throws BaseException { |
232 |
super.doDispose();
|
233 |
if (store != null) { |
234 |
store.dispose(); |
235 |
store = null;
|
236 |
} |
237 |
} |
238 |
|
239 |
/* (non-Javadoc)
|
240 |
* @see org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect#setLegend(org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend)
|
241 |
*/
|
242 |
public void setLegend(IVectorLegend legend) throws LegendLayerException { |
243 |
if (legend instanceof IVectorialUniqueValueLegend){ |
244 |
super.setLegend(legend);
|
245 |
this.legend = (IVectorialUniqueValueLegend)legend;
|
246 |
}else{
|
247 |
throw new LegendLayerException("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend", null); |
248 |
} |
249 |
} |
250 |
} |