Merge pull request #5 from CartoDB/add-cdb_geocoder_server-extension-structure

Initial cdb_geocoder_server commit
This commit is contained in:
Rafa de la Torre 2015-11-10 11:39:39 +01:00
commit 47ff4d4d44
11 changed files with 114 additions and 2 deletions

View File

View File

@ -1,2 +0,0 @@
# Geocoder API Server
The CartoDB Geocoder SQL API server

View File

@ -1,3 +1,4 @@
results/
regression.diffs
regression.out
cdb_geocoder_server--0.0.1.sql

26
server/extension/Makefile Normal file
View File

@ -0,0 +1,26 @@
# Makefile to generate the extension out of separate sql source files.
# Once a version is released, it is not meant to be changed. E.g: once version 0.0.1 is out, it SHALL NOT be changed.
EXTENSION = cdb_geocoder_server
EXTVERSION = $(shell grep default_version $(EXTENSION).control | sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/")
DATA = $(EXTENSION)--$(EXTVERSION).sql
REGRESS = $(notdir $(basename $(wildcard sql/*test.sql)))
# postgres build stuff
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
SOURCES_DATA = $(wildcard sql/$(EXTVERSION)/*.sql)
$(DATA): $(SOURCES_DATA)
rm -f $@
cat $(SOURCES_DATA) >> $@
all: $(DATA)
# Only meant for development time, do not use once a version is released
devclean:
rm -f $(DATA)

View File

@ -0,0 +1,45 @@
# CartoDB geocoder API server extension
Postgres extension for the CartoDB geocoder API, server side.
## Dependencies
This extension is thought to be used on top of CartoDB geocoder extension, for the internal geocoder.
The following is a non-comprehensive list of dependencies:
- Postgres 9.3+
- Postgis extension
- Schema triggers extension
- CartoDB extension
## Installation into the db cluster
This requires root privileges
```
sudo make all install
```
## Execute tests
```
PGUSER=postgres make installcheck
```
## Build, install & test
One-liner:
```
sudo PGUSER=postgres make all install installcheck
```
## Install onto a cartodb user's database
Remember that **is mandatory to install it on top of cdb_geocoder**
```
psql -U postgres cartodb_dev_user_fe3b850a-01c0-48f9-8a26-a82f09e9b53f_db
```
and then:
```sql
CREATE EXTENSION cdb_geocoder_server;
```
The extension creation in the user's db requires **superuser** privileges.

View File

@ -0,0 +1,6 @@
# cdb geocoder server extension
comment = 'CartoDB server geocoder extension'
default_version = '0.0.1'
requires = 'plpythonu, schema_triggers, postgis, cdb_geocoder'
superuser = true
schema = cdb_geocoder_server

View File

@ -0,0 +1,8 @@
-- Install dependencies
CREATE EXTENSION postgis;
CREATE EXTENSION schema_triggers;
CREATE EXTENSION plpythonu;
CREATE EXTENSION cartodb;
CREATE EXTENSION cdb_geocoder;
-- Install the extension
CREATE EXTENSION cdb_geocoder_server;

View File

@ -0,0 +1,2 @@
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION cdb_geocoder_server" to load this file. \quit

View File

@ -0,0 +1,16 @@
-- Geocodes a street address given a searchtext and a state and/or country
CREATE OR REPLACE FUNCTION geocode_street(searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
RETURNS Geometry
AS $$
from heremaps import heremapsgeocoder
geocoder = heremapsgeocoder.Geocoder(app_id, app_code)
results = geocoder.geocode_address(searchtext=searchtext, city=city, state=state_province, country=country)
coordinates = geocoder.extract_lng_lat_from_result(results[0])
plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])
point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0]
return point['st_setsrid']
$$ LANGUAGE plpythonu;

View File

@ -0,0 +1,10 @@
-- Install dependencies
CREATE EXTENSION postgis;
CREATE EXTENSION schema_triggers;
CREATE EXTENSION plpythonu;
CREATE EXTENSION cartodb;
CREATE EXTENSION cdb_geocoder;
-- Install the extension
CREATE EXTENSION cdb_geocoder_server;