Setting Up Your Own Geocoder Part 2

So you have two options for setting up the server for your geocoder. Option 1 is to provision a server (Part 1 of this guide covers setting up a Linux server). Option 2 is to locally host it on your current device. While users have reported on Github that its possible to run pelias in a Windows environment, I wasn’t able to successfully do it. Odds are that if you have the technical expertise to do so, you also wouldn’t need this guide. Otherwise using your device as a local host is very possible in a Linux or MacOS environment. Option is really only an option for a single user. If the geocoder is meant to be used full time, it really should be deployed on a Linux server.

Before we continue, it is assumed that you read Part 1 and have Docker Engine and Docker Compose installed. Beyond docker, Git and util-linux should also be installed on your server. Git and util-linux are usually installed by default. You can confirm by running the following command in the terminal:

git --version

If git isn’t installed, i.e., the above command returns nothing or an error, you can install it by using the following commands:

sudo apt install git
git --version

If you need util-linux, the following command installs it:

sudo apt-get install util-linux

The following commands will install Pelias for you. Keep in mind, the process may take several days depending on the dataset you use and the length of time you hang out on the machine. For example, it took me 3 days to install the North American dataset while monitoring it during my 8-5. Keep in mind that the following script uses the Portland-metro dataset (recommended initial install).

set -x

# change directory to the where you would like to install Pelias
# cd /path/to/install

# clone this repository
git clone https://github.com/pelias/docker.git && cd docker

# install pelias script
# this is the _only_ setup command that should require `sudo`
sudo ln -s "$(pwd)/pelias" /usr/local/bin/pelias

# cd into the project directory
cd projects/portland-metro

# create a directory to store Pelias data files
# see: https://github.com/pelias/docker#variable-data_dir
mkdir ./data
sed -i '/DATA_DIR/d' .env
echo 'DATA_DIR=./data' >> .env

# run build
pelias compose pull
pelias elastic start
pelias elastic wait
pelias elastic create
pelias download all
pelias prepare all
pelias import all
pelias compose up

# optionally run tests
pelias test run

And thats it! Pelias is all set up. Now lets test it. Generally speaking, you’re going to use a get request to return the information from your Pelias geocoder. However, it also works using your web browser, so lets start there. The syntax of your url is the following:

http://<server_ip>:4000/v1/search?text={street_no}%20{street_name}%20{street_type}%20{city}%20{state_abbr}

Now lets discuss. I can’t tell you what your server ip is. That information you’ll have to determine based on your server. If you’re running it on your local machine, e.g., you installed it on your personal device, you can usually use either localhost or 127.0.0.1. The :4000 refers to port 4000. That is assigned by elastic. So your beginning url, assuming you’re running Pelias on the same device, will be http://127.0.0.1:4000/v1/search?text=

Fill in the appropriate information for the address you’re looking for in the curly brackets. For example, if I wanted to look up the geocoded location of my alma mater, UC Santa Cruz, I would first look up its address, which is 1156 High St, Santa Cruz, CA 95064. The url would then be http://127.0.0.1:4000/v1/search?text=1156%20high%20st%20santa%20cruz%20ca%2095064

When you enter that into your browser, you’re going to get back a long json string. If I wanted to do this in python, mainly because you’re probably not going through all this trouble to just geocode one address, the basic script would be:

import requests
import json

url = requests.get("http://127.0.0.1:4000/v1/search?text=1156%20high%20st%20santa%20cruz%20ca%2095064", 
headers = {"content-type":"application/json"})

url = json.loads(url.text)
my_points = url["features"][0]["geometry"]["coordinates"]

The featured image is by NASA on Unsplash.

Leave a comment