1 |
package skrueger.geotools; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.net.URL; |
7 |
import java.util.Date; |
8 |
import java.util.HashMap; |
9 |
import java.util.Map; |
10 |
import java.util.Random; |
11 |
|
12 |
import javax.swing.ImageIcon; |
13 |
import javax.swing.JPanel; |
14 |
|
15 |
import org.apache.log4j.Logger; |
16 |
import org.geotools.data.FeatureSource; |
17 |
import org.geotools.feature.AttributeType; |
18 |
import org.geotools.styling.Style; |
19 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
20 |
|
21 |
import schmitzm.geotools.styling.StylingUtil; |
22 |
import skrueger.AttributeMetaData; |
23 |
import skrueger.i8n.Translation; |
24 |
|
25 |
import com.vividsolutions.jts.geom.Envelope; |
26 |
|
27 |
/** |
28 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
29 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
30 |
* |
31 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
32 |
*/ |
33 |
public class StyledFS implements StyledFeatureSourceInterface { |
34 |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
35 |
|
36 |
private final FeatureSource fs; |
37 |
|
38 |
/** |
39 |
* A unique ID which identifies the Layer in the Atlas. It's more important |
40 |
* than it should be ;-) |
41 |
*/ |
42 |
private String id; |
43 |
|
44 |
private Style style; |
45 |
|
46 |
private Translation title; |
47 |
|
48 |
private Translation desc; |
49 |
|
50 |
private File sldFile; |
51 |
|
52 |
private HashMap<Integer, AttributeMetaData> map; |
53 |
|
54 |
/** |
55 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
56 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
57 |
* |
58 |
* @param fs |
59 |
* {@link FeatureSource} that is beeing styled. |
60 |
* |
61 |
* @param sldFile |
62 |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
63 |
* import and associate with this {@link StyledFS} |
64 |
*/ |
65 |
public StyledFS(FeatureSource fs, File sldFile) { |
66 |
|
67 |
this.fs = fs; |
68 |
id = StyledFS.class.getSimpleName() |
69 |
+ new Random(new Date().getTime()).nextInt(10000000); |
70 |
|
71 |
this.sldFile = sldFile; |
72 |
|
73 |
if ((sldFile != null) && (sldFile.exists())) { |
74 |
try { |
75 |
style = StylingUtil.loadSLD(sldFile)[0]; |
76 |
} catch (FileNotFoundException e) { |
77 |
LOGGER |
78 |
.debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null"); |
79 |
} |
80 |
} |
81 |
|
82 |
title = new Translation(); |
83 |
title.fromOneLine(sldFile.getName()); |
84 |
|
85 |
desc = new Translation(); |
86 |
desc.fromOneLine(sldFile.getAbsolutePath()); |
87 |
} |
88 |
|
89 |
public void dispose() { |
90 |
} |
91 |
|
92 |
/** |
93 |
* Returnes human readable {@link String} of the CRS natively used by this |
94 |
* {@link DpLayer} |
95 |
* |
96 |
* If crs == null, it will call {@link #getGeoObject()} |
97 |
* |
98 |
*/ |
99 |
public String getCRSString() { |
100 |
if (getCrs() == null) |
101 |
return "CRS?"; |
102 |
|
103 |
return getCrs().getName().getCode(); |
104 |
} |
105 |
|
106 |
public CoordinateReferenceSystem getCrs() { |
107 |
return fs.getSchema().getDefaultGeometry().getCoordinateSystem(); |
108 |
} |
109 |
|
110 |
public Translation getDesc() { |
111 |
return desc; |
112 |
} |
113 |
|
114 |
public Envelope getEnvelope() { |
115 |
try { |
116 |
return fs.getBounds(); |
117 |
} catch (IOException e) { |
118 |
e.printStackTrace(); |
119 |
return null; |
120 |
} |
121 |
} |
122 |
|
123 |
public FeatureSource getGeoObject() throws Exception { |
124 |
return fs; |
125 |
} |
126 |
|
127 |
public String getId() { |
128 |
return id; |
129 |
} |
130 |
|
131 |
public ImageIcon getImageIcon() { |
132 |
return null; |
133 |
} |
134 |
|
135 |
public URL getInfoURL() { |
136 |
return null; |
137 |
} |
138 |
|
139 |
public Translation getKeywords() { |
140 |
return null; |
141 |
} |
142 |
|
143 |
public Style getStyle() { |
144 |
return style; |
145 |
} |
146 |
|
147 |
public Translation getTitle() { |
148 |
return title; |
149 |
} |
150 |
|
151 |
public boolean isDisposed() { |
152 |
return false; |
153 |
} |
154 |
|
155 |
/** |
156 |
* If true, this layer will not be shown in the legend. Default = false |
157 |
*/ |
158 |
/** |
159 |
* |
160 |
* Killed by SK: 6. April 09: Ein Layer soll nicht generell auf |
161 |
* verstecken/nicht verstecken gestellt werden können. Das sind |
162 |
* Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer. |
163 |
* Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer |
164 |
* nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher |
165 |
* die Funktion genutzt. |
166 |
* |
167 |
// public boolean isHideInLegend() { |
168 |
// return false; |
169 |
// } |
170 |
*/ |
171 |
|
172 |
public void setDesc(Translation dec) { |
173 |
this.desc = dec; |
174 |
} |
175 |
|
176 |
public void setImageIcon(ImageIcon icon) { |
177 |
} |
178 |
|
179 |
public void setKeywords(Translation keywords) { |
180 |
} |
181 |
|
182 |
public void setStyle(Style style) { |
183 |
this.style = style; |
184 |
|
185 |
} |
186 |
|
187 |
public void setTitle(Translation title) { |
188 |
this.title = title; |
189 |
|
190 |
} |
191 |
|
192 |
public void uncache() { |
193 |
} |
194 |
|
195 |
/** |
196 |
* |
197 |
*/ |
198 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
199 |
if (map == null) { |
200 |
|
201 |
map = new HashMap<Integer, AttributeMetaData>(); |
202 |
|
203 |
// Leaving out the first one, it will be the_geom |
204 |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
205 |
AttributeType att = fs.getSchema().getAttributeType(i); |
206 |
|
207 |
AttributeMetaData attMetaData = new AttributeMetaData(i, att.getLocalName()); |
208 |
map.put(i, attMetaData); |
209 |
} |
210 |
} |
211 |
return map; |
212 |
} |
213 |
|
214 |
/** |
215 |
* @return The {@link File} where the SLD was loaded from or |
216 |
* <code>null</code> if there didn't exist a {@link File}. (It |
217 |
* could be a WFS or a PostGIS |
218 |
* |
219 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
220 |
*/ |
221 |
public File getSldFile() { |
222 |
return sldFile; |
223 |
} |
224 |
|
225 |
public void setSldFile(File sldFile) { |
226 |
this.sldFile = sldFile; |
227 |
} |
228 |
|
229 |
} |