comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/CrossSectionArtifact.java @ 1967:27bb2e24f7f8

Preparations for better CrossSection diagrams. flys-artifacts/trunk@3378 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 09 Dec 2011 15:57:32 +0000
parents 85e442933e6d
children 741d2067cfe1
comparison
equal deleted inserted replaced
1966:f572536af56c 1967:27bb2e24f7f8
11 import de.intevation.artifacts.ArtifactFactory; 11 import de.intevation.artifacts.ArtifactFactory;
12 import de.intevation.artifacts.CallMeta; 12 import de.intevation.artifacts.CallMeta;
13 13
14 import de.intevation.flys.artifacts.model.CrossSectionFacet; 14 import de.intevation.flys.artifacts.model.CrossSectionFacet;
15 15
16 import de.intevation.flys.model.CrossSection;
17 import de.intevation.flys.model.CrossSectionLine;
18 import de.intevation.flys.artifacts.model.CrossSectionFactory;
16 import de.intevation.artifacts.common.ArtifactNamespaceContext; 19 import de.intevation.artifacts.common.ArtifactNamespaceContext;
17 import de.intevation.artifacts.common.utils.XMLUtils; 20 import de.intevation.artifacts.common.utils.XMLUtils;
18 21
19 import de.intevation.flys.artifacts.states.StaticState; 22 import de.intevation.flys.artifacts.states.StaticState;
20 23
34 /** Name of Artifact. */ 37 /** Name of Artifact. */
35 public static final String CS_ARTIFACT_NAME = "cross_section"; 38 public static final String CS_ARTIFACT_NAME = "cross_section";
36 39
37 /** Name of state. */ 40 /** Name of state. */
38 public static final String STATIC_STATE_NAME = "state.cross_section"; 41 public static final String STATIC_STATE_NAME = "state.cross_section";
42
43 /** Name of data item keeping the position. */
44 public static final String DATA_KM = "cross_section.km";
45
46 /** Name of data item keeping the database id of this c.s.. */
47 public static final String DATA_DBID = "cross_section.dbid";
48
49 /** Name of data item keeping the database id of this c.s.. */
50 public static final String DATA_IS_MASTER = "cross_section.master?";
39 51
40 /** Own logger. */ 52 /** Own logger. */
41 private static final Logger logger = 53 private static final Logger logger =
42 Logger.getLogger(CrossSectionArtifact.class); 54 Logger.getLogger(CrossSectionArtifact.class);
43 55
64 76
65 String ids = XMLUtils.xpathString( 77 String ids = XMLUtils.xpathString(
66 data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE); 78 data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE);
67 79
68 if (ids != null && ids.length() > 0) { 80 if (ids != null && ids.length() > 0) {
69 addStringData("ids", ids); 81 addStringData(DATA_DBID, ids);
82 logger.debug("CrossSectionArtifacts db-id: " + ids);
70 } 83 }
71 else { 84 else {
72 throw new IllegalArgumentException("No attribute 'ids' found!"); 85 throw new IllegalArgumentException("No attribute 'ids' found!");
73 } 86 }
87
88 // Assume we start at km 0.
89 addStringData(DATA_KM, "0");
90 addStringData(DATA_IS_MASTER, "0");
74 91
75 List<Facet> fs = new ArrayList<Facet>(); 92 List<Facet> fs = new ArrayList<Facet>();
76 fs.add(new CrossSectionFacet(0, "TODO GET NAME FROM DB")); 93 fs.add(new CrossSectionFacet(0, "TODO GET NAME FROM DB"));
77 94
78 StaticState state = (StaticState) getCurrentState(context); 95 StaticState state = (StaticState) getCurrentState(context);
98 /** 115 /**
99 * Create and return a new StaticState with charting output. 116 * Create and return a new StaticState with charting output.
100 */ 117 */
101 @Override 118 @Override
102 public State getCurrentState(Object cc) { 119 public State getCurrentState(Object cc) {
103 State state = new StaticState(STATIC_STATE_NAME); 120 StaticState state = new StaticState(STATIC_STATE_NAME);
104 121
105 List<Facet> fs = facets.get(getCurrentStateId()); 122 List<Facet> fs = facets.get(getCurrentStateId());
106 123
107 DefaultOutput o = new DefaultOutput( 124 state.addDefaultChartOutput("cross_section", fs);
108 "cross_section",
109 "cross_section",
110 "image/png",
111 fs,
112 "chart");
113
114 state.getOutputs().add(o);
115 125
116 return state; 126 return state;
117 } 127 }
118 128
119 129
127 ArrayList<State> states = new ArrayList<State>(); 137 ArrayList<State> states = new ArrayList<State>();
128 states.add(getCurrentState(context)); 138 states.add(getCurrentState(context));
129 139
130 return states; 140 return states;
131 } 141 }
142
143 // TODO all data access needs proper caching.
144
145 /**
146 * Get a DataItem casted to int (0 if fails).
147 */
148 public int getDataAsIntNull(String dataName) {
149 String val = getDataAsString(dataName);
150 try {
151 return Integer.valueOf(val);
152 }
153 catch (NumberFormatException e) {
154 logger.warn("Could not get data " + dataName + " as int", e);
155 return 0;
156 }
157 }
158
159
160 /** Returns database-id of cross-section (from data). */
161 protected int getDBID() {
162 return getDataAsIntNull(DATA_DBID);
163 }
164
165
166 /**
167 * Return position (km) from data, 0 if not found.
168 */
169 protected double getKm() {
170 String val = getDataAsString(DATA_KM);
171 try {
172 return Double.valueOf(val);
173 }
174 catch (NumberFormatException e) {
175 logger.warn("Could not get data " + DATA_KM + " as double", e);
176 return 0;
177 }
178 }
179
180
181 /** Returns true if artifact is set to be a "master" (other facets will
182 * refer to this). */
183 public boolean isMaster() {
184 return !getDataAsString(DATA_IS_MASTER).equals("0");
185 }
186
187
188 /**
189 * Get points of Profile of cross section at given kilometer.
190 *
191 * @return an array holding coordinates of points of profile (
192 * in the form {{x1, x2} {y1, y2}} ).
193 */
194 public double [][] getCrossSectionData() {
195 logger.info("getCrossSectionData() for cross_section.km "
196 + getDataAsString(DATA_KM));
197 CrossSectionLine line = searchCrossSectionLine();
198
199 return line != null
200 ? line.fetchCrossSectionProfile()
201 : null;
202 }
203
204
205 /**
206 * Get CrossSectionLine spatially closest to what is specified in the data
207 * "cross_section.km".
208 *
209 * @return CrossSectionLine closest to "cross_section.km".
210 */
211 public CrossSectionLine searchCrossSectionLine() {
212 double wishKM = getKm();
213
214 CrossSection crossSection = CrossSectionFactory.getCrossSection(getDBID());
215 logger.debug("dbid " + getDBID() + " : " + crossSection);
216 List<CrossSectionLine> crossSectionLines =
217 crossSection.getLines();
218
219 // Get the cross section closest to requested km.
220 // Naive, linear approach.
221 CrossSectionLine oldLine = crossSectionLines.get(0);
222 double oldDiff = Math.abs(wishKM - oldLine.getKm().doubleValue());
223 for (CrossSectionLine line: crossSectionLines) {
224 double diff = Math.abs(wishKM - line.getKm().doubleValue());
225 if (diff > oldDiff) {
226 break;
227 }
228 oldDiff = diff;
229 oldLine = line;
230 }
231 return oldLine;
232 }
132 } 233 }
133 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 234 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org