25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
32 |
import java.util.Iterator; |
import java.util.HashMap; |
33 |
import java.util.Map; |
import java.util.LinkedHashSet; |
34 |
import java.util.Vector; |
import java.util.List; |
35 |
|
|
36 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
37 |
import org.geotools.data.DefaultQuery; |
import org.geotools.data.DefaultQuery; |
38 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
39 |
import org.geotools.data.Query; |
import org.geotools.data.Query; |
|
import org.geotools.data.memory.MemoryDataStore; |
|
40 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
41 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
42 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
43 |
import org.opengis.feature.type.AttributeDescriptor; |
import org.opengis.feature.type.AttributeDescriptor; |
44 |
|
import org.opengis.feature.type.Name; |
45 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
46 |
|
|
47 |
|
import schmitzm.geotools.feature.FeatureUtil; |
48 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
49 |
import skrueger.AttributeMetaData; |
import skrueger.AttributeMetadataImpl; |
50 |
import skrueger.i8n.I8NUtil; |
import skrueger.AttributeMetadataInterface; |
|
import skrueger.i8n.Translation; |
|
51 |
|
|
52 |
import com.vividsolutions.jts.geom.Envelope; |
import com.vividsolutions.jts.geom.Envelope; |
53 |
|
|
54 |
/** |
/** |
55 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
* This class extends the the {@link FeatureCollectionTableModel} with the |
56 |
* functionalities of the {@link AttributeMetaData} of |
* functionalities of the {@link AttributeMetadataImpl}. |
|
* {@linkplain StyledLayerInterface styled objects}. |
|
57 |
* <ul> |
* <ul> |
58 |
* <li>column names are translated according to |
* <li>column names are translated according to |
59 |
* {@link AttributeMetaData#getTitle()}</li> |
* {@link AttributeMetadataImpl#getTitle()}</li> |
60 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
61 |
|
* <li>Any filter defined in the {@link StyledFeaturesInterface} will be |
62 |
|
* applied.</li> |
63 |
* </ul> |
* </ul> |
64 |
* |
* |
65 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
* @author Stefan A. Tzeggai |
|
* (University of Bonn/Germany) |
|
66 |
*/ |
*/ |
67 |
public class StyledFeatureCollectionTableModel extends |
public class StyledFeatureCollectionTableModel extends |
68 |
FeatureCollectionTableModel { |
FeatureCollectionTableModel { |
69 |
final static private Logger LOGGER = Logger |
final static private Logger LOGGER = Logger |
70 |
.getLogger(StyledFeatureCollectionTableModel.class); |
.getLogger(StyledFeatureCollectionTableModel.class); |
71 |
/** Holds the data source as styled layer. */ |
/** |
72 |
protected StyledLayerInterface<?> layer = null; |
* Contains the complete {@link AttributeMetadataImpl}-Map of the styled |
73 |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
* layer. |
74 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
*/ |
75 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null; |
|
protected FeatureSource featureSource = null; |
|
|
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
|
|
protected Map<Integer, AttributeMetaData> origAMD = null; |
|
76 |
/** Holds the current filter on the table */ |
/** Holds the current filter on the table */ |
77 |
protected Filter filter = null; |
protected Filter filter = null; |
78 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
79 |
protected Envelope bounds; |
protected Envelope bounds; |
|
|
|
|
|
|
|
|
|
80 |
/** |
/** |
81 |
* Creates a new table model for a styled layer. |
* Tooltips für die Spaltennamen. Wird nur beim Aufruf von |
82 |
* |
* {@link #reorganize} befuellt. |
|
* @param layer |
|
|
* the styled layer |
|
83 |
*/ |
*/ |
84 |
public StyledFeatureCollectionTableModel( |
protected String[] colTooltips = null; |
85 |
StyledFeatureCollectionInterface layer) { |
|
86 |
this(layer, Filter.INCLUDE); |
/** |
87 |
} |
* A cache for the #sortedValuesVisibleOnly(). WHen the amd is changed |
88 |
|
* externally, this object will stay the same |
89 |
|
**/ |
90 |
|
protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null; |
91 |
|
|
92 |
/** |
/** |
93 |
* Creates a new table model for a styled layer. |
* Creates a new table model for a styled layer. |
94 |
* |
* |
95 |
* @param layer |
* @param styledFeatures |
96 |
* the styled layer |
* the styled layer |
97 |
* @param filter |
* @param filter |
98 |
* filter applied to the table |
* filter applied to the table |
99 |
*/ |
*/ |
100 |
public StyledFeatureCollectionTableModel( |
public StyledFeatureCollectionTableModel( |
101 |
StyledFeatureCollectionInterface layer, Filter filter) { |
StyledFeaturesInterface<?> styledFeatures) { |
102 |
super(); |
setStyledFeatures(styledFeatures); |
|
setFeatureCollection(layer, filter); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Creates a new table model for a styled layer. |
|
|
* |
|
|
* @param layer |
|
|
* the styled layer |
|
|
*/ |
|
|
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) { |
|
|
this(layer, Filter.INCLUDE); |
|
103 |
} |
} |
104 |
|
|
105 |
/** |
/** |
106 |
* Creates a new table model for a styled layer. |
* This overwritten method filters the values for NODATA-values defined in |
107 |
* |
* the {@link AttributeMetadataImpl} |
|
* @param layer |
|
|
* the styled layer |
|
|
* @param filter |
|
|
* filter applied to the table |
|
108 |
*/ |
*/ |
109 |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer, |
@Override |
110 |
Filter filter) { |
public Object getValueAt(int row, int col) { |
111 |
super(); |
Object rawValue = super.getValueAt(row, col); |
112 |
setFeatureCollection(layer, filter); |
return amdMapVisibleOnly.get(col).fiterNodata(rawValue); |
113 |
} |
} |
114 |
|
|
115 |
/** |
/** |
117 |
* |
* |
118 |
* @param fs |
* @param fs |
119 |
* the feature source |
* the feature source |
120 |
* @param amd |
* @param amdm |
121 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetadataImpl}-Map to define the visible |
122 |
* and translation |
* attributes and translation |
123 |
*/ |
*/ |
124 |
protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
protected void setFeatureSource( |
125 |
Map<Integer, AttributeMetaData> amd, Filter filter) |
FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
126 |
throws Exception { |
AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, |
127 |
|
Filter filter) throws Exception { |
128 |
|
|
129 |
if (filter == null) |
if (filter == null) |
130 |
filter = Filter.INCLUDE; |
filter = Filter.INCLUDE; |
131 |
|
|
132 |
this.featureSource = fs; |
// this.featureSource = fs; |
133 |
this.filter = filter; |
this.filter = filter; |
134 |
this.origAMD = amd; |
this.amdMap = amdm; |
135 |
this.visibleAMD = null; |
this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly(); |
136 |
|
|
137 |
FeatureCollection fc = null; |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null; |
138 |
if (fs != null) { |
if (fs != null) { |
139 |
|
|
140 |
bounds = fs.getBounds(); |
bounds = fs.getBounds(); |
141 |
|
|
142 |
Query query = new DefaultQuery(fs.getSchema().getName().getLocalPart(), filter); |
final SimpleFeatureType schema = fs.getSchema(); |
143 |
if (amd != null) { |
Query query = new DefaultQuery(schema.getTypeName(), filter); |
144 |
// determine the names of the visible Attributes |
if (amdm != null) { |
145 |
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>(); |
146 |
amd, true); |
|
147 |
Vector<String> visibleAttrNames = new Vector<String>(); |
// Add the column with the geometry (usually "the_geom") always |
148 |
// Add the column with the geometry (usually "the_geom") |
visibleAttrNames.add(schema.getGeometryDescriptor() |
|
visibleAttrNames.add(fs.getSchema().getGeometryDescriptor() |
|
149 |
.getLocalName()); |
.getLocalName()); |
|
for (int attrIdx : visibleAMD.keySet()) { |
|
150 |
|
|
151 |
/** |
// Add other visible attributes as ordered by weights |
152 |
* If the user removed columns from the schema of the DBF |
for (AttributeMetadataInterface a : amdMapVisibleOnly) { |
153 |
* file, there might exist AttributeMetaData for columns |
visibleAttrNames.add(a.getLocalName()); |
|
* that don't exists. We check here to avoid an |
|
|
* ArrayOutOfIndex. |
|
|
*/ |
|
|
if (attrIdx < fs.getSchema().getAttributeCount()) { |
|
|
final AttributeDescriptor attributeTypeAtIdx = fs.getSchema() |
|
|
.getAttributeDescriptors().get(attrIdx); |
|
|
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
|
|
} else { |
|
|
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
|
|
} |
|
154 |
} |
} |
155 |
|
|
156 |
// create a query for the visible attributes |
// Tested with 2.6.x trunk from 2009-11-26 and it now works. So |
157 |
String[] properties = visibleAttrNames.toArray(new String[0]); |
// we only request the properties we need! |
158 |
|
// /** |
159 |
LOGGER.debug("Query contains the following attributes: " |
// * I got NPEs when properties contained only [the_geom] |
160 |
+ visibleAttrNames); |
// ?!??!!?? |
161 |
|
// */ |
162 |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
// if (properties.length > 1) { |
163 |
properties); |
query = new DefaultQuery(schema.getTypeName(), filter, |
164 |
|
visibleAttrNames.toArray(new String[] {})); |
165 |
|
// } else { |
166 |
|
// query = new DefaultQuery(schema.getTypeName(), filter); |
167 |
|
// } |
168 |
} |
} |
169 |
fc = fs.getFeatures(query); |
fc = fs.getFeatures(query); |
|
|
|
|
// FAILS:!!!, even with query = new |
|
|
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
|
|
// java.lang.UnsupportedOperationException: Unknown feature |
|
|
// attribute: PQM_MOD |
|
|
// at |
|
|
// schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93) |
|
|
// bounds = fc.getBounds(); |
|
|
// SK, 17.4.2009 |
|
|
// |
|
|
// System.out.println("Filter = "+filter); |
|
|
// System.out.println("Size of FC = "+fc.size()); |
|
|
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
|
170 |
} |
} |
171 |
setFeatureCollection(fc); |
setFeatureCollection(fc); |
172 |
} |
} |
178 |
* @param fs |
* @param fs |
179 |
* the feature source |
* the feature source |
180 |
* @param amd |
* @param amd |
181 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetadataImpl}-Map to define the visible |
182 |
* and translation |
* attributes and translation |
183 |
*/ |
*/ |
184 |
public void setFeatureCollection(StyledFeaturesInterface layer, |
public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) { |
|
Filter filter) { |
|
|
this.layer = layer; |
|
185 |
try { |
try { |
186 |
if (layer == null) |
if (styledFeatures == null) |
187 |
setFeatureSource(null, null, null); |
setFeatureSource(null, null, null); |
188 |
else { |
else { |
189 |
FeatureCollection fc = layer.getFeatureCollection(); |
setFeatureSource(styledFeatures.getFeatureSource(), |
190 |
String fcName = fc.getSchema().getName().getLocalPart(); |
styledFeatures.getAttributeMetaDataMap(), |
191 |
FeatureSource fs = new MemoryDataStore(fc) |
styledFeatures.getFilter()); |
|
.getFeatureSource(fcName); |
|
|
setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter); |
|
192 |
} |
} |
193 |
} catch (Exception err) { |
} catch (Exception err) { |
194 |
throw new RuntimeException(err); |
throw new RuntimeException(err); |
196 |
} |
} |
197 |
|
|
198 |
/** |
/** |
|
* Sets the {@code StyledFeatureCollection} as new data source for the |
|
|
* table. |
|
|
* |
|
|
* @param fs |
|
|
* the feature source |
|
|
* @param amd |
|
|
* {@link AttributeMetaData}-Map to define the visible attributes |
|
|
* and translation |
|
|
*/ |
|
|
public void setFeatureCollection(StyledFeatureSourceInterface layer, |
|
|
Filter filter) { |
|
|
this.layer = layer; |
|
|
try { |
|
|
if (layer == null) |
|
|
setFeatureSource(null, null, null); |
|
|
else |
|
|
setFeatureSource(layer.getGeoObject(), layer |
|
|
.getAttributeMetaDataMap(), filter); |
|
|
} catch (Exception err) { |
|
|
throw new RuntimeException(err); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Resets the filter for the table. |
|
|
* |
|
|
* @param filter |
|
|
* a filter |
|
|
*/ |
|
|
public void setFilter(Filter filter) { |
|
|
try { |
|
|
setFeatureSource(this.featureSource, this.origAMD, filter); |
|
|
} catch (Exception err) { |
|
|
LOGGER.error("Setting the filter of the table model", err); |
|
|
throw new RuntimeException(err); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
|
|
* Features |
|
|
*/ |
|
|
public Filter getFilter() { |
|
|
return this.filter; |
|
|
} |
|
|
|
|
|
/** |
|
199 |
* After calling {@code super.reorganize(.)} this method replaced the column |
* After calling {@code super.reorganize(.)} this method replaced the column |
200 |
* descriptions with the titles of the {@code AttributeMetaData}. |
* descriptions with the titles of the {@code AttributeMetaData}. |
201 |
* |
* |
204 |
*/ |
*/ |
205 |
@Override |
@Override |
206 |
protected void reorganize(boolean fireTableStructureChanged) { |
protected void reorganize(boolean fireTableStructureChanged) { |
207 |
super.reorganize(false); |
|
208 |
// translate the column names |
featureArray = FeatureUtil.featuresToArray(featureTable); |
209 |
if (visibleAMD != null) { |
if (featureArray == null || featureArray.length == 0) { |
210 |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
colNames = new String[0]; |
211 |
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
colTooltips = new String[0]; // Only set and used in |
212 |
Translation title = visibleAMD.get(keys.next()).getTitle(); |
// StyledFeatureCollectionTableModel |
213 |
if (!I8NUtil.isEmpty(title)) { |
colClass = new Class[0]; |
214 |
// System.out.println("set colname " + i + " to " + |
} else { |
215 |
// title.toString()); |
// Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen |
216 |
colNames[i] = title.toString(); |
SimpleFeatureType schema = featureArray[0].getFeatureType(); |
217 |
} |
// Pruefen, welche Attribute angezeigt werden |
218 |
|
attrTypes.clear(); |
219 |
|
for (AttributeMetadataInterface amd : amdMapVisibleOnly) { |
220 |
|
Name name = amd.getName(); |
221 |
|
AttributeDescriptor type = schema.getDescriptor(name); |
222 |
|
// if type can not be determined by complete name, |
223 |
|
// try only the local name |
224 |
|
if (type == null) |
225 |
|
type = schema.getDescriptor(name.getLocalPart()); |
226 |
|
if (attrFilter == null || attrFilter.accept(type)) |
227 |
|
attrTypes.add(type); |
228 |
|
} |
229 |
|
// Namen und Attribut-Indizes der angezeigten Spalten ermitteln |
230 |
|
colNames = new String[attrTypes.size()]; |
231 |
|
colTooltips = new String[attrTypes.size()]; // Only set and used in |
232 |
|
// StyledFeatureCollectionTableModel |
233 |
|
colClass = new Class[attrTypes.size()]; |
234 |
|
attrIdxForCol = new int[attrTypes.size()]; |
235 |
|
for (int i = 0; i < colNames.length; i++) { |
236 |
|
Name name = amdMapVisibleOnly.get(i).getName(); |
237 |
|
AttributeDescriptor descriptor = schema.getDescriptor(name); |
238 |
|
// if type can not be determined by complete name, |
239 |
|
// try only the local name |
240 |
|
if (descriptor == null) |
241 |
|
descriptor = schema.getDescriptor(name.getLocalPart()); |
242 |
|
|
243 |
|
// Not so nice in 26: find the index of an attribute... |
244 |
|
int idx = schema.getAttributeDescriptors().indexOf(descriptor); |
245 |
|
attrIdxForCol[i] = idx; |
246 |
|
|
247 |
|
String attName = schema.getAttributeDescriptors().get(idx) |
248 |
|
.getLocalName(); |
249 |
|
colNames[i] = amdMap.get(attName).getTitle().toString(); |
250 |
|
AttributeMetadataInterface amd = amdMap.get(attName); |
251 |
|
colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>" |
252 |
|
+ amd.getName() + "</html>"; |
253 |
|
colClass[i] = schema.getAttributeDescriptors().get(idx) |
254 |
|
.getType().getBinding(); |
255 |
} |
} |
256 |
} |
} |
257 |
|
|
258 |
|
// store feature indexes in HashMap to optimize findFeature(.) |
259 |
|
featureIdx = new HashMap<String, Integer>(); |
260 |
|
for (int i = 0; i < featureArray.length; i++) |
261 |
|
if (featureArray[i] != null) |
262 |
|
featureIdx.put(featureArray[i].getID(), i); |
263 |
|
// |
264 |
|
// // translate the column names |
265 |
|
// if (amdMap != null) { |
266 |
|
// for (int i = 0; i < colNames.length; i++) { |
267 |
|
// colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString() |
268 |
|
// + "<br>" + colNames[i]; |
269 |
|
// colNames[i] = amdMap.get(colNames[i]).getTitle().toString(); |
270 |
|
// |
271 |
|
// } |
272 |
|
// } |
273 |
if (fireTableStructureChanged) |
if (fireTableStructureChanged) |
274 |
fireTableStructureChanged(); |
fireTableStructureChanged(); |
275 |
|
|
276 |
} |
} |
277 |
|
|
278 |
/** |
/** |