1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.concurrent.ConcurrentHashMap;
|
---|
9 | import java.util.stream.Stream;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * The details of a layer of this WMS server.
|
---|
15 | */
|
---|
16 | public class LayerDetails {
|
---|
17 | private final Map<String, String> styles = new ConcurrentHashMap<>(); // name -> title
|
---|
18 | private final Collection<String> crs = new ArrayList<>();
|
---|
19 | /**
|
---|
20 | * The layer name (WMS {@code Title})
|
---|
21 | */
|
---|
22 | private String title;
|
---|
23 | /**
|
---|
24 | * The layer name (WMS {@code Name})
|
---|
25 | */
|
---|
26 | private String name;
|
---|
27 | /**
|
---|
28 | * The layer abstract (WMS {@code Abstract})
|
---|
29 | * @since 13199
|
---|
30 | */
|
---|
31 | private String abstr;
|
---|
32 | private final LayerDetails parentLayer;
|
---|
33 | private Bounds bounds;
|
---|
34 | private List<LayerDetails> children = new ArrayList<>();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructor pointing to parent layer. Set to null if this is topmost layer.
|
---|
38 | * This is needed to properly handle layer attributes inheritance.
|
---|
39 | *
|
---|
40 | * @param parentLayer parent layer
|
---|
41 | */
|
---|
42 | public LayerDetails(LayerDetails parentLayer) {
|
---|
43 | this.parentLayer = parentLayer;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Returns projections that are supported by this layer.
|
---|
48 | * @return projections that are supported by this layer
|
---|
49 | */
|
---|
50 | public Collection<String> getCrs() {
|
---|
51 | Collection<String> ret = new ArrayList<>();
|
---|
52 | if (parentLayer != null) {
|
---|
53 | ret.addAll(parentLayer.getCrs());
|
---|
54 | }
|
---|
55 | ret.addAll(crs);
|
---|
56 | return ret;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Returns styles defined for this layer.
|
---|
61 | * @return styles defined for this layer
|
---|
62 | */
|
---|
63 | public Map<String, String> getStyles() {
|
---|
64 | Map<String, String> ret = new ConcurrentHashMap<>();
|
---|
65 | if (parentLayer != null) {
|
---|
66 | ret.putAll(parentLayer.getStyles());
|
---|
67 | }
|
---|
68 | ret.putAll(styles);
|
---|
69 | return ret;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns "Human readable" title of this layer
|
---|
74 | * @return "Human readable" title of this layer
|
---|
75 | * @see LayerDetails#getName()
|
---|
76 | */
|
---|
77 | public String getTitle() {
|
---|
78 | return title;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Sets title of this layer
|
---|
83 | * @param title title of this layer
|
---|
84 | * @see LayerDetails#getName()
|
---|
85 | */
|
---|
86 | public void setTitle(String title) {
|
---|
87 | this.title = title;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | *
|
---|
92 | * Citation from OGC WMS specification (WMS 1.3.0):<p><i>
|
---|
93 | * A number of elements have both a {@literal <Name>} and a {@literal <Title>}. The Name is a text string used for machine-to-machine
|
---|
94 | * communication while the Title is for the benefit of humans. For example, a dataset might have the descriptive Title
|
---|
95 | * “Maximum Atmospheric Temperature” and be requested using the abbreviated Name “ATMAX”.</i></p>
|
---|
96 | *
|
---|
97 | * And second citation:<p><i>
|
---|
98 | * If, and only if, a layer has a {@literal <Name>}, then it is a map layer that can be requested by using that Name in the
|
---|
99 | * LAYERS parameter of a GetMap request. A Layer that contains a {@literal <Name>} element is referred to as a “named
|
---|
100 | * layer” in this International Standard. If the layer has a Title but no Name, then that layer is only a category title for
|
---|
101 | * all the layers nested within.</i></p>
|
---|
102 | * @return name of this layer
|
---|
103 | */
|
---|
104 | public String getName() {
|
---|
105 | return name;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Sets the name of this Layer.
|
---|
110 | * @param name the name of this Layer
|
---|
111 | * @see LayerDetails#getName()
|
---|
112 | */
|
---|
113 | public void setName(String name) {
|
---|
114 | this.name = name;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Add style to list of styles defined by this layer
|
---|
119 | * @param name machine-to-machine name of this style
|
---|
120 | * @param title human readable title of this style
|
---|
121 | */
|
---|
122 | public void addStyle(String name, String title) {
|
---|
123 | this.styles.put(name, title == null ? "" : title);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Add projection supported by this layer
|
---|
128 | * @param crs projection code
|
---|
129 | */
|
---|
130 | public void addCrs(String crs) {
|
---|
131 | this.crs.add(crs);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Returns bounds within layer might be queried.
|
---|
136 | * @return bounds within layer might be queried
|
---|
137 | */
|
---|
138 | public Bounds getBounds() {
|
---|
139 | return bounds;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Sets bounds of this layer
|
---|
144 | * @param bounds of this layer
|
---|
145 | */
|
---|
146 | public void setBounds(Bounds bounds) {
|
---|
147 | this.bounds = bounds;
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public String toString() {
|
---|
152 | String baseName = (title == null || title.isEmpty()) ? name : title;
|
---|
153 | return abstr == null || abstr.equalsIgnoreCase(baseName) ? baseName : baseName + " (" + abstr + ')';
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Returns parent layer for this layer.
|
---|
158 | * @return parent layer for this layer
|
---|
159 | */
|
---|
160 | public LayerDetails getParent() {
|
---|
161 | return parentLayer;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * sets children layers for this layer
|
---|
166 | * @param children children of this layer
|
---|
167 | */
|
---|
168 | public void setChildren(List<LayerDetails> children) {
|
---|
169 | this.children = children;
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Returns children layers of this layer.
|
---|
175 | * @return children layers of this layer
|
---|
176 | */
|
---|
177 | public List<LayerDetails> getChildren() {
|
---|
178 | return children;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * if user may select this layer (is it possible to request it from server)
|
---|
183 | * @return true if user may select this layer, false if this layer is only grouping other layers
|
---|
184 | */
|
---|
185 | public boolean isSelectable() {
|
---|
186 | return !(name == null || name.isEmpty());
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns abstract of this layer.
|
---|
191 | * @return "Narrative description of the layer"
|
---|
192 | */
|
---|
193 | public String getAbstract() {
|
---|
194 | return abstr;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Sets abstract of this layer
|
---|
199 | * @param abstr abstract of this layer
|
---|
200 | */
|
---|
201 | public void setAbstract(String abstr) {
|
---|
202 | this.abstr = abstr;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Returns flattened stream of this layer and its children.
|
---|
207 | * @return flattened stream of this layer and its children (as well as recursively children of its children)
|
---|
208 | */
|
---|
209 | public Stream<LayerDetails> flattened() {
|
---|
210 | return Stream.concat(
|
---|
211 | Stream.of(this),
|
---|
212 | getChildren().stream().flatMap(LayerDetails::flattened)
|
---|
213 | );
|
---|
214 | }
|
---|
215 | }
|
---|