--- Makefile.am.orig	2012-08-26 11:09:26.078980700 +0200
+++ Makefile.am	2012-08-25 13:11:24.909415778 +0200
@@ -1,15 +1,16 @@
 ACLOCAL_AMFLAGS = -I m4
 
 DIST_SUBDIRS = legacy
 
-bin_PROGRAMS = osm2pgsql nodecachefilereader
+bin_PROGRAMS = osm2pgsql nodecachefilereader convertnodestabletofile
 
 osm2pgsql_SOURCES = build_geometry.cpp input.c middle.h middle-ram.h output-gazetteer.h output-pgsql.c rb.c sanitizer.h text-tree.h build_geometry.h input.h middle-pgsql.c osm2pgsql.c output.h output-pgsql.h rb.h sprompt.c UTF8sanitizer.c expire-tiles.c keyvals.c middle-pgsql.h osmtypes.h output-null.c parse-primitive.c parse-primitive.h parse-xml2.c parse-xml2.h pgsql.c reprojection.c sprompt.h expire-tiles.h keyvals.h middle-ram.c output-gazetteer.c output-null.h pgsql.h reprojection.h text-tree.c node-ram-cache.c wildcmp.c node-ram-cache.h node-persistent-cache.c node-persistent-cache.h binarysearcharray.c binarysearcharray.h 
 
 nodecachefilereader_SOURCES = node-persistent-cache-reader.c node-persistent-cache.c node-ram-cache.c binarysearcharray.c
 
+convertnodestabletofile_SOURCES = convert-nodes-table-to-file.c node-persistent-cache.c node-ram-cache.c binarysearcharray.c
 
 if READER_PBF
 osm2pgsql_SOURCES += parse-pbf.c parse-pbf.h fileformat.pb-c.c fileformat.pb-c.h osmformat.pb-c.c osmformat.pb-c.h
 
 fileformat.pb-c.c: protobuf/fileformat.proto
--- /dev/null	2012-08-17 16:48:12.633683002 +0200
+++ ./convert-nodes-table-to-file.c	2012-08-26 11:15:57.002085822 +0200
@@ -0,0 +1,135 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <math.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+
+#include "osmtypes.h"
+#include "output.h"
+#include "node-persistent-cache.h"
+#include "node-ram-cache.h"
+#include "binarysearcharray.h"
+
+static int scale = 100;
+
+void exit_nicely()
+{
+    fprintf(stderr, "Error occurred, cleaning up\n");
+    exit(1);
+}
+
+void printhelp(char * progname) {
+    printf("Syntax: %s flatnodesfile\n", progname);
+    printf("You need to pipe the output from the command\n");
+    printf(" psql -d osm -q -c 'COPY  (select id,lat,lon from planet_osm_nodes order by id asc) TO STDOUT WITH CSV;'\n");
+    printf("into this program. It will then create the flatnodes cache\n");
+    printf("file for you with the data from the psql dump.\n");
+    printf("(adapt psql parameters where necessary, e.g. set the\n");
+    printf(" correct database / table name)\n");
+    exit(1);
+}
+
+int main(int argc, char *argv[]) {
+    int i, p;
+    struct output_options options;
+    struct timeval start, stop;
+    osmid_t osmid;
+    double lat; double lon;
+    int node_cnt;
+    unsigned long long linesprocessed = 0;
+
+    if ((argc != 2) || (strcmp(argv[1], "--help") == 0)) {
+        printhelp(argv[0]);
+    }
+    options.append = 1;
+    options.scale = 100;
+    options.flat_node_cache_enabled = 1;
+    options.flat_node_file = argv[1];
+    init_node_persistent_cache(&options, 0, 0);
+
+    while (!feof(stdin)) {
+        int pis;
+#ifdef FIXED_POINT
+        int fplat; int fplon;
+#endif /* FIXED_POINT */
+        linesprocessed++;
+        if ((linesprocessed % 1000000) == 0) {
+            printf("%lluM lines... ", linesprocessed / 1000000); fflush(stdout);
+        }
+#ifdef FIXED_POINT
+        pis = fscanf(stdin, "%" PRIdOSMID ",%d,%d", &osmid, &fplat, &fplon);
+#else
+/* Note: this is untested. */
+        pis = fscanf(stdin, "%" PRIdOSMID ",%lf,%lf", &osmid, &lat, &lon);
+#endif /* !FIXED_POINT */
+        if (pis != 3) {
+            if (pis > 1) {
+                printf("Failed to parse line %llu\n", linesprocessed);
+                exit_nicely();
+            }
+            continue;
+        }
+#ifdef FIXED_POINT
+        lat = FIX_TO_DOUBLE(fplat);
+        lon = FIX_TO_DOUBLE(fplon);
+        lat = ((double)fplat / 100.0);
+        lon = ((double)fplon / 100.0);
+        if (1) {
+            /* This is a very dirty hack: The DOUBLE_TO_FIX macro in osm2pgsql is crap,
+             * as it generates rounding errors despite there being only 2 digits anyways.
+             * So we work around it by giving the macro values that will make it generate
+             * the correct result.
+             * The proper solution would be to fix the macro (probably to use round()
+             * instead of an integer cast) though. */
+            int chklat; int chklon;
+            chklat = DOUBLE_TO_FIX(lat);
+            chklon = DOUBLE_TO_FIX(lon);
+            if ((chklat != fplat) || (chklon != fplon)) {
+                int i;
+                /* printf("! Rounding error detected: lat %d <-> %d,  lon %d <-> %d\n", chklat, fplat, chklon, fplon); */
+                if (chklat != fplat) {
+                    for (i = -10; i <= 10; i++) {
+                        double addme = 0.001 * i;
+                        chklat = DOUBLE_TO_FIX(lat + addme);
+                        if (chklat == fplat) {
+                            /* printf("...lat can be fixed by i=%d\n", i); */
+                            lat += addme;
+                            break;
+                        }
+                    }
+                    if (i >= 10) {
+                        printf("...lat could not be fixed.\n");
+                    }
+                }
+                if (chklon != fplon) {
+                    for (i = -10; i <= 10; i++) {
+                        double addme = 0.001 * i;
+                        chklon = DOUBLE_TO_FIX(lon + addme);
+                        if (chklon == fplon) {
+                            /* printf("...lon can be fixed by i=%d\n", i); */
+                            lon += addme;
+                            break;
+                        }
+                    }
+                    if (i >= 10) {
+                        printf("...lon could not be fixed.\n");
+                    }
+                }
+            }
+        }
+#endif /* FIXED_POINT */
+        persistent_cache_nodes_set(osmid, lat, lon);
+    }
+
+    printf("EOF reached: processed %llu lines total.\n", linesprocessed);
+
+    shutdown_node_persistent_cache();
+}
+
