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; |
42 |
|
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. */ |
/** Contains the complete {@link AttributeMetadataImpl}-Map of the styled layer. */ |
72 |
protected StyledLayerInterface<?> layer = null; |
protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null; |
|
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
|
|
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
|
|
/** Holds the data source for the table as {@code FeatureSource}. */ |
|
|
protected FeatureSource featureSource = null; |
|
|
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
|
|
protected Map<Integer, AttributeMetaData> origAMD = null; |
|
73 |
/** Holds the current filter on the table */ |
/** Holds the current filter on the table */ |
74 |
protected Filter filter = null; |
protected Filter filter = null; |
75 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
76 |
protected Envelope bounds; |
protected Envelope bounds; |
|
|
|
|
|
|
|
|
|
77 |
/** |
/** |
78 |
* Creates a new table model for a styled layer. |
* Tooltips für die Spaltennamen. Wird nur beim Aufruf von |
79 |
* |
* {@link #reorganize} befuellt. |
|
* @param layer |
|
|
* the styled layer |
|
80 |
*/ |
*/ |
81 |
public StyledFeatureCollectionTableModel( |
protected String[] colTooltips = null; |
82 |
StyledFeatureCollectionInterface layer) { |
|
83 |
this(layer, Filter.INCLUDE); |
/** A cache for the #sortedValuesVisibleOnly() **/ |
84 |
} |
protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null; |
85 |
|
|
86 |
/** |
/** |
87 |
* Creates a new table model for a styled layer. |
* Creates a new table model for a styled layer. |
88 |
* |
* |
89 |
* @param layer |
* @param styledFeatures |
90 |
* the styled layer |
* the styled layer |
91 |
* @param filter |
* @param filter |
92 |
* filter applied to the table |
* filter applied to the table |
93 |
*/ |
*/ |
94 |
public StyledFeatureCollectionTableModel( |
public StyledFeatureCollectionTableModel( |
95 |
StyledFeatureCollectionInterface layer, Filter filter) { |
StyledFeaturesInterface<?> styledFeatures) { |
96 |
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); |
|
97 |
} |
} |
98 |
|
|
99 |
/** |
/** |
100 |
* Creates a new table model for a styled layer. |
* This overwritten method filters the values for NODATA-values defined in |
101 |
* |
* the {@link AttributeMetadataImpl} |
|
* @param layer |
|
|
* the styled layer |
|
|
* @param filter |
|
|
* filter applied to the table |
|
102 |
*/ |
*/ |
103 |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer, |
@Override |
104 |
Filter filter) { |
public Object getValueAt(int row, int col) { |
105 |
super(); |
Object rawValue = super.getValueAt(row, col); |
106 |
setFeatureCollection(layer, filter); |
return amdMap.sortedValuesVisibleOnly().get(col).fiterNodata(rawValue); |
107 |
} |
} |
108 |
|
|
109 |
/** |
/** |
110 |
* Sets a new data source for the table. |
* Sets a new data source for the table. |
111 |
* |
* |
112 |
* @param fs |
* @param fs |
113 |
* the feature source |
* the feature source |
114 |
* @param amd |
* @param amdm |
115 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetadataImpl}-Map to define the visible attributes |
116 |
* and translation |
* and translation |
117 |
*/ |
*/ |
118 |
protected void setFeatureSource(FeatureSource fs, |
protected void setFeatureSource( |
119 |
Map<Integer, AttributeMetaData> amd, Filter filter) |
FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
120 |
throws Exception { |
AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, Filter filter) throws Exception { |
121 |
|
|
122 |
if (filter == null) |
if (filter == null) |
123 |
filter = Filter.INCLUDE; |
filter = Filter.INCLUDE; |
124 |
|
|
125 |
this.featureSource = fs; |
// this.featureSource = fs; |
126 |
this.filter = filter; |
this.filter = filter; |
127 |
this.origAMD = amd; |
this.amdMap = amdm; |
128 |
this.visibleAMD = null; |
this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly(); |
129 |
|
|
130 |
FeatureCollection fc = null; |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null; |
131 |
if (fs != null) { |
if (fs != null) { |
132 |
|
|
133 |
bounds = fs.getBounds(); |
bounds = fs.getBounds(); |
134 |
|
|
135 |
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
final SimpleFeatureType schema = fs.getSchema(); |
136 |
if (amd != null) { |
Query query = new DefaultQuery(schema.getTypeName(), filter); |
137 |
// determine the names of the visible Attributes |
if (amdm != null) { |
138 |
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>(); |
139 |
amd, true); |
|
140 |
Vector<String> visibleAttrNames = new Vector<String>(); |
// Add the column with the geometry (usually "the_geom") always |
141 |
// Add the column with the geometry (usually "the_geom") |
String geomColumnLocalName = schema.getGeometryDescriptor() |
142 |
visibleAttrNames.add(fs.getSchema().getDefaultGeometry() |
.getLocalName(); |
143 |
.getLocalName()); |
visibleAttrNames.add(geomColumnLocalName); |
144 |
for (int attrIdx : visibleAMD.keySet()) { |
|
145 |
|
// Add other visible attributes as ordered by weights |
146 |
/** |
for (AttributeMetadataInterface a : amdMapVisibleOnly) { |
147 |
* If the user removed columns from the schema of the DBF |
visibleAttrNames.add(a.getLocalName()); |
|
* file, there might exist AttributeMetaData for columns |
|
|
* that don't exists. We check here to avoid an |
|
|
* ArrayOutOfIndex. |
|
|
*/ |
|
|
if (attrIdx < fs.getSchema().getAttributeCount()) { |
|
|
final AttributeDescriptor attributeTypeAtIdx = fs.getSchema() |
|
|
.getAttributeType(attrIdx); |
|
|
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
|
|
} else { |
|
|
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
|
|
} |
|
148 |
} |
} |
149 |
|
|
150 |
// create a query for the visible attributes |
// Tested with 2.6.x trunk from 2009-11-26 and it now works. So |
151 |
String[] properties = visibleAttrNames.toArray(new String[0]); |
// we only request the properties we need! |
152 |
|
// /** |
153 |
LOGGER.debug("Query contains the following attributes: " |
// * I got NPEs when properties contained only [the_geom] |
154 |
+ visibleAttrNames); |
// ?!??!!?? |
155 |
|
// */ |
156 |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
// if (properties.length > 1) { |
157 |
properties); |
query = new DefaultQuery(schema.getTypeName(), filter, |
158 |
|
visibleAttrNames.toArray(new String[] {})); |
159 |
|
// } else { |
160 |
|
// query = new DefaultQuery(schema.getTypeName(), filter); |
161 |
|
// } |
162 |
|
|
163 |
|
System.out.println(query.getPropertyNames()); |
164 |
} |
} |
165 |
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()); |
|
166 |
} |
} |
167 |
setFeatureCollection(fc); |
setFeatureCollection(fc); |
168 |
} |
} |
174 |
* @param fs |
* @param fs |
175 |
* the feature source |
* the feature source |
176 |
* @param amd |
* @param amd |
177 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetadataImpl}-Map to define the visible attributes |
178 |
* and translation |
* and translation |
179 |
*/ |
*/ |
180 |
public void setFeatureCollection(StyledFeaturesInterface layer, |
public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) { |
|
Filter filter) { |
|
|
this.layer = layer; |
|
181 |
try { |
try { |
182 |
if (layer == null) |
if (styledFeatures == null) |
183 |
setFeatureSource(null, null, null); |
setFeatureSource(null, null, null); |
184 |
else { |
else { |
185 |
FeatureCollection fc = layer.getFeatureCollection(); |
setFeatureSource(styledFeatures.getFeatureSource(), |
186 |
String fcName = fc.getSchema().getTypeName(); |
styledFeatures.getAttributeMetaDataMap(), |
187 |
FeatureSource fs = new MemoryDataStore(fc) |
styledFeatures.getFilter()); |
|
.getFeatureSource(fcName); |
|
|
setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter); |
|
188 |
} |
} |
189 |
} catch (Exception err) { |
} catch (Exception err) { |
190 |
throw new RuntimeException(err); |
throw new RuntimeException(err); |
192 |
} |
} |
193 |
|
|
194 |
/** |
/** |
|
* 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; |
|
|
} |
|
|
|
|
|
/** |
|
195 |
* After calling {@code super.reorganize(.)} this method replaced the column |
* After calling {@code super.reorganize(.)} this method replaced the column |
196 |
* descriptions with the titles of the {@code AttributeMetaData}. |
* descriptions with the titles of the {@code AttributeMetaData}. |
197 |
* |
* |
200 |
*/ |
*/ |
201 |
@Override |
@Override |
202 |
protected void reorganize(boolean fireTableStructureChanged) { |
protected void reorganize(boolean fireTableStructureChanged) { |
203 |
super.reorganize(false); |
|
204 |
// translate the column names |
featureArray = FeatureUtil.featuresToArray(featureTable); |
205 |
if (visibleAMD != null) { |
if (featureArray == null || featureArray.length == 0) { |
206 |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
colNames = new String[0]; |
207 |
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
colTooltips = new String[0]; // Only set and used in |
208 |
Translation title = visibleAMD.get(keys.next()).getTitle(); |
// StyledFeatureCollectionTableModel |
209 |
if (!I8NUtil.isEmpty(title)) { |
colClass = new Class[0]; |
210 |
// System.out.println("set colname " + i + " to " + |
} else { |
211 |
// title.toString()); |
// Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen |
212 |
colNames[i] = title.toString(); |
SimpleFeatureType schema = featureArray[0].getFeatureType(); |
213 |
} |
// Pruefen, welche Attribute angezeigt werden |
214 |
|
attrTypes.clear(); |
215 |
|
for (AttributeMetadataInterface amd : amdMapVisibleOnly) { |
216 |
|
Name name = amd.getName(); |
217 |
|
AttributeDescriptor type = schema.getDescriptor(name); |
218 |
|
// if type can not be determined by complete name, |
219 |
|
// try only the local name |
220 |
|
if ( type == null ) |
221 |
|
type = schema.getDescriptor(name.getLocalPart()); |
222 |
|
if (attrFilter == null || attrFilter.accept(type)) |
223 |
|
attrTypes.add(type); |
224 |
|
} |
225 |
|
// Namen und Attribut-Indizes der angezeigten Spalten ermitteln |
226 |
|
colNames = new String[attrTypes.size()]; |
227 |
|
colTooltips = new String[attrTypes.size()]; // Only set and used in |
228 |
|
// StyledFeatureCollectionTableModel |
229 |
|
colClass = new Class[attrTypes.size()]; |
230 |
|
attrIdxForCol = new int[attrTypes.size()]; |
231 |
|
for (int i = 0; i < colNames.length; i++) { |
232 |
|
Name name = amdMapVisibleOnly.get(i).getName(); |
233 |
|
AttributeDescriptor descriptor = schema.getDescriptor(name); |
234 |
|
// if type can not be determined by complete name, |
235 |
|
// try only the local name |
236 |
|
if ( descriptor == null ) |
237 |
|
descriptor = schema.getDescriptor(name.getLocalPart()); |
238 |
|
|
239 |
|
// Not so nice in 26: find the index of an attribute... |
240 |
|
int idx = schema.getAttributeDescriptors().indexOf(descriptor); |
241 |
|
attrIdxForCol[i] = idx; |
242 |
|
|
243 |
|
String attName = schema.getAttributeDescriptors().get(idx) |
244 |
|
.getLocalName(); |
245 |
|
colNames[i] = amdMap.get(attName).getTitle().toString(); |
246 |
|
AttributeMetadataInterface amd = amdMap.get(attName); |
247 |
|
colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>" |
248 |
|
+ amd.getName() + "</html>"; |
249 |
|
colClass[i] = schema.getAttributeDescriptors().get(idx) |
250 |
|
.getType().getBinding(); |
251 |
} |
} |
252 |
} |
} |
253 |
|
|
254 |
|
// store feature indexes in HashMap to optimize findFeature(.) |
255 |
|
featureIdx = new HashMap<String, Integer>(); |
256 |
|
for (int i = 0; i < featureArray.length; i++) |
257 |
|
if (featureArray[i] != null) |
258 |
|
featureIdx.put(featureArray[i].getID(), i); |
259 |
|
// |
260 |
|
// // translate the column names |
261 |
|
// if (amdMap != null) { |
262 |
|
// for (int i = 0; i < colNames.length; i++) { |
263 |
|
// colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString() |
264 |
|
// + "<br>" + colNames[i]; |
265 |
|
// colNames[i] = amdMap.get(colNames[i]).getTitle().toString(); |
266 |
|
// |
267 |
|
// } |
268 |
|
// } |
269 |
if (fireTableStructureChanged) |
if (fireTableStructureChanged) |
270 |
fireTableStructureChanged(); |
fireTableStructureChanged(); |
271 |
|
|
272 |
} |
} |
273 |
|
|
274 |
/** |
/** |