comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/CoordinateSelectionState.java @ 335:e964a3d8f7bc

Some Refactoring work done. Moved Transition to State gnv-artifacts/trunk@401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 08 Dec 2009 08:39:03 +0000
parents
children 3ddc22aab764
comparison
equal deleted inserted replaced
334:e37930705daa 335:e964a3d8f7bc
1 /**
2 *
3 */
4 package de.intevation.gnv.state;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Iterator;
9
10 import org.apache.log4j.Logger;
11
12 import com.vividsolutions.jts.geom.Point;
13 import com.vividsolutions.jts.io.ParseException;
14 import com.vividsolutions.jts.io.WKTReader;
15
16 import de.intevation.gnv.geobackend.base.Result;
17 import de.intevation.gnv.state.describedata.DefaultKeyValueDescribeData;
18 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
19 import de.intevation.gnv.state.describedata.NamedArrayList;
20 import de.intevation.gnv.state.describedata.NamedCollection;
21 import de.intevation.gnv.utils.InputValidator;
22 import de.intevation.gnv.utils.exception.ValidationException;
23
24 /**
25 * @author Tim Englich <tim.englich@intevation.de>
26 *
27 */
28 public class CoordinateSelectionState extends StateBase {
29
30 /**
31 * the logger, used to log exceptions and additonaly information
32 */
33 private static Logger log = Logger.getLogger(CoordinateSelectionState.class);
34
35 /**
36 * The UID of this Class
37 */
38 private static final long serialVersionUID = 6318923553625195063L;
39
40 /**
41 * Constructor
42 */
43 public CoordinateSelectionState() {
44 super();
45 }
46
47 @Override
48 protected void purifyResult(Collection<Result> result, String uuid) {
49 log.debug("CoordinateSelectionState.purifyResult");
50 Collection<Object> describeData = this.getDescibeData(uuid);
51 if (describeData == null) {
52 describeData = new ArrayList<Object>();
53 }
54 NamedCollection<KeyValueDescibeData> keyValueDescibeData = this
55 .extractKVP(result, "FEATUREID", "SHAPE");
56 describeData.add(keyValueDescibeData);
57 this.setDescibeData(uuid, describeData);
58 }
59
60 @Override
61 protected String prepareInputData4RegionDBQuery(String value) {
62 log.debug("CoordinateSelectionState.prepareInputData4RegionDBQuery");
63 double distance=0.;
64 String returnValue = null;
65 try {
66 Point center = new InputValidator().getPointValue(value);
67 String meshIDValue = super.inputData.get("meshid").getValue();
68 int segments = 97;
69 int meshid = new Integer(meshIDValue);
70 if (meshid == 3 || meshid == 5){ // TODO nicht hier festbrennen.
71 distance = 0.15; //nord-ostsee 6nm // 97 Stützpunkte
72 }else if (meshid == 1 || meshid == 4){
73 distance = 0.03; //Kuestenmodell 1 nm
74 }else if (meshid == 2 || meshid == 321){
75 distance = 0.3; //SST Karten ca 20km
76 }else if (meshid == 641){
77 distance = 0.3; //Eisdaten Klima
78 }
79 returnValue = center.buffer(distance,segments).toText();
80 } catch (NumberFormatException e) {
81 log.error(e,e);
82 } catch (ValidationException e) {
83 log.error(e,e);
84 }
85 return returnValue;
86
87 }
88
89 /**
90 * @see de.intevation.gnv.state.StateBase#extractKVP(java.util.Collection, java.lang.String, java.lang.String)
91 */
92 @Override
93 protected NamedCollection<KeyValueDescibeData> extractKVP(
94 Collection<Result> result,
95 String keyid,
96 String valueid) {
97 Iterator<Result> rit = result.iterator();
98 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(
99 this.dataName, result.size());
100 keyValueDescibeData.setMultiSelect(this.dataMultiSelect);
101 String prevKey = null;
102 while (rit.hasNext()) {
103 Result resultValue = rit.next();
104 String key = resultValue.getString(keyid);
105 if(prevKey == null || !prevKey.equals(key)){ // müssen wir so machen, da die sde kein select distinct auf layern zulässt.
106 String geomString = this.convert2DisplayCoordinate(resultValue.getString(valueid));
107 String value = geomString;
108 if (resultValue.getResultDescriptor().getColumnIndex("VALUE") > 0){
109 value = resultValue.getString("VALUE") + " - "+value;
110 }
111
112
113 keyValueDescibeData.add(new DefaultKeyValueDescribeData(key,value ));
114 }
115 prevKey = key;
116 }
117 return keyValueDescibeData;
118 }
119
120 protected String convert2DisplayCoordinate(String wkt){
121 String formattedCoordinate = null;
122 try {
123 Point p = (Point)new WKTReader().read(wkt);
124 double lat = p.getY();
125 double lon =p.getX();
126 String nord="N";
127 String ost="E";
128 if (lat <0 ){nord="S"; lat=-lat;}
129 if (lon <0 ){ost="W"; lon=-lon;}
130 formattedCoordinate = String.format("%1$02d°%2$1S %3$05.2f' %4$03d°%5$1S %6$05.2f'",
131 (int)lat, nord,60.*(lat-((int)lat)),(int)lon,ost,60.*(lon-((int)lon)));
132 } catch (ParseException e) {
133 log.error(e,e);
134 }
135
136 return formattedCoordinate;
137 }
138 }

http://dive4elements.wald.intevation.org