comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/CoordinateSelectionState.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents dec4257ad570
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.state;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList;
21
22 import com.vividsolutions.jts.geom.Point;
23
24 import de.intevation.artifacts.common.utils.Config;
25 import de.intevation.gnv.geobackend.base.Result;
26 import de.intevation.gnv.state.describedata.DefaultKeyValueDescribeData;
27 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
28 import de.intevation.gnv.state.describedata.NamedArrayList;
29 import de.intevation.gnv.state.describedata.NamedCollection;
30 import de.intevation.gnv.utils.ArtifactXMLUtilities;
31 import de.intevation.gnv.utils.InputValidator;
32 import de.intevation.gnv.utils.WKTUtils;
33 import de.intevation.gnv.utils.exception.ValidationException;
34
35 /**
36 * This state handles coordinate input by the user. It searches database results
37 * for coordinates and turns them into a human readable form.
38 *
39 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
40 *
41 */
42 public class CoordinateSelectionState extends StateBase {
43
44 /**
45 * the logger, used to log exceptions and additonaly information
46 */
47 private static Logger log = Logger.getLogger(CoordinateSelectionState.class);
48
49 /**
50 * The UID of this Class
51 */
52 private static final long serialVersionUID = 6318923553625195063L;
53
54 /**
55 * XPATH Expressions for the setup.
56 */
57
58 private final static String MESH_WIDTH_XPATH = "mesh-width";
59 private final static String XLINK_XPATH = "xlink:href";
60 private final static String MESH_LIST_XPATH = "/mesh-widths/mesh";
61 private final static String ID_XPATH = "id";
62 private final static String WIDTH_VALUE_XPATH = "width";
63
64 /**
65 * Holds all given Widths between two MeshPoints for different Meshes.
66 */
67 private HashMap<String, Double> meshWidths = null;
68
69 /**
70 * Constructor
71 */
72 public CoordinateSelectionState() {
73 super();
74 }
75
76 @Override
77 protected List<Object> purifyResult(Collection<Result> result, String uuid) {
78 log.debug("CoordinateSelectionState.purifyResult");
79 List<Object> describeData = new ArrayList<Object>();
80
81 NamedCollection<KeyValueDescibeData> keyValueDescibeData =
82 extractKVP(result, "FEATUREID", "SHAPE");
83
84 describeData.add(keyValueDescibeData);
85 return describeData;
86 }
87
88
89 @Override
90 protected String prepareInputData4RegionDBQuery(String value) {
91 log.debug("CoordinateSelectionState.prepareInputData4RegionDBQuery");
92 double distance=0.;
93 String returnValue = null;
94 try {
95 Point center = InputValidator.getPointValue(value);
96 String meshId = super.inputData.get("meshid").getValue();
97 int segments = 97;
98 if (meshWidths != null){
99 Double distanceValue = this.meshWidths.get(meshId);
100 if (distanceValue != null){
101 log.debug("User "+distanceValue+" as Buffer around given Point");
102 distance = distanceValue.doubleValue();
103 }else{
104 log.warn("No distance is configured for Mesh with id"+ meshId);
105 }
106 }else{
107 log.warn("No MeshWidth configured. Check if this is correct.");
108 }
109 returnValue = center.buffer(distance,segments).toText();
110 } catch (NumberFormatException e) {
111 log.error(e,e);
112 } catch (ValidationException e) {
113 log.error(e,e);
114 }
115 return returnValue;
116
117 }
118
119
120 @Override
121 protected NamedCollection<KeyValueDescibeData> extractKVP(
122 Collection<Result> result,
123 String keyid,
124 String valueid) {
125 Iterator<Result> rit = result.iterator();
126 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(
127 this.dataName, result.size());
128 keyValueDescibeData.setMultiSelect(this.dataMultiSelect);
129 String prevKey = null;
130 while (rit.hasNext()) {
131 Result resultValue = rit.next();
132 String key = resultValue.getString(keyid);
133 if(prevKey == null || !prevKey.equals(key)){ // TODO: FIXME: We have to do that because the arcsde does not support a distinct Query on Layers
134 String geomString = CoordinateSelectionState.convert2DisplayCoordinate(resultValue.getString(valueid));
135 String value = geomString;
136 if (resultValue.getResultDescriptor().getColumnIndex("VALUE") > 0){
137 value = resultValue.getString("VALUE") + " - "+value;
138 }
139
140
141 keyValueDescibeData.add(new DefaultKeyValueDescribeData(key,value ));
142 }
143 prevKey = key;
144 }
145 return keyValueDescibeData;
146 }
147
148 /**
149 * Turn coordinate into a human readable format.
150 *
151 * @param wkt Coordinate as wkt string.
152 * @return formatted coordinate.
153 */
154 protected static String convert2DisplayCoordinate(String wkt){
155 return WKTUtils.toText(wkt);
156 }
157
158
159 @Override
160 public void setup(Node configuration) {
161 super.setup(configuration);
162 Element widthElement = (Element)Config.getNodeXPath(configuration, MESH_WIDTH_XPATH);
163
164 if (widthElement != null){
165 String fileName = widthElement.getAttribute(XLINK_XPATH);
166 fileName = Config.replaceConfigDir(fileName);
167 Node configurationNode = new ArtifactXMLUtilities().readConfiguration(fileName);
168 NodeList meshNodes = Config.getNodeSetXPath(configurationNode,MESH_LIST_XPATH );
169 if (meshNodes != null){
170 meshWidths = new HashMap<String, Double>(meshNodes.getLength());
171 for (int i = 0; i < meshNodes.getLength(); i++){
172 Element meshNode = (Element)meshNodes.item(i);
173 String id = meshNode.getAttribute(ID_XPATH);
174 Double value = Double.parseDouble(meshNode.getAttribute(WIDTH_VALUE_XPATH));
175 meshWidths.put(id, value);
176 }
177 }
178 }else{
179 log.warn("No Mesh Width defined.");
180 }
181 }
182 }
183 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org