Statistics
| Revision:

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 / FLyrVect.java @ 44369

History | View | Annotate | Download (43.5 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 42289 jjdelcerro
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 40435 jjdelcerro
 *
11 42289 jjdelcerro
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 40435 jjdelcerro
 *
16 42289 jjdelcerro
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 40435 jjdelcerro
 *
20 42289 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.fmap.mapcontext.layers.vectorial;
24
25
import java.awt.Graphics2D;
26
import java.awt.Point;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Point2D;
29
import java.awt.image.BufferedImage;
30 43141 jjdelcerro
import java.io.File;
31 42293 jjdelcerro
import java.util.Iterator;
32 40435 jjdelcerro
import java.util.Set;
33
import java.util.TreeSet;
34 42972 jjdelcerro
import java.util.logging.Level;
35
import java.util.logging.Logger;
36 40435 jjdelcerro
37
import org.cresques.cts.ICoordTrans;
38
import org.cresques.cts.IProjection;
39
import org.slf4j.LoggerFactory;
40
41
import org.gvsig.compat.print.PrintAttributes;
42 42972 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
43 40435 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
44
import org.gvsig.fmap.dal.exception.DataException;
45 42972 jjdelcerro
import org.gvsig.fmap.dal.exception.InitializeException;
46 40435 jjdelcerro
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureQuery;
49
import org.gvsig.fmap.dal.feature.FeatureSet;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
52
import org.gvsig.fmap.dal.feature.FeatureType;
53
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
54 43141 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
55 40435 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
57
import org.gvsig.fmap.geom.Geometry.TYPES;
58
import org.gvsig.fmap.geom.GeometryLocator;
59
import org.gvsig.fmap.geom.GeometryManager;
60
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
61
import org.gvsig.fmap.geom.primitive.Circle;
62
import org.gvsig.fmap.geom.primitive.Envelope;
63
import org.gvsig.fmap.geom.type.GeometryType;
64
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
65
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
66
import org.gvsig.fmap.mapcontext.MapContextLocator;
67
import org.gvsig.fmap.mapcontext.MapContextManager;
68
import org.gvsig.fmap.mapcontext.ViewPort;
69
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
70
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
71
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException;
72
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
73 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.DefaultLayerInformationBuilder;
74 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
75
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
76
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
77 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.LayerInformationBuilder;
78 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
79
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
80
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
81
import org.gvsig.fmap.mapcontext.rendering.legend.LegendException;
82
import org.gvsig.fmap.mapcontext.rendering.legend.events.FeatureDrawnNotification;
83
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
84
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendClearEvent;
85
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener;
86
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
87
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
88
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
89
import org.gvsig.metadata.exceptions.MetadataException;
90
import org.gvsig.tools.ToolsLocator;
91
import org.gvsig.tools.dynobject.DynObjectSet;
92
import org.gvsig.tools.dynobject.DynStruct;
93 42820 dmartinezizquierdo
import org.gvsig.tools.evaluator.Evaluator;
94 40435 jjdelcerro
import org.gvsig.tools.exception.BaseException;
95
import org.gvsig.tools.locator.LocatorException;
96
import org.gvsig.tools.observer.Observable;
97
import org.gvsig.tools.observer.Observer;
98
import org.gvsig.tools.persistence.PersistenceManager;
99
import org.gvsig.tools.persistence.PersistentState;
100
import org.gvsig.tools.persistence.exception.PersistenceException;
101
import org.gvsig.tools.task.Cancellable;
102 41840 jjdelcerro
import org.gvsig.tools.util.Callable;
103 40435 jjdelcerro
104
/**
105
 * Capa b?sica Vectorial.
106 42289 jjdelcerro
 *
107 40435 jjdelcerro
 */
108 43215 jjdelcerro
public class FLyrVect
109
    extends FLyrDefault
110
    implements VectorLayer,
111
    LegendContentsChangedListener, Observer {
112 40435 jjdelcerro
113 42289 jjdelcerro
    final static private org.slf4j.Logger logger
114 43215 jjdelcerro
        = LoggerFactory.getLogger(FLyrVect.class);
115 42289 jjdelcerro
    private final GeometryManager geomManager
116 43215 jjdelcerro
        = GeometryLocator.getGeometryManager();
117 40435 jjdelcerro
118 42289 jjdelcerro
    /**
119
     * Leyenda de la capa vectorial
120
     */
121 40435 jjdelcerro
    private IVectorLegend legend;
122
    private int typeShape = -1;
123
    private FeatureStore featureStore = null;
124
    private SpatialCache spatialCache = new SpatialCache();
125
126
    /**
127
     * An implementation of gvSIG spatial index
128
     */
129
    // protected ISpatialIndex spatialIndex = null;
130
    private IVectorLegend loadLegend = null;
131
132
    private boolean isLabeled;
133
    protected ILabelingStrategy strategy;
134
//        private ReprojectDefaultGeometry reprojectTransform;
135 42775 jjdelcerro
    private FeatureQuery baseQuery = null;
136 40435 jjdelcerro
137
    public FLyrVect() {
138
        super();
139
    }
140 42289 jjdelcerro
141 43215 jjdelcerro
    @Override
142 42289 jjdelcerro
    public String getTocImageIcon() {
143 43215 jjdelcerro
        return MapContextLocator.getMapContextManager().getIconLayer(this.getDataStore());
144 42289 jjdelcerro
    }
145
146 40435 jjdelcerro
    /**
147 43215 jjdelcerro
     * Return the store associated to the layer.
148 42289 jjdelcerro
     *
149 43215 jjdelcerro
     * @return the store
150 40435 jjdelcerro
     */
151 43215 jjdelcerro
    @Override
152 40435 jjdelcerro
    public DataStore getDataStore() {
153
        return featureStore;
154
    }
155 42289 jjdelcerro
156 40435 jjdelcerro
    /**
157 42289 jjdelcerro
     * Asigna el data-store a la capa. Esta operacion no se deneria poder hacer
158
     * desde fuera de la clase.
159
     *
160 40435 jjdelcerro
     * @param dataStore
161
     * @throws LoadLayerException
162
     * @deprecated use {@link #bindToDataStore(DataStore)}
163
     */
164
    public void setDataStore(DataStore dataStore) throws LoadLayerException {
165 42289 jjdelcerro
        bindToDataStore(dataStore);
166 40435 jjdelcerro
    }
167 42289 jjdelcerro
168 40435 jjdelcerro
    /**
169
     * Enlaza la capa con el DataStore indicado.
170 42289 jjdelcerro
     *
171 40435 jjdelcerro
     * @param dataStore
172
     * @throws LoadLayerException
173
     */
174
    protected void bindToDataStore(DataStore dataStore) throws LoadLayerException {
175 43215 jjdelcerro
        if( this.featureStore != null && this.featureStore != dataStore ) {
176 40435 jjdelcerro
            this.featureStore.deleteObserver(this);
177
        }
178
179
        featureStore = (FeatureStore) dataStore;
180
181 42289 jjdelcerro
        MapContextManager mapContextManager
182 43215 jjdelcerro
            = MapContextLocator.getMapContextManager();
183 42289 jjdelcerro
184 40435 jjdelcerro
        //Set the legend
185 42289 jjdelcerro
        IVectorLegend legend
186 43215 jjdelcerro
            = (IVectorLegend) mapContextManager.getLegend(dataStore);
187 40435 jjdelcerro
188 43215 jjdelcerro
        if( legend == null ) {
189 40435 jjdelcerro
            throw new LegendLayerException(this.getName());
190
        }
191
192
        this.setLegend(legend);
193
194
        //Set the labeling strategy
195 42289 jjdelcerro
        ILabelingStrategy labeler
196 43215 jjdelcerro
            = (ILabelingStrategy) mapContextManager.getLabelingStrategy(dataStore);
197 40435 jjdelcerro
198 43215 jjdelcerro
        if( labeler != null ) {
199 40435 jjdelcerro
            labeler.setLayer(this);
200
            this.setLabelingStrategy(labeler);
201
            this.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t?
202 42289 jjdelcerro
            // etiquetes?????
203 40435 jjdelcerro
        }
204
205
        this.delegate(dataStore);
206
207
        dataStore.addObserver(this);
208
209
        ToolsLocator.getDisposableManager().bind(dataStore);
210
    }
211
212
    public Envelope getFullEnvelope() throws ReadException {
213
        Envelope rAux;
214 43215 jjdelcerro
        if( getFeatureStore() == null ) {
215
            return null;
216
        }
217 40435 jjdelcerro
        try {
218
            rAux = getFeatureStore().getEnvelope();
219
        } catch (BaseException e) {
220
            throw new ReadException(getName(), e);
221
        }
222
223
        // Esto es para cuando se crea una capa nueva con el fullExtent de ancho
224
        // y alto 0.
225 43215 jjdelcerro
        if( rAux == null || rAux.isEmpty() || rAux.getMaximum(0) - rAux.getMinimum(0) == 0
226
            && rAux.getMaximum(1) - rAux.getMinimum(1) == 0 ) {
227 40435 jjdelcerro
            try {
228 42289 jjdelcerro
                rAux
229 43215 jjdelcerro
                    = geomManager.createEnvelope(0, 0, 90, 90, SUBTYPES.GEOM2D);
230 40435 jjdelcerro
            } catch (CreateEnvelopeException e) {
231
                logger.error("Error creating the envelope", e);
232
                e.printStackTrace();
233
            }
234
        }
235
        // Si existe reproyecci?n, reproyectar el extent
236
        ICoordTrans ct = getCoordTrans();
237 43215 jjdelcerro
        if( ct != null ) {
238 42877 fdiaz
            boolean originalEnvelopeIsEmpty = rAux.isEmpty();
239 42289 jjdelcerro
            rAux = rAux.convert(ct);
240 43215 jjdelcerro
            if( !originalEnvelopeIsEmpty && rAux.isEmpty() ) {
241 42877 fdiaz
                try {
242
                    this.setAvailable(false);
243
                    throw new EnvelopeCantBeInitializedException();
244 43215 jjdelcerro
                } catch (EnvelopeCantBeInitializedException e) {
245 42877 fdiaz
                    this.addError(e);
246
                }
247
            }
248 40435 jjdelcerro
        }
249
        return rAux;
250
251
    }
252 42820 dmartinezizquierdo
253 42775 jjdelcerro
    public void setBaseQuery(FeatureQuery baseQuery) {
254
        this.baseQuery = baseQuery;
255
    }
256 40435 jjdelcerro
257 42775 jjdelcerro
    @Override
258
    public FeatureQuery getBaseQuery() {
259
        return this.baseQuery;
260
    }
261
262 42820 dmartinezizquierdo
    public void addBaseFilter(Evaluator filter) {
263
        if( this.baseQuery == null ) {
264
            this.baseQuery = this.getFeatureStore().createFeatureQuery();
265
        }
266
        this.baseQuery.addFilter(filter);
267
    }
268
269 42972 jjdelcerro
    public void addBaseFilter(String filter) {
270
        try {
271 43215 jjdelcerro
            this.addBaseFilter(DALLocator.getDataManager().createExpresion(filter));
272 42972 jjdelcerro
        } catch (InitializeException ex) {
273 43215 jjdelcerro
            throw new RuntimeException("Can't create filter with '" + filter + "'", ex);
274 42972 jjdelcerro
        }
275
    }
276
277 40435 jjdelcerro
    /**
278
     * Draws using IFeatureIterator. This method will replace the old draw(...)
279
     * one.
280 42289 jjdelcerro
     *
281 40435 jjdelcerro
     * @autor jaume dominguez faus - jaume.dominguez@iver.es
282
     * @param image
283
     * @param g
284
     * @param viewPort
285
     * @param cancel
286
     * @param scale
287
     * @throws ReadDriverException
288
     */
289
    public void draw(BufferedImage image,
290 43215 jjdelcerro
        Graphics2D g,
291
        ViewPort viewPort,
292
        Cancellable cancel,
293
        double scale) throws ReadException {
294 40435 jjdelcerro
295 43215 jjdelcerro
        if( legend == null ) {
296 40435 jjdelcerro
            return;
297
        }
298
299 43215 jjdelcerro
        if( !this.isWithinScale(scale) ) {
300 40435 jjdelcerro
            return;
301
        }
302 43215 jjdelcerro
        if( cancel.isCanceled() ) {
303 40435 jjdelcerro
            return;
304
        }
305
306 43215 jjdelcerro
        if( spatialCache.isEnabled() ) {
307 40435 jjdelcerro
            spatialCache.clearAll();
308
            legend.addDrawingObserver(this);
309
        }
310
311 42809 jbadia
        FeatureQuery featureQuery = createFeatureQuery();
312 42775 jjdelcerro
313 40435 jjdelcerro
        try {
314 42289 jjdelcerro
            FeatureAttributeDescriptor featureAttributeDescriptor
315 43215 jjdelcerro
                = getFeatureStore().getDefaultFeatureType().getDefaultTimeAttribute();
316 40435 jjdelcerro
317 43215 jjdelcerro
            if( (viewPort.getTime() != null) && (featureAttributeDescriptor != null) ) {
318 42289 jjdelcerro
                IntersectsTimeEvaluator intersectsTimeEvaluator
319 43215 jjdelcerro
                    = new IntersectsTimeEvaluator(viewPort.getTime(), featureAttributeDescriptor.getName());
320 40435 jjdelcerro
                featureQuery.addFilter(intersectsTimeEvaluator);
321
            }
322
        } catch (DataException e1) {
323
            logger.error("Impossible to get the temporal filter", e1);
324
        }
325
326
        try {
327 42289 jjdelcerro
328 40435 jjdelcerro
            long tini = System.currentTimeMillis();
329 42289 jjdelcerro
330 40435 jjdelcerro
            legend.draw(image,
331 43215 jjdelcerro
                g,
332
                viewPort,
333
                cancel,
334
                scale,
335
                null,
336
                getCoordTrans(),
337
                getFeatureStore(),
338
                featureQuery);
339 40435 jjdelcerro
340 42289 jjdelcerro
            logger.debug("Layer " + this.getName() + " drawn in "
341 43215 jjdelcerro
                + (System.currentTimeMillis() - tini) + " milliseconds.");
342 42289 jjdelcerro
343 40435 jjdelcerro
        } catch (LegendException e) {
344 42811 jjdelcerro
            this.setAvailable(false);
345
            this.setError(e);
346 40435 jjdelcerro
            throw new ReadException(getName(), e);
347
        } finally {
348 43215 jjdelcerro
            if( spatialCache.isEnabled() ) {
349 40435 jjdelcerro
                legend.deleteDrawingObserver(this);
350
            }
351
        }
352
    }
353
354
    public void print(Graphics2D g,
355 43215 jjdelcerro
        ViewPort viewPort,
356
        Cancellable cancel,
357
        double scale,
358
        PrintAttributes properties) throws ReadException {
359
        if( !this.isWithinScale(scale) ) {
360 40435 jjdelcerro
            return;
361
        }
362 43215 jjdelcerro
        if( cancel.isCanceled() ) {
363 40435 jjdelcerro
            return;
364
        }
365 42775 jjdelcerro
        FeatureQuery featureQuery = createFeatureQuery();
366 40435 jjdelcerro
367
        try {
368
            legend.print(g,
369 43215 jjdelcerro
                viewPort,
370
                cancel,
371
                scale,
372
                null,
373
                getCoordTrans(),
374
                getFeatureStore(),
375
                featureQuery,
376
                properties);
377 40435 jjdelcerro
378
        } catch (LegendException e) {
379
            this.setVisible(false);
380
            this.setActive(false);
381
            throw new ReadException(getName(), e);
382
        }
383
    }
384
385
    public void setLegend(IVectorLegend legend) throws LegendLayerException {
386 43215 jjdelcerro
        if( this.legend == legend ) {
387 40435 jjdelcerro
            return;
388
        }
389 43215 jjdelcerro
        if( this.legend != null && this.legend.equals(legend) ) {
390 40435 jjdelcerro
            return;
391
        }
392
        IVectorLegend oldLegend = this.legend;
393
        this.legend = legend;
394 43215 jjdelcerro
        if( oldLegend != null ) {
395 40435 jjdelcerro
            oldLegend.removeLegendListener(this);
396
            oldLegend.deleteDrawingObserver(this);
397
        }
398 43215 jjdelcerro
        if( legend != null ) {
399 40435 jjdelcerro
            this.legend.addDrawingObserver(this);
400
            this.legend.addLegendListener(this);
401
        }
402 42802 jjdelcerro
        LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, this.legend);
403 40435 jjdelcerro
        e.setLayer(this);
404
        updateDrawVersion();
405
        callLegendChanged(e);
406
    }
407
408
    /**
409
     * Devuelve la Leyenda de la capa.
410 42289 jjdelcerro
     *
411 40435 jjdelcerro
     * @return Leyenda.
412
     */
413
    public ILegend getLegend() {
414
        return legend;
415
    }
416
417
    public int getShapeType() throws ReadException {
418 43215 jjdelcerro
        if( typeShape == -1 ) {
419 42289 jjdelcerro
            FeatureType featureType = null;
420
            try {
421 43215 jjdelcerro
                if( getDataStore() != null ) {
422 42289 jjdelcerro
                    featureType
423 43215 jjdelcerro
                        = (((FeatureStore) getDataStore()).getDefaultFeatureType());
424 42289 jjdelcerro
                }
425
            } catch (DataException e) {
426
                throw new ReadException(getName(), e);
427
            }
428 43215 jjdelcerro
            if( featureType != null ) {
429 42289 jjdelcerro
                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
430
                typeShape
431 43215 jjdelcerro
                    = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
432 42289 jjdelcerro
            }
433
        }
434
        return typeShape;
435 40435 jjdelcerro
    }
436
437
    /**
438
     * Returns the layer's geometry type
439 42289 jjdelcerro
     *
440 40435 jjdelcerro
     * @return the geometry type
441 42289 jjdelcerro
     *
442
     * @throws ReadException if there is an error getting the geometry type
443 40435 jjdelcerro
     */
444
    public GeometryType getGeometryType() throws ReadException {
445
        FeatureType featureType = null;
446
        try {
447 43215 jjdelcerro
            if( getDataStore() != null ) {
448 42289 jjdelcerro
                featureType
449 43215 jjdelcerro
                    = (((FeatureStore) getDataStore()).getDefaultFeatureType());
450 40435 jjdelcerro
            }
451
        } catch (DataException e) {
452
            throw new ReadException(getName(), e);
453
        }
454
        return featureType == null ? null : featureType
455 43215 jjdelcerro
            .getDefaultGeometryAttribute().getGeomType();
456 40435 jjdelcerro
    }
457
458
    public void saveToState(PersistentState state) throws PersistenceException {
459
460 41807 jjdelcerro
        FeatureStore featureStore = null;
461
462 43215 jjdelcerro
        if( !this.isAvailable() ) {
463 41807 jjdelcerro
            logger.info("The '" + this.getName() + "' layer is not available, it will persist not.");
464 40435 jjdelcerro
            return;
465
        }
466 42289 jjdelcerro
467 41807 jjdelcerro
        try {
468
            super.saveToState(state);
469 40435 jjdelcerro
470 43215 jjdelcerro
            if( getLegend() != null ) {
471 41807 jjdelcerro
                state.set("legend", getLegend());
472
            }
473 40435 jjdelcerro
474 41807 jjdelcerro
            featureStore = getFeatureStore();
475 40435 jjdelcerro
476 43215 jjdelcerro
            if( featureStore != null ) {
477 41807 jjdelcerro
                state.set("featureStore", featureStore);
478
            }
479 40435 jjdelcerro
480 41807 jjdelcerro
            state.set("isLabeled", isLabeled);
481 40435 jjdelcerro
482 43215 jjdelcerro
            if( strategy != null ) {
483 41807 jjdelcerro
                state.set("labelingStrategy", strategy);
484
            }
485 40435 jjdelcerro
486 43215 jjdelcerro
            if( getLinkProperties() != null ) {
487 41807 jjdelcerro
                state.set("linkProperties", getLinkProperties());
488
            }
489 40435 jjdelcerro
490 41807 jjdelcerro
            state.set("typeShape", typeShape);
491
        } catch (PersistenceException ex) {
492
            logger.warn("Can't persist to state the layer '" + this.getName() + "'.", ex);
493
            throw ex;
494
        } catch (RuntimeException ex) {
495
            logger.warn("Can't persist to state the layer '" + this.getName() + "'.", ex);
496
            throw ex;
497 40435 jjdelcerro
        }
498
499
    }
500
501 43215 jjdelcerro
    @Override
502 40435 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
503
504 43215 jjdelcerro
        FeatureStore store = null;
505 40435 jjdelcerro
        try {
506 42289 jjdelcerro
            super.loadFromState(state);
507 40435 jjdelcerro
508 43215 jjdelcerro
            this.setIsLabeled(false);
509
            this.setLabelingStrategy(null);
510
511
            typeShape = state.getInt("typeShape", Geometry.TYPES.GEOMETRY);
512
513
            store = (FeatureStore) state.get("featureStore");
514
            if( store == null ) { // Averiguar como puede llegar aqui un null.
515
                this.setAvailable(false);
516
                this.addError(new RuntimeException("Can't retrieve store information."));
517
            } else {
518
                if( store.isBroken() ) {
519
                    this.setAvailable(false);
520
                    this.addError(store.getBreakingsCause());
521
                }
522
            }
523 41807 jjdelcerro
            try {
524
                this.bindToDataStore(store);
525 43215 jjdelcerro
            } catch (Exception ex) {
526
                this.setAvailable(false);
527
                this.addError(ex);
528 41807 jjdelcerro
            }
529 40435 jjdelcerro
530 41807 jjdelcerro
            try {
531 43215 jjdelcerro
                IVectorLegend vectorLegend = (IVectorLegend) state.get("legend");
532 41807 jjdelcerro
                this.setLegend(vectorLegend);
533 43215 jjdelcerro
            } catch (Exception ex) {
534
                this.setAvailable(false);
535
                this.addError(ex);
536 40435 jjdelcerro
            }
537
538 41807 jjdelcerro
            try {
539 43215 jjdelcerro
                if( state.getBoolean("isLabeled", false) ) {
540
                    this.setIsLabeled(true);
541
                    ILabelingStrategy labelingStrategy = (ILabelingStrategy) state.get("labelingStrategy");
542
                    this.setLabelingStrategy(labelingStrategy);
543 41807 jjdelcerro
                }
544
            } catch (Exception ex) {
545 43215 jjdelcerro
                this.setAvailable(false);
546
                this.addError(ex);
547 41807 jjdelcerro
            }
548 40435 jjdelcerro
549 41807 jjdelcerro
        } catch (Throwable e) {
550 42289 jjdelcerro
            String storeName = (store == null) ? "unknow" : store.getFullName();
551
            logger.warn("can't load layer '" + this.getName() + "' (store=" + storeName + ") from persisted state.", e);
552 41807 jjdelcerro
            this.setAvailable(false);
553 43215 jjdelcerro
            this.addError(e);
554 41807 jjdelcerro
        }
555
556 40435 jjdelcerro
    }
