Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java @ 480:211cad2fb5ba
Rebased "Horizonalschnitte" to own state class to break from the not
well fitting TimeSeriesOutputState.
gnv-artifacts/trunk@552 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 17 Jan 2010 12:22:56 +0000 |
parents | ab29e4ff2fda |
children | 20dde2b6f1b5 |
comparison
equal
deleted
inserted
replaced
479:d47b478e662b | 480:211cad2fb5ba |
---|---|
1 /** | 1 /** |
2 * | 2 * |
3 */ | 3 */ |
4 package de.intevation.gnv.state.profile.horizontalcrosssection; | 4 package de.intevation.gnv.state.profile.horizontalcrosssection; |
5 | 5 |
6 import com.vividsolutions.jts.geom.Coordinate; | |
7 import com.vividsolutions.jts.geom.Envelope; | |
8 import com.vividsolutions.jts.geom.Polygon; | |
9 | |
10 import de.intevation.artifactdatabase.Config; | |
11 import de.intevation.artifactdatabase.XMLUtils; | |
12 | |
13 import de.intevation.artifacts.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.CallContext; | |
15 | |
16 import de.intevation.gnv.artifacts.cache.CacheFactory; | |
17 | |
18 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
19 | |
20 import de.intevation.gnv.geobackend.base.Result; | |
21 import de.intevation.gnv.geobackend.base.ResultDescriptor; | |
22 | |
23 import de.intevation.gnv.geobackend.base.query.QueryExecutor; | |
24 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; | |
25 | |
26 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
27 | |
28 import de.intevation.gnv.geobackend.sde.datasources.RasterObject; | |
29 | |
30 import de.intevation.gnv.math.AttributedPoint2ds; | |
31 | |
32 import de.intevation.gnv.state.InputData; | |
33 import de.intevation.gnv.state.OutputStateBase; | |
34 | |
35 import de.intevation.gnv.state.exception.StateException; | |
36 | |
37 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; | |
38 | |
39 import de.intevation.gnv.utils.StringUtils; | |
40 import de.intevation.gnv.utils.WKTUtils; | |
41 | |
6 import java.io.File; | 42 import java.io.File; |
7 import java.io.IOException; | 43 import java.io.IOException; |
8 import java.io.OutputStream; | 44 import java.io.OutputStream; |
9 import java.io.OutputStreamWriter; | 45 |
10 import java.io.UnsupportedEncodingException; | |
11 | |
12 import java.util.Arrays; | |
13 import java.util.Collection; | 46 import java.util.Collection; |
14 import java.util.Iterator; | |
15 import java.util.List; | |
16 import java.util.Locale; | |
17 | 47 |
18 import org.apache.log4j.Logger; | 48 import org.apache.log4j.Logger; |
19 | 49 |
20 import org.jfree.chart.ChartTheme; | 50 import org.w3c.dom.Document; |
21 | |
22 import org.w3c.dom.Node; | 51 import org.w3c.dom.Node; |
23 | |
24 import com.vividsolutions.jts.geom.Coordinate; | |
25 import com.vividsolutions.jts.geom.Envelope; | |
26 import com.vividsolutions.jts.geom.Polygon; | |
27 | |
28 import au.com.bytecode.opencsv.CSVWriter; | |
29 | |
30 import de.intevation.gnv.artifacts.cache.CacheFactory; | |
31 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
32 | |
33 import de.intevation.gnv.chart.Chart; | |
34 import de.intevation.gnv.chart.ChartLabels; | |
35 | |
36 import de.intevation.gnv.geobackend.base.Result; | |
37 | |
38 import de.intevation.gnv.geobackend.base.query.QueryExecutor; | |
39 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; | |
40 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
41 | |
42 import de.intevation.gnv.state.InputData; | |
43 | |
44 import de.intevation.gnv.state.exception.StateException; | |
45 | |
46 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; | |
47 | |
48 import de.intevation.gnv.statistics.Statistics; | |
49 | |
50 import de.intevation.gnv.utils.StringUtils; | |
51 import de.intevation.gnv.utils.WKTUtils; | |
52 | |
53 import de.intevation.artifactdatabase.Config; | |
54 | |
55 import de.intevation.artifacts.CallContext; | |
56 | |
57 import de.intevation.gnv.geobackend.sde.datasources.RasterObject; | |
58 | 52 |
59 /** | 53 /** |
60 * @author Tim Englich (tim.englich@intevation.de) | 54 * @author Tim Englich (tim.englich@intevation.de) |
61 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | 55 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) |
62 */ | 56 */ |
63 public class HorizontalCrossSectionMeshOutputState | 57 public class HorizontalCrossSectionMeshOutputState |
64 extends TimeSeriesOutputState | 58 extends OutputStateBase |
65 { | 59 { |
66 private static Logger log = Logger | 60 private static Logger log = Logger |
67 .getLogger(HorizontalCrossSectionMeshOutputState.class); | 61 .getLogger(HorizontalCrossSectionMeshOutputState.class); |
68 | 62 |
69 /** | 63 /** |
75 | 69 |
76 /** | 70 /** |
77 * Constructor | 71 * Constructor |
78 */ | 72 */ |
79 public HorizontalCrossSectionMeshOutputState() { | 73 public HorizontalCrossSectionMeshOutputState() { |
80 super(); | 74 } |
81 super.domainLable = "chart.horizontalcrosssection.title.xaxis"; | 75 |
82 } | 76 public void out( |
83 | 77 Document format, |
84 @Override | 78 Collection<InputData> inputData, |
85 protected Chart getChart( | 79 OutputStream outputStream, |
86 ChartLabels chartLables, | 80 String uuid, |
87 ChartTheme theme, | 81 CallContext callContext |
88 Collection parameters, | 82 ) |
89 Collection measurements, | 83 throws StateException |
90 Collection dates, | 84 { |
91 Object result, | 85 String outputMode = XMLUtils.xpathString( |
92 Locale locale, | 86 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); |
93 String uuid, | 87 |
94 boolean linesVisible, | 88 String mimeType = XMLUtils.xpathString( |
95 boolean shapesVisible, | 89 format, XPATH_MIME_TYPE, ArtifactNamespaceContext.INSTANCE); |
96 CallContext callContext | 90 |
91 if (outputMode == null || mimeType == null) { | |
92 throw new StateException("cannot find outputMode or mime"); | |
93 } | |
94 | |
95 outputMode = outputMode.toLowerCase(); | |
96 | |
97 log.debug("---- asking for: " + outputMode); | |
98 | |
99 if ("zip".equals(outputMode)) { | |
100 } | |
101 else if ("wms".equals(outputMode)) { | |
102 } | |
103 else if ("statistics".equals(outputMode)) { | |
104 // TODO: REMOVE THIS! | |
105 try { | |
106 outputStream.write("<fake/>\n".getBytes()); | |
107 } | |
108 catch (IOException ioe) { | |
109 } | |
110 } | |
111 else { | |
112 throw new StateException("unsupported output mode"); | |
113 } | |
114 } | |
115 | |
116 protected Object getResult(String uuid, CallContext callContext) | |
117 throws StateException | |
118 { | |
119 CacheFactory cf = CacheFactory.getInstance(); | |
120 String key = uuid + super.getID(); | |
121 | |
122 if (cf.isInitialized()) { | |
123 net.sf.ehcache.Element value = cf.getCache().get(key); | |
124 if (value != null) { | |
125 return value.getObjectValue(); | |
126 } | |
127 } | |
128 | |
129 Object result = produceResult(callContext); | |
130 | |
131 if (result != null && cf.isInitialized()) { | |
132 cf.getCache().put(new net.sf.ehcache.Element(key, result)); | |
133 } | |
134 | |
135 return result; | |
136 } | |
137 | |
138 protected Object produceResult(CallContext callContext) | |
139 throws StateException | |
140 { | |
141 InputData meshPolygon = inputData.get("mesh_polygon"); | |
142 InputData meshId = inputData.get("meshid"); | |
143 | |
144 if (meshPolygon == null) { | |
145 log.error("mesh_polygon is not defined"); | |
146 throw new StateException("missing mesh_linestring"); | |
147 } | |
148 | |
149 if (meshId == null) { | |
150 log.error("meshid is not defined"); | |
151 throw new StateException("missing meshid"); | |
152 } | |
153 | |
154 Polygon p = WKTUtils.toPolygon(meshPolygon.getValue()); | |
155 | |
156 if (p == null) { | |
157 log.error("no valid polygon"); | |
158 throw new StateException("no valid polygon"); | |
159 } | |
160 | |
161 try { | |
162 Envelope env = p.getEnvelopeInternal(); | |
163 | |
164 Coordinate [] coords = new Coordinate [] { | |
165 new Coordinate(env.getMinX(), env.getMinY()), | |
166 new Coordinate(env.getMinX(), env.getMaxY()), | |
167 new Coordinate(env.getMaxX(), env.getMaxY()), | |
168 new Coordinate(env.getMaxX(), env.getMinY()) }; | |
169 | |
170 String additionWhere = | |
171 WKTUtils.worldEnvelopeCoordinatesToIndex( | |
172 coords, | |
173 meshId.getValue(), | |
174 ijkQueryID); | |
175 | |
176 String[] addedFilterValues = StringUtils.append( | |
177 generateFilterValuesFromInputData(), | |
178 additionWhere); | |
179 | |
180 QueryExecutor queryExecutor = QueryExecutorFactory | |
181 .getInstance() | |
182 .getQueryExecutor(); | |
183 | |
184 return process( | |
185 env, | |
186 p, | |
187 numSamples(callContext), | |
188 preprocess( | |
189 queryExecutor.executeQuery( | |
190 queryID, | |
191 addedFilterValues))); | |
192 } | |
193 catch (QueryException e) { | |
194 log.error(e,e); | |
195 } | |
196 | |
197 throw new StateException("no result produced"); | |
198 } | |
199 | |
200 public AttributedPoint2ds preprocess(Collection<Result> results) { | |
201 | |
202 boolean debug = log.isDebugEnabled(); | |
203 | |
204 if (debug) { | |
205 log.debug("--- preprocess: " + results.size() + " results"); | |
206 } | |
207 | |
208 AttributedPoint2ds ap2ds = new AttributedPoint2ds(); | |
209 | |
210 boolean first = true; | |
211 | |
212 for (Result result: results) { | |
213 | |
214 if (debug && first) { | |
215 first = false; | |
216 ResultDescriptor rd = result.getResultDescriptor(); | |
217 log.debug(rd); | |
218 } | |
219 } | |
220 | |
221 return ap2ds; | |
222 } | |
223 | |
224 public Object process( | |
225 Envelope env, | |
226 Polygon polygon, | |
227 int numSamples, | |
228 AttributedPoint2ds input | |
97 ) { | 229 ) { |
98 Chart chart = null; | 230 return input; |
99 | 231 } |
100 if (CACHE_CHART) { | 232 |
101 log.info("Try to get horizontalcrosssection chart from cache."); | |
102 chart = (Chart) getChartFromCache(uuid, callContext); | |
103 } | |
104 | |
105 if (chart != null) | |
106 return chart; | |
107 | |
108 log.info("Chart not in cache yet."); | |
109 | |
110 log.warn("This sort of chart is not implemented yet."); | |
111 | |
112 InputData meshPolygon = inputData.get("mesh_polygon"); | |
113 String meshPolygonWkt = null; | |
114 if (meshPolygon != null){ | |
115 meshPolygonWkt = meshPolygon.getValue(); | |
116 | |
117 } | |
118 log.debug("Used Polygon: "+meshPolygonWkt); | |
119 | |
120 return chart; | |
121 } | |
122 | |
123 /** | |
124 * @see de.intevation.gnv.state.OutputStateBase#getChartResult(java.lang.String, de.intevation.artifacts.CallContext) | |
125 */ | |
126 @Override | |
127 protected Object getChartResult(String uuid, CallContext callContext) { | |
128 log.debug("HorizontalProfileMeshCrossOutputState.getChartResult"); | |
129 Collection<Result> result = null; | |
130 if (CacheFactory.getInstance().isInitialized()) { | |
131 String key = uuid + super.getID(); | |
132 log.debug("Hash for Queryelements: " + key); | |
133 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key); | |
134 if (value != null) { | |
135 result = (Collection<Result>) (value.getObjectValue()); | |
136 }else{ | |
137 | |
138 InputData meshPolygon = inputData.get("mesh_polygon"); | |
139 InputData meshId = inputData.get("meshid"); | |
140 | |
141 if (meshPolygon == null) { | |
142 log.error("mesh_polygon is not defined"); | |
143 throw new IllegalStateException("missing mesh_linestring"); | |
144 } | |
145 | |
146 if (meshId == null) { | |
147 log.error("meshid is not defined"); | |
148 throw new IllegalStateException("missing meshid"); | |
149 } | |
150 | |
151 Polygon p = WKTUtils.toPolygon(meshPolygon.getValue()); | |
152 | |
153 if (p == null) { | |
154 log.error("no valid polygon"); | |
155 throw new IllegalStateException("no valid polygon"); | |
156 } | |
157 | |
158 try { | |
159 Envelope env = p.getEnvelopeInternal(); | |
160 | |
161 Coordinate [] coords = new Coordinate [] { | |
162 new Coordinate(env.getMinX(), env.getMinY()), | |
163 new Coordinate(env.getMinX(), env.getMaxY()), | |
164 new Coordinate(env.getMaxX(), env.getMaxY()), | |
165 new Coordinate(env.getMaxX(), env.getMinY()) }; | |
166 | |
167 String additionWhere = | |
168 WKTUtils.worldEnvelopeCoordinatesToIndex( | |
169 coords, | |
170 result, | |
171 meshId.getValue(), | |
172 ijkQueryID); | |
173 | |
174 String[] addedFilterValues = StringUtils.append( | |
175 generateFilterValuesFromInputData(), | |
176 additionWhere); | |
177 | |
178 QueryExecutor queryExecutor = QueryExecutorFactory | |
179 .getInstance() | |
180 .getQueryExecutor(); | |
181 | |
182 result = process( | |
183 Arrays.asList(coords), | |
184 numSamples(callContext), | |
185 queryExecutor.executeQuery( | |
186 queryID, | |
187 addedFilterValues), | |
188 p); | |
189 } | |
190 catch (QueryException e) { | |
191 log.error(e,e); | |
192 } | |
193 | |
194 if (CacheFactory.getInstance().isInitialized()) { | |
195 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result)); | |
196 } | |
197 } | |
198 } | |
199 return result; | |
200 } | |
201 | |
202 public static Collection<Result> process( | |
203 List<Coordinate> path, | |
204 int numSamples, | |
205 Collection<Result> input, | |
206 Polygon polygon | |
207 ) { | |
208 // TODO: IMPLEMENT ME INTEGRATE POLYGONCLIPPING | |
209 return null; | |
210 } | |
211 | |
212 /** | |
213 * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator() | |
214 */ | |
215 @Override | |
216 protected Statistics getStatisticsGenerator() { | |
217 return null; //Statistics are not supported for this kind of OutputState. | |
218 } | |
219 | |
220 /** | |
221 * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#createCSV(java.io.OutputStream, | |
222 * java.util.Collection) | |
223 */ | |
224 @Override | |
225 protected void createCSV( | |
226 OutputStream outputStream, | |
227 Collection<Result> chartResult | |
228 ) throws UnsupportedEncodingException, IOException, StateException | |
229 { | |
230 /* | |
231 if (chartResult != null) { | |
232 try { | |
233 CSVWriter writer = new CSVWriter(new OutputStreamWriter( | |
234 outputStream, "ISO-8859-1"), ','); | |
235 // USE THIS ENCODING BECAUSE OF | |
236 // PROBLEMS WITH EXCEL AND UTF-8 | |
237 Iterator<Result> it = chartResult.iterator(); | |
238 while (it.hasNext()) { | |
239 Result result = it.next(); | |
240 int i = 0; | |
241 String[] entries = new String[5]; | |
242 entries[i++] = result.getString("SHAPE"); | |
243 entries[i++] = result.getString("YORDINATE"); | |
244 entries[i++] = result.getString("IPOSITION"); | |
245 entries[i++] = result.getString("JPOSITION"); | |
246 entries[i++] = result.getString("KPOSITION"); | |
247 writer.writeNext(entries); | |
248 } | |
249 writer.close(); | |
250 } catch (Exception e) { | |
251 log.error(e,e); | |
252 throw new StateException( | |
253 "Exception occured while parsing an Point from WKT."); | |
254 } | |
255 } else { | |
256 log.error("No Data given for generating an CSV-File."); | |
257 throw new StateException( | |
258 "No Data given for generating an CSV-File."); | |
259 } | |
260 */ | |
261 } | |
262 | 233 |
263 /** | 234 /** |
264 * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#setup(org.w3c.dom.Node) | 235 * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#setup(org.w3c.dom.Node) |
265 */ | 236 */ |
266 @Override | 237 @Override |