source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java@ 1640

Last change on this file since 1640 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm.visitor;
3
4import org.openstreetmap.josm.Main;
5import org.openstreetmap.josm.data.Bounds;
6import org.openstreetmap.josm.data.coor.EastNorth;
7import org.openstreetmap.josm.data.coor.LatLon;
8import org.openstreetmap.josm.data.osm.Node;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.RelationMember;
12import org.openstreetmap.josm.data.osm.Way;
13
14/**
15 * Calculates the total bounding rectangle of a series of {@link OsmPrimitive} objects, using the
16 * EastNorth values as reference.
17 * @author imi
18 */
19public class BoundingXYVisitor extends AbstractVisitor {
20
21 public EastNorth min, max;
22
23 public void visit(Node n) {
24 visit(n.getEastNorth());
25 }
26
27 public void visit(Way w) {
28 w.visitNodes(this);
29 }
30
31 public void visit(Relation e) {
32 // only use direct members
33 for (RelationMember m : e.members) {
34 if (!(m.member instanceof Relation)) {
35 m.member.visit(this);
36 }
37 }
38 }
39
40 public void visit(EastNorth eastNorth) {
41 if (eastNorth != null) {
42 if (min == null)
43 min = eastNorth;
44 else if (eastNorth.east() < min.east() || eastNorth.north() < min.north())
45 min = new EastNorth(Math.min(min.east(), eastNorth.east()), Math.min(min.north(), eastNorth.north()));
46
47 if (max == null)
48 max = eastNorth;
49 else if (eastNorth.east() > max.east() || eastNorth.north() > max.north())
50 max = new EastNorth(Math.max(max.east(), eastNorth.east()), Math.max(max.north(), eastNorth.north()));
51 }
52 }
53
54 /**
55 * @return The bounding box or <code>null</code> if no coordinates have passed
56 */
57 public Bounds getBounds() {
58 if (min == null || max == null)
59 return null;
60 return new Bounds(Main.proj.eastNorth2latlon(min), Main.proj.eastNorth2latlon(max));
61 }
62
63 /**
64 * Enlarges the calculated bounding box by 0.002 degrees.
65 * If the bounding box has not been set (<code>min</code> or <code>max</code>
66 * equal <code>null</code>) this method does not do anything.
67 */
68 public void enlargeBoundingBox() {
69 enlargeBoundingBox(0.002);
70 }
71
72 /**
73 * Enlarges the calculated bounding box by the specified number of degrees.
74 * If the bounding box has not been set (<code>min</code> or <code>max</code>
75 * equal <code>null</code>) this method does not do anything.
76 *
77 * @param enlargeDegree
78 */
79 public void enlargeBoundingBox(double enlargeDegree) {
80 if (min == null || max == null)
81 return;
82 LatLon minLatlon = Main.proj.eastNorth2latlon(min);
83 min = Main.proj.latlon2eastNorth(new LatLon(minLatlon.lat() - enlargeDegree, minLatlon.lon() - enlargeDegree));
84 LatLon maxLatlon = Main.proj.eastNorth2latlon(max);
85 max = Main.proj.latlon2eastNorth(new LatLon(maxLatlon.lat() + enlargeDegree, maxLatlon.lon() + enlargeDegree));
86 }
87}
Note: See TracBrowser for help on using the repository browser.