557
558
    /**
559
     * Sobreimplementaci?n del m?todo toString para que las bases de datos
560
     * identifiquen la capa.
561 42289 jjdelcerro
     *
562 40435 jjdelcerro
     * @return DOCUMENT ME!
563
     */
564
    public String toString() {
565
        /*
566
         * Se usa internamente para que la parte de datos identifique de forma
567
         * un?voca las tablas
568
         */
569
        String ret = super.toString();
570
571 43215 jjdelcerro
        return ret; //"layer" + ret.substring(ret.indexOf('@') + 1);
572 40435 jjdelcerro
    }
573
574
    public boolean isEditing() {
575
        FeatureStore fs = getFeatureStore();
576 43215 jjdelcerro
        if( fs == null ) {
577 40435 jjdelcerro
            /*
578
             * This happens when layer is not available, for example,
579
             * it was not possible to load from persistence
580
             */
581
            return false;
582
        } else {
583
            return fs.isEditing();
584
        }
585
    }
586 42289 jjdelcerro
587 40435 jjdelcerro
    public void setEditing(boolean b) throws StartEditionLayerException {
588 42289 jjdelcerro
589 40435 jjdelcerro
        try {
590
            throw new RuntimeException();
591
        } catch (Throwable th) {
592
            logger.info("This method is deprecated. ", th);
593
        }
594 42289 jjdelcerro
595 43215 jjdelcerro
        if( b == super.isEditing() ) {
596 40435 jjdelcerro
            return;
597
        }
598 42289 jjdelcerro
599 40435 jjdelcerro
        super.setEditing(b);
600
        FeatureStore fs = getFeatureStore();
601 43215 jjdelcerro
        if( b ) {
602 40435 jjdelcerro
            try {
603
                fs.edit();
604
            } catch (DataException e) {
605
                throw new StartEditionLayerException(getName(), e);
606
            }
607
        }
608
        setSpatialCacheEnabled(b);
609
        callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
610
    }
