svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 40559
History | View | Annotate | Download (12.9 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
25 | *
|
||
26 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
27 | * of the Valencian Government (CIT)
|
||
28 | *
|
||
29 | * This program is free software; you can redistribute it and/or
|
||
30 | * modify it under the terms of the GNU General Public License
|
||
31 | * as published by the Free Software Foundation; either version 2
|
||
32 | * of the License, or (at your option) any later version.
|
||
33 | *
|
||
34 | * This program is distributed in the hope that it will be useful,
|
||
35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
37 | * GNU General Public License for more details.
|
||
38 | *
|
||
39 | * You should have received a copy of the GNU General Public License
|
||
40 | * along with this program; if not, write to the Free Software
|
||
41 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
42 | * MA 02110-1301, USA.
|
||
43 | *
|
||
44 | */
|
||
45 | |||
46 | /*
|
||
47 | * AUTHORS (In addition to CIT):
|
||
48 | * 2009 {Iver T.I.} {Task}
|
||
49 | */
|
||
50 | package org.gvsig.fmap.mapcontext.layers.vectorial.impl; |
||
51 | |||
52 | import java.util.Iterator; |
||
53 | |||
54 | import org.cresques.cts.ICoordTrans; |
||
55 | import org.cresques.cts.IProjection; |
||
56 | import org.gvsig.fmap.dal.DALLocator; |
||
57 | import org.gvsig.fmap.dal.DataManager; |
||
58 | import org.gvsig.fmap.dal.DataTypes; |
||
59 | import org.gvsig.fmap.dal.exception.DataException; |
||
60 | import org.gvsig.fmap.dal.exception.ReadException; |
||
61 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
62 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
63 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
64 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
65 | import org.gvsig.fmap.dal.feature.Feature; |
||
66 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
67 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
68 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
69 | import org.gvsig.fmap.geom.Geometry; |
||
70 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
71 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
72 | import org.gvsig.fmap.geom.GeometryLocator; |
||
73 | import org.gvsig.fmap.geom.GeometryManager; |
||
74 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
75 | import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
||
76 | import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
||
77 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
78 | import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
||
79 | import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
||
80 | import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
||
81 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
82 | import org.gvsig.tools.dispose.DisposableIterator; |
||
83 | import org.gvsig.tools.exception.BaseException; |
||
84 | import org.slf4j.Logger; |
||
85 | import org.slf4j.LoggerFactory; |
||
86 | |||
87 | /**
|
||
88 | * Default {@link GraphicLayer} implementation.
|
||
89 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
||
90 | */
|
||
91 | public class DefaultGraphicLayer extends FLyrVect implements GraphicLayer{ |
||
92 | private static final String DEFAULT = "default"; |
||
93 | protected static final Logger logger = LoggerFactory.getLogger(DefaultGraphicLayer.class); |
||
94 | private static DataManager dataManager = DALLocator.getDataManager(); |
||
95 | private IVectorialUniqueValueLegend legend = null; |
||
96 | |||
97 | private FeatureStore store = null; |
||
98 | private long ids = 0; |
||
99 | |||
100 | private int symbolId = -1; |
||
101 | |||
102 | private int featureIdIndex; |
||
103 | private int groupIdIndex; |
||
104 | private int geomIndex; |
||
105 | private int idsymIndex; |
||
106 | private int labelIndex; |
||
107 | private int tagIndex; |
||
108 | private int priorityIndex; |
||
109 | |||
110 | public void initialize(IProjection projection) throws ValidateDataParametersException, DataException, LoadLayerException { |
||
111 | GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
112 | store = dataManager.createMemoryStore(FEATURE_ATTR_PRIORITY); |
||
113 | store.edit(); |
||
114 | |||
115 | EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable(); |
||
116 | |||
117 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GROUPID, DataTypes.STRING); |
||
118 | |||
119 | EditableFeatureAttributeDescriptor geometryDescriptor = editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GEOMETRY, DataTypes.GEOMETRY); |
||
120 | try {
|
||
121 | geometryDescriptor.setGeometryType(geomManager.getGeometryType(TYPES.GEOMETRY, SUBTYPES.GEOM2D)); |
||
122 | } catch (Exception e) { |
||
123 | logger.info("Can't create/assign geomery type to feature type of GraphicLayer.", e);
|
||
124 | } |
||
125 | geometryDescriptor.setSRS(projection); |
||
126 | editableFeatureType.setDefaultGeometryAttributeName(GraphicLayer.FEATURE_ATTR_GEOMETRY); |
||
127 | |||
128 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_IDSYMBOL, DataTypes.INT); |
||
129 | |||
130 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_LABEL, DataTypes.STRING); |
||
131 | |||
132 | EditableFeatureAttributeDescriptor featureIdDescriptor = |
||
133 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_FEATUREID, DataTypes.LONG); |
||
134 | |||
135 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_TAG, DataTypes.OBJECT); |
||
136 | editableFeatureType.add(GraphicLayer.FEATURE_ATTR_PRIORITY, DataTypes.INT); |
||
137 | |||
138 | featureIdDescriptor.setIsPrimaryKey(true);
|
||
139 | editableFeatureType.setHasOID(true);
|
||
140 | |||
141 | store.update(editableFeatureType); |
||
142 | |||
143 | store.finishEditing(); |
||
144 | |||
145 | FeatureType ftype = store.getDefaultFeatureType(); |
||
146 | featureIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_FEATUREID); |
||
147 | groupIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GROUPID); |
||
148 | geomIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GEOMETRY); |
||
149 | idsymIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_IDSYMBOL); |
||
150 | labelIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_LABEL); |
||
151 | tagIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_TAG); |
||
152 | priorityIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_PRIORITY); |
||
153 | |||
154 | this.bindToDataStore(store);
|
||
155 | |||
156 | setName("Graphic Layer");
|
||
157 | } |
||
158 | |||
159 | public void addGraphic(String groupId, Geometry geom, int idsym) { |
||
160 | addGraphic(groupId, geom, idsym, null, null, DEFAULT_PRIORITY); |
||
161 | } |
||
162 | |||
163 | public void addGraphic(Geometry geom, int idsym) { |
||
164 | addGraphic(DEFAULT, geom, idsym, null, null, DEFAULT_PRIORITY); |
||
165 | } |
||
166 | |||
167 | public void addGraphic(Geometry geom, int idsym, String label) { |
||
168 | addGraphic(DEFAULT, geom, idsym, label, null, DEFAULT_PRIORITY);
|
||
169 | } |
||
170 | |||
171 | public void addGraphic(String groupId, Geometry geom, int idsym, String label) { |
||
172 | addGraphic(groupId, geom, idsym, label, null, DEFAULT_PRIORITY);
|
||
173 | } |
||
174 | |||
175 | public void addGraphic(String groupId, Geometry geom, int idsym, String label, |
||
176 | Object tag, int priority) { |
||
177 | |||
178 | try{
|
||
179 | store.beginComplexNotification(); |
||
180 | // Just in case another thread is going to read from the store
|
||
181 | synchronized (store) {
|
||
182 | if (!store.isEditing()){
|
||
183 | store.edit(FeatureStore.MODE_APPEND); |
||
184 | insertGeometry(groupId, geom, idsym, label, tag, priority); |
||
185 | store.finishEditing(); |
||
186 | } else {
|
||
187 | insertGeometry(groupId, geom, idsym, label, tag, priority); |
||
188 | } |
||
189 | } |
||
190 | store.endComplexNotification(); |
||
191 | } catch (DataException e) {
|
||
192 | logger.error("Error adding a geometry to the graphic layer", e);
|
||
193 | } |
||
194 | } |
||
195 | |||
196 | private void insertGeometry(String groupId, Geometry geom, int idsym, |
||
197 | String label, Object tag, int priority) throws DataException { |
||
198 | EditableFeature feature = store.createNewFeature().getEditable(); |
||
199 | feature.setString(groupIdIndex, groupId); |
||
200 | feature.setGeometry(geomIndex, geom); |
||
201 | feature.setInt(idsymIndex, idsym); |
||
202 | feature.setString(labelIndex, label); |
||
203 | feature.setLong(featureIdIndex, ids); |
||
204 | feature.set(tagIndex, tag); |
||
205 | feature.setInt(priorityIndex, priority); |
||
206 | |||
207 | ids++; |
||
208 | |||
209 | store.insert(feature); |
||
210 | } |
||
211 | |||
212 | public void addGraphics(String groupId, Iterator geoms, int idsym) { |
||
213 | addGraphics(groupId, geoms, idsym, null, null, DEFAULT_PRIORITY); |
||
214 | } |
||
215 | |||
216 | public void addGraphics(String groupId, Iterator geoms, int idsym, |
||
217 | String label) {
|
||
218 | addGraphics(groupId, geoms, idsym, label, null, DEFAULT_PRIORITY);
|
||
219 | } |
||
220 | |||
221 | public void addGraphics(String groupId, Iterator geoms, int idsym, |
||
222 | String label, Object tag, int priority) { |
||
223 | try {
|
||
224 | // Just in case another thread is going to read from the store
|
||
225 | synchronized (store) {
|
||
226 | if (!store.isEditing()) {
|
||
227 | store.edit(FeatureStore.MODE_APPEND); |
||
228 | store.disableNotifications(); |
||
229 | for (; geoms.hasNext();) {
|
||
230 | Geometry geom = (Geometry) geoms.next(); |
||
231 | insertGeometry(groupId, geom, idsym, label, tag, priority); |
||
232 | } |
||
233 | store.enableNotifications(); |
||
234 | store.finishEditing(); |
||
235 | } else {
|
||
236 | store.disableNotifications(); |
||
237 | for (; geoms.hasNext();) {
|
||
238 | Geometry geom = (Geometry) geoms.next(); |
||
239 | insertGeometry(groupId, geom, idsym, label, tag, priority); |
||
240 | } |
||
241 | store.enableNotifications(); |
||
242 | } |
||
243 | } |
||
244 | } catch (DataException e) {
|
||
245 | logger.error("Error adding a geometry to the graphic layer", e);
|
||
246 | } |
||
247 | } |
||
248 | |||
249 | public int addSymbol(ISymbol newSymbol) { |
||
250 | symbolId++; |
||
251 | legend.addSymbol(new Integer(symbolId), newSymbol); |
||
252 | return symbolId;
|
||
253 | } |
||
254 | |||
255 | public ISymbol getSymbol(int symbolPos) { |
||
256 | return legend.getSymbolByValue(new Integer(symbolPos)); |
||
257 | } |
||
258 | |||
259 | public int getSymbolId(ISymbol symbol) { |
||
260 | Object key = legend.getSymbolKey(symbol);
|
||
261 | return key == null ? -1 : ((Number) key).intValue(); |
||
262 | } |
||
263 | |||
264 | public void clearAllGraphics() { |
||
265 | DisposableIterator iterator = null;
|
||
266 | FeatureSet featureSet = null;
|
||
267 | try{
|
||
268 | if (!store.isEditing()){
|
||
269 | store.edit(); |
||
270 | featureSet = store.getFeatureSet(); |
||
271 | for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
||
272 | Feature feature = (Feature) iterator.next(); |
||
273 | featureSet.delete(feature); |
||
274 | } |
||
275 | store.finishEditing(); |
||
276 | } else {
|
||
277 | featureSet = store.getFeatureSet(); |
||
278 | for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
||
279 | Feature feature = (Feature) iterator.next(); |
||
280 | featureSet.delete(feature); |
||
281 | } |
||
282 | } |
||
283 | } catch (DataException e) {
|
||
284 | logger.error("Error clearing all the geometry of the graphic layer", e);
|
||
285 | } finally {
|
||
286 | if (featureSet != null) { |
||
287 | featureSet.dispose(); |
||
288 | } |
||
289 | if (iterator != null) { |
||
290 | iterator.dispose(); |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | public int clearAllSymbols() { |
||
296 | legend.clear(); |
||
297 | symbolId = -1;
|
||
298 | return symbolId;
|
||
299 | } |
||
300 | |||
301 | public void removeGraphics(String groupId) { |
||
302 | DisposableIterator iterator = null;
|
||
303 | FeatureSet featureSet = null;
|
||
304 | try{
|
||
305 | store.beginComplexNotification(); |
||
306 | if (!store.isEditing()){
|
||
307 | store.edit(); |
||
308 | featureSet = store.getFeatureSet(); |
||
309 | store.beginEditingGroup(groupId); |
||
310 | for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
||
311 | Feature feature = (Feature) iterator.next(); |
||
312 | if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
|
||
313 | featureSet.delete(feature); |
||
314 | } |
||
315 | } |
||
316 | store.endEditingGroup(); |
||
317 | store.finishEditing(); |
||
318 | } else {
|
||
319 | featureSet = store.getFeatureSet(); |
||
320 | store.beginEditingGroup(groupId); |
||
321 | for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
||
322 | Feature feature = (Feature) iterator.next(); |
||
323 | if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
|
||
324 | featureSet.delete(feature); |
||
325 | } |
||
326 | } |
||
327 | store.endEditingGroup(); |
||
328 | } |
||
329 | store.endComplexNotification(); |
||
330 | } catch (DataException e) {
|
||
331 | logger.error("Error clearing all the geometry of the graphic layer", e);
|
||
332 | } finally {
|
||
333 | if (featureSet != null) { |
||
334 | featureSet.dispose(); |
||
335 | } |
||
336 | if (iterator != null) { |
||
337 | iterator.dispose(); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | protected void doDispose() throws BaseException { |
||
343 | super.doDispose();
|
||
344 | if (store != null) { |
||
345 | store.dispose(); |
||
346 | store = null;
|
||
347 | } |
||
348 | } |
||
349 | |||
350 | public void setLegend(IVectorLegend legend) throws LegendLayerException { |
||
351 | if (legend instanceof IVectorialUniqueValueLegend){ |
||
352 | super.setLegend(legend);
|
||
353 | this.legend = (IVectorialUniqueValueLegend)legend;
|
||
354 | this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL}); |
||
355 | }else{
|
||
356 | logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
|
||
357 | } |
||
358 | } |
||
359 | |||
360 | public Envelope getFullEnvelope() throws ReadException { |
||
361 | Envelope rAux; |
||
362 | try {
|
||
363 | rAux = getFeatureStore().getEnvelope(); |
||
364 | } catch (BaseException e) {
|
||
365 | throw new ReadException(getName(), e); |
||
366 | } |
||
367 | |||
368 | if (rAux == null) { |
||
369 | return null; |
||
370 | } |
||
371 | |||
372 | ICoordTrans ct = getCoordTrans(); |
||
373 | if (ct != null) { |
||
374 | rAux = rAux.convert(ct); |
||
375 | } |
||
376 | return rAux;
|
||
377 | } |
||
378 | } |