Nominatim + homebrew on OSX + OSM data + PHP = open sourced reverse geocoder
This installation guide (at the time of writing) was tested on SVN trunk of OSM2PGSQL and running on latest/stable Postgres/Postgis versions on OSX via homebrew.
1. Summary
OSX Snow Leopard OSM2PGSQL: Head http://svn.openstreetmap.org/applications/utils/export/osm2pgsql Revision: 27034 Last Changed Author: frederik Last Changed Rev: 27030 Last Changed Date: 2011-11-09 10:57:49 +1100 (Wed, 09 Nov 2011) POSTGRES: 9.0.4 POSTGIS: "POSTGIS="1.5.3" GEOS="3.3.1-CAPI-1.7.1" PROJ="Rel. 4.7.1, 23 September 2009" LIBXML="2.7.3" USE_STATS" # SELECT POSTGIS_FULL_VERSION(); PHP: 5.3.8 (cli) (built: Nov 14 2011 14:41:52) #php -v
2. Installation
Most of the software is installed via homebrew.
# Install homebrew % /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)" # Install postgresql % brew install postgresql % initdb -E utf8 -D /usr/local/var/postgres % cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/ % launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist % psql -d postgres -f /usr/local/Cellar/postgresql/9.0.4/share/postgresql/contrib/adminpack.sql # Install postgis % brew install proj % brew install geos % brew install postgis # Create template_postgis_osm % createdb -E utf8 template_postgis_osm % psql -d template_postgis_osm -f "/usr/local/Cellar/postgresql/9.0.4/share/postgresql/contrib/pg_trgm.sql" % psql -d template_postgis_osm -f /usr/local/Cellar/postgis/1.5.3/share/postgis/postgis.sql % psql -d template_postgis_osm -f /usr/local/Cellar/postgis/1.5.3/share/postgis/spatial_ref_sys.sql # Install osm2pgsql. Can skip this. # % brew install osm2pgsql
For detail instructions on installing Postgres/Postgis via Homebrew, read this homebrew + postgresql9.0.4 + postgis.1.5.3 + proj4 + geos3.3.1 + osm2pgsql. If you are having problems installing GEOS, then read that link as it shows you how to upgrade GEOS to 3.3.1.
OSM2PGSQL needs GEOS as well. Note that brew only install the osm2pgsql binary. Don’t worry, we will compile this via source later.
3. More Installation.
We need to get PHP installed to run gazetteer http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/gazetteer/website/
# Install PHP % brew install php --with-mysql --with-pgsql --with-apache # Hookup with Apache # Edit httpd.conf to LoadModule # Install PEAR DB % pear install db
4. OSM2PGSQL
Read this wiki: http://wiki.openstreetmap.org/wiki/Osm2pgsql. Well, we eventually need the whole OSM2PGSQL source as it contains the website (gazetteer).
% svn co http://svn.openstreetmap.org/applications/utils/export/osm2pgsql osm2pgsql % cd osm2pgsql % ./autogen.sh % ./configure % make # At this point there should be an osm2pgsql binary.
We need to compile gazetteer for gazetteer.so which is used by gazetteer-functions.sql
gis/osm2pgsql/gazetteer% make clean gis/osm2pgsql/gazetteer% make gis/osm2pgsql/gazetteer% make install test -z "/usr/local/lib/osm2pgsql" || .././install-sh -c -d "/usr/local/lib/osm2pgsql" /bin/sh ../libtool --mode=install /usr/bin/install -c 'gazetteer.la' '/usr/local/lib/osm2pgsql/gazetteer.la' libtool: install: /usr/bin/install -c .libs/gazetteer.so /usr/local/lib/osm2pgsql/gazetteer.so libtool: install: /usr/bin/install -c .libs/gazetteer.lai /usr/local/lib/osm2pgsql/gazetteer.la ---------------------------------------------------------------------- Libraries have been installed in: /usr/local/lib/osm2pgsql If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable during execution See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- test -z "/usr/local/share/gazetteer" || .././install-sh -c -d "/usr/local/share/gazetteer" /usr/bin/install -c -m 644 'extract_countrynames.sql' '/usr/local/share/gazetteer/extract_countrynames.sql' /usr/bin/install -c -m 644 'gazetteer-index.sql' '/usr/local/share/gazetteer/gazetteer-index.sql' /usr/bin/install -c -m 644 'gazetteer-loaddata.sql' '/usr/local/share/gazetteer/gazetteer-loaddata.sql' /usr/bin/install -c -m 644 'gazetteer-tables.sql' '/usr/local/share/gazetteer/gazetteer-tables.sql' /usr/bin/install -c -m 644 'import_country_name.sql' '/usr/local/share/gazetteer/import_country_name.sql' /usr/bin/install -c -m 644 'import_country_osm_grid.sql' '/usr/local/share/gazetteer/import_country_osm_grid.sql' /usr/bin/install -c -m 644 'import_gb_postcodearea.sql' '/usr/local/share/gazetteer/import_gb_postcodearea.sql' /usr/bin/install -c -m 644 'import_gb_postcode.sql' '/usr/local/share/gazetteer/import_gb_postcode.sql' /usr/bin/install -c -m 644 'import_specialwords.sql' '/usr/local/share/gazetteer/import_specialwords.sql' /usr/bin/install -c -m 644 'import_us_statecounty.sql' '/usr/local/share/gazetteer/import_us_statecounty.sql' /usr/bin/install -c -m 644 'import_us_state.sql' '/usr/local/share/gazetteer/import_us_state.sql' /usr/bin/install -c -m 644 'import_worldboundaries.sql' '/usr/local/share/gazetteer/import_worldboundaries.sql' /usr/bin/install -c -m 644 'gazetteer-functions.sql' '/usr/local/share/gazetteer/gazetteer-functions.sql'
5. Download Data
You can get some regional OSM data from cloudmade. http://downloads.cloudmade.com/oceania/australia_and_new_zealand/australia/victoria
I suggest you download a regional extract prior to downloading/testing with the whole planet-osm. If you don’t believe me that it will take long, you can read http://wiki.openstreetmap.org/wiki/Nominatim/Installation
6. Load and Index Data
Basically, this is the summary of commands taken from http://wiki.openstreetmap.org/wiki/Nominatim/Installation At the time of writing this, I had issues such as “planet_osm_ways” (and several tables) does not exist. So I did a pg_dump and restored the tables afterwards. Be very careful with using the script below, you can comment the indexing part just to speed up on loading and see if you have errors, etc.
DATABASE_NAME=gazetteer_vic OSM2PGSQL_HOME=/Users/rupert/projects/gis/osm2pgsql SOURCE_DATA=/Users/rupert/Desktop/australia/victoria.osm DUMP_DIR=/Users/rupert/Desktop/pg_dumps/streetlookup dropdb $DATABASE_NAME #dropuser www-data createdb $DATABASE_NAME -E UTF8 -T template_postgis_osm createuser -SDR www-data # This will create the planet_osm_ways, etc $OSM2PGSQL_HOME/osm2pgsql --create --latlong --database $DATABASE_NAME --username rupert --slim --prefix planet_osm --cache 2048 $SOURCE_DATA pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_ways.backup" --table public.planet_osm_ways $DATABASE_NAME pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_nodes.backup" --table public.planet_osm_nodes $DATABASE_NAME pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_rels.backup" --table public.planet_osm_rels $DATABASE_NAME # This will create the place table $OSM2PGSQL_HOME/osm2pgsql --latlong -O gazetteer --database $DATABASE_NAME --username rupert --slim --prefix planet_osm --cache 2048 $SOURCE_DATA pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_ways.backup" pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_nodes.backup" pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_rels.backup" rm -Rf $DUMP_DIR/*.backup psql -d $DATABASE_NAME -q -f import_country_osm_grid.sql psql -d $DATABASE_NAME -q -f import_worldboundaries.sql psql -d $DATABASE_NAME -q -f import_country_name.sql psql -d $DATABASE_NAME -q -f import_gb_postcode.sql psql -d $DATABASE_NAME -q -f import_gb_postcodearea.sql psql -d $DATABASE_NAME -q -f import_us_state.sql psql -d $DATABASE_NAME -q -f import_us_statecounty.sql psql -d $DATABASE_NAME -f gazetteer-functions.sql psql -d $DATABASE_NAME -f gazetteer-tables.sql psql -d $DATABASE_NAME -f gazetteer-functions.sql psql -d $DATABASE_NAME -f gazetteer-loaddata.sql #Indexing psql -d $DATABASE_NAME -f gazetteer-index.sql
Save this as run.sh in /Users/rupert/projects/gis/osm2pgsql/gazetteer
Where do you run this?
% cd /Users/rupert/projects/gis/osm2pgsql/gazetteer % sh run.sh
6. Test
If you are successful, you should have a “placex” table. Now that we have a postgis database running, you can now run spatial statements thru pgadmin. See the guts of reverse.php
SELECT * FROM placex WHERE ST_DWithin( ST_SetSRID(ST_Point(145.234377, -37.856320),4326), geometry, 0.0001) AND ST_GeometryType(geometry) NOT IN ('ST_Polygon', 'ST_MultiPolygon')
This one took only 21 ms.
7. Website
Make sure www-data have permissions to the tables. Rememeber to replace gazetteer_vic with your DATABASE_NAME.
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" gazetteer_vic` ; do psql -c "alter table $tbl owner to \"www-data\"" gazetteer_vic; done
Assuming you have PHP and PEAR DB installed. Then update the data connection settings found in http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/gazetteer/website/.htlib/settings.php
Run the same query but using reverse.php.

