Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/ManualPointsArtifact.java @ 9425:3f49835a00c3
Extended CrossSectionFacet so it may fetch different data from within the artifact result.
Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author | gernotbelger |
---|---|
date | Fri, 17 Aug 2018 15:31:02 +0200 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
9424:da19f1f58d72 | 9425:3f49835a00c3 |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.artifacts; | 9 package org.dive4elements.river.artifacts; |
10 | 10 |
11 import java.awt.geom.Point2D; | 11 import java.io.Serializable; |
12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.List; | 13 import java.util.List; |
14 | 14 |
15 import org.apache.log4j.Logger; | 15 import org.apache.log4j.Logger; |
16 import org.json.JSONArray; | |
17 import org.json.JSONException; | |
18 import org.w3c.dom.Document; | |
19 | |
20 import org.dive4elements.artifactdatabase.state.Facet; | 16 import org.dive4elements.artifactdatabase.state.Facet; |
21 import org.dive4elements.artifacts.Artifact; | 17 import org.dive4elements.artifacts.Artifact; |
22 import org.dive4elements.artifacts.ArtifactFactory; | 18 import org.dive4elements.artifacts.ArtifactFactory; |
23 import org.dive4elements.artifacts.CallContext; | 19 import org.dive4elements.artifacts.CallContext; |
24 import org.dive4elements.artifacts.CallMeta; | 20 import org.dive4elements.artifacts.CallMeta; |
25 import org.dive4elements.river.artifacts.geom.Lines; | |
26 import org.dive4elements.river.artifacts.model.FacetTypes; | 21 import org.dive4elements.river.artifacts.model.FacetTypes; |
27 import org.dive4elements.river.artifacts.states.DefaultState; | 22 import org.dive4elements.river.artifacts.states.DefaultState; |
28 import org.dive4elements.river.model.FastCrossSectionLine; | 23 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType; |
29 | 24 import org.json.JSONArray; |
25 import org.json.JSONException; | |
26 import org.w3c.dom.Document; | |
30 | 27 |
31 /** | 28 /** |
32 * Artifact to store user-added points and water lines. | 29 * Artifact to store user-added points and water lines. |
33 */ | 30 */ |
34 public class ManualPointsArtifact | 31 public class ManualPointsArtifact extends StaticD4EArtifact implements FacetTypes, WaterLineArtifact { |
35 extends StaticD4EArtifact | 32 |
36 implements FacetTypes, WaterLineArtifact | |
37 { | |
38 private static final long serialVersionUID = 7096025125474986011L; | 33 private static final long serialVersionUID = 7096025125474986011L; |
39 | 34 |
40 /** The log for this class. */ | 35 /** The log for this class. */ |
41 private static Logger log = Logger.getLogger(ManualPointsArtifact.class); | 36 private static Logger log = Logger.getLogger(ManualPointsArtifact.class); |
42 | 37 |
43 /** The name of the artifact. */ | 38 /** The name of the artifact. */ |
44 public static final String ARTIFACT_NAME = "manualpoints"; | 39 public static final String ARTIFACT_NAME = "manualpoints"; |
45 | 40 |
46 | |
47 public ManualPointsArtifact() { | 41 public ManualPointsArtifact() { |
48 log.debug("ManualPointsArtifact.ManualPointsArtifact()"); | 42 log.debug("ManualPointsArtifact.ManualPointsArtifact()"); |
49 } | 43 } |
50 | |
51 | 44 |
52 /** | 45 /** |
53 * Gets called from factory to set things up. | 46 * Gets called from factory to set things up. |
54 */ | 47 */ |
55 @Override | 48 @Override |
56 public void setup( | 49 public void setup(final String identifier, final ArtifactFactory factory, final Object context, final CallMeta callMeta, final Document data, |
57 String identifier, | 50 final List<Class> loadFacets) { |
58 ArtifactFactory factory, | |
59 Object context, | |
60 CallMeta callMeta, | |
61 Document data, | |
62 List<Class> loadFacets) | |
63 { | |
64 log.debug("ManualPointsArtifact.setup"); | 51 log.debug("ManualPointsArtifact.setup"); |
65 super.setup(identifier, factory, context, callMeta, data, loadFacets); | 52 super.setup(identifier, factory, context, callMeta, data, loadFacets); |
66 initialize(null, context, callMeta); | 53 initialize(null, context, callMeta); |
67 } | 54 } |
68 | |
69 | 55 |
70 /** Return the name of this artifact. */ | 56 /** Return the name of this artifact. */ |
71 @Override | 57 @Override |
72 public String getName() { | 58 public String getName() { |
73 return ARTIFACT_NAME; | 59 return ARTIFACT_NAME; |
74 } | 60 } |
75 | 61 |
76 | |
77 /** Access state data storing the jsonstring with points. */ | 62 /** Access state data storing the jsonstring with points. */ |
78 public String getPointsData(String facetName) { | 63 public String getPointsData(final String facetName) { |
79 return getDataAsString(facetName + ".data"); | 64 return getDataAsString(facetName + ".data"); |
80 } | 65 } |
81 | 66 |
82 | |
83 /** | 67 /** |
84 * Access state data storing the jsonstring with lines. | 68 * Access state data storing the jsonstring with lines. |
85 * @param facetName Name of facet or null if the so far | 69 * |
86 * only known case should be picked. | 70 * @param facetName |
71 * Name of facet or null if the so far | |
72 * only known case should be picked. | |
87 * @return (String) value of data element (expect json). | 73 * @return (String) value of data element (expect json). |
88 */ | 74 */ |
89 public String getLinesData(String facetName) { | 75 private String getLinesData(final String facetName) { |
90 if (facetName == null) | 76 if (facetName == null) |
91 return getDataAsString("cross_section.manualpoints.lines"); | 77 return getDataAsString("cross_section.manualpoints.lines"); |
92 // TODO .lineS? | 78 // TODO .lineS? |
93 return getDataAsString(facetName + ".line"); | 79 return getDataAsString(facetName + ".line"); |
94 } | 80 } |
95 | 81 |
96 | |
97 /** Setup state and facet. */ | 82 /** Setup state and facet. */ |
98 @Override | 83 @Override |
99 protected void initialize( | 84 protected void initialize(final Artifact artifact, final Object context, final CallMeta meta) { |
100 Artifact artifact, | |
101 Object context, | |
102 CallMeta meta | |
103 ) { | |
104 log.debug("ManualPointsArtifact.initialize"); | 85 log.debug("ManualPointsArtifact.initialize"); |
105 List<Facet> fs = new ArrayList<Facet>(); | 86 final List<Facet> fs = new ArrayList<>(); |
106 | 87 |
107 DefaultState state = (DefaultState) getCurrentState(context); | 88 final DefaultState state = (DefaultState) getCurrentState(context); |
108 state.computeInit(this, hash(), context, meta, fs); | 89 state.computeInit(this, hash(), context, meta, fs); |
109 if (!fs.isEmpty()) { | 90 if (!fs.isEmpty()) { |
110 log.debug("Facets to add in ManualPointsArtifact.initialize ."); | 91 log.debug("Facets to add in ManualPointsArtifact.initialize ."); |
111 addFacets(getCurrentStateId(), fs); | 92 addFacets(getCurrentStateId(), fs); |
112 } | 93 } else { |
113 else { | 94 log.debug("No facets to add in ManualPointsArtifact.initialize (" + state.getID() + ")."); |
114 log.debug("No facets to add in ManualPointsArtifact.initialize (" | |
115 + state.getID() + ")."); | |
116 } | 95 } |
117 } | 96 } |
118 | 97 |
119 | |
120 /** | 98 /** |
121 * Get value of line at index. | 99 * Get value of line at index. |
122 * @param index index in json array defining lines. | 100 * |
101 * @param index | |
102 * index in json array defining lines. | |
123 * @return water height of line at given index. | 103 * @return water height of line at given index. |
124 */ | 104 */ |
125 protected double getLine(int index) { | 105 private double getLine(final int index) { |
126 try { | 106 try { |
127 JSONArray lines = new JSONArray(getLinesData(null)); | 107 final JSONArray lines = new JSONArray(getLinesData(null)); |
128 JSONArray array = lines.getJSONArray(index); | 108 final JSONArray array = lines.getJSONArray(index); |
129 | 109 |
130 return array.getDouble(0); | 110 return array.getDouble(0); |
131 } | 111 } |
132 catch(JSONException e){ | 112 catch (final JSONException e) { |
133 log.error("Could not decode json for line."); | 113 log.error("Could not decode json for line.", e); |
134 return 0d; | 114 return 0d; |
135 } | 115 } |
136 } | 116 } |
137 | 117 |
138 | |
139 /** | 118 /** |
140 * Get the water line "surface". | 119 * Get the water line "surface". |
141 * @param index index of facets data. | 120 * |
142 * @param csl 'ground' against which to determine water surface. | 121 * @param index |
143 * @param a (ignored in this implementation). | 122 * index of facets data. |
144 * @param b (ignored in this implementation). | 123 * @param csl |
145 * @param context (ignored in this implementation). | 124 * 'ground' against which to determine water surface. |
125 * @param a | |
126 * (ignored in this implementation). | |
127 * @param b | |
128 * (ignored in this implementation). | |
129 * @param context | |
130 * (ignored in this implementation). | |
146 */ | 131 */ |
147 @Override | 132 @Override |
148 public Lines.LineData getWaterLines( | 133 public double getWaterLevel(final ComputeType type, final String hash, final String stateId, final double currentKm, final Serializable waterLineIndex, |
149 int index, | 134 final double nextKm, final double prevKm, final CallContext context) { |
150 FastCrossSectionLine csl, | 135 final int index = (int) waterLineIndex; |
151 double a, double b, | 136 return getLine(index); |
152 CallContext context | |
153 ) { | |
154 List<Point2D> points = csl.getPoints(); | |
155 return Lines.createWaterLines(points, getLine(index)); | |
156 } | 137 } |
157 } | 138 } |
158 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |