source: josm/trunk/src/org/openstreetmap/josm/data/vector/VectorWay.java@ 17862

Last change on this file since 17862 was 17862, checked in by simon04, 3 years ago

fix #17177 - Add support for Mapbox Vector Tile (patch by taylor.smock)

Signed-off-by: Taylor Smock <tsmock@…>

File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.vector;
3
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7import java.util.stream.Collectors;
8
9import org.openstreetmap.josm.data.osm.BBox;
10import org.openstreetmap.josm.data.osm.INode;
11import org.openstreetmap.josm.data.osm.IWay;
12import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
13import org.openstreetmap.josm.data.osm.UniqueIdGenerator;
14import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
15
16/**
17 * The "Way" type for a Vector layer
18 *
19 * @author Taylor Smock
20 * @since xxx
21 */
22public class VectorWay extends VectorPrimitive implements IWay<VectorNode> {
23 private static final UniqueIdGenerator WAY_GENERATOR = new UniqueIdGenerator();
24 private final List<VectorNode> nodes = new ArrayList<>();
25 private BBox cachedBBox;
26
27 /**
28 * Create a new way for a layer
29 * @param layer The layer for the way
30 */
31 public VectorWay(String layer) {
32 super(layer);
33 }
34
35 @Override
36 public UniqueIdGenerator getIdGenerator() {
37 return WAY_GENERATOR;
38 }
39
40 @Override
41 public void accept(PrimitiveVisitor visitor) {
42 visitor.visit(this);
43 }
44
45 @Override
46 public BBox getBBox() {
47 if (this.cachedBBox == null) {
48 BBox tBBox = new BBox();
49 for (INode node : this.getNodes()) {
50 tBBox.add(node.getBBox());
51 }
52 this.cachedBBox = tBBox.toImmutable();
53 }
54 return this.cachedBBox;
55 }
56
57 @Override
58 public int getNodesCount() {
59 return this.getNodes().size();
60 }
61
62 @Override
63 public VectorNode getNode(int index) {
64 return this.getNodes().get(index);
65 }
66
67 @Override
68 public List<VectorNode> getNodes() {
69 return Collections.unmodifiableList(this.nodes);
70 }
71
72 @Override
73 public void setNodes(List<VectorNode> nodes) {
74 this.nodes.forEach(node -> node.removeReferrer(this));
75 this.nodes.clear();
76 nodes.forEach(node -> node.addReferrer(this));
77 this.nodes.addAll(nodes);
78 this.cachedBBox = null;
79 }
80
81 @Override
82 public List<Long> getNodeIds() {
83 return this.getNodes().stream().map(VectorNode::getId).collect(Collectors.toList());
84 }
85
86 @Override
87 public long getNodeId(int idx) {
88 return this.getNodes().get(idx).getId();
89 }
90
91 @Override
92 public boolean isClosed() {
93 return this.firstNode() != null && this.firstNode().equals(this.lastNode());
94 }
95
96 @Override
97 public VectorNode firstNode() {
98 if (this.nodes.isEmpty()) {
99 return null;
100 }
101 return this.getNode(0);
102 }
103
104 @Override
105 public VectorNode lastNode() {
106 if (this.nodes.isEmpty()) {
107 return null;
108 }
109 return this.getNode(this.getNodesCount() - 1);
110 }
111
112 @Override
113 public boolean isFirstLastNode(INode n) {
114 if (this.nodes.isEmpty()) {
115 return false;
116 }
117 return this.firstNode().equals(n) || this.lastNode().equals(n);
118 }
119
120 @Override
121 public boolean isInnerNode(INode n) {
122 if (this.nodes.isEmpty()) {
123 return false;
124 }
125 return !this.firstNode().equals(n) && !this.lastNode().equals(n) && this.nodes.stream()
126 .anyMatch(vectorNode -> vectorNode.equals(n));
127 }
128
129 @Override
130 public OsmPrimitiveType getType() {
131 return this.isClosed() ? OsmPrimitiveType.CLOSEDWAY : OsmPrimitiveType.WAY;
132 }
133}
Note: See TracBrowser for help on using the repository browser.