2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
34 |
import java.io.IOException; |
import java.io.IOException; |
35 |
import java.net.URL; |
import java.net.URL; |
36 |
import java.util.Date; |
import java.util.Date; |
|
import java.util.HashMap; |
|
|
import java.util.Map; |
|
37 |
import java.util.Random; |
import java.util.Random; |
38 |
|
|
39 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
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.AttributeType; |
import org.geotools.data.store.EmptyFeatureCollection; |
45 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
46 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
47 |
|
import org.opengis.feature.simple.SimpleFeature; |
48 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
49 |
|
import org.opengis.feature.type.AttributeDescriptor; |
50 |
|
import org.opengis.filter.Filter; |
51 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
52 |
|
|
53 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
65 |
public class StyledFS implements StyledFeatureSourceInterface { |
public class StyledFS implements StyledFeatureSourceInterface { |
66 |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
67 |
|
|
68 |
private final FeatureSource fs; |
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
69 |
|
|
70 |
/** |
/** |
71 |
* A unique ID which identifies the Layer in the Atlas. It's more important |
* A unique ID which identifies the Layer in the Atlas. It's more important |
81 |
|
|
82 |
private File sldFile; |
private File sldFile; |
83 |
|
|
84 |
private HashMap<Integer, AttributeMetaData> map; |
private AttributeMetadataMap map; |
85 |
|
|
86 |
|
private Filter filter = Filter.INCLUDE; |
87 |
|
|
88 |
/** |
/** |
89 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
* This class enables a non Atlas context to use the Atlas LayerPanel |
96 |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
97 |
* import and associate with this {@link StyledFS} |
* import and associate with this {@link StyledFS} |
98 |
*/ |
*/ |
99 |
public StyledFS(FeatureSource fs, File sldFile) { |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
100 |
|
File sldFile) { |
101 |
|
|
102 |
this.fs = fs; |
this.fs = fs; |
103 |
id = StyledFS.class.getSimpleName() |
id = StyledFS.class.getSimpleName() |
105 |
|
|
106 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
107 |
|
|
108 |
if ((sldFile != null) && (sldFile.exists())) { |
if (sldFile != null && sldFile.exists()) { |
109 |
try { |
try { |
110 |
style = StylingUtil.loadSLD(sldFile)[0]; |
style = StylingUtil.loadSLD(sldFile)[0]; |
111 |
} catch (FileNotFoundException e) { |
} catch (FileNotFoundException e) { |
116 |
} |
} |
117 |
} |
} |
118 |
title = new Translation(); |
title = new Translation(); |
|
title.fromOneLine(sldFile.getName()); |
|
|
|
|
119 |
desc = new Translation(); |
desc = new Translation(); |
120 |
desc.fromOneLine(sldFile.getAbsolutePath()); |
|
121 |
|
if (sldFile != null) { |
122 |
|
title.fromOneLine(sldFile.getName()); |
123 |
|
desc.fromOneLine(sldFile.getAbsolutePath()); |
124 |
|
} |
125 |
|
|
126 |
|
} |
127 |
|
|
128 |
|
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
129 |
|
this(fs, null); |
130 |
} |
} |
131 |
|
|
132 |
public void dispose() { |
public void dispose() { |
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
136 |
* Returnes human readable {@link String} of the CRS natively used by this |
* Returns human readable {@link String} of the CRS natively used by this |
137 |
* {@link DpLayer} |
* {@link DpLayer} |
138 |
* |
* |
139 |
* If crs == null, it will call {@link #getGeoObject()} |
* If CRS == null, it will call {@link #getGeoObject()} |
140 |
* |
* |
141 |
*/ |
*/ |
142 |
public String getCRSString() { |
public String getCRSString() { |
147 |
} |
} |
148 |
|
|
149 |
public CoordinateReferenceSystem getCrs() { |
public CoordinateReferenceSystem getCrs() { |
150 |
return fs.getSchema().getDefaultGeometry().getCoordinateSystem(); |
return fs.getSchema().getCoordinateReferenceSystem(); |
151 |
} |
} |
152 |
|
|
153 |
public Translation getDesc() { |
public Translation getDesc() { |
163 |
} |
} |
164 |
} |
} |
165 |
|
|
166 |
public FeatureSource getGeoObject() { |
public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() { |
167 |
return fs; |
return fs; |
168 |
} |
} |
169 |
|
|
236 |
/** |
/** |
237 |
* |
* |
238 |
*/ |
*/ |
239 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
public AttributeMetadataMap getAttributeMetaDataMap() { |
240 |
if (map == null) { |
if (map == null) { |
241 |
|
|
242 |
map = new HashMap<Integer, AttributeMetaData>(); |
map = new AttributeMetadataMap(); |
243 |
|
|
244 |
// Leaving out the first one, it will be the_geom |
// Leaving out the first one, it will be the_geom |
245 |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
246 |
AttributeType att = fs.getSchema().getAttributeType(i); |
AttributeDescriptor att = fs.getSchema().getDescriptor(i); |
247 |
|
|
248 |
AttributeMetaData attMetaData = new AttributeMetaData(i, att |
AttributeMetaData attMetaData = new AttributeMetaData(i, att |
249 |
.getLocalName()); |
.getLocalName()); |
255 |
|
|
256 |
/** |
/** |
257 |
* @return The {@link File} where the SLD was loaded from or |
* @return The {@link File} where the SLD was loaded from or |
258 |
* <code>null</code> if there didn't exist a {@link File}. |
* <code>null</code> if there didn't exist a {@link File}. |
259 |
* |
* |
260 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons |
261 |
* Krüger</a> |
* Krüger</a> |
274 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
275 |
*/ |
*/ |
276 |
@Override |
@Override |
277 |
public FeatureCollection getFeatureCollection() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() { |
278 |
FeatureCollection features; |
FeatureCollection<SimpleFeatureType, SimpleFeature> features; |
279 |
try { |
try { |
280 |
features = getGeoObject().getFeatures(); |
features = getGeoObject().getFeatures(); |
281 |
} catch (IOException e) { |
} catch (IOException e) { |
292 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
293 |
*/ |
*/ |
294 |
@Override |
@Override |
295 |
public FeatureSource getFeatureSource() { |
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() { |
296 |
return getGeoObject(); |
return getGeoObject(); |
297 |
} |
} |
298 |
|
|
299 |
|
@Override |
300 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
301 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
302 |
|
if (filter == Filter.EXCLUDE) |
303 |
|
return new EmptyFeatureCollection(fc.getSchema()); |
304 |
|
if (filter == Filter.INCLUDE) |
305 |
|
return fc; |
306 |
|
return fc.subCollection(filter); |
307 |
|
} |
308 |
|
|
309 |
|
@Override |
310 |
|
public Filter getFilter() { |
311 |
|
return filter; |
312 |
|
} |
313 |
|
|
314 |
|
@Override |
315 |
|
public void setFilter(Filter filter) { |
316 |
|
this.filter = filter; |
317 |
|
} |
318 |
|
|
319 |
|
@Override |
320 |
|
public SimpleFeatureType getSchema() { |
321 |
|
return getGeoObject().getSchema(); |
322 |
|
} |
323 |
|
|
324 |
} |
} |