Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.core / src / main / java / es / unex / sextante / dataObjects / AbstractVectorLayer.java @ 315

History | View | Annotate | Download (3.81 KB)

1

    
2

    
3
package es.unex.sextante.dataObjects;
4

    
5
import java.awt.geom.Rectangle2D;
6
import java.util.ArrayList;
7
import java.util.List;
8

    
9
import com.vividsolutions.jts.geom.Envelope;
10

    
11
import es.unex.sextante.dataObjects.vectorFilters.IVectorLayerFilter;
12
import es.unex.sextante.exceptions.IteratorException;
13

    
14

    
15
/**
16
 * A convenience class which implements some of the methods of the IVectorLayer interface. Extending this class is recommended
17
 * instead of implementing the interface directly
18
 * 
19
 * @author volaya
20
 * 
21
 */
22
public abstract class AbstractVectorLayer
23
         implements
24
            IVectorLayer {
25

    
26
   private int                                 m_iShapesCount;
27
   private boolean                             m_bShapesCountAndExtentCalculated = false;
28
   private Rectangle2D                         m_Extent;
29
   private final ArrayList<IVectorLayerFilter> m_Filters                         = new ArrayList<IVectorLayerFilter>();
30

    
31

    
32
   @Override
33
   public abstract Object getBaseDataObject();
34

    
35

    
36
   @Override
37
   public String[] getFieldNames() {
38

    
39
      final String[] names = new String[getFieldCount()];
40

    
41
      for (int i = 0; i < names.length; i++) {
42
         names[i] = getFieldName(i);
43
      }
44

    
45
      return names;
46

    
47
   }
48

    
49

    
50
   @Override
51
   public int getFieldIndexByName(final String sFieldName) {
52

    
53
      for (int i = 0; i < this.getFieldCount(); i++) {
54
         final String sName = getFieldName(i);
55
         if (sName.equalsIgnoreCase(sFieldName)) {
56
            return i;
57
         }
58
      }
59

    
60
      return -1;
61

    
62
   }
63

    
64

    
65
   @Override
66
   public Class[] getFieldTypes() {
67

    
68
      final Class[] types = new Class[getFieldCount()];
69

    
70
      for (int i = 0; i < types.length; i++) {
71
         types[i] = getFieldType(i);
72
      }
73

    
74
      return types;
75

    
76
   }
77

    
78

    
79
   @Override
80
   public String toString() {
81

    
82
      return this.getName();
83

    
84
   }
85

    
86

    
87
   @Override
88
   public void addFeature(final IFeature feature) {
89

    
90
      addFeature(feature.getGeometry(), feature.getRecord().getValues());
91

    
92
   }
93

    
94

    
95
   @Override
96
   public int getShapesCount() {
97

    
98
      if (!m_bShapesCountAndExtentCalculated) {
99
         calculateShapesCountAndExtent();
100
      }
101
      return m_iShapesCount;
102

    
103
   }
104

    
105

    
106
   @Override
107
   public Rectangle2D getFullExtent() {
108

    
109
      if (!m_bShapesCountAndExtentCalculated) {
110
         calculateShapesCountAndExtent();
111
      }
112
      return m_Extent;
113

    
114
   }
115

    
116

    
117
   private void calculateShapesCountAndExtent() {
118

    
119
      Envelope envelope = null;
120
      final IFeatureIterator iter = iterator();
121
      m_iShapesCount = 0;
122

    
123
      while (iter.hasNext()) {
124
         IFeature feature;
125
         try {
126
            feature = iter.next();
127
         }
128
         catch (final IteratorException e) {
129
            m_Extent = new Rectangle2D.Double(0, 0, 0, 0);
130
            m_iShapesCount = 0;
131
            return;
132
         }
133
         if (m_iShapesCount == 0) {
134
            envelope = feature.getGeometry().getEnvelopeInternal();
135
         }
136
         else {
137
            envelope.expandToInclude(feature.getGeometry().getEnvelopeInternal());
138
         }
139
         m_iShapesCount++;
140
      }
141

    
142
      if (m_iShapesCount == 0) {
143
         m_Extent = new Rectangle2D.Double();
144
      }
145
      else {
146
         m_Extent = new Rectangle2D.Double(envelope.getMinX(), envelope.getMinY(), envelope.getWidth(), envelope.getHeight());
147
      }
148

    
149
      m_bShapesCountAndExtentCalculated = true;
150

    
151
   }
152

    
153

    
154
   @Override
155
   public void addFilter(final IVectorLayerFilter filter) {
156

    
157
      m_Filters.add(filter);
158
      m_bShapesCountAndExtentCalculated = false;
159

    
160
   }
161

    
162

    
163
   @Override
164
   public void removeFilters() {
165

    
166
      m_Filters.clear();
167
      m_bShapesCountAndExtentCalculated = false;
168

    
169
   }
170

    
171

    
172
   @Override
173
   public List<IVectorLayerFilter> getFilters() {
174

    
175
      return m_Filters;
176

    
177
   }
178

    
179

    
180
}