611
612
    /**
613
     * @deprecated Use {@link #getSpatialCache()}
614
     */
615
    public void clearSpatialCache() {
616
        spatialCache.clearAll();
617
    }
618
619
    /**
620
     * @deprecated Use {@link #getSpatialCache()}
621
     */
622
    public boolean isSpatialCacheEnabled() {
623
        return spatialCache.isEnabled();
624
    }
625
626
    /**
627
     * @deprecated Use {@link #getSpatialCache()}
628
     */
629
    public void setSpatialCacheEnabled(boolean spatialCacheEnabled) {
630
        spatialCache.setEnabled(spatialCacheEnabled);
631
    }
632
633
    public SpatialCache getSpatialCache() {
634
        return spatialCache;
635
    }
636
637
    /**
638
     * Siempre es un numero mayor de 1000
639 42289 jjdelcerro
     *
640 40435 jjdelcerro
     * @param maxFeatures
641
     */
642
    public void setMaxFeaturesInEditionCache(int maxFeatures) {
643 43215 jjdelcerro
        if( maxFeatures > spatialCache.getMaxFeatures() ) {
644 40435 jjdelcerro
            spatialCache.setMaxFeatures(maxFeatures);
645
        }
646
647
    }
648
649
    /**
650 42289 jjdelcerro
     * This method returns a boolean that is used by the FPopMenu to make
651
     * visible the properties menu or not. It is visible by default, and if a
652
     * later don't have to show this menu only has to override this method.
653
     *
654
     * @return If the properties menu is visible (or not)
655 40435 jjdelcerro
     */
