[OSM-dev] JOSM patch to request T at H renders

Robert Hart bathterror at gmail.com
Thu Mar 22 23:54:51 GMT 2007


I've been working on a patch for JOSM that will request tiles at home
updates whenever data is uploaded.

I'd like to make this a standard feature of JOSM rather than a plugin,
because one of the big newbie problems seems to be that the map doesn't
reliably update, and they don't know how to manually request tiles.

I haven't been able to test this thoroughly yet, but as I'll be away for the
next few days, I thought I'd put it out there for review and comment.


Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20070322/a440cd8c/attachment.html>
-------------- next part --------------
Index: src/org/openstreetmap/josm/actions/UploadAction.java
===================================================================
--- src/org/openstreetmap/josm/actions/UploadAction.java	(revision 203)
+++ src/org/openstreetmap/josm/actions/UploadAction.java	(working copy)
@@ -20,6 +20,7 @@
 import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
 import org.openstreetmap.josm.io.OsmServerWriter;
+import org.openstreetmap.josm.io.TAHServerWriter;
 import org.openstreetmap.josm.tools.GBC;
 import org.xml.sax.SAXException;
 
@@ -67,6 +68,7 @@
 			return;
 
 		final OsmServerWriter server = new OsmServerWriter();
+		final TAHServerWriter tah = new TAHServerWriter();
 		final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
 		all.addAll(add);
 		all.addAll(update);
@@ -75,6 +77,7 @@
 		PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading data")){
 			@Override protected void realRun() throws SAXException {
 				server.uploadOsm(all);
+				tah.requestTiles(all);
 			}
 			@Override protected void finish() {
 				Main.main.editLayer().cleanData(server.processed, !add.isEmpty());
Index: src/org/openstreetmap/josm/io/TAHServerWriter.java
===================================================================
--- src/org/openstreetmap/josm/io/TAHServerWriter.java	(revision 0)
+++ src/org/openstreetmap/josm/io/TAHServerWriter.java	(revision 0)
@@ -0,0 +1,109 @@
+package org.openstreetmap.josm.io;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.UnknownHostException;
+import java.util.Collection;
+import java.util.LinkedList;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Segment;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
+import org.openstreetmap.josm.data.osm.visitor.Visitor;
+import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
+import org.xml.sax.SAXException;
+
+/**
+ * Class that finds tiles that have been touched and pings the T at H script
+ *
+ * @author rhart
+ */
+public class TAHServerWriter extends OsmConnection {
+
+	/**
+	 * Whether the operation should be aborted as soon as possible.
+	 */
+	private boolean cancel = false;
+
+	public void requestTiles(Collection<OsmPrimitive> list) throws SAXException {
+	    int maxi, mini, maxj, minj;
+
+	    BoundingXYVisitor v = new BoundingXYVisitor();
+	    for (OsmPrimitive osm : list) {
+		osm.visit(v);
+	    }
+	    mini = LonToTile(v.getBounds().min.lon());
+	    maxi = LonToTile(v.getBounds().max.lon());
+	    maxj = LatToTile(v.getBounds().min.lat());
+	    minj = LatToTile(v.getBounds().max.lat());
+	    System.out.println(v.getBounds() );
+	    System.out.println("tiles: " + mini + "x" + minj + " to " + maxi + "x" + maxj );
+	    Main.pleaseWaitDlg.progress.setMaximum((maxi-mini+1)*(maxj-minj+1));
+	    Main.pleaseWaitDlg.progress.setValue(0);
+
+	    try {
+		for (int i=mini;i<=maxi;i++){
+		    for (int j=minj;j<=maxj;j++){
+			sendRequest(i,j);
+			Main.pleaseWaitDlg.currentAction.setText(tr("Requsting {0} {1}...", i, j));
+			Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1);
+		    }
+		}
+	    } catch (RuntimeException e) {
+		e.printStackTrace();
+		throw new SAXException("An error occoured: "+e.getMessage());
+	    }
+	}
+
+
+	/**
+	 * Send the request. 
+	 */
+	@SuppressWarnings("unchecked")
+	private void sendRequest(int i, int j) {
+		try {
+			URL url = new URL(
+					  "http://dev.openstreetmap.org/~ojw/NeedRender/" +
+					  /* Main.pref.get("tah-server.url") + */
+					  "?x=" + i +
+					  "&y=" + j + 
+					  "&priority=" + 1 +
+					  "&src=" + "JOSM");
+			System.out.println("upload to: "+url);
+			activeConnection = (HttpURLConnection)url.openConnection();
+			activeConnection.setConnectTimeout(15000);
+			//			activeConnection.setRequestMethod();
+			activeConnection.connect();
+
+			//not sure we really care.
+			int retCode = activeConnection.getResponseCode();
+
+		} catch (UnknownHostException e) {
+			throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e);
+		} catch (Exception e) {
+			if (cancel)
+				return; // assume cancel
+			if (e instanceof RuntimeException)
+				throw (RuntimeException)e;
+			throw new RuntimeException(e.getMessage(), e);
+		}
+	}
+    private int LatToTile(double lat) {
+	double latr = lat*Math.PI / 180.0;
+	return (int)(4096.0 * ( Math.PI - ( Math.log( Math.tan(latr) + (1/Math.cos(latr)) ) ))/(2*Math.PI));
+    }
+    private int LonToTile(double lon) {
+	return (int)(4096.0*(lon+180.0)/360.0);
+    }
+}


More information about the dev mailing list