comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/CoordinateSelectionState.java @ 875:5e9efdda6894

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

http://dive4elements.wald.intevation.org