30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
32 |
import java.io.File; |
import java.io.File; |
|
import java.io.FileNotFoundException; |
|
33 |
import java.io.IOException; |
import java.io.IOException; |
34 |
import java.net.URL; |
import java.net.URL; |
35 |
import java.util.Date; |
import java.util.Date; |
42 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
43 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
44 |
import org.geotools.feature.NameImpl; |
import org.geotools.feature.NameImpl; |
45 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
46 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
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; |
62 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
* This class enables a non Atlas context to use the Atlas LayerPanel |
63 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
* {@link JPanel} as a {@link MapContextManagerInterface} |
64 |
* |
* |
65 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
66 |
* |
* |
67 |
* TODO Rename to StyledShapefile |
* TODO Rename to StyledShapefile |
68 |
*/ |
*/ |
78 |
* 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 |
79 |
* than it should be ;-) |
* than it should be ;-) |
80 |
*/ |
*/ |
81 |
private String id; |
final private String id; |
82 |
|
|
83 |
private Style style; |
private Style style; |
84 |
|
|
89 |
private File sldFile; |
private File sldFile; |
90 |
|
|
91 |
/** A map of simple attribute names to their meta-data **/ |
/** A map of simple attribute names to their meta-data **/ |
92 |
private AttributeMetadataMap<AttributeMetadataImpl> map; |
private AttributeMetadataMap<AttributeMetadataImpl> attMap; |
93 |
|
|
94 |
private Filter filter = Filter.INCLUDE; |
private Filter filter = Filter.INCLUDE; |
95 |
|
|
103 |
* @param sldFile |
* @param sldFile |
104 |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
105 |
* import and associate with this {@link StyledFS} |
* import and associate with this {@link StyledFS} |
106 |
|
* |
107 |
|
* @param id |
108 |
|
* <code>null</code> is allowed and will autogenerate an id |
109 |
*/ |
*/ |
110 |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
111 |
File sldFile) { |
File sldFile, String id) { |
112 |
|
|
113 |
this.fs = fs; |
this.fs = fs; |
114 |
|
|
115 |
id = StyledFS.class.getSimpleName() |
if (id == null) { |
116 |
+ new Random(new Date().getTime()).nextInt(10000000); |
this.id = StyledFS.class.getSimpleName() |
117 |
|
+ new Random(new Date().getTime()).nextInt(10000000); |
118 |
|
} else { |
119 |
|
this.id = id; |
120 |
|
} |
121 |
|
|
122 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
123 |
|
|
142 |
} |
} |
143 |
|
|
144 |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
145 |
this(fs, null); |
this(fs, (File) null, null); |
146 |
|
} |
147 |
|
|
148 |
|
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
149 |
|
String id) { |
150 |
|
this(fs, null, id); |
151 |
} |
} |
152 |
|
|
153 |
public void dispose() { |
public void dispose() { |
170 |
public CoordinateReferenceSystem getCrs() { |
public CoordinateReferenceSystem getCrs() { |
171 |
if (crs == null) { |
if (crs == null) { |
172 |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
173 |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
if (crs == null) { |
174 |
LOGGER.warn("Could not determine the CRS of " + getTitle() |
|
175 |
+ ". Using default " + GeoImportUtil.getDefaultCRS()); |
crs = fs.getSchema().getGeometryDescriptor() |
176 |
crs = GeoImportUtil.getDefaultCRS(); |
.getCoordinateReferenceSystem(); |
177 |
|
|
178 |
|
if (crs == null) { |
179 |
|
LOGGER.warn("Could not determine the CRS of " + getTitle() |
180 |
|
+ ". Using default " |
181 |
|
+ GeoImportUtil.getDefaultCRS()); |
182 |
|
crs = GeoImportUtil.getDefaultCRS(); |
183 |
|
} |
184 |
} |
} |
185 |
} |
} |
186 |
return crs; |
return crs; |
272 |
/** |
/** |
273 |
* |
* |
274 |
*/ |
*/ |
275 |
|
@Override |
276 |
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
277 |
if (map == null) { |
if (attMap == null) { |
278 |
|
attMap = StyledLayerUtil |
279 |
map = new AttributeMetadataImplMap(); |
.createDefaultAttributeMetadataMap(getSchema()); |
|
|
|
|
// // Leaving out the first one, it will be the_geom |
|
|
// for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
|
|
// AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
|
|
// |
|
|
// AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
|
|
// new NameImpl(attDesc |
|
|
// .getName().getNamespaceURI(), attDesc |
|
|
// .getName().getLocalPart()), map.getLanguages()); |
|
|
// map.put(attDesc.getName(), attMetaData); |
|
|
// } |
|
|
|
|
|
// Leaving out the first one, it will be the_geom |
|
|
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
|
|
AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
|
|
|
|
|
// TODO AttributeMetadataAS would be nicer, which would not work |
|
|
// with Translations ;-) |
|
|
AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
|
|
new NameImpl(attDesc.getName().getNamespaceURI(), |
|
|
attDesc.getName().getLocalPart()), map |
|
|
.getLanguages()); |
|
|
if (String.class.isAssignableFrom(attDesc.getType() |
|
|
.getBinding())) { |
|
|
// For Strings we add the "" as NODATA values |
|
|
attMetaData.addNodataValue(""); |
|
|
} |
|
|
map.put(attDesc.getName(), attMetaData); |
|
|
} |
|
280 |
} |
} |
281 |
return map; |
return attMap; |
282 |
} |
} |
283 |
|
|
284 |
/** |
/** |
285 |
* @return The {@link File} where the SLD was loaded from or |
* @return The {@link File} where the SLD was loaded from or |
286 |
* <code>null</code> if there didn't exist a {@link File}. |
* <code>null</code> if there didn't exist a {@link File}. |
287 |
* |
* |
288 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
|
* Krüger</a> |
|
289 |
*/ |
*/ |
290 |
public File getSldFile() { |
public File getSldFile() { |
291 |
return sldFile; |
return sldFile; |
292 |
} |
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Associates this .sld with the {@link FeatureSource}, but does not |
296 |
|
* automatically load the file. It must not even exist. |
297 |
|
* |
298 |
|
* @param sldFile |
299 |
|
*/ |
300 |
public void setSldFile(File sldFile) { |
public void setSldFile(File sldFile) { |
301 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
302 |
} |
} |
340 |
// return fc.subCollection(filter); |
// return fc.subCollection(filter); |
341 |
|
|
342 |
try { |
try { |
343 |
return getFeatureSource().getFeatures(filter); |
if (filter != Filter.INCLUDE) |
344 |
|
return getFeatureSource().getFeatures(filter); |
345 |
|
else |
346 |
|
return getFeatureSource().getFeatures(); |
347 |
} catch (IOException e) { |
} catch (IOException e) { |
348 |
throw new RuntimeException(e); |
throw new RuntimeException(e); |
349 |
} |
} |
367 |
/** |
/** |
368 |
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
369 |
* the file doesn't exits, return <code>null</code>; |
* the file doesn't exits, return <code>null</code>; |
370 |
|
* |
371 |
* @return <code>true</code> is style was loaded |
* @return <code>true</code> is style was loaded |
372 |
*/ |
*/ |
373 |
public boolean loadStyle() { |
public boolean loadStyle() { |
374 |
if (getSldFile() == null) |
if (getSldFile() == null) |
375 |
return false; |
return false; |
376 |
|
|
377 |
try { |
try { |
378 |
Style[] loadSLD = StylingUtil.loadSLD(getSldFile()); |
Style[] loadSLD = StylingUtil.loadSLD(getSldFile()); |
379 |
setStyle(loadSLD[0]); |
setStyle(loadSLD[0]); |
384 |
|
|
385 |
} |
} |
386 |
|
|
387 |
|
public void setTitle(String title) { |
388 |
|
setTitle(new Translation(title)); |
389 |
|
} |
390 |
|
|
391 |
|
public void setDesc(String desc) { |
392 |
|
setDesc(new Translation(desc)); |
393 |
|
} |
394 |
|
|
395 |
|
public void setCRS(CoordinateReferenceSystem crs2) { |
396 |
|
crs = crs2; |
397 |
|
} |
398 |
|
|
399 |
|
@Override |
400 |
|
public ReferencedEnvelope getReferencedEnvelope() { |
401 |
|
return new ReferencedEnvelope(getEnvelope(), getCrs()); |
402 |
|
} |
403 |
|
|
404 |
} |
} |