10 |
**/ |
**/ |
11 |
package skrueger.geotools; |
package skrueger.geotools; |
12 |
|
|
|
|
|
13 |
import java.util.Iterator; |
import java.util.Iterator; |
14 |
import java.util.Map; |
import java.util.Map; |
15 |
import java.util.Vector; |
import java.util.Vector; |
19 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
20 |
import org.geotools.data.Query; |
import org.geotools.data.Query; |
21 |
import org.geotools.data.memory.MemoryDataStore; |
import org.geotools.data.memory.MemoryDataStore; |
22 |
|
import org.geotools.feature.AttributeType; |
23 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
24 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
25 |
|
|
35 |
* functionalities of the {@link AttributeMetaData} of |
* functionalities of the {@link AttributeMetaData} of |
36 |
* {@linkplain StyledMapInterface styled objects}. |
* {@linkplain StyledMapInterface styled objects}. |
37 |
* <ul> |
* <ul> |
38 |
* <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li> |
* <li>column names are translated according to |
39 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
* {@link AttributeMetaData#getTitle()}</li> |
40 |
|
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
41 |
* </ul> |
* </ul> |
42 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
* |
43 |
|
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
44 |
|
* (University of Bonn/Germany) |
45 |
*/ |
*/ |
46 |
public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel { |
public class StyledFeatureCollectionTableModel extends |
47 |
final static private Logger LOGGER = Logger.getLogger(StyledFeatureCollectionTableModel.class); |
FeatureCollectionTableModel { |
48 |
/** Holds the data source as styled map. */ |
final static private Logger LOGGER = Logger |
49 |
protected StyledMapInterface<?> map = null; |
.getLogger(StyledFeatureCollectionTableModel.class); |
50 |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
/** Holds the data source as styled map. */ |
51 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
protected StyledMapInterface<?> map = null; |
52 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
53 |
protected FeatureSource featureSource = null; |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
54 |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
/** Holds the data source for the table as {@code FeatureSource}. */ |
55 |
protected Map<Integer, AttributeMetaData> origAMD = null; |
protected FeatureSource featureSource = null; |
56 |
/** Holds the current filter on the table */ |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
57 |
protected Filter filter = null; |
protected Map<Integer, AttributeMetaData> origAMD = null; |
58 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
/** Holds the current filter on the table */ |
59 |
protected Envelope bounds; |
protected Filter filter = null; |
60 |
|
/** Holds the Bounds for all features. Only set once during the constructor **/ |
61 |
/** |
protected Envelope bounds; |
62 |
* Creates a new table model for a styled map. |
|
63 |
* @param map the styled map |
/** |
64 |
*/ |
* Creates a new table model for a styled map. |
65 |
public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) { |
* |
66 |
this(map,Filter.INCLUDE); |
* @param map |
67 |
} |
* the styled map |
68 |
|
*/ |
69 |
/** |
public StyledFeatureCollectionTableModel( |
70 |
* Creates a new table model for a styled map. |
StyledFeatureCollectionInterface map) { |
71 |
* @param map the styled map |
this(map, Filter.INCLUDE); |
72 |
* @param filter filter applied to the table |
} |
73 |
*/ |
|
74 |
public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) { |
/** |
75 |
super(); |
* Creates a new table model for a styled map. |
76 |
setFeatureCollection(map, filter); |
* |
77 |
} |
* @param map |
78 |
|
* the styled map |
79 |
/** |
* @param filter |
80 |
* Creates a new table model for a styled map. |
* filter applied to the table |
81 |
* @param map the styled map |
*/ |
82 |
*/ |
public StyledFeatureCollectionTableModel( |
83 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) { |
StyledFeatureCollectionInterface map, Filter filter) { |
84 |
this(map,Filter.INCLUDE); |
super(); |
85 |
} |
setFeatureCollection(map, filter); |
86 |
|
} |
87 |
/** |
|
88 |
* Creates a new table model for a styled map. |
/** |
89 |
* @param map the styled map |
* Creates a new table model for a styled map. |
90 |
* @param filter filter applied to the table |
* |
91 |
*/ |
* @param map |
92 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) { |
* the styled map |
93 |
super(); |
*/ |
94 |
setFeatureCollection(map, filter); |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) { |
95 |
} |
this(map, Filter.INCLUDE); |
96 |
|
} |
97 |
/** |
|
98 |
* Sets a new data source for the table. |
/** |
99 |
* @param fs the feature source |
* Creates a new table model for a styled map. |
100 |
* @param amd {@link AttributeMetaData}-Map to define the visible attributes |
* |
101 |
* and translation |
* @param map |
102 |
*/ |
* the styled map |
103 |
protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception { |
* @param filter |
104 |
if ( filter == null ) |
* filter applied to the table |
105 |
filter = Filter.INCLUDE; |
*/ |
106 |
|
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, |
107 |
this.featureSource = fs; |
Filter filter) { |
108 |
this.filter = filter; |
super(); |
109 |
this.origAMD = amd; |
setFeatureCollection(map, filter); |
110 |
this.visibleAMD = null; |
} |
111 |
|
|
112 |
FeatureCollection fc = null; |
/** |
113 |
if (fs != null) { |
* Sets a new data source for the table. |
114 |
|
* |
115 |
bounds = fs.getBounds(); |
* @param fs |
116 |
|
* the feature source |
117 |
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
* @param amd |
118 |
if (amd != null) { |
* {@link AttributeMetaData}-Map to define the visible attributes |
119 |
// determine the names of the visible Attributes |
* and translation |
120 |
this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true); |
*/ |
121 |
Vector<String> visibleAttrNames = new Vector<String>(); |
protected void setFeatureSource(FeatureSource fs, |
122 |
// Add the column with the geometry (usually "the_geom") |
Map<Integer, AttributeMetaData> amd, Filter filter) |
123 |
visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName()); |
throws Exception { |
124 |
for (int attrIdx : visibleAMD.keySet()) |
if (filter == null) |
125 |
visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName()); |
filter = Filter.INCLUDE; |
126 |
|
|
127 |
// create a query for the visible attributes |
this.featureSource = fs; |
128 |
String[] properties = visibleAttrNames.toArray(new String[0]); |
this.filter = filter; |
129 |
|
this.origAMD = amd; |
130 |
LOGGER.debug("Query contains the following attributes: " + visibleAttrNames); |
this.visibleAMD = null; |
131 |
|
|
132 |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties); |
FeatureCollection fc = null; |
133 |
} |
if (fs != null) { |
134 |
fc = fs.getFeatures(query); |
|
135 |
|
bounds = fs.getBounds(); |
136 |
|
|
137 |
|
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
138 |
|
if (amd != null) { |
139 |
|
// determine the names of the visible Attributes |
140 |
|
this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData( |
141 |
|
amd, true); |
142 |
|
Vector<String> visibleAttrNames = new Vector<String>(); |
143 |
|
// Add the column with the geometry (usually "the_geom") |
144 |
|
visibleAttrNames.add(fs.getSchema().getDefaultGeometry() |
145 |
|
.getLocalName()); |
146 |
|
for (int attrIdx : visibleAMD.keySet()) { |
147 |
|
|
148 |
|
/** |
149 |
|
* If the user removed columns from the schema of the DBF |
150 |
|
* file, there might exist AttributeMetaData for columns |
151 |
|
* that don't exists. We check here to avoid an |
152 |
|
* ArrayOutOfIndex. |
153 |
|
*/ |
154 |
|
if (attrIdx < fs.getSchema().getAttributeCount()) { |
155 |
|
final AttributeType attributeTypeAtIdx = fs.getSchema() |
156 |
|
.getAttributeType(attrIdx); |
157 |
|
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
158 |
|
} else { |
159 |
|
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
|
// create a query for the visible attributes |
164 |
|
String[] properties = visibleAttrNames.toArray(new String[0]); |
165 |
|
|
166 |
|
LOGGER.debug("Query contains the following attributes: " |
167 |
|
+ visibleAttrNames); |
168 |
|
|
169 |
|
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
170 |
|
properties); |
171 |
|
} |
172 |
|
fc = fs.getFeatures(query); |
173 |
|
|
174 |
// FAILS:!!!, even with query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
// FAILS:!!!, even with query = new |
175 |
|
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
176 |
// java.lang.UnsupportedOperationException: Unknown feature |
// java.lang.UnsupportedOperationException: Unknown feature |
177 |
// attribute: PQM_MOD |
// attribute: PQM_MOD |
178 |
// at |
// at |
183 |
// System.out.println("Filter = "+filter); |
// System.out.println("Filter = "+filter); |
184 |
// System.out.println("Size of FC = "+fc.size()); |
// System.out.println("Size of FC = "+fc.size()); |
185 |
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
186 |
} |
} |
187 |
setFeatureCollection(fc); |
setFeatureCollection(fc); |
188 |
} |
} |
189 |
|
|
190 |
/** |
/** |
191 |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
192 |
* and sets this as the new data source for the table. |
* and sets this as the new data source for the table. |
193 |
* @param fs the feature source |
* |
194 |
* @param amd {@link AttributeMetaData}-Map to define the visible attributes |
* @param fs |
195 |
* and translation |
* the feature source |
196 |
*/ |
* @param amd |
197 |
public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) { |
* {@link AttributeMetaData}-Map to define the visible attributes |
198 |
this.map = map; |
* and translation |
199 |
try { |
*/ |
200 |
if (map == null) |
public void setFeatureCollection(StyledFeatureCollectionInterface map, |
201 |
setFeatureSource(null, null, null); |
Filter filter) { |
202 |
else { |
this.map = map; |
203 |
FeatureCollection fc = map.getGeoObject(); |
try { |
204 |
String fcName = fc.getSchema().getTypeName(); |
if (map == null) |
205 |
FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName); |
setFeatureSource(null, null, null); |
206 |
setFeatureSource(fs, map.getAttributeMetaDataMap(), filter); |
else { |
207 |
} |
FeatureCollection fc = map.getGeoObject(); |
208 |
} catch (Exception err) { |
String fcName = fc.getSchema().getTypeName(); |
209 |
throw new RuntimeException(err); |
FeatureSource fs = new MemoryDataStore(fc) |
210 |
} |
.getFeatureSource(fcName); |
211 |
} |
setFeatureSource(fs, map.getAttributeMetaDataMap(), filter); |
212 |
|
} |
213 |
/** |
} catch (Exception err) { |
214 |
* Sets the {@code StyledFeatureCollection} as new data source for the table. |
throw new RuntimeException(err); |
215 |
* @param fs the feature source |
} |
216 |
* @param amd {@link AttributeMetaData}-Map to define the visible attributes |
} |
217 |
* and translation |
|
218 |
*/ |
/** |
219 |
public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) { |
* Sets the {@code StyledFeatureCollection} as new data source for the |
220 |
this.map = map; |
* table. |
221 |
try { |
* |
222 |
if (map == null) |
* @param fs |
223 |
setFeatureSource(null, null, null); |
* the feature source |
224 |
else |
* @param amd |
225 |
setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter); |
* {@link AttributeMetaData}-Map to define the visible attributes |
226 |
} catch (Exception err) { |
* and translation |
227 |
throw new RuntimeException(err); |
*/ |
228 |
} |
public void setFeatureCollection(StyledFeatureSourceInterface map, |
229 |
} |
Filter filter) { |
230 |
|
this.map = map; |
231 |
/** |
try { |
232 |
* Resets the filter for the table. |
if (map == null) |
233 |
* @param filter a filter |
setFeatureSource(null, null, null); |
234 |
*/ |
else |
235 |
public void setFilter(Filter filter) { |
setFeatureSource(map.getGeoObject(), map |
236 |
try{ |
.getAttributeMetaDataMap(), filter); |
237 |
setFeatureSource(this.featureSource, this.origAMD, filter); |
} catch (Exception err) { |
238 |
} catch (Exception err) { |
throw new RuntimeException(err); |
239 |
LOGGER.error("Setting the filter of the table model", err); |
} |
240 |
throw new RuntimeException(err); |
} |
241 |
} |
|
242 |
} |
/** |
243 |
|
* Resets the filter for the table. |
244 |
/** |
* |
245 |
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the Features |
* @param filter |
246 |
*/ |
* a filter |
247 |
public Filter getFilter() { |
*/ |
248 |
return this.filter; |
public void setFilter(Filter filter) { |
249 |
} |
try { |
250 |
|
setFeatureSource(this.featureSource, this.origAMD, filter); |
251 |
/** |
} catch (Exception err) { |
252 |
* After calling {@code super.reorganize(.)} this method replaced the |
LOGGER.error("Setting the filter of the table model", err); |
253 |
* column descriptions with the titles of the {@code AttributeMetaData}. |
throw new RuntimeException(err); |
254 |
* @param fireTableStructureChanged indicates whether a table event is |
} |
255 |
* initiated after reorganize |
} |
256 |
*/ |
|
257 |
@Override |
/** |
258 |
protected void reorganize(boolean fireTableStructureChanged) { |
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
259 |
super.reorganize(false); |
* Features |
260 |
// translate the column names |
*/ |
261 |
if (visibleAMD != null) { |
public Filter getFilter() { |
262 |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
return this.filter; |
263 |
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
} |
264 |
Translation title = visibleAMD.get(keys.next()).getTitle(); |
|
265 |
if (!I8NUtil.isEmpty(title)) { |
/** |
266 |
// System.out.println("set colname " + i + " to " + title.toString()); |
* After calling {@code super.reorganize(.)} this method replaced the column |
267 |
colNames[i] = title.toString(); |
* descriptions with the titles of the {@code AttributeMetaData}. |
268 |
} |
* |
269 |
} |
* @param fireTableStructureChanged |
270 |
} |
* indicates whether a table event is initiated after reorganize |
271 |
if ( fireTableStructureChanged ) |
*/ |
272 |
fireTableStructureChanged(); |
@Override |
273 |
} |
protected void reorganize(boolean fireTableStructureChanged) { |
274 |
|
super.reorganize(false); |
275 |
/** |
// translate the column names |
276 |
* @return Cached bounds for the whole dataset (without applying the |
if (visibleAMD != null) { |
277 |
* filter) or <code>null</code> |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
278 |
|
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
279 |
|
Translation title = visibleAMD.get(keys.next()).getTitle(); |
280 |
|
if (!I8NUtil.isEmpty(title)) { |
281 |
|
// System.out.println("set colname " + i + " to " + |
282 |
|
// title.toString()); |
283 |
|
colNames[i] = title.toString(); |
284 |
|
} |
285 |
|
} |
286 |
|
} |
287 |
|
if (fireTableStructureChanged) |
288 |
|
fireTableStructureChanged(); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
292 |
|
* @return Cached bounds for the whole dataset (without applying the filter) |
293 |
|
* or <code>null</code> |
294 |
*/ |
*/ |
295 |
public Envelope getBounds() { |
public Envelope getBounds() { |
296 |
return bounds; |
return bounds; |