Index: src/uk/me/parabola/mkgmap/build/Locator.java
===================================================================
--- src/uk/me/parabola/mkgmap/build/Locator.java	(revision 3508)
+++ src/uk/me/parabola/mkgmap/build/Locator.java	(working copy)
@@ -20,9 +20,9 @@
 
 import uk.me.parabola.log.Logger;
 import uk.me.parabola.mkgmap.general.MapPoint;
-import uk.me.parabola.mkgmap.general.MapPointKdTree;
 import uk.me.parabola.mkgmap.reader.osm.Tags;
 import uk.me.parabola.util.EnhancedProperties;
+import uk.me.parabola.util.KdTree;
 import uk.me.parabola.util.MultiHashMap;
 
 public class Locator {
@@ -31,7 +31,7 @@
     /** hash map to collect equally named MapPoints*/ 
 	private final MultiHashMap<String, MapPoint> cityMap = new MultiHashMap<String, MapPoint>();
 	
-	private final MapPointKdTree cityFinder = new MapPointKdTree();
+	private final KdTree cityFinder = new KdTree();
 	private final List<MapPoint> placesMap  =  new ArrayList<MapPoint>();
 
 	/** Contains the tags defined by the option name-tag-list */
@@ -251,7 +251,7 @@
 	
 	public MapPoint findNextPoint(MapPoint p)
 	{
-		return cityFinder.findNextPoint(p);
+		return (MapPoint) cityFinder.findNextPoint(p);
 	}
 	
 	public MapPoint findNearbyCityByName(MapPoint p) {
@@ -376,7 +376,7 @@
 					} else if (locationAutofill.contains("nearest") && (runCount + 1) == maxRuns) {
 						// In the last resolve run just take info from the next
 						// known city
-						near = cityFinder.findNextPoint(place);
+						near = (MapPoint) cityFinder.findNextPoint(place);
 						if (near != null && near.getCountry() != null) {
 							if (place.getCity() == null)
 								place.setCity(place.getName());
Index: src/uk/me/parabola/mkgmap/general/MapPoint.java
===================================================================
--- src/uk/me/parabola/mkgmap/general/MapPoint.java	(revision 3508)
+++ src/uk/me/parabola/mkgmap/general/MapPoint.java	(working copy)
@@ -17,6 +17,7 @@
 
 import uk.me.parabola.imgfmt.app.Area;
 import uk.me.parabola.imgfmt.app.Coord;
+import uk.me.parabola.util.Locatable;
 
 /**
  * A point on the map.  This will appear as a symbol on the map and it will
@@ -24,7 +25,7 @@
  *
  * @author Steve Ratcliffe
  */
-public class MapPoint extends MapElement {
+public class MapPoint extends MapElement implements Locatable{
 	private Coord location;
 	private boolean isRoadNamePoi;
 
Index: src/uk/me/parabola/util/KdTree.java
===================================================================
--- src/uk/me/parabola/util/KdTree.java	(revision 0)
+++ src/uk/me/parabola/util/KdTree.java	(working copy)
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2012.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 or
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+package uk.me.parabola.util;
+
+
+import uk.me.parabola.imgfmt.app.Coord;
+
+
+/**
+ * A kd-tree (2D) implementation to solve the nearest neighbor problem.
+ * The tree is not explicitly balanced.
+ * 
+ * @author GerdP
+ *
+ */
+public class KdTree {
+	private static final boolean ROOT_NODE_USES_LONGITUDE = false;
+	
+	private static class KdNode {
+		Locatable point;
+		KdNode left;
+		KdNode right;
+
+		KdNode(Locatable p) {
+			point = p;
+		}
+	}
+	// the tree root
+    private KdNode root;
+    // number of saved MapPoint objects  
+    private int size;
+
+    // helpers 
+    private Locatable nextPoint ;
+    private double minDist;
+
+    /**
+     *  create an empty tree
+     */
+	public KdTree() {
+		root = null;
+	}
+
+	public long size()
+	{
+		return size;
+	}
+
+	
+	/**
+	 * Start the add action with the root
+	 * @param toAdd
+	 */
+	public void add(Locatable toAdd) {
+		size++;
+		root = add(toAdd, root, ROOT_NODE_USES_LONGITUDE);
+	}
+
+	/**
+	 * Compares the given axis of both points. 
+	 * @param longitude <code>true</code>: compare longitude; <code>false</code> compare latitude
+	 * @param c1 a point
+	 * @param c2 another point
+	 * @return <code>true</code> the axis value of c1 is smaller than c2; 
+	 * 		<code>false</code> the axis value of c1 is equal or larger than c2
+	 */
+	private boolean isSmaller(boolean longitude, Coord c1, Coord c2) {
+		if (longitude) {
+			return c1.getLongitude() < c2.getLongitude();
+		} else {
+			return c1.getLatitude() < c2.getLatitude();
+		}
+	}
+	
+	/**
+	 * Recursive routine to find the right place for inserting 
+	 * into the tree.  
+	 * @param toAdd the point
+	 * @param tree the subtree root node where to add (maybe <code>null</code>)
+	 * @param useLongitude <code>true</code> the tree node uses longitude for comparison; 
+	 * 		<code>false</code> the tree node uses latitude for comparison
+	 * @return the subtree root node after insertion
+	 */
+    private KdNode add( Locatable toAdd, KdNode tree,  boolean useLongitude){
+        if( tree == null ) {
+            tree = new KdNode( toAdd );
+        } else {
+        	if(isSmaller(useLongitude, toAdd.getLocation(), tree.point.getLocation())) {
+        		tree.left = add(toAdd, tree.left, !useLongitude);
+        	} else {
+        		tree.right = add(toAdd, tree.right, !useLongitude);
+        	}
+        }
+        return tree;
+    }
+    
+	/**
+	 * Searches for the point that has smallest distance to the given point.
+	 * @param p the point to search for
+	 * @return the point with shortest distance to <var>p</var>
+	 */
+	public Locatable findNextPoint(Locatable p) {
+		// reset 
+		minDist = Double.MAX_VALUE;
+		nextPoint = null;
+		
+		// false => first node is a latitude level
+		return findNextPoint(p, root, ROOT_NODE_USES_LONGITUDE);
+	}
+
+	private Locatable findNextPoint(Locatable p, KdNode tree, boolean useLongitude) {
+		boolean continueWithLeft = false;
+		if (tree == null)
+			return nextPoint;
+		
+		if (tree.left == null && tree.right == null){
+			double dist = tree.point.getLocation().distanceInDegreesSquared(p.getLocation());
+			if (dist < minDist){
+				nextPoint = tree.point;
+				minDist = dist;
+			}
+			return nextPoint;
+		}
+		else {
+			if (isSmaller(useLongitude, p.getLocation(), tree.point.getLocation())){
+				continueWithLeft = false;
+				nextPoint = findNextPoint(p, tree.left, !useLongitude);
+			}
+			else {
+				continueWithLeft = true;
+				nextPoint = findNextPoint(p, tree.right, !useLongitude);
+			}
+		}
+		
+		double dist = tree.point.getLocation().distanceInDegreesSquared(p.getLocation());
+		if (dist < minDist){
+			nextPoint = tree.point;
+			minDist = dist;
+		}
+		
+		// do we have to search the other part of the tree?
+		Coord test;
+		if (useLongitude)
+			test = Coord.makeHighPrecCoord(p.getLocation().getHighPrecLat(), tree.point.getLocation().getHighPrecLon());
+		else
+			test = Coord.makeHighPrecCoord(tree.point.getLocation().getHighPrecLat(), p.getLocation().getHighPrecLon());
+		if (test.distanceInDegreesSquared(p.getLocation()) < minDist){
+			if (continueWithLeft) 
+				nextPoint = findNextPoint(p, tree.left, !useLongitude);
+			else
+				nextPoint = findNextPoint(p, tree.right, !useLongitude);
+		}
+		return nextPoint;
+	}
+} 
Index: src/uk/me/parabola/util/Locatable.java
===================================================================
--- src/uk/me/parabola/util/Locatable.java	(revision 0)
+++ src/uk/me/parabola/util/Locatable.java	(working copy)
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2015 Gerd Petermann
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 or
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+package uk.me.parabola.util;
+
+import uk.me.parabola.imgfmt.app.Coord;
+
+/**
+ * For objects that have a location which can be represented as a Coord instance.
+ * 
+ * @author Gerd Petermann
+ */
+public interface Locatable {
+
+	/**
+	 * get the location of the object
+	 */
+	public Coord getLocation();
+}
Index: test/uk/me/parabola/mkgmap/general/MapPointKdTreeTest.java
===================================================================
--- test/uk/me/parabola/mkgmap/general/MapPointKdTreeTest.java	(revision 3508)
+++ test/uk/me/parabola/mkgmap/general/MapPointKdTreeTest.java	(working copy)
@@ -17,12 +17,13 @@
 import org.junit.Test;
 
 import uk.me.parabola.imgfmt.app.Coord;
+import uk.me.parabola.util.KdTree;
 
 public class MapPointKdTreeTest {
 
 	@Test
 	public void TestFindNextPoint(){
-        MapPointKdTree t = new MapPointKdTree( );
+        KdTree t = new KdTree( );
         
         int [][]test = {{70,20}, {50,40}, {90,60}, {20,30}, {40,70}, {80,10}, {-10,20}, {-30,-40} }  ;
         Coord []testCoords = new Coord[test.length]; 
@@ -48,7 +49,7 @@
         			}
         		}
         		toFind.setLocation(co);
-        		MapPoint next = t.findNextPoint(toFind);
+        		MapPoint next = (MapPoint) t.findNextPoint(toFind);
     			double dist =  next.getLocation().distanceInDegreesSquared(co);
     			double delta = Math.abs(dist - minDist); 
     			// if this test fails because 
