annotate artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/UedauernConfiguration.java @ 9559:ba0561906f81

Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
author gernotbelger
date Wed, 24 Oct 2018 18:40:38 +0200
parents 787fc085459b
children 63bbd5e45839
rev   line source
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
2 * Software engineering by
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
5 *
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
9 */
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
10 package org.dive4elements.river.artifacts.uinfo.inundationduration;
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
11
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
12 import java.io.File;
9198
810f47bba83e Compile errors fixed
gernotbelger
parents: 9197
diff changeset
13 import java.io.IOException;
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
14 import java.io.UnsupportedEncodingException;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
15 import java.util.Collection;
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
16 import java.util.HashMap;
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
17 import java.util.LinkedHashMap;
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
18 import java.util.List;
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
19 import java.util.Map;
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
20
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
21 import org.apache.commons.lang.StringUtils;
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
22 import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
23 import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException;
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
24
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
25 /**
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
26 * @author Domenico Nardi Tironi
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
27 *
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
28 */
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
29 public class UedauernConfiguration {
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
30
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
31 public final static class WmsConfig {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
32 private final String label;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
33 private final String url;
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
34 private final String layer_default;
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
35
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
36 public WmsConfig(final String label, final String url, final String layer) {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
37 this.label = label;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
38 this.url = url;
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
39 this.layer_default = layer;
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
40 }
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
41
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
42 public String getLabel() {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
43 return this.label;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
44 }
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
45
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
46 public String getUrl() {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
47 return this.url;
9198
810f47bba83e Compile errors fixed
gernotbelger
parents: 9197
diff changeset
48 }
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
49
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
50 public String getLayer() {
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
51 return this.layer_default;
9198
810f47bba83e Compile errors fixed
gernotbelger
parents: 9197
diff changeset
52 }
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
53 }
9184
dace17e26d33 code review uinfo.inundationduration
gernotbelger
parents: 9178
diff changeset
54
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
55 public static enum YearType {
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
56 jahre, mittel, szenario
9184
dace17e26d33 code review uinfo.inundationduration
gernotbelger
parents: 9178
diff changeset
57 }
dace17e26d33 code review uinfo.inundationduration
gernotbelger
parents: 9178
diff changeset
58
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
59 private final int DEFAULT_WMS_INDEX = 0;
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
60 private final int VEGETATIONZONE_WMS_INDEX = 1;
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
61
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
62 private static Map<String, UedauernConfiguration> cache = new HashMap<>();
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
63
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
64 private final Map<String, WmsConfig[]> wmsConfigs;
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
65
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
66 public static synchronized UedauernConfiguration getInstance(final String rivername, final YearType type) throws IOException, TsvReaderException {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
67
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
68 final String cacheKey = type.name() + "#" + rivername;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
69 if (!cache.containsKey(cacheKey)) {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
70 final UedauernConfiguration instance = loadConfiguration(rivername, type);
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
71 cache.put(cacheKey, instance);
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
72 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
73
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
74 return cache.get(cacheKey);
9184
dace17e26d33 code review uinfo.inundationduration
gernotbelger
parents: 9178
diff changeset
75 }
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
76
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
77 private UedauernConfiguration(final String rivername, final YearType type, final Map<String, WmsConfig[]> wmsConfigs) {
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
78 this.wmsConfigs = wmsConfigs;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
79 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
80
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
81 private static UedauernConfiguration loadConfiguration(final String rivername, final YearType type) throws IOException, TsvReaderException {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
82
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
83 final String configFile = makeFileName(rivername, type);
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
84 final File file = TsvHelper.makeFile2(configFile, rivername);
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
85 final List<String[]> tsv = TsvHelper.readTsv(file, 5);
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
86
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
87 final Map<String, WmsConfig[]> wmsConfigs = new LinkedHashMap<>(tsv.size());
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
88
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
89 for (final String[] line : tsv) {
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
90 if (line != null && line.length > 0 && !StringUtils.isEmpty(line[0])) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
91 final String label = line[0];
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
92 WmsConfig defaultConfig = null;
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
93 WmsConfig vegZoneConfig = null;
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
94
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
95 if (line.length > 2 && !StringUtils.isEmpty(line[1]) && !StringUtils.isEmpty(line[2])) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
96 defaultConfig = new WmsConfig(label, line[2], line[1]);
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
97
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
98 if (line.length > 4 && !StringUtils.isEmpty(line[3]) && !StringUtils.isEmpty(line[4])) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
99 vegZoneConfig = new WmsConfig(label, line[4], line[3]);
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
100 }
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
101 }
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
102 wmsConfigs.put(label, new WmsConfig[] { defaultConfig, vegZoneConfig });
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
103 }
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
104 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
105 return new UedauernConfiguration(rivername, type, wmsConfigs);
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
106 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
107
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
108 private static final String makeFileName(final String river, final YearType type) {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
109 return "wms_uedauern_" + String.valueOf(type) + "_" + river + ".tsv";
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
110 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
111
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
112 public Collection<String> getLabels() throws UnsupportedEncodingException {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
113 return this.wmsConfigs.keySet();
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
114 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
115
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
116 public WmsConfig getDefaultWmsConfig(final String label) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
117 return this.getConfig(label, this.DEFAULT_WMS_INDEX);
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
118 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
119
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
120 public WmsConfig getVegWmsConfig(final String label) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
121 return this.getConfig(label, this.VEGETATIONZONE_WMS_INDEX);
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
122 }
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
123
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
124 private WmsConfig getConfig(final String label, final int index) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
125 if (this.wmsConfigs.containsKey(label))
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
126 if (this.wmsConfigs.get(label) != null && index < this.wmsConfigs.get(label).length)
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
127 return this.wmsConfigs.get(label)[index];
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
128
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
129 return null;
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
130 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
131
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
132 public static boolean filesExistsForRiver(final String river) {
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
133
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
134 for (final YearType t : YearType.values()) {
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
135 final String configFile = makeFileName(river, t);
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
136 final File file = TsvHelper.makeFile2(configFile, river);
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
137
9559
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
138 if (TsvHelper.checkFile(file) == null)
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
139 return false;
ba0561906f81 Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents: 9481
diff changeset
140 }
9481
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
141
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
142 return true;
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
143 }
787fc085459b TSV introduced; uinfo.inundationWMS-Config
gernotbelger
parents: 9469
diff changeset
144
9178
2f5052835b76 uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
gernotbelger
parents:
diff changeset
145 }

http://dive4elements.wald.intevation.org