Top Banner
39

Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

Apr 14, 2017

Download

Software

Shaun Lewis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 2: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 3: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 4: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 5: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 6: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 7: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 8: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 9: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 10: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 11: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 12: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 13: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 14: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 15: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 16: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 17: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 18: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 19: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 20: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 21: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

CREATE TABLE os_open_names.os_open_names( id varchar not null, names_uri varchar not null, name1 varchar not null, name1language varchar, name2 varchar, name2language varchar, type varchar not null, local_type varchar not null, geometry_x numeric not null, geometry_y numeric not null, most_detail_view_res int not null, least_detail_view_res int not null, mbr_xmin numeric, mbr_ymin numeric, mbr_xmax numeric, mbr_ymax numeric, postcode_district varchar, postcode_district_uri varchar, populated_place varchar, populated_place_uri varchar, populated_place_type varchar, district_borough varchar, district_borough_uri varchar, district_borough_type varchar, county_unitary varchar, county_unitary_uri varchar, county_unitary_type varchar, region varchar not null, region_uri varchar not null, country varchar not null, country_uri varchar not null, related_spatial_object varchar, same_as_dpedia varchar, same_as_geonames varchar );

--Create table os_open_names in schema os_open_names to store OS Open Names

Page 22: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

--Populate table from file using COPY command

COPY os_open_names.os_open_names FROM 'C:/Temp/os_open_names.csv' WITH CSV DELIMITER ',' QUOTE '"';

--Add geometry column geom to store point geometry with EPSG:27700

SELECT AddGeometryColumn ('os_open_names', 'os_open_names', 'geom', 27700, 'POINT', 2);

--Populate geometry column using geometry constructor ST_Point()

UPDATE os_open_names.os_open_names SET geom = ST_SetSRID(ST_Point(geometry_x, geometry_y), 27700);

--Create spatial index on geom

CREATE INDEX os_open_names_geom_idx ON os_open_names.os_open_names USING GiST(geom);

Page 23: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 24: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

CREATE TABLE os_open_names.os_open_names_dev AS( SELECTname1 AS name, COALESCE(name1language, 'unknown') AS language, county_unitary, country, geomFROMos_open_names.os_open_names) UNION( SELECTname2 AS name, COALESCE(name2language, 'unknown') AS language, county_unitary, country, geomFROMos_open_names.os_open_namesWHERE name2 != '');

/* Field name1 is populated against every record, name2 is not Only consider name2 where it is not an empty string COALESCE is used to replace NULL language values with 'unknown' UNION combines the results of the two queries together */

Page 25: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

SELECT * FROM os_open_names.os_open_names_dev WHERE name ILIKE 'cardiff%';

--2.9secs

Page 26: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

SELECT show_trgm('Cardiff');

--{" c"," ca",ard,car,dif,"ff ",iff,rdi}

Page 27: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 28: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

--Enable extension pg_trgm

CREATE EXTENSION pg_trgm;

--Option 1 - GIN index creation on field name

CREATE INDEX os_open_names_trgm_gin_idx ON os_open_names.os_open_names_dev USING GIN(name gin_trgm_ops);

SELECT * FROM os_open_names.os_open_names_dev WHERE name ILIKE 'cardiff%';

--40msec

--Option 2 - GiST index creation on field name

CREATE INDEX os_open_names_trgm_gist_idx ON os_open_names.os_open_names_dev USING GiST(name gist_trgm_ops);

SELECT * FROM os_open_names.os_open_names_dev WHERE name ILIKE 'cardiff%';

--100msec

Page 29: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 30: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 31: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 32: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 33: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 34: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

SELECT to_tsvector(name) FROM os_open_names.os_open_names_devWHERE name = 'Cardiff International Airport';

--"'airport':3 'cardiff':1 'intern':2"

Page 35: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis

--Add field fts of data type tsvector

ALTER TABLE os_open_names.os_open_names_dev ADD COLUMN fts tsvector;

--Populate field fts using to_tsvector() utilising field name and English dictionary

UPDATE os_open_names.os_open_names_dev SET fts = to_tsvector('english', name);

--Create GIN index on field fts

CREATE INDEX os_open_names_dev_fts_gin_idx ON os_open_names.os_open_names_dev USING GIN(fts);

--Full Text Search query format using to_tsquery()

SELECT * FROM os_open_names.os_open_names_dev WHERE fts @@ to_tsquery('english', 'cardiff & international');

--10 msec--Returns a single record for name 'Cardiff International Airport'

--Full Text Search query format using plainto_tsquery()

SELECT * FROM os_open_names.os_open_names_dev WHERE fts @@ plainto_tsquery('english', 'cardiff international');

--30 msec--Returns a single record for name 'Cardiff International Airport'

Page 36: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 37: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 38: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis
Page 39: Steven Kingston - OS open data – visualisation and gazetteer searching with qgis and postgis