Giter Site home page Giter Site logo

ott-addr-search's Introduction

Ottawa Address Search

Testing out pg_featureserv and pg_tileserv using City of Ottawa Open Data - Municipal Addresses.

export DATABASE_URL="host=localhost user=postgres password=password"

Setup

Install Go

sudo add-apt-repository ppa:longsleep/golang-backports

sudo apt update

sudo apt install golang-go

Prepare the database

sudo -i

su - postgres

createdb addr

psql -U postgres -d addr -c 'CREATE EXTENSION postgis;'

psql -U posgres -c 'CREATE SCHEMA IF NOT EXISTS postgisftw;'

Download data

wget https://opendata.arcgis.com/datasets/36df55a87f394987875b7f79648c9603_0.zip

unzip Municipal_Address_Points-shp.zip

Load data into PostgreSQL

ogr2ogr -f "PostgreSQL" PG:"host='localhost' user='postgres' dbname='addr' password='password'" /data/Municipal_Address_Points.shp -lco GEOMETRY_NAME=geom -lco FID=gid -lco PRECISION=no -nln ottpts -overwrite

Address queries

ALTER TABLE postgisftw.ottpts ADD COLUMN ts tsvector;

UPDATE postgisftw.ottpts SET ts = 
    to_tsvector('addressing_en', fulladdr);

VACUUM (ANALYZE, FULL) postgisftw.ottpts;

CREATE INDEX ottpts_ts_x ON postgisftw.ottpts USING GIN (ts);
CREATE INDEX ottpts_geom_x ON postgisftw.ottpts USING GIST (geom);
CREATE OR REPLACE FUNCTION to_tsquery_partial(text)
RETURNS tsquery 
AS $$
BEGIN
  RETURN to_tsquery('simple',
             array_to_string(
               regexp_split_to_array(
                 trim($1),E'\\s+'),' & ') 
             || CASE WHEN $1 ~ ' $' THEN '' ELSE ':*' END
           );
END;
$$ 
LANGUAGE 'plpgsql'
PARALLEL SAFE
IMMUTABLE
STRICT;
DROP FUNCTION IF EXISTS postgisftw.address_query;

CREATE OR REPLACE FUNCTION postgisftw.address_query(
    partialstr text DEFAULT '')
RETURNS TABLE(gid integer, value text, rank real, geom geometry)
AS $$
BEGIN
    RETURN QUERY
        SELECT
          p.gid AS id,
          initcap(fulladdr) AS value,
          ts_rank_cd(p.ts, query) AS rank,
          p.geom
        FROM postgisftw.ottpts p,
             to_tsquery_partial(partialstr) AS query
        WHERE ts @@ query
        ORDER BY rank DESC
        LIMIT 15;
END;
$$
LANGUAGE 'plpgsql'
PARALLEL SAFE
STABLE
STRICT;

Acknowledgements

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.