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 |
|
|
38 |
|
|
39 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
40 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
|
import javax.xml.transform.TransformerException; |
|
41 |
|
|
42 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
43 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
44 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
45 |
|
import org.geotools.feature.NameImpl; |
46 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
|
import org.jfree.util.AttributedStringUtilities; |
|
47 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
48 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
49 |
import org.opengis.feature.type.AttributeDescriptor; |
import org.opengis.feature.type.AttributeDescriptor; |
52 |
|
|
53 |
import schmitzm.geotools.io.GeoImportUtil; |
import schmitzm.geotools.io.GeoImportUtil; |
54 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
55 |
import skrueger.AttributeMetadata; |
import skrueger.AttributeMetadataImpl; |
56 |
|
import skrueger.AttributeMetadataInterface; |
57 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
58 |
|
|
59 |
import com.vividsolutions.jts.geom.Envelope; |
import com.vividsolutions.jts.geom.Envelope; |
64 |
* |
* |
65 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
66 |
* |
* |
67 |
* TODO Rename to StyledShapefile |
* TODO Rename to StyledShapefile |
68 |
*/ |
*/ |
69 |
public class StyledFS implements StyledFeatureSourceInterface { |
public class StyledFS implements StyledFeatureSourceInterface { |
70 |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
71 |
|
|
72 |
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
73 |
|
|
74 |
/** Caching the CRS of the layer **/ |
/** Caching the CRS of the layer **/ |
75 |
CoordinateReferenceSystem crs = null; |
CoordinateReferenceSystem crs = null; |
76 |
|
|
88 |
|
|
89 |
private File sldFile; |
private File sldFile; |
90 |
|
|
91 |
private AttributeMetadataMap map; |
/** A map of simple attribute names to their meta-data **/ |
92 |
|
private AttributeMetadataMap<AttributeMetadataImpl> map; |
93 |
|
|
94 |
private Filter filter = Filter.INCLUDE; |
private Filter filter = Filter.INCLUDE; |
95 |
|
|
157 |
|
|
158 |
public CoordinateReferenceSystem getCrs() { |
public CoordinateReferenceSystem getCrs() { |
159 |
if (crs == null) { |
if (crs == null) { |
160 |
crs = fs.getSchema() |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
|
.getCoordinateReferenceSystem(); |
|
161 |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
162 |
LOGGER.warn("Could not determine the CRS of " + getTitle() |
LOGGER.warn("Could not determine the CRS of " + getTitle() |
163 |
+ ". Using default " + GeoImportUtil.getDefaultCRS()); |
+ ". Using default " + GeoImportUtil.getDefaultCRS()); |
253 |
/** |
/** |
254 |
* |
* |
255 |
*/ |
*/ |
256 |
public AttributeMetadataMap getAttributeMetaDataMap() { |
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
257 |
if (map == null) { |
if (map == null) { |
258 |
|
|
259 |
map = new AttributeMetadataMap(new String[] { Translation |
map = new AttributeMetadataImplMap(); |
260 |
.getActiveLang() }); |
|
261 |
|
// // Leaving out the first one, it will be the_geom |
262 |
|
// for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
263 |
|
// AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
264 |
|
// |
265 |
|
// AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
266 |
|
// new NameImpl(attDesc |
267 |
|
// .getName().getNamespaceURI(), attDesc |
268 |
|
// .getName().getLocalPart()), map.getLanguages()); |
269 |
|
// map.put(attDesc.getName(), attMetaData); |
270 |
|
// } |
271 |
|
|
272 |
// Leaving out the first one, it will be the_geom |
// Leaving out the first one, it will be the_geom |
273 |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
274 |
AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
275 |
|
|
276 |
AttributeMetadata attMetaData = new AttributeMetadata(attDesc |
// TODO AttributeMetadataAS would be nicer, which would not work |
277 |
.getName(), map.getLanguages()); |
// with Translations ;-) |
278 |
|
AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
279 |
|
new NameImpl(attDesc.getName().getNamespaceURI(), |
280 |
|
attDesc.getName().getLocalPart()), map |
281 |
|
.getLanguages()); |
282 |
|
if (String.class.isAssignableFrom(attDesc.getType() |
283 |
|
.getBinding())) { |
284 |
|
// For Strings we add the "" as NODATA values |
285 |
|
attMetaData.addNodataValue(""); |
286 |
|
} |
287 |
map.put(attDesc.getName(), attMetaData); |
map.put(attDesc.getName(), attMetaData); |
288 |
} |
} |
289 |
} |
} |
300 |
public File getSldFile() { |
public File getSldFile() { |
301 |
return sldFile; |
return sldFile; |
302 |
} |
} |
303 |
|
|
304 |
public void setSldFile(File sldFile) { |
public void setSldFile(File sldFile) { |
305 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
306 |
} |
} |
365 |
return getGeoObject().getSchema(); |
return getGeoObject().getSchema(); |
366 |
} |
} |
367 |
|
|
368 |
|
/** |
369 |
|
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
370 |
|
* the file doesn't exits, return <code>null</code>; |
371 |
|
* @return <code>true</code> is style was loaded |
372 |
|
*/ |
373 |
|
public boolean loadStyle() { |
374 |
|
if (getSldFile() == null) |
375 |
|
return false; |
376 |
|
|
377 |
|
try { |
378 |
|
Style[] loadSLD = StylingUtil.loadSLD(getSldFile()); |
379 |
|
setStyle(loadSLD[0]); |
380 |
|
return true; |
381 |
|
} catch (Exception e) { |
382 |
|
return false; |
383 |
|
} |
384 |
|
|
385 |
|
} |
386 |
|
|
387 |
} |
} |