Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/CoordinateSelectionState.java @ 540:80630520e25a
merged gnv-artifacts/0.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:49 +0200 |
parents | c5ed4ea8fb64 |
children | 292fbcd5e9ac |
comparison
equal
deleted
inserted
replaced
415:9f4a0b990d27 | 540:80630520e25a |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.state; | |
5 | |
6 import java.util.ArrayList; | |
7 import java.util.Collection; | |
8 import java.util.HashMap; | |
9 import java.util.Iterator; | |
10 import java.util.List; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 import org.w3c.dom.Element; | |
14 import org.w3c.dom.Node; | |
15 import org.w3c.dom.NodeList; | |
16 | |
17 import com.vividsolutions.jts.geom.Point; | |
18 | |
19 import de.intevation.artifactdatabase.Config; | |
20 import de.intevation.gnv.geobackend.base.Result; | |
21 import de.intevation.gnv.state.describedata.DefaultKeyValueDescribeData; | |
22 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
23 import de.intevation.gnv.state.describedata.NamedArrayList; | |
24 import de.intevation.gnv.state.describedata.NamedCollection; | |
25 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
26 import de.intevation.gnv.utils.InputValidator; | |
27 import de.intevation.gnv.utils.WKTUtils; | |
28 import de.intevation.gnv.utils.exception.ValidationException; | |
29 | |
30 /** | |
31 * @author Tim Englich <tim.englich@intevation.de> | |
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 void purifyResult(Collection<Result> result, String uuid) { | |
70 log.debug("CoordinateSelectionState.purifyResult"); | |
71 List<Object> describeData = this.getDescibeData(uuid); | |
72 if (describeData == null) { | |
73 describeData = new ArrayList<Object>(); | |
74 } | |
75 NamedCollection<KeyValueDescibeData> keyValueDescibeData = this | |
76 .extractKVP(result, "FEATUREID", "SHAPE"); | |
77 describeData.add(keyValueDescibeData); | |
78 this.setDescibeData(uuid, describeData); | |
79 } | |
80 | |
81 /** | |
82 * @see de.intevation.gnv.state.StateBase#prepareInputData4RegionDBQuery(java.lang.String) | |
83 */ | |
84 @Override | |
85 protected String prepareInputData4RegionDBQuery(String value) { | |
86 log.debug("CoordinateSelectionState.prepareInputData4RegionDBQuery"); | |
87 double distance=0.; | |
88 String returnValue = null; | |
89 try { | |
90 Point center = new InputValidator().getPointValue(value); | |
91 String meshId = super.inputData.get("meshid").getValue(); | |
92 int segments = 97; | |
93 if (meshWidths != null){ | |
94 Double distanceValue = this.meshWidths.get(meshId); | |
95 if (distanceValue != null){ | |
96 log.debug("User "+distanceValue+" as Buffer around given Point"); | |
97 distance = distanceValue.doubleValue(); | |
98 }else{ | |
99 log.warn("No distance is configured for Mesh with id"+ meshId); | |
100 } | |
101 }else{ | |
102 log.warn("No MeshWidth configured. Check if this is correct."); | |
103 } | |
104 returnValue = center.buffer(distance,segments).toText(); | |
105 } catch (NumberFormatException e) { | |
106 log.error(e,e); | |
107 } catch (ValidationException e) { | |
108 log.error(e,e); | |
109 } | |
110 return returnValue; | |
111 | |
112 } | |
113 | |
114 /** | |
115 * @see de.intevation.gnv.state.StateBase#extractKVP(java.util.Collection, java.lang.String, java.lang.String) | |
116 */ | |
117 @Override | |
118 protected NamedCollection<KeyValueDescibeData> extractKVP( | |
119 Collection<Result> result, | |
120 String keyid, | |
121 String valueid) { | |
122 Iterator<Result> rit = result.iterator(); | |
123 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>( | |
124 this.dataName, result.size()); | |
125 keyValueDescibeData.setMultiSelect(this.dataMultiSelect); | |
126 String prevKey = null; | |
127 while (rit.hasNext()) { | |
128 Result resultValue = rit.next(); | |
129 String key = resultValue.getString(keyid); | |
130 if(prevKey == null || !prevKey.equals(key)){ // TODO: FIXME: We have to do that because the arcsde does not support a distinct Query on Layers | |
131 String geomString = this.convert2DisplayCoordinate(resultValue.getString(valueid)); | |
132 String value = geomString; | |
133 if (resultValue.getResultDescriptor().getColumnIndex("VALUE") > 0){ | |
134 value = resultValue.getString("VALUE") + " - "+value; | |
135 } | |
136 | |
137 | |
138 keyValueDescibeData.add(new DefaultKeyValueDescribeData(key,value )); | |
139 } | |
140 prevKey = key; | |
141 } | |
142 return keyValueDescibeData; | |
143 } | |
144 | |
145 protected static String convert2DisplayCoordinate(String wkt){ | |
146 return WKTUtils.toText(wkt); | |
147 } | |
148 | |
149 /** | |
150 * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node) | |
151 */ | |
152 @Override | |
153 public void setup(Node configuration) { | |
154 super.setup(configuration); | |
155 Element widthElement = (Element)Config.getNodeXPath(configuration, MESH_WIDTH_XPATH); | |
156 | |
157 if (widthElement != null){ | |
158 String fileName = widthElement.getAttribute(XLINK_XPATH); | |
159 fileName = Config.replaceConfigDir(fileName); | |
160 Node configurationNode = new ArtifactXMLUtilities().readConfiguration(fileName); | |
161 NodeList meshNodes = Config.getNodeSetXPath(configurationNode,MESH_LIST_XPATH ); | |
162 if (meshNodes != null){ | |
163 meshWidths = new HashMap<String, Double>(meshNodes.getLength()); | |
164 for (int i = 0; i < meshNodes.getLength(); i++){ | |
165 Element meshNode = (Element)meshNodes.item(i); | |
166 String id = meshNode.getAttribute(ID_XPATH); | |
167 Double value = Double.parseDouble(meshNode.getAttribute(WIDTH_VALUE_XPATH)); | |
168 meshWidths.put(id, value); | |
169 } | |
170 } | |
171 }else{ | |
172 log.warn("No Mesh Width defined."); | |
173 } | |
174 } | |
175 } |