Installing pear DB
php.ini configuration:
To install pear in freebsd via ports, go to /usr/ports/database/pear-DB then do a make && make install.
[root@genghis /usr/ports/databases/pear-DB]# ls Makefile distinfo pkg-descr [root@genghis /usr/ports/databases/pear-DB]# make ===> WARNING: Vulnerability database out of date, checking anyway ===> License check disabled, port has not defined LICENSE => DB-1.7.13.tgz doesn't seem to exist in /usr/ports/distfiles/PEAR. => Attempting to fetch http://pear.php.net/get/DB-1.7.13.tgz DB-1.7.13.tgz 100% of 129 kB 62 kBps ===> Extracting for pear-DB-1.7.13,1 => SHA256 Checksum OK for PEAR/DB-1.7.13.tgz. ===> Patching for pear-DB-1.7.13,1 ===> pear-DB-1.7.13,1 depends on file: /usr/local/share/pear/PEAR.php - not found ===> Verifying install for /usr/local/share/pear/PEAR.php in /usr/ports/devel/pear ===> WARNING: Vulnerability database out of date, checking anyway ===> License check disabled, port has not defined LICENSE => pear-1.9.3.tar.bz2 doesn't seem to exist in /usr/ports/distfiles/. => Attempting to fetch http://bsdcrew.de/distfiles/pear-1.9.3.tar.bz2 pear-1.9.3.tar.bz2 100% of 321 kB 78 kBps ===> Extracting for pear-1.9.3 => SHA256 Checksum OK for pear-1.9.3.tar.bz2. ===> Patching for pear-1.9.3 ===> Applying FreeBSD patches for pear-1.9.3 ===> Configuring for pear-1.9.3 ===> Installing for pear-1.9.3 ===> pear-1.9.3 depends on file: /usr/local/include/php/main/php.h - found ===> pear-1.9.3 depends on file: /usr/local/lib/php/20060613/pcre.so - found ===> pear-1.9.3 depends on file: /usr/local/lib/php/20060613/xml.so - found ===> Generating temporary packing list ===> Checking if devel/pear already installed ===> Warning: your umask is "0002". If this is not desired, set it to an appropriate value and install this port again by ``make reinstall''. Bootstrapping Installer................... Bootstrapping PEAR.php............(local) ok Bootstrapping Archive/Tar.php............(local) ok Bootstrapping Console/Getopt.php............(local) ok Extracting installer.................. Using local package: PEAR.............ok Using local package: Structures_Graph....ok Preparing installer.................. Updating channel "doc.php.net" Update of Channel "doc.php.net" succeeded Updating channel "pear.php.net" Update of Channel "pear.php.net" succeeded Updating channel "pecl.php.net" Update of Channel "pecl.php.net" succeeded Installing selected packages.................. Installing bootstrap package: PEAR...................warning: pear/PEAR requires package "pear/Archive_Tar" (version >= 1.3.7) warning: pear/PEAR requires package "pear/Structures_Graph" (recommended version 1.0.4) warning: pear/PEAR requires package "pear/Console_Getopt" (recommended version 1.2.3) warning: pear/PEAR requires package "pear/XML_Util" (recommended version 1.2.1) install ok: channel://pear.php.net/PEAR-1.9.3 PEAR: Optional feature webinstaller available (PEAR's web-based installer) PEAR: Optional feature gtkinstaller available (PEAR's PHP-GTK-based installer) PEAR: Optional feature gtk2installer available (PEAR's PHP-GTK2-based installer) PEAR: To install optional features use "pear install pear/PEAR#featurename" Installing bootstrap package: Structures_Graph.......warning: pear/pear dependency package "pear/Structures_Graph" downloaded version 1.0.3 is not the recommended version 1.0.4 install ok: channel://pear.php.net/Structures_Graph-1.0.3 Installing local package: Archive_Tar-stable.........install ok: channel://pear.php.net/Archive_Tar-1.3.7 Installing local package: Console_Getopt-stable.......install ok: channel://pear.php.net/Console_Getopt-1.2.3 ===> Registering installation for pear-1.9.3 ===> Returning to build of pear-DB-1.7.13,1 ===> Configuring for pear-DB-1.7.13,1 [root@genghis /usr/ports/databases/pear-DB]# make install clean ===> Installing for pear-DB-1.7.13,1 ===> pear-DB-1.7.13,1 depends on file: /usr/local/share/pear/PEAR.php - found ===> pear-DB-1.7.13,1 depends on executable: pear - found ===> Generating packing list ===> Generating temporary packing list ===> Checking if databases/pear-DB already installed ===> Warning: your umask is "0002". If this is not desired, set it to an appropriate value and install this port again by ``make reinstall''. ===> Installing documentation in /usr/local/share/doc/pear/DB. ===> Installing tests in /usr/local/share/pear/tests/DB. install ok: channel://pear.php.net/DB-1.7.13 ===> Registering installation for pear-DB-1.7.13,1 ===> Cleaning for pear-1.9.3 ===> Cleaning for pear-DB-1.7.13,1 [root@genghis /usr/ports/databases/pear-DB]# which pear /usr/local/bin/pear