656
    public boolean isPropertiesMenuVisible() {
657
        return true;
658
    }
659
660
    public void reload() throws ReloadLayerException {
661
        super.reload();
662
        try {
663
            getFeatureStore().refresh();
664
        } catch (Exception e) {
665
            throw new ReloadLayerException(getName(), e);
666
        }
667
    }
668
669
    protected void setLoadSelection(Object xml) {
670
        // this.loadSelection = xml;
671
    }
672
673
    protected void setLoadLegend(IVectorLegend legend) {
674
        this.loadLegend = legend;
675
    }
676
677
    protected void putLoadSelection() {
678
        // if (this.loadSelection == null) return;
679
        // try {
680
        // this.getRecordset().getSelectionSupport().setXMLEntity(this.loadSelection);
681
        // } catch (ReadDriverException e) {
682
        // throw new XMLException(e);
683
        // }
684
        // this.loadSelection = null;
685
686
    }
687
688
    protected void putLoadLegend() throws LegendLayerException {
689 43215 jjdelcerro
        if( this.loadLegend == null ) {
690 40435 jjdelcerro
            return;
691
        }
692
        this.setLegend(this.loadLegend);
693
        this.loadLegend = null;
694
    }
695
696
    protected void cleanLoadOptions() {
697
        this.loadLegend = null;
698
    }
