logo separator

[mkgmap-dev] [PATCH 03/14] Refactor to pass Nodes as objects

From Jeffrey C. Ollie jeff at ocjtech.us on Thu Sep 9 21:12:03 BST 2010

From: Scott Crosby <scrosby at cs.rice.edu>

---
 .../me/parabola/splitter/DensityMapCollector.java  |   11 ++------
 src/uk/me/parabola/splitter/MapProcessor.java      |   26 +++----------------
 src/uk/me/parabola/splitter/NodeCollector.java     |   12 ++-------
 src/uk/me/parabola/splitter/OSMParser.java         |   10 ++++---
 src/uk/me/parabola/splitter/SplitProcessor.java    |   20 +++------------
 5 files changed, 20 insertions(+), 59 deletions(-)

diff --git a/src/uk/me/parabola/splitter/DensityMapCollector.java b/src/uk/me/parabola/splitter/DensityMapCollector.java
index e77e3d4..ef13a01 100644
--- a/src/uk/me/parabola/splitter/DensityMapCollector.java
+++ b/src/uk/me/parabola/splitter/DensityMapCollector.java
@@ -48,9 +48,9 @@ class DensityMapCollector implements MapCollector {
 	}
 
 	@Override
-	public void startNode(int id, double lat, double lon) {
-		int glat = Utils.toMapUnit(lat);
-		int glon = Utils.toMapUnit(lon);
+	public void processNode(Node n) {
+		int glat = Utils.toMapUnit(n.getLat());
+		int glon = Utils.toMapUnit(n.getLon());
 		densityMap.addNode(glat, glon);
 		details.addToBounds(glat, glon);
 	}
@@ -61,8 +61,6 @@ class DensityMapCollector implements MapCollector {
 	@Override
 	public void startRelation(int id) {}
 
-	@Override
-	public void nodeTag(String key, String value) {}
 
 	@Override
 	public void wayTag(String key, String value) {}
@@ -80,9 +78,6 @@ class DensityMapCollector implements MapCollector {
 	public void relationWay(int wayId, String role) {}
 
 	@Override
-	public void endNode() {}
-
-	@Override
 	public void endWay() {}
 
 	@Override
diff --git a/src/uk/me/parabola/splitter/MapProcessor.java b/src/uk/me/parabola/splitter/MapProcessor.java
index ce3e3a1..536bcb1 100644
--- a/src/uk/me/parabola/splitter/MapProcessor.java
+++ b/src/uk/me/parabola/splitter/MapProcessor.java
@@ -34,14 +34,6 @@ public interface MapProcessor {
 	void boundTag(Area bounds);
 
 	/**
-	 * Called when a node is encountered.
-	 * @param id the node's ID.
-	 * @param lat the node's latitude.
-	 * @param lon the node's longitude.
-	 */
-	void startNode(int id, double lat, double lon);
-
-	/**
 	 * Called when a way is encountered.
 	 * @param id the way's ID.
 	 */
@@ -53,14 +45,11 @@ public interface MapProcessor {
 	 */
 	void startRelation(int id);
 
+	
 	/**
-	 * Called when a tag is encountered on a node. This method will be
-	 * called for every tag associated with the node that was specified
-	 * in the most recent call to {@link #startNode(int, double, double)}.
-	 * @param key the tag's key.
-	 * @param value the tag's value.
-	 */
-	void nodeTag(String key, String value);
+	 * Called when a whole node has been processed. 
+	*/
+	void processNode(Node n);
 
 	/**
 	 * Called when a tag is encountered on a way. This method will be
@@ -105,13 +94,6 @@ public interface MapProcessor {
 	void relationWay(int wayId, String role);
 
 	/**
-	 * Called when processing is complete for a node. This method will be called once
-	 * there is no further data available for the node specified in the most recent
-	 * call to {@link #startNode(int, double, double)}.
-	 */
-	void endNode();
-
-	/**
 	 * Called when processing is complete for a way. This method will be called once
 	 * there is no further data available for the way specified in the most recent
 	 * call to {@link #startWay(int)}.
diff --git a/src/uk/me/parabola/splitter/NodeCollector.java b/src/uk/me/parabola/splitter/NodeCollector.java
index 4cc3d71..6556ee6 100644
--- a/src/uk/me/parabola/splitter/NodeCollector.java
+++ b/src/uk/me/parabola/splitter/NodeCollector.java
@@ -36,12 +36,12 @@ class NodeCollector implements MapCollector {
 	}
 
 	@Override
-	public void startNode(int id, double lat, double lon) {
+	public void processNode(Node n) {
 		// Since we are rounding areas to fit on a low zoom boundary we
 		// can drop the bottom 8 bits of the lat and lon and then fit
 		// the whole lot into a single int.
-		int glat = Utils.toMapUnit(lat);
-		int glon = Utils.toMapUnit(lon);
+		int glat = Utils.toMapUnit(n.getLat());
+		int glon = Utils.toMapUnit(n.getLon());		
 		int coord = ((glat << 8) & 0xffff0000) + ((glon >> 8) & 0xffff);
 
 		coords.add(coord);
@@ -55,9 +55,6 @@ class NodeCollector implements MapCollector {
 	public void startRelation(int id) {}
 
 	@Override
-	public void nodeTag(String key, String value) {}
-
-	@Override
 	public void wayTag(String key, String value) {}
 
 	@Override
@@ -73,9 +70,6 @@ class NodeCollector implements MapCollector {
 	public void relationWay(int wayId, String role) {}
 
 	@Override
-	public void endNode() {}
-
-	@Override
 	public void endWay() {}
 
 	@Override
diff --git a/src/uk/me/parabola/splitter/OSMParser.java b/src/uk/me/parabola/splitter/OSMParser.java
index c902909..80f0c29 100644
--- a/src/uk/me/parabola/splitter/OSMParser.java
+++ b/src/uk/me/parabola/splitter/OSMParser.java
@@ -29,6 +29,8 @@ class OSMParser extends AbstractXppParser implements MapReader {
 		Node, Way, Relation, None
 	}
 
+	private Node currentNode = new Node();	
+
 	private final MapProcessor processor;
 
 	// There are mixed nodes and ways in the file
@@ -139,7 +141,8 @@ class OSMParser extends AbstractXppParser implements MapReader {
 			maxNodeId = id;
 		}
 
-		processor.startNode(id, lat, lon);
+		currentNode = new Node();
+		currentNode.set(id, lat, lon);
 		state = State.Node;
 	}
 
@@ -155,7 +158,7 @@ class OSMParser extends AbstractXppParser implements MapReader {
 
 	private void processNode(CharSequence name) {
 		if (name.equals("tag")) {
-			processor.nodeTag(getAttr("k"), getAttr("v"));
+			currentNode.addTag(getAttr("k"), getAttr("v"));
 		}
 	}
 
@@ -235,8 +238,7 @@ class OSMParser extends AbstractXppParser implements MapReader {
 	public void endElement(String name) {
 		if (state == State.Node) {
 			if (name.equals("node")) {
-				if (!startNodeOnly)
-					processor.endNode();
+				processor.processNode(currentNode);
 				state = State.None;
 				nodeCount++;
 				if (nodeCount % NODE_STATUS_UPDATE_THRESHOLD == 0) {
diff --git a/src/uk/me/parabola/splitter/SplitProcessor.java b/src/uk/me/parabola/splitter/SplitProcessor.java
index 92578df..f02c1af 100644
--- a/src/uk/me/parabola/splitter/SplitProcessor.java
+++ b/src/uk/me/parabola/splitter/SplitProcessor.java
@@ -33,7 +33,6 @@ class SplitProcessor implements MapProcessor {
 	private final BlockingQueue<InputQueueInfo> writerInputQueue;
 	private final ArrayList<Thread> workerThreads;
 
-	private Node currentNode = new Node();
 	private int currentNodeAreaSet;
 
 	private Way currentWay = new Way();
@@ -77,11 +76,6 @@ class SplitProcessor implements MapProcessor {
 	}
 
 	@Override
-	public void startNode(int id, double lat, double lon) {
-		currentNode.set(id, lat, lon);
-	}
-
-	@Override
 	public void startWay(int id) {
 		currentWay.set(id);
 	}
@@ -92,11 +86,6 @@ class SplitProcessor implements MapProcessor {
 	}
 
 	@Override
-	public void nodeTag(String key, String value) {
-		currentNode.addTag(key, value);
-	}
-
-	@Override
 	public void wayTag(String key, String value) {
 		currentWay.addTag(key, value);
 	}
@@ -172,13 +161,12 @@ class SplitProcessor implements MapProcessor {
 	}
 
 	@Override
-	public void endNode() {
+	public void processNode(Node n) {
 		try {
-			writeNode();
-			currentNode = new Node();
+			writeNode(n);
 			currentNodeAreaSet = 0;
 		} catch (IOException e) {
-			throw new RuntimeException("failed to write node " + currentNode.getId(), e);
+			throw new RuntimeException("failed to write node " + n.getId(), e);
 		}
 	}
 
@@ -225,7 +213,7 @@ class SplitProcessor implements MapProcessor {
 		}
 	}
 
-	private void writeNode() throws IOException {
+	private void writeNode(Node currentNode) throws IOException {
 		for (int n = 0; n < writers.length; n++) {
 			boolean found = writers[n].nodeBelongsToThisArea(currentNode); 
 			if (found) {
-- 
1.7.2.3




More information about the mkgmap-dev mailing list