OsmAnd/OsmAnd-java/src/net/osmand/osm/edit/Way.java

195 lines
3.8 KiB
Java
Raw Normal View History

package net.osmand.osm.edit;
import gnu.trove.list.array.TLongArrayList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.osmand.data.LatLon;
2013-09-24 00:36:25 +02:00
import net.osmand.data.QuadRect;
public class Way extends Entity {
2016-06-08 23:00:51 +02:00
// lazy loading
private TLongArrayList nodeIds = null;
private List<Node> nodes = null;
public Way(long id) {
super(id);
}
2016-06-08 23:00:51 +02:00
public Way(Way w) {
super(w.getId());
if (w.nodeIds != null) {
nodeIds = new TLongArrayList(w.nodeIds);
}
if (w.nodes != null) {
nodes = new ArrayList<Node>(w.nodes);
}
}
2016-06-08 23:00:51 +02:00
public Way(long id, List<Node> nodes) {
super(id);
this.nodes = new ArrayList<Node>(nodes);
nodeIds = new TLongArrayList(nodes.size());
for (Node n : nodes) {
nodeIds.add(n.getId());
}
}
2016-06-08 23:00:51 +02:00
public void addNode(long id) {
if (nodeIds == null) {
nodeIds = new TLongArrayList();
}
nodeIds.add(id);
}
2016-06-08 23:00:51 +02:00
public long getFirstNodeId() {
if (nodeIds == null) {
return -1;
}
return nodeIds.get(0);
}
2016-06-08 23:00:51 +02:00
public long getLastNodeId() {
if (nodeIds == null) {
return -1;
}
return nodeIds.get(nodeIds.size() - 1);
}
2013-09-15 18:06:28 +02:00
2016-06-08 23:00:51 +02:00
public Node getFirstNode() {
if (nodes == null || nodes.size() == 0) {
2013-09-15 18:06:28 +02:00
return null;
}
return nodes.get(0);
}
2016-06-08 23:00:51 +02:00
public Node getLastNode() {
if (nodes == null || nodes.size() == 0) {
2013-09-15 18:06:28 +02:00
return null;
}
return nodes.get(nodes.size() - 1);
}
2016-06-08 23:00:51 +02:00
public void addNode(Node n) {
if (nodeIds == null) {
nodeIds = new TLongArrayList();
}
2016-06-08 23:00:51 +02:00
if (nodes == null) {
nodes = new ArrayList<Node>();
}
nodeIds.add(n.getId());
nodes.add(n);
}
2016-06-08 23:00:51 +02:00
public void addNode(Node n, int index) {
if (nodeIds == null) {
nodeIds = new TLongArrayList();
}
2016-06-08 23:00:51 +02:00
if (nodes == null) {
nodes = new ArrayList<Node>();
}
nodeIds.insert(index, n.getId());
nodes.add(index, n);
}
2016-06-08 23:00:51 +02:00
public long removeNodeByIndex(int i) {
if (nodeIds == null) {
return -1;
}
long toReturn = nodeIds.removeAt(i);
2016-06-08 23:00:51 +02:00
if (nodes != null && nodes.size() > i) {
nodes.remove(i);
}
return toReturn;
}
2016-06-08 23:00:51 +02:00
public TLongArrayList getNodeIds() {
if (nodeIds == null) {
return new TLongArrayList(0);
}
return nodeIds;
}
2016-06-08 23:00:51 +02:00
public List<EntityId> getEntityIds() {
if (nodeIds == null) {
return Collections.emptyList();
}
List<EntityId> ls = new ArrayList<EntityId>();
for (int i = 0; i < nodeIds.size(); i++) {
ls.add(new EntityId(EntityType.NODE, nodeIds.get(i)));
}
return ls;
}
2016-06-08 23:00:51 +02:00
public List<Node> getNodes() {
2016-06-08 23:00:51 +02:00
if (nodes == null) {
return Collections.emptyList();
}
return nodes;
}
2016-06-08 23:00:51 +02:00
@Override
public void initializeLinks(Map<EntityId, Entity> entities) {
if (nodeIds != null) {
2016-06-08 23:00:51 +02:00
if (nodes == null) {
nodes = new ArrayList<Node>();
} else {
nodes.clear();
}
int nIsize = nodeIds.size();
for (int i = 0; i < nIsize; i++) {
2016-06-08 23:00:51 +02:00
nodes.add((Node) entities.get(new EntityId(EntityType.NODE, nodeIds.get(i))));
}
}
}
2013-09-24 00:36:25 +02:00
public QuadRect getLatLonBBox() {
QuadRect qr = null;
2016-06-08 23:00:51 +02:00
if (nodes != null) {
for (Node n : nodes) {
if (qr == null) {
2013-09-24 00:47:01 +02:00
qr = new QuadRect();
2013-09-24 00:36:25 +02:00
qr.left = (float) n.getLongitude();
qr.right = (float) n.getLongitude();
2016-06-08 23:00:51 +02:00
qr.top = (float) n.getLatitude();
2013-09-24 00:36:25 +02:00
qr.bottom = (float) n.getLatitude();
}
2016-06-08 23:00:51 +02:00
if (n.getLongitude() < qr.left) {
2013-09-24 00:36:25 +02:00
qr.left = (float) n.getLongitude();
2016-06-08 23:00:51 +02:00
} else if (n.getLongitude() > qr.right) {
2013-09-24 00:36:25 +02:00
qr.right = (float) n.getLongitude();
}
2016-06-08 23:00:51 +02:00
if (n.getLatitude() > qr.top) {
2013-09-24 00:36:25 +02:00
qr.top = (float) n.getLatitude();
2016-06-08 23:00:51 +02:00
} else if (n.getLatitude() < qr.bottom) {
2013-09-24 00:36:25 +02:00
qr.bottom = (float) n.getLatitude();
}
}
}
return qr;
}
2016-06-08 23:00:51 +02:00
@Override
public LatLon getLatLon() {
2016-06-08 23:00:51 +02:00
if (nodes == null) {
return null;
}
2015-07-07 20:28:17 +02:00
return OsmMapUtils.getWeightCenterForWay(this);
}
2013-08-29 00:36:26 +02:00
2016-06-08 23:00:51 +02:00
public void reverseNodes() {
if (nodes != null) {
Collections.reverse(nodes);
}
if (nodeIds != null) {
nodeIds.reverse();
;
}
}
}