699
700
    public boolean isWritable() {
701
        return getFeatureStore().allowWrite();
702
    }
703
704
    public FLayer cloneLayer() throws Exception {
705
        FLyrVect clonedLayer = new FLyrVect();
706
        clonedLayer.bindToDataStore(getDataStore());
707
        // if (isJoined()) {
708
        // clonedLayer.setIsJoined(true);
709
        // }
710
        clonedLayer.setVisible(isVisible());
711
        // clonedLayer.setISpatialIndex(getISpatialIndex());
712
        clonedLayer.setName(getName());
713
        clonedLayer.setCoordTrans(getCoordTrans());
714
715
        clonedLayer.setLegend((IVectorLegend) getLegend().cloneLegend());
716
717
        clonedLayer.setIsLabeled(isLabeled());
718
        ILabelingStrategy labelingStrategy = getLabelingStrategy();
719 43215 jjdelcerro
        if( labelingStrategy != null ) {
720 40435 jjdelcerro
            clonedLayer.setLabelingStrategy(labelingStrategy);
721
        }
722
723
        return clonedLayer;
724
    }
725
726
    protected boolean isOnePoint(AffineTransform graphicsTransform,
727 43215 jjdelcerro
        ViewPort viewPort,
728
        double dpi,
729
        CartographicSupport csSym,
730
        Geometry geom,
731
        int[] xyCoords) {
732 40435 jjdelcerro
        return isOnePoint(graphicsTransform, viewPort, geom, xyCoords)
733 43215 jjdelcerro
            && csSym.getCartographicSize(viewPort, dpi, geom) <= 1;
734 40435 jjdelcerro
    }
735
736
    private boolean isOnePoint(AffineTransform graphicsTransform,
737 43215 jjdelcerro
        ViewPort viewPort,
738
        Geometry geom,
739
        int[] xyCoords) {
740 40435 jjdelcerro
        boolean onePoint = false;
741
        int type = geom.getType();
742 43215 jjdelcerro
        if( type == Geometry.TYPES.NULL ) {
743 40435 jjdelcerro
            return false;
744
        }
745 43215 jjdelcerro
        if( type != Geometry.TYPES.POINT && type != Geometry.TYPES.MULTIPOINT ) {
746 40435 jjdelcerro
747
            Envelope geomBounds = geom.getEnvelope();
748
749
            // ICoordTrans ct = getCoordTrans();
750
            // Se supone que la geometria ya esta reproyectada
751
            // if (ct!=null) {
752
            // // geomBounds = ct.getInverted().convert(geomBounds);
753
            // geomBounds = geomBounds.convert(ct);
754
            // }
755
            double dist1Pixel = viewPort.getDist1pixel();
756
757 42289 jjdelcerro
            onePoint
758 43215 jjdelcerro
                = (geomBounds.getLength(0) <= dist1Pixel && geomBounds.getLength(1) <= dist1Pixel);
759 40435 jjdelcerro
760 43215 jjdelcerro
            if( onePoint ) {
761 40435 jjdelcerro
                // avoid out of range exceptions
762
                org.gvsig.fmap.geom.primitive.Point p;
763
                try {
764 42289 jjdelcerro
                    p
765 43215 jjdelcerro
                        = geomManager.createPoint(geomBounds.getMinimum(0),
766
                            geomBounds.getMinimum(1),
767
                            SUBTYPES.GEOM2D);
768 40435 jjdelcerro
                    p.transform(viewPort.getAffineTransform());
769
                    p.transform(graphicsTransform);
770
                    xyCoords[0] = (int) p.getX();
771
                    xyCoords[1] = (int) p.getY();
772
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
773
                    logger.error("Error creating a point", e);
774
                }
775
776
            }
777
778
        }
779
        return onePoint;
780
    }
781
782
    public boolean isLabeled() {
783
        return isLabeled;
784
    }
785
786
    public void setIsLabeled(boolean isLabeled) {
787
        this.isLabeled = isLabeled;
788
    }
789
790
    public ILabelingStrategy getLabelingStrategy() {
791
        return strategy;
792
    }
793
794
    public void setLabelingStrategy(ILabelingStrategy strategy) {
795
        this.strategy = strategy;
796 43215 jjdelcerro
        if( strategy == null ) {
797 40435 jjdelcerro
            return;
798
        }
799
        strategy.setLayer(this);
800
        updateDrawVersion();
801
    }
802
803
    public void drawLabels(BufferedImage image,
804 43215 jjdelcerro
        Graphics2D g,
805
        ViewPort viewPort,
806
        Cancellable cancel,
807
        double scale,
808
        double dpi) throws ReadException {
809
        if( strategy != null && isWithinScale(scale) ) {
810 41676 jjdelcerro
            strategy.draw(image, g, scale, viewPort, cancel, dpi);
811 40435 jjdelcerro
        }
812
    }
813
814
    public void printLabels(Graphics2D g,
815 43215 jjdelcerro
        ViewPort viewPort,
816
        Cancellable cancel,
817
        double scale,
818
        PrintAttributes properties) throws ReadException {
819
        if( strategy != null ) {
820 41676 jjdelcerro
            strategy.print(g, scale, viewPort, cancel, properties);
821 40435 jjdelcerro
        }
822
    }
823
824
    /**
825
     * Return true, because a Vectorial Layer supports HyperLink
826 42289 jjdelcerro
     *
827 40435 jjdelcerro
     * @deprecated the hiperlink functionaliti is out the layer now
828
     */
829
    public boolean allowLinks() {
830
        return false;
831
    }
832
833
    public void load() throws LoadLayerException {
834
        super.load();
835
    }
836
837
    public FeatureStore getFeatureStore() {
838
        return (FeatureStore) getDataStore();
839
    }
840
841 42775 jjdelcerro
    public FeatureQuery createFeatureQuery() {
842 43215 jjdelcerro
        if( this.baseQuery == null ) {
843 42775 jjdelcerro
            return this.getFeatureStore().createFeatureQuery();
844
        }
845
        try {
846
            return (FeatureQuery) baseQuery.clone();
847
        } catch (CloneNotSupportedException ex) {
848
            throw new RuntimeException(ex);
849
        }
850
    }
851 42820 dmartinezizquierdo
852 40435 jjdelcerro
    /**
853 42289 jjdelcerro
     * @deprecated use instead
854
     * {@link #queryByPoint(org.gvsig.fmap.geom.primitive.Point, double, FeatureType)}
855 40435 jjdelcerro
     */
