1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.Main;
|
---|
5 | import org.openstreetmap.josm.data.Bounds;
|
---|
6 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
7 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
8 | import org.openstreetmap.josm.data.osm.Node;
|
---|
9 | import org.openstreetmap.josm.data.osm.Way;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Calculates the total bounding rectangle of a serie of OsmPrimitives, using the
|
---|
13 | * EastNorth values as reference.
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class BoundingXYVisitor implements Visitor {
|
---|
17 |
|
---|
18 | public EastNorth min, max;
|
---|
19 |
|
---|
20 | public void visit(Node n) {
|
---|
21 | visit(n.eastNorth);
|
---|
22 | }
|
---|
23 |
|
---|
24 | public void visit(Way w) {
|
---|
25 | for (Node n : w.nodes)
|
---|
26 | visit(n);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void visit(Relation e) {
|
---|
30 | // relations have no bounding box.
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void visit(EastNorth eastNorth) {
|
---|
34 | if (eastNorth != null) {
|
---|
35 | if (min == null)
|
---|
36 | min = eastNorth;
|
---|
37 | else if (eastNorth.east() < min.east() || eastNorth.north() < min.north())
|
---|
38 | min = new EastNorth(Math.min(min.east(), eastNorth.east()), Math.min(min.north(), eastNorth.north()));
|
---|
39 |
|
---|
40 | if (max == null)
|
---|
41 | max = eastNorth;
|
---|
42 | else if (eastNorth.east() > max.east() || eastNorth.north() > max.north())
|
---|
43 | max = new EastNorth(Math.max(max.east(), eastNorth.east()), Math.max(max.north(), eastNorth.north()));
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @return The bounding box or <code>null</code> if no coordinates have passed
|
---|
49 | */
|
---|
50 | public Bounds getBounds() {
|
---|
51 | if (min == null || max == null)
|
---|
52 | return null;
|
---|
53 | return new Bounds(Main.proj.eastNorth2latlon(min), Main.proj.eastNorth2latlon(max));
|
---|
54 | }
|
---|
55 | }
|
---|