svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / vectorial / impl / DefaultGraphicLayer.java @ 39058
History | View | Annotate | Download (12 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.ICoordTrans; |
32 |
import org.cresques.cts.IProjection; |
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.DataManager; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.exception.ReadException; |
38 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 |
import org.gvsig.fmap.dal.feature.Feature; |
43 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
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.geom.GeometryLocator; |
50 |
import org.gvsig.fmap.geom.GeometryManager; |
51 |
import org.gvsig.fmap.geom.primitive.Envelope; |
52 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
53 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
54 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
55 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
56 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
57 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
58 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
59 |
import org.gvsig.tools.dispose.DisposableIterator; |
60 |
import org.gvsig.tools.exception.BaseException; |
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
/**
|
65 |
* Default {@link GraphicLayer} implementation.
|
66 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
67 |
*/
|
68 |
public class DefaultGraphicLayer extends FLyrVect implements GraphicLayer{ |
69 |
private static final String DEFAULT = "default"; |
70 |
protected static final Logger logger = LoggerFactory.getLogger(DefaultGraphicLayer.class); |
71 |
private static DataManager dataManager = DALLocator.getDataManager(); |
72 |
private IVectorialUniqueValueLegend legend = null; |
73 |
|
74 |
private FeatureStore store = null; |
75 |
private long ids = 0; |
76 |
|
77 |
private int symbolId = -1; |
78 |
|
79 |
private int featureIdIndex; |
80 |
private int groupIdIndex; |
81 |
private int geomIndex; |
82 |
private int idsymIndex; |
83 |
private int labelIndex; |
84 |
private int tagIndex; |
85 |
private int priorityIndex; |
86 |
|
87 |
public void initialize(IProjection projection) throws ValidateDataParametersException, DataException, LoadLayerException { |
88 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
89 |
store = dataManager.createMemoryStore(FEATURE_ATTR_PRIORITY); |
90 |
store.edit(); |
91 |
|
92 |
EditableFeatureType editableFeatureType = store.getDefaultFeatureType().getEditable(); |
93 |
|
94 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GROUPID, DataTypes.STRING); |
95 |
|
96 |
EditableFeatureAttributeDescriptor geometryDescriptor = editableFeatureType.add(GraphicLayer.FEATURE_ATTR_GEOMETRY, DataTypes.GEOMETRY); |
97 |
try {
|
98 |
geometryDescriptor.setGeometryType(geomManager.getGeometryType(TYPES.GEOMETRY, SUBTYPES.GEOM2D)); |
99 |
} catch (Exception e) { |
100 |
logger.info("Can't create/assign geomery type to feature type of GraphicLayer.", e);
|
101 |
} |
102 |
geometryDescriptor.setSRS(projection); |
103 |
editableFeatureType.setDefaultGeometryAttributeName(GraphicLayer.FEATURE_ATTR_GEOMETRY); |
104 |
|
105 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_IDSYMBOL, DataTypes.INT); |
106 |
|
107 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_LABEL, DataTypes.STRING); |
108 |
|
109 |
EditableFeatureAttributeDescriptor featureIdDescriptor = |
110 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_FEATUREID, DataTypes.LONG); |
111 |
|
112 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_TAG, DataTypes.OBJECT); |
113 |
editableFeatureType.add(GraphicLayer.FEATURE_ATTR_PRIORITY, DataTypes.INT); |
114 |
|
115 |
featureIdDescriptor.setIsPrimaryKey(true);
|
116 |
editableFeatureType.setHasOID(true);
|
117 |
|
118 |
store.update(editableFeatureType); |
119 |
|
120 |
store.finishEditing(); |
121 |
|
122 |
FeatureType ftype = store.getDefaultFeatureType(); |
123 |
featureIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_FEATUREID); |
124 |
groupIdIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GROUPID); |
125 |
geomIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_GEOMETRY); |
126 |
idsymIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_IDSYMBOL); |
127 |
labelIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_LABEL); |
128 |
tagIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_TAG); |
129 |
priorityIndex = ftype.getIndex(GraphicLayer.FEATURE_ATTR_PRIORITY); |
130 |
|
131 |
this.bindToDataStore(store);
|
132 |
|
133 |
setName("Graphic Layer");
|
134 |
} |
135 |
|
136 |
public void addGraphic(String groupId, Geometry geom, int idsym) { |
137 |
addGraphic(groupId, geom, idsym, null, null, DEFAULT_PRIORITY); |
138 |
} |
139 |
|
140 |
public void addGraphic(Geometry geom, int idsym) { |
141 |
addGraphic(DEFAULT, geom, idsym, null, null, DEFAULT_PRIORITY); |
142 |
} |
143 |
|
144 |
public void addGraphic(Geometry geom, int idsym, String label) { |
145 |
addGraphic(DEFAULT, geom, idsym, label, null, DEFAULT_PRIORITY);
|
146 |
} |
147 |
|
148 |
public void addGraphic(String groupId, Geometry geom, int idsym, String label) { |
149 |
addGraphic(groupId, geom, idsym, label, null, DEFAULT_PRIORITY);
|
150 |
} |
151 |
|
152 |
public void addGraphic(String groupId, Geometry geom, int idsym, String label, |
153 |
Object tag, int priority) { |
154 |
|
155 |
try{
|
156 |
store.beginComplexNotification(); |
157 |
// Just in case another thread is going to read from the store
|
158 |
synchronized (store) {
|
159 |
if (!store.isEditing()){
|
160 |
store.edit(FeatureStore.MODE_APPEND); |
161 |
insertGeometry(groupId, geom, idsym, label, tag, priority); |
162 |
store.finishEditing(); |
163 |
} else {
|
164 |
insertGeometry(groupId, geom, idsym, label, tag, priority); |
165 |
} |
166 |
} |
167 |
store.endComplexNotification(); |
168 |
} catch (DataException e) {
|
169 |
logger.error("Error adding a geometry to the graphic layer", e);
|
170 |
} |
171 |
} |
172 |
|
173 |
private void insertGeometry(String groupId, Geometry geom, int idsym, |
174 |
String label, Object tag, int priority) throws DataException { |
175 |
EditableFeature feature = store.createNewFeature().getEditable(); |
176 |
feature.setString(groupIdIndex, groupId); |
177 |
feature.setGeometry(geomIndex, geom); |
178 |
feature.setInt(idsymIndex, idsym); |
179 |
feature.setString(labelIndex, label); |
180 |
feature.setLong(featureIdIndex, ids); |
181 |
feature.set(tagIndex, tag); |
182 |
feature.setInt(priorityIndex, priority); |
183 |
|
184 |
ids++; |
185 |
|
186 |
store.insert(feature); |
187 |
} |
188 |
|
189 |
public void addGraphics(String groupId, Iterator geoms, int idsym) { |
190 |
addGraphics(groupId, geoms, idsym, null, null, DEFAULT_PRIORITY); |
191 |
} |
192 |
|
193 |
public void addGraphics(String groupId, Iterator geoms, int idsym, |
194 |
String label) {
|
195 |
addGraphics(groupId, geoms, idsym, label, null, DEFAULT_PRIORITY);
|
196 |
} |
197 |
|
198 |
public void addGraphics(String groupId, Iterator geoms, int idsym, |
199 |
String label, Object tag, int priority) { |
200 |
try {
|
201 |
// Just in case another thread is going to read from the store
|
202 |
synchronized (store) {
|
203 |
if (!store.isEditing()) {
|
204 |
store.edit(FeatureStore.MODE_APPEND); |
205 |
store.disableNotifications(); |
206 |
for (; geoms.hasNext();) {
|
207 |
Geometry geom = (Geometry) geoms.next(); |
208 |
insertGeometry(groupId, geom, idsym, label, tag, priority); |
209 |
} |
210 |
store.enableNotifications(); |
211 |
store.finishEditing(); |
212 |
} else {
|
213 |
store.disableNotifications(); |
214 |
for (; geoms.hasNext();) {
|
215 |
Geometry geom = (Geometry) geoms.next(); |
216 |
insertGeometry(groupId, geom, idsym, label, tag, priority); |
217 |
} |
218 |
store.enableNotifications(); |
219 |
} |
220 |
} |
221 |
} catch (DataException e) {
|
222 |
logger.error("Error adding a geometry to the graphic layer", e);
|
223 |
} |
224 |
} |
225 |
|
226 |
public int addSymbol(ISymbol newSymbol) { |
227 |
symbolId++; |
228 |
legend.addSymbol(new Integer(symbolId), newSymbol); |
229 |
return symbolId;
|
230 |
} |
231 |
|
232 |
public ISymbol getSymbol(int symbolPos) { |
233 |
return legend.getSymbolByValue(new Integer(symbolPos)); |
234 |
} |
235 |
|
236 |
public int getSymbolId(ISymbol symbol) { |
237 |
Object key = legend.getSymbolKey(symbol);
|
238 |
return key == null ? -1 : ((Number) key).intValue(); |
239 |
} |
240 |
|
241 |
public void clearAllGraphics() { |
242 |
DisposableIterator iterator = null;
|
243 |
FeatureSet featureSet = null;
|
244 |
try{
|
245 |
if (!store.isEditing()){
|
246 |
store.edit(); |
247 |
featureSet = store.getFeatureSet(); |
248 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
249 |
Feature feature = (Feature) iterator.next(); |
250 |
featureSet.delete(feature); |
251 |
} |
252 |
store.finishEditing(); |
253 |
} else {
|
254 |
featureSet = store.getFeatureSet(); |
255 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
256 |
Feature feature = (Feature) iterator.next(); |
257 |
featureSet.delete(feature); |
258 |
} |
259 |
} |
260 |
} catch (DataException e) {
|
261 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
262 |
} finally {
|
263 |
if (featureSet != null) { |
264 |
featureSet.dispose(); |
265 |
} |
266 |
if (iterator != null) { |
267 |
iterator.dispose(); |
268 |
} |
269 |
} |
270 |
} |
271 |
|
272 |
public int clearAllSymbols() { |
273 |
legend.clear(); |
274 |
symbolId = -1;
|
275 |
return symbolId;
|
276 |
} |
277 |
|
278 |
public void removeGraphics(String groupId) { |
279 |
DisposableIterator iterator = null;
|
280 |
FeatureSet featureSet = null;
|
281 |
try{
|
282 |
store.beginComplexNotification(); |
283 |
if (!store.isEditing()){
|
284 |
store.edit(); |
285 |
featureSet = store.getFeatureSet(); |
286 |
store.beginEditingGroup(groupId); |
287 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
288 |
Feature feature = (Feature) iterator.next(); |
289 |
if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
|
290 |
featureSet.delete(feature); |
291 |
} |
292 |
} |
293 |
store.endEditingGroup(); |
294 |
store.finishEditing(); |
295 |
} else {
|
296 |
featureSet = store.getFeatureSet(); |
297 |
store.beginEditingGroup(groupId); |
298 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
299 |
Feature feature = (Feature) iterator.next(); |
300 |
if (feature.get(FEATURE_ATTR_GROUPID).equals(groupId)) {
|
301 |
featureSet.delete(feature); |
302 |
} |
303 |
} |
304 |
store.endEditingGroup(); |
305 |
} |
306 |
store.endComplexNotification(); |
307 |
} catch (DataException e) {
|
308 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
309 |
} finally {
|
310 |
if (featureSet != null) { |
311 |
featureSet.dispose(); |
312 |
} |
313 |
if (iterator != null) { |
314 |
iterator.dispose(); |
315 |
} |
316 |
} |
317 |
} |
318 |
|
319 |
protected void doDispose() throws BaseException { |
320 |
super.doDispose();
|
321 |
if (store != null) { |
322 |
store.dispose(); |
323 |
store = null;
|
324 |
} |
325 |
} |
326 |
|
327 |
public void setLegend(IVectorLegend legend) throws LegendLayerException { |
328 |
if (legend instanceof IVectorialUniqueValueLegend){ |
329 |
super.setLegend(legend);
|
330 |
this.legend = (IVectorialUniqueValueLegend)legend;
|
331 |
this.legend.setClassifyingFieldNames(new String[]{FEATURE_ATTR_IDSYMBOL}); |
332 |
}else{
|
333 |
logger.warn("The legend for the graphics layer must be a instance of IVectorialUniqueValueLegend");
|
334 |
} |
335 |
} |
336 |
|
337 |
public Envelope getFullEnvelope() throws ReadException { |
338 |
Envelope rAux; |
339 |
try {
|
340 |
rAux = getFeatureStore().getEnvelope(); |
341 |
} catch (BaseException e) {
|
342 |
throw new ReadException(getName(), e); |
343 |
} |
344 |
|
345 |
if (rAux == null) { |
346 |
return null; |
347 |
} |
348 |
|
349 |
ICoordTrans ct = getCoordTrans(); |
350 |
if (ct != null) { |
351 |
rAux = rAux.convert(ct); |
352 |
} |
353 |
return rAux;
|
354 |
} |
355 |
} |