856
    public FeatureSet queryByPoint(Point2D mapPoint,
857 43215 jjdelcerro
        double tol,
858
        FeatureType featureType) throws DataException {
859 40435 jjdelcerro
        logger.warn("Deprecated use of queryByPoint.");
860
        GeometryManager manager = GeometryLocator.getGeometryManager();
861
        org.gvsig.fmap.geom.primitive.Point center;
862
        try {
863 42289 jjdelcerro
            center
864 43215 jjdelcerro
                = (org.gvsig.fmap.geom.primitive.Point) manager.create(TYPES.POINT,
865
                    SUBTYPES.GEOM2D);
866 40435 jjdelcerro
            center.setX(mapPoint.getX());
867
            center.setY(mapPoint.getY());
868 42289 jjdelcerro
            Circle circle
869 43215 jjdelcerro
                = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
870 40435 jjdelcerro
            circle.setPoints(center, tol);
871
            return queryByGeometry(circle, featureType);
872
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
873
            throw new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e);
874
        }
875
    }
876
877
    public FeatureSet queryByPoint(org.gvsig.fmap.geom.primitive.Point point,
878 43215 jjdelcerro
        double tol,
879
        FeatureType featureType) throws DataException {
880 40435 jjdelcerro
        GeometryManager manager = GeometryLocator.getGeometryManager();
881
        try {
882 42289 jjdelcerro
            Circle circle
883 43215 jjdelcerro
                = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
884 40435 jjdelcerro
            circle.setPoints(point, tol);
885
            return queryByGeometry(circle, featureType);
886
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
887
            throw new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e);
888
        }
889
    }
890
891 40941 jldominguez
    /**
892
     * Input geom must be in the CRS of the view.
893 42289 jjdelcerro
     *
894 40941 jldominguez
     * @param geom
895
     * @param featureType
896
     * @return
897
     * @throws DataException
898
     */
899 40435 jjdelcerro
    public FeatureSet queryByGeometry(Geometry geom, FeatureType featureType) throws DataException {
900 42775 jjdelcerro
        FeatureQuery featureQuery = createFeatureQuery();
901 40435 jjdelcerro
        featureQuery.setFeatureType(featureType);
902 42289 jjdelcerro
903 41813 jjdelcerro
        Geometry query_geo = this.transformToSourceCRS(geom, true);
904 40435 jjdelcerro
        IProjection query_proj = getMapContext().getProjection();
905 43215 jjdelcerro
        if( this.getCoordTrans() != null ) {
906 42289 jjdelcerro
            query_proj = this.getCoordTrans().getPOrig();
907 40435 jjdelcerro
        }
908 43020 jjdelcerro
        Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects(
909
            query_geo,
910
            query_proj,
911
            featureStore
912
        );
913 40435 jjdelcerro
        featureQuery.setFilter(iee);
914
        featureQuery.setAttributeNames(null);
915
        return getFeatureStore().getFeatureSet(featureQuery);
916
917
    }
918
919
    /**
920
     * It return the {@link FeatureSet} that intersects with the envelope.
921 42289 jjdelcerro
     *
922
     * @param envelope envelope that defines the area for the query.
923
     * @param featureType only the features with this feature type are used in
924
     * the query.
925
     * @return the set of features that intersect with the envelope.
926 40435 jjdelcerro
     * @throws DataException
927
     */
928
    public FeatureSet queryByEnvelope(Envelope envelope, FeatureType featureType) throws DataException {
929
        return queryByEnvelope(envelope, featureType, null);
930
    }
931
932
    /**
933
     * It return the {@link FeatureSet} that intersects with the envelope.
934 42289 jjdelcerro
     *
935
     * @param envelope envelope that defines the area for the query in viewport
936
     * CRS
937
     * @param featureType only the features with this feature type are used in
938
     * the query.
939
     * @param names the feature attributes that have to be checked.
940
     * @return the set of features that intersect with the envelope.
941 40435 jjdelcerro
     * @throws DataException
942
     */
943
    public FeatureSet queryByEnvelope(Envelope envelope,
944 43215 jjdelcerro
        FeatureType featureType,
945
        String[] names) throws DataException {
946 42775 jjdelcerro
        FeatureQuery featureQuery = createFeatureQuery();
947 43215 jjdelcerro
        if( names == null ) {
948 40435 jjdelcerro
            featureQuery.setFeatureType(featureType);
949
        } else {
950
            featureQuery.setAttributeNames(names);
951
            featureQuery.setFeatureTypeId(featureType.getId());
952
        }
953 42289 jjdelcerro
954 40435 jjdelcerro
        Envelope query_env = fromViewPortCRSToSourceCRS(this, envelope);
955
        IProjection query_proj = getMapContext().getProjection();
956 43215 jjdelcerro
        if( this.getCoordTrans() != null ) {
957 42289 jjdelcerro
            query_proj = this.getCoordTrans().getPOrig();
958 40435 jjdelcerro
        }
959 42289 jjdelcerro
960 43020 jjdelcerro
        Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects(
961 43215 jjdelcerro
            query_env.getGeometry(),
962
            query_proj,
963
            featureStore
964 43020 jjdelcerro
        );
965 40435 jjdelcerro
        featureQuery.setFilter(iee);
966
        return getFeatureStore().getFeatureSet(featureQuery);
967
968
    }
969
970
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel) throws LoadLayerException,
971 43215 jjdelcerro
        DataException {
972 40435 jjdelcerro
973
        return getInfo(p, tolerance, cancel, true);
974
    }
975
976
    public DynObjectSet getInfo(Point p,
977 43215 jjdelcerro
        double tolerance,
978
        Cancellable cancel,
979
        boolean fast) throws LoadLayerException, DataException {
980 40435 jjdelcerro
        Point2D infop = new Point2D.Double(p.x, p.y);
981
        Point2D pReal = this.getMapContext().getViewPort().toMapPoint(infop);
982
        return queryByPoint(pReal,
983 43215 jjdelcerro
            tolerance,
984
            getFeatureStore().getDefaultFeatureType()).getDynObjectSet(fast);
985 40435 jjdelcerro
    }
986
987
    public DynObjectSet getInfo(org.gvsig.fmap.geom.primitive.Point p,
988 43215 jjdelcerro
        double tolerance) throws LoadLayerException, DataException {
989 42289 jjdelcerro
        return queryByPoint(p, tolerance, getFeatureStore().getDefaultFeatureType()).getDynObjectSet(false);
990 40435 jjdelcerro
    }
991 42289 jjdelcerro
992 42802 jjdelcerro
    @Override
993 40435 jjdelcerro
    public void legendCleared(LegendClearEvent event) {
994
        this.updateDrawVersion();
995 43215 jjdelcerro
        LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(legend, event);
996 40435 jjdelcerro
        this.callLegendChanged(e);
997
    }
998
999 42802 jjdelcerro
    @Override
1000 40435 jjdelcerro
    public boolean symbolChanged(SymbolLegendEvent e) {
1001
        this.updateDrawVersion();
1002 42802 jjdelcerro
        LegendChangedEvent ev = LegendChangedEvent.createLegendChangedEvent(legend, e);
1003 40435 jjdelcerro
        this.callLegendChanged(ev);
1004
        return true;
1005
    }
1006
1007
    public void update(Observable observable, Object notification) {
1008 43215 jjdelcerro
        if( observable.equals(this.featureStore) ) {
1009
            if( notification instanceof FeatureStoreNotification ) {
1010 42289 jjdelcerro
                FeatureStoreNotification event
1011 43215 jjdelcerro
                    = (FeatureStoreNotification) notification;
1012
                if( event.getType() == FeatureStoreNotification.AFTER_DELETE
1013
                    || event.getType() == FeatureStoreNotification.AFTER_UNDO
1014
                    || event.getType() == FeatureStoreNotification.AFTER_REDO
1015
                    || event.getType() == FeatureStoreNotification.AFTER_REFRESH
1016
                    || event.getType() == FeatureStoreNotification.AFTER_UPDATE
1017
                    || event.getType() == FeatureStoreNotification.AFTER_UPDATE_TYPE
1018
                    || event.getType() == FeatureStoreNotification.SELECTION_CHANGE
1019
                    || event.getType() == FeatureStoreNotification.AFTER_INSERT ) {
1020 40435 jjdelcerro
                    this.updateDrawVersion();
1021 42289 jjdelcerro
1022 43215 jjdelcerro
                } else if( event.getType() == FeatureStoreNotification.AFTER_CANCELEDITING ) {
1023 42289 jjdelcerro
1024 40435 jjdelcerro
                    setSpatialCacheEnabled(false);
1025
                    callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
1026
                    this.updateDrawVersion();
1027 42289 jjdelcerro
1028 43215 jjdelcerro
                } else if( event.getType() == FeatureStoreNotification.AFTER_STARTEDITING ) {
1029 42289 jjdelcerro
1030 40435 jjdelcerro
                    setSpatialCacheEnabled(true);
1031
                    callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
1032
1033 43215 jjdelcerro
                } else if( event.getType() == FeatureStoreNotification.TRANSFORM_CHANGE ) {
1034 40435 jjdelcerro
                    //If a transform has to be applied, try to reload the layer.
1035
                    try {
1036
                        reload();
1037
                    } catch (ReloadLayerException e) {
1038 41132 jldominguez
                        logger.info("While reloading layer.", e);
1039 40435 jjdelcerro
                        this.setAvailable(false);
1040
                    }
1041 43215 jjdelcerro
                } else if( event.getType() == FeatureStoreNotification.RESOURCE_CHANGED ) {
1042 40435 jjdelcerro
                    this.setAvailable(false);
1043 43215 jjdelcerro
                } else if( event.getType() == FeatureStoreNotification.AFTER_FINISHEDITING ) {
1044 40435 jjdelcerro
                    this.setAvailable(true);
1045
                    setSpatialCacheEnabled(false);
1046
                    callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
1047
                    this.updateDrawVersion();
1048
                }
1049
            }
1050 43215 jjdelcerro
        } else if( notification instanceof FeatureDrawnNotification
1051
            && (isEditing() || isLayerToSnap()) ) {
1052 40614 jldominguez
            // This code is needed in editing mode
1053 42289 jjdelcerro
            // for all layers involved in snapping
1054
            // (including the layer being edited)
1055
            Geometry geometry
1056 43215 jjdelcerro
                = ((FeatureDrawnNotification) notification).getDrawnGeometry();
1057 40435 jjdelcerro
            spatialCache.insert(geometry.getEnvelope(), geometry);
1058
        }
1059
    }
1060
1061 40614 jldominguez
    private boolean isLayerToSnap() {
1062 41223 jldominguez
1063 43215 jjdelcerro
        if( this.getMapContext() == null ) {
1064 41223 jldominguez
            /*
1065
             * This happens with the graphics layer because it has no parent
1066
             */
1067
            return false;
1068
        } else {
1069
            return this.getMapContext().getLayersToSnap().contains(this);
1070
        }
1071 40614 jldominguez
1072 42289 jjdelcerro
        /*
1073
         Iterator itersnap = this.getMapContext().getLayersToSnap().iterator();
1074
         Object item = null;
1075
         while (itersnap.hasNext()) {
1076
         item = itersnap.next();
1077
         if (item == this) {
1078
         return true;
1079
         }
1080
         }
1081
         return false;
1082
         */
1083
    }
1084
1085
    /*
1086 40435 jjdelcerro
     * (non-Javadoc)
1087 42820 dmartinezizquierdo
     *
1088 40435 jjdelcerro
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
1089
     */
1090
    public Set getMetadataChildren() {
1091
        Set ret = new TreeSet();
1092
        ret.add(this.featureStore);
1093
        return ret;
1094
    }
1095
1096
    /*
1097
     * (non-Javadoc)
1098 42820 dmartinezizquierdo
     *
1099 40435 jjdelcerro
     * @see org.gvsig.metadata.Metadata#getMetadataID()
1100
     */
1101
    public Object getMetadataID() throws MetadataException {
1102
        return "Layer(" + this.getName() + "):"
1103 43215 jjdelcerro
            + this.featureStore.getMetadataID();
1104 40435 jjdelcerro
    }
1105
1106
    public GeometryType getTypeVectorLayer() throws DataException,
1107 43215 jjdelcerro
        LocatorException,
1108
        GeometryTypeNotSupportedException,
1109
        GeometryTypeNotValidException {
1110 40435 jjdelcerro
        // FIXME Esto deberia de pedirse a FType!!!!
1111
        FeatureStore fs = this.getFeatureStore();
1112
        FeatureType fType = fs.getDefaultFeatureType();
1113 42289 jjdelcerro
        FeatureAttributeDescriptor attr
1114 43215 jjdelcerro
            = fType.getAttributeDescriptor(fType.getDefaultGeometryAttributeIndex());
1115 42289 jjdelcerro
        GeometryType geomType
1116 43215 jjdelcerro
            = GeometryLocator.getGeometryManager()
1117 40435 jjdelcerro
                .getGeometryType(attr.getGeometryType(),
1118 43215 jjdelcerro
                    attr.getGeometrySubType());
1119 40435 jjdelcerro
        return geomType;
1120
    }
1121
1122 43215 jjdelcerro
    public static class RegisterPersistence
1123
        implements Callable {
1124 40435 jjdelcerro
1125 41840 jjdelcerro
        public Object call() {
1126
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1127 40435 jjdelcerro
1128 41840 jjdelcerro
            DynStruct definition
1129 43215 jjdelcerro
                = manager.addDefinition(FLyrVect.class,
1130
                    "FLyrVect",
1131
                    "FLyrVect Persistence definition",
1132
                    null,
1133
                    null);
1134 41840 jjdelcerro
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE,
1135 43215 jjdelcerro
                "FLyrDefault");
1136 41840 jjdelcerro
1137
            definition.addDynFieldObject("legend")
1138 43215 jjdelcerro
                .setClassOfValue(IVectorLegend.class)
1139
                .setMandatory(true);
1140 41840 jjdelcerro
            definition.addDynFieldObject("featureStore")
1141 43215 jjdelcerro
                .setClassOfValue(FeatureStore.class)
1142
                .setMandatory(true);
1143 41840 jjdelcerro
            definition.addDynFieldBoolean("isLabeled").setMandatory(true);
1144
            definition.addDynFieldInt("typeShape").setMandatory(true);
1145
            definition.addDynFieldObject("labelingStrategy")
1146 43215 jjdelcerro
                .setClassOfValue(ILabelingStrategy.class)
1147
                .setMandatory(false);
1148 41840 jjdelcerro
1149
            return Boolean.TRUE;
1150
        }
1151 40435 jjdelcerro
    }
1152
1153
    protected void doDispose() throws BaseException {
1154
        dispose(featureStore);
1155
        spatialCache.clearAll();
1156
    }
1157 42289 jjdelcerro
1158 40435 jjdelcerro
    /**
1159 42289 jjdelcerro
     * Returns envelope in layer's data source CRS from envelope provided in
1160
     * viewport CRS
1161
     *
1162 40435 jjdelcerro
     * @param lyr
1163
     * @param env
1164
     * @return
1165
     */
1166
    public static Envelope fromViewPortCRSToSourceCRS(FLayer lyr, Envelope env) {
1167
1168 43215 jjdelcerro
        if( lyr == null || env == null ) {
1169 40435 jjdelcerro
            return null;
1170
        }
1171
1172
        ICoordTrans ct = lyr.getCoordTrans();
1173 43215 jjdelcerro
        if( ct == null ) {
1174 40435 jjdelcerro
            return env;
1175
        } else {
1176
            return env.convert(ct.getInverted());
1177
        }
1178
    }
1179
1180 41813 jjdelcerro
    public Geometry transformToSourceCRS(Geometry geom, boolean clone) {
1181 42289 jjdelcerro
        return fromViewPortCRSToSourceCRS(this, geom, clone);
1182 41813 jjdelcerro
    }
1183 42289 jjdelcerro
1184 40435 jjdelcerro
    /**
1185 42289 jjdelcerro
     * Returns geometry in layer's data source CRS from geometry provided in
1186
     * viewport CRS
1187
     *
1188 40435 jjdelcerro
     * @param lyr
1189
     * @param geo
1190
     * @param clone
1191
     * @return
1192 41813 jjdelcerro
     * @deprecated use the transformToSourceCRS method of layer.
1193 40435 jjdelcerro
     */
1194
    public static Geometry fromViewPortCRSToSourceCRS(
1195 43215 jjdelcerro
        FLayer lyr,
1196
        Geometry geo,
1197
        boolean clone) {
1198 42289 jjdelcerro
1199 43215 jjdelcerro
        if( lyr == null || geo == null ) {
1200 40435 jjdelcerro
            return null;
1201
        }
1202
        ICoordTrans ct = lyr.getCoordTrans();
1203
        Geometry resp = geo;
1204 43215 jjdelcerro
        if( clone ) {
1205 40435 jjdelcerro
            resp = resp.cloneGeometry();
1206
        }
1207 43215 jjdelcerro
        if( ct != null ) {
1208 40435 jjdelcerro
            resp.reProject(ct.getInverted());
1209
        }
1210
        return resp;
1211
    }
1212
1213 42293 jjdelcerro
    public Iterator iterator() {
1214
        return this.getFeatureStore().iterator();
1215
    }
1216 42877 fdiaz
1217 43215 jjdelcerro
    protected class EnvelopeCantBeInitializedException
1218
        extends BaseException {
1219 42877 fdiaz
1220
        /**
1221
         *
1222
         */
1223
        private static final long serialVersionUID = 4572797479347381552L;
1224
        private final static String MESSAGE_FORMAT = "The envelope can't be initialized, maybe the layer has a wrong projection. Change the projection in layer properties of the add layer dialog to try fix it.";
1225
        private final static String MESSAGE_KEY = "_EnvelopeCantBeInitializedException";
1226
1227
        public EnvelopeCantBeInitializedException() {
1228
            super(MESSAGE_FORMAT, null, MESSAGE_KEY, serialVersionUID);
1229
        }
1230
    }
1231
1232 43141 jjdelcerro
    @Override
1233
    public String getInfoString() {
1234 43215 jjdelcerro
        LayerInformationBuilder builder = MapContextLocator.getMapContextManager().createLayerInformationBuilder();
1235
1236
        if( !this.isAvailable() ) {
1237
            builder.title().labelkey("Warning");
1238
            builder.text().value("The layer is not available.");
1239
        }
1240
1241 43141 jjdelcerro
        Envelope env = null;
1242
        FeatureAttributeDescriptor attrgeom = null;
1243 43215 jjdelcerro
1244 43141 jjdelcerro
        try {
1245
            env = this.getFullEnvelope();
1246 43215 jjdelcerro
        } catch (Exception ex) {
1247 43141 jjdelcerro
        }
1248
        FeatureStore store = this.getFeatureStore();
1249
        try {
1250
            FeatureType ft = store.getDefaultFeatureType();
1251
            attrgeom = ft.getDefaultGeometryAttribute();
1252 43215 jjdelcerro
        } catch (Exception ex) {
1253 43141 jjdelcerro
        }
1254
1255
        builder.title().labelkey("Data_source");
1256
        builder.property().labelkey("Source_type").value(store.getProviderName());
1257
        builder.property().labelkey("origen").value(store.getFullName());
1258
        if( store.getParameters() instanceof FilesystemStoreParameters ) {
1259 43215 jjdelcerro
            File f = ((FilesystemStoreParameters) (store.getParameters())).getFile();
1260
            if( f != null && f.exists() ) {
1261 43141 jjdelcerro
                builder.property().labelkey("_Size").value("%d bytes", f.length());
1262
            }
1263
        }
1264
        if( attrgeom != null ) {
1265
            builder.property().labelkey("_Geometry_column").value(attrgeom.getName());
1266
            builder.property().labelkey("_Geometry_type").value(attrgeom.getGeomType().getFullName());
1267
        }
1268
1269
        builder.title().labelkey("_Coordenadas_geograficas");
1270 43215 jjdelcerro
        if( this.isAvailable() ) {
1271
            builder.envelope().value(env);
1272
        }
1273 43141 jjdelcerro
1274
        builder.title().labelkey("_Projection");
1275 43215 jjdelcerro
        if( this.isAvailable() ) {
1276
            builder.property().value(this.getProjection());
1277
            builder.text().asWKT(this.getProjection());
1278
        }
1279 43141 jjdelcerro
        return builder.toString();
1280
    }
1281
1282 40435 jjdelcerro
}