Project

General

Profile

« Previous | Next » 

Revision 11555

Renamed biengeo install scripts to setup scripts.

It seems to make more sense to call these setup scripts, since they are
only setting up the database and tables, and not actually installing any
files anywhere on the OS.

View differences:

derived/biengeo/install.geonames-to-gadm.sql
1
BEGIN;
2

  
3
-----------------------
4
-- Level 0 (Country) --
5
-----------------------
6

  
7
-- maps gadm2 level0 to geonames countries
8
CREATE TABLE gadm_country_lookup (
9
        countryid integer primary key references geonames (geonameid),
10
        name_0 text
11
);
12

  
13
------------------------------
14
-- Level 1 (State/Province) --
15
------------------------------
16

  
17
-- maps gadm2 level1 to geonames ADM1
18
CREATE TABLE gadm_stateprovince_lookup (
19
        stateprovinceid integer primary key references geonames (geonameid),
20
        name_0 text,
21
        name_1 text
22
);
23

  
24
/*
25
-- another way to create gadm_stateprovince_lookup, though this won't
26
-- pick up multiple matches from geonames (good or bad??)
27
CREATE TABLE gadm_stateprovince_lookup AS
28
SELECT DISTINCT name_0, name_1
29
  FROM gadm2;
30
ALTER TABLE gadm_stateprovince_lookup ADD COLUMN stateprovinceid integer;
31
UPDATE gadm_stateprovince_lookup gs
32
  SET stateprovinceid = g.geonameid
33
  FROM gadm_country_lookup gc,
34
       alternatenames a,
35
       geonames g,
36
       countries c
37
  WHERE gs.name_0=gc.name_0
38
    AND gs.name_1=a.alternatename
39
    AND a.geonameid=g.geonameid
40
    AND g.countrycode=c.iso
41
    AND gc.countryid=c.geonameid
42
    AND g.featurecode='ADM1';
43
-- UPDATE 2137
44
-- Time: 600.345 ms
45
*/
46

  
47
----------------------
48
-- Level 2 (County) --
49
----------------------
50

  
51
-- maps gadm2 level2 to geonames ADM2
52
CREATE TABLE gadm_county_lookup (
53
        countyid integer primary key references geonames (geonameid),
54
        name_0 text,
55
        name_1 text,
56
        name_2 text
57
);
58

  
59
COMMIT;
60

  
derived/biengeo/install.geonames.sql
1
BEGIN;
2

  
3
-- define custom function to pass characters back through latin-1
4
-- encoding to revert errors; in principle this could introduce new
5
-- errors, but it seems to do more good than harm
6
CREATE OR REPLACE FUNCTION u2L2u(text) RETURNS text AS $$
7
DECLARE
8
    string text;
9
BEGIN
10
  string :=
11
    convert_from(convert($1::bytea, 'UTF8', 'LATIN1'), 'UTF8');
12
  RETURN string;
13
EXCEPTION
14
  WHEN data_exception THEN
15
    RETURN $1;
16
END;
17
$$ LANGUAGE plpgsql;
18

  
19
CREATE TABLE geonames (
20
    geonameid serial primary key,
21
    name text,
22
    asciiname text,
23
    alternatenames text,
24
    latitude numeric,
25
    longitude numeric,
26
    featureclass char(1),
27
    featurecode text,
28
    countrycode char(2),
29
    cc2 char(60),
30
    admin1code text,
31
    admin2code text,
32
    admin3code text,
33
    admin4code text,
34
    population bigint,
35
    elevation int,
36
    dem int,
37
    timezone text,
38
    modification date
39
);
40

  
41
--
42
-- load geonames.org alternate names
43
--
44

  
45
CREATE TABLE alternateNames (
46
    alternateNameId serial primary key,
47
    geonameid int references geonames (geonameid),
48
    isolanguage varchar(7),
49
    alternateName varchar(200),
50
    isPreferredName char(1),
51
    isShortName char(1),
52
    isColloquial char(1),
53
    isHistoric char(1)
54
);
55

  
56
--
57
-- load geonames.org country info
58
--
59

  
60
CREATE TABLE countries (
61
    iso char(2),
62
    iso3 char(3),
63
    isonumeric text,
64
    fips text,
65
    country text UNIQUE,
66
    capital text,
67
    area text,
68
    population text,
69
    continent text,
70
    tld text,
71
    currencyCode text,
72
    currencyName text,
73
    phone text,
74
    postalCodeFormat text,
75
    postalCodeRegex text,
76
    languages text,
77
    geonameid int,
78
    neighbours text,
79
    equivalentFipsCode text
80
);
81

  
82
--
83
-- load geonames.org hierarchy info
84
--
85

  
86
CREATE TABLE hierarchy (
87
    parentId int,-- references geonames (geonameid),
88
    childId int,-- references geonames (geonameid),
89
    type text
90
);
91

  
92
--
93
-- tables for additional custom name-scrub mappings
94
--
95

  
96
-- table for custom country alternate names that we need
97
CREATE TABLE alt_country (
98
    country text references countries (country),
99
    alternatename text
100
);
101

  
102
-- table for custom stateprovince alternate names that we need
103
CREATE TABLE alt_stateprovince (
104
    country text references countries (country),
105
    stateprovince text,
106
    alternatename text
107
);
108

  
109
COMMIT;
110

  
derived/biengeo/install.sh
1
#!/bin/bash
2

  
3
# Bash script to create a new postgis database and prep it with GADM
4
# data for geovalidation purposes.
5
#
6
# Basic workflow:
7
#  1. Create postgis-enabled template db
8
#  2. Create postgis-enabled db from the template
9
#  3. Create required tables in postgis-enabled db
10
#  4. Load gadm2 and geonames.org data into database
11
#
12
# For postgis installation see:
13
#   http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS20Ubuntu1204
14
#
15
# Jim Regetz
16
# NCEAS
17
# Created November 2012
18
#
19
# refactored and reorganized by
20
# Paul Sarando
21
# iPlant Collaborative
22
# Updated Oct 2013
23

  
24
DB_NAME="geoscrub"
25
DB_USER="bien"
26
DB_HOST="localhost"
27
SCRIPT_DIR="$(dirname $0)"
28

  
29
function run_sudo_sql_cmd {
30
    local SQL_CMD="$1"
31

  
32
    sudo -u postgres psql -e -h "$DB_HOST" -d "$DB_NAME" -c "$SQL_CMD"
33
    if [[ $? != 0 ]]; then
34
        echo "Error while executing SQL command."
35
        echo -n "You may drop the ${DB_NAME} database"
36
        echo " and attempt to run this $0 script again."
37
        exit 1
38
    fi
39
}
40

  
41
function run_sql_script {
42
    local SCRIPT="$1"
43

  
44
    psql -e --set ON_ERROR_STOP=1 -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME" < "$SCRIPT"
45
    if [[ $? != 0 ]]; then
46
        echo "Error while executing SQL script ${SCRIPT}"
47
        exit 1
48
    fi
49
}
50

  
51
# create postgis template if one doesn't already exist
52
echo "Setting up template_postgis database..."
53
sudo -u postgres createdb template_postgis
54
if [[ $? == 0 ]]; then
55
    # successfully created template_postgis, which means it didn't already exist
56
    sudo -u postgres psql -e -d template_postgis < "${SCRIPT_DIR}"/update.template_postgis.sql
57
    if [[ $? != 0 ]]; then
58
        echo "Could not create postgis extesion on template_postgis database."
59
        exit 1
60
    fi
61
fi
62

  
63
# create postgis db from template
64
echo "Creating the ${DB_NAME} database..."
65
createdb -U "$DB_USER" -h "$DB_HOST" "$DB_NAME" -T template_postgis
66
if [[ $? != 0 ]]; then
67
    echo "Could not create ${DB_NAME} database."
68
    exit 1
69
fi
70

  
71
run_sudo_sql_cmd "ALTER TABLE spatial_ref_sys OWNER TO ${DB_USER}"
72
run_sudo_sql_cmd "ALTER VIEW geography_columns OWNER TO ${DB_USER}"
73
run_sudo_sql_cmd "ALTER VIEW geometry_columns OWNER TO ${DB_USER}"
74

  
75
echo "Setting up ${DB_NAME} database tables..."
76
run_sql_script "${SCRIPT_DIR}"/install.geonames.sql
77
run_sql_script "${SCRIPT_DIR}"/install.geonames-to-gadm.sql
78
run_sql_script "${SCRIPT_DIR}"/install.geoscrub.sql
79

  
80
echo "Install successfully completed."
81

  
82 0

  
derived/biengeo/install.geoscrub.sql
1
/*
2
  SQL statements to create tables for country name, stateprovince name, and
3
  county name scrubbing on vegbien location data. Currently hard-coded
4
  to operate on a 'vegbien_geoscrub' table that contains the input for
5
  geoscrubbing, with the following columns:
6
     decimallatitude  (latitude, WGS84 decimal degrees)
7
     decimallongitude (longitude, WGS84 decimal degrees)
8
     country          (original asserted country name)
9
     stateprovince    (original asserted stateprovince name)
10
     county           (original asserted county name)
11
  (Note that the lat/lon columns are not needed for scrubbing, but are
12
  used later for geovalidation.)
13

  
14
  Jim Regetz
15
  NCEAS
16
  Created Nov 2012
17
*/
18

  
19
BEGIN;
20

  
21
CREATE TABLE geoscrub (
22
    decimallatitude double precision,
23
    decimallongitude double precision,
24
    country text,
25
    stateprovince text,
26
    county text,
27
    countryid integer,
28
    stateprovinceid integer,
29
    countyid integer,
30
    countrystd text,
31
    stateprovincestd text,
32
    countystd text,
33
    latlonvalidity int,
34
    countryvalidity int,
35
    stateprovincevalidity int,
36
    countyvalidity int,
37
    pointcountry text,
38
    pointstateprovince text,
39
    pointcounty text
40
);
41
SELECT AddGeometryColumn('public', 'geoscrub', 'geom', 4326, 'POINT', 2 );
42
ALTER TABLE geoscrub ADD COLUMN geog geography(POINT, 4326);
43

  
44
CREATE TABLE vegbien_geoscrub (
45
    country text,
46
    stateprovince text,
47
    county text,
48
    decimalLatitude double precision,
49
    decimalLongitude double precision
50
);
51

  
52
CREATE TABLE vcountry (
53
    country text,
54
    countryutf8 text,
55
    countryid integer,
56
    countrystd text
57
);
58

  
59
CREATE TABLE vstate (
60
    countryid integer,
61
    stateProvince text,
62
    stateprovinceutf8 text,
63
    stateprovinceid integer,
64
    stateprovincestd text
65
);
66

  
67
CREATE TABLE vcounty (
68
    countryid integer,
69
    stateprovinceid integer,
70
    county text,
71
    countyutf8 text,
72
    countyid integer,
73
    countystd text
74
);
75

  
76
COMMIT;
77

  
derived/biengeo/setup.geonames-to-gadm.sql
1
BEGIN;
2

  
3
-----------------------
4
-- Level 0 (Country) --
5
-----------------------
6

  
7
-- maps gadm2 level0 to geonames countries
8
CREATE TABLE gadm_country_lookup (
9
        countryid integer primary key references geonames (geonameid),
10
        name_0 text
11
);
12

  
13
------------------------------
14
-- Level 1 (State/Province) --
15
------------------------------
16

  
17
-- maps gadm2 level1 to geonames ADM1
18
CREATE TABLE gadm_stateprovince_lookup (
19
        stateprovinceid integer primary key references geonames (geonameid),
20
        name_0 text,
21
        name_1 text
22
);
23

  
24
/*
25
-- another way to create gadm_stateprovince_lookup, though this won't
26
-- pick up multiple matches from geonames (good or bad??)
27
CREATE TABLE gadm_stateprovince_lookup AS
28
SELECT DISTINCT name_0, name_1
29
  FROM gadm2;
30
ALTER TABLE gadm_stateprovince_lookup ADD COLUMN stateprovinceid integer;
31
UPDATE gadm_stateprovince_lookup gs
32
  SET stateprovinceid = g.geonameid
33
  FROM gadm_country_lookup gc,
34
       alternatenames a,
35
       geonames g,
36
       countries c
37
  WHERE gs.name_0=gc.name_0
38
    AND gs.name_1=a.alternatename
39
    AND a.geonameid=g.geonameid
40
    AND g.countrycode=c.iso
41
    AND gc.countryid=c.geonameid
42
    AND g.featurecode='ADM1';
43
-- UPDATE 2137
44
-- Time: 600.345 ms
45
*/
46

  
47
----------------------
48
-- Level 2 (County) --
49
----------------------
50

  
51
-- maps gadm2 level2 to geonames ADM2
52
CREATE TABLE gadm_county_lookup (
53
        countyid integer primary key references geonames (geonameid),
54
        name_0 text,
55
        name_1 text,
56
        name_2 text
57
);
58

  
59
COMMIT;
60

  
derived/biengeo/setup.geonames.sql
1
BEGIN;
2

  
3
-- define custom function to pass characters back through latin-1
4
-- encoding to revert errors; in principle this could introduce new
5
-- errors, but it seems to do more good than harm
6
CREATE OR REPLACE FUNCTION u2L2u(text) RETURNS text AS $$
7
DECLARE
8
    string text;
9
BEGIN
10
  string :=
11
    convert_from(convert($1::bytea, 'UTF8', 'LATIN1'), 'UTF8');
12
  RETURN string;
13
EXCEPTION
14
  WHEN data_exception THEN
15
    RETURN $1;
16
END;
17
$$ LANGUAGE plpgsql;
18

  
19
CREATE TABLE geonames (
20
    geonameid serial primary key,
21
    name text,
22
    asciiname text,
23
    alternatenames text,
24
    latitude numeric,
25
    longitude numeric,
26
    featureclass char(1),
27
    featurecode text,
28
    countrycode char(2),
29
    cc2 char(60),
30
    admin1code text,
31
    admin2code text,
32
    admin3code text,
33
    admin4code text,
34
    population bigint,
35
    elevation int,
36
    dem int,
37
    timezone text,
38
    modification date
39
);
40

  
41
--
42
-- load geonames.org alternate names
43
--
44

  
45
CREATE TABLE alternateNames (
46
    alternateNameId serial primary key,
47
    geonameid int references geonames (geonameid),
48
    isolanguage varchar(7),
49
    alternateName varchar(200),
50
    isPreferredName char(1),
51
    isShortName char(1),
52
    isColloquial char(1),
53
    isHistoric char(1)
54
);
55

  
56
--
57
-- load geonames.org country info
58
--
59

  
60
CREATE TABLE countries (
61
    iso char(2),
62
    iso3 char(3),
63
    isonumeric text,
64
    fips text,
65
    country text UNIQUE,
66
    capital text,
67
    area text,
68
    population text,
69
    continent text,
70
    tld text,
71
    currencyCode text,
72
    currencyName text,
73
    phone text,
74
    postalCodeFormat text,
75
    postalCodeRegex text,
76
    languages text,
77
    geonameid int,
78
    neighbours text,
79
    equivalentFipsCode text
80
);
81

  
82
--
83
-- load geonames.org hierarchy info
84
--
85

  
86
CREATE TABLE hierarchy (
87
    parentId int,-- references geonames (geonameid),
88
    childId int,-- references geonames (geonameid),
89
    type text
90
);
91

  
92
--
93
-- tables for additional custom name-scrub mappings
94
--
95

  
96
-- table for custom country alternate names that we need
97
CREATE TABLE alt_country (
98
    country text references countries (country),
99
    alternatename text
100
);
101

  
102
-- table for custom stateprovince alternate names that we need
103
CREATE TABLE alt_stateprovince (
104
    country text references countries (country),
105
    stateprovince text,
106
    alternatename text
107
);
108

  
109
COMMIT;
110

  
derived/biengeo/setup.geoscrub.sql
1
/*
2
  SQL statements to create tables for country name, stateprovince name, and
3
  county name scrubbing on vegbien location data. Currently hard-coded
4
  to operate on a 'vegbien_geoscrub' table that contains the input for
5
  geoscrubbing, with the following columns:
6
     decimallatitude  (latitude, WGS84 decimal degrees)
7
     decimallongitude (longitude, WGS84 decimal degrees)
8
     country          (original asserted country name)
9
     stateprovince    (original asserted stateprovince name)
10
     county           (original asserted county name)
11
  (Note that the lat/lon columns are not needed for scrubbing, but are
12
  used later for geovalidation.)
13

  
14
  Jim Regetz
15
  NCEAS
16
  Created Nov 2012
17
*/
18

  
19
BEGIN;
20

  
21
CREATE TABLE geoscrub (
22
    decimallatitude double precision,
23
    decimallongitude double precision,
24
    country text,
25
    stateprovince text,
26
    county text,
27
    countryid integer,
28
    stateprovinceid integer,
29
    countyid integer,
30
    countrystd text,
31
    stateprovincestd text,
32
    countystd text,
33
    latlonvalidity int,
34
    countryvalidity int,
35
    stateprovincevalidity int,
36
    countyvalidity int,
37
    pointcountry text,
38
    pointstateprovince text,
39
    pointcounty text
40
);
41
SELECT AddGeometryColumn('public', 'geoscrub', 'geom', 4326, 'POINT', 2 );
42
ALTER TABLE geoscrub ADD COLUMN geog geography(POINT, 4326);
43

  
44
CREATE TABLE vegbien_geoscrub (
45
    country text,
46
    stateprovince text,
47
    county text,
48
    decimalLatitude double precision,
49
    decimalLongitude double precision
50
);
51

  
52
CREATE TABLE vcountry (
53
    country text,
54
    countryutf8 text,
55
    countryid integer,
56
    countrystd text
57
);
58

  
59
CREATE TABLE vstate (
60
    countryid integer,
61
    stateProvince text,
62
    stateprovinceutf8 text,
63
    stateprovinceid integer,
64
    stateprovincestd text
65
);
66

  
67
CREATE TABLE vcounty (
68
    countryid integer,
69
    stateprovinceid integer,
70
    county text,
71
    countyutf8 text,
72
    countyid integer,
73
    countystd text
74
);
75

  
76
COMMIT;
77

  
derived/biengeo/setup.sh
1
#!/bin/bash
2

  
3
# Bash script to create a new postgis database and prep it with GADM
4
# data for geovalidation purposes.
5
#
6
# Basic workflow:
7
#  1. Create postgis-enabled template db
8
#  2. Create postgis-enabled db from the template
9
#  3. Create required tables in postgis-enabled db
10
#  4. Load gadm2 and geonames.org data into database
11
#
12
# For postgis installation see:
13
#   http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS20Ubuntu1204
14
#
15
# Jim Regetz
16
# NCEAS
17
# Created November 2012
18
#
19
# refactored and reorganized by
20
# Paul Sarando
21
# iPlant Collaborative
22
# Updated Oct 2013
23

  
24
DB_NAME="geoscrub"
25
DB_USER="bien"
26
DB_HOST="localhost"
27
SCRIPT_DIR="$(dirname $0)"
28

  
29
function run_sudo_sql_cmd {
30
    local SQL_CMD="$1"
31

  
32
    sudo -u postgres psql -e -h "$DB_HOST" -d "$DB_NAME" -c "$SQL_CMD"
33
    if [[ $? != 0 ]]; then
34
        echo "Error while executing SQL command."
35
        echo -n "You may drop the ${DB_NAME} database"
36
        echo " and attempt to run this $0 script again."
37
        exit 1
38
    fi
39
}
40

  
41
function run_sql_script {
42
    local SCRIPT="$1"
43

  
44
    psql -e --set ON_ERROR_STOP=1 -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME" < "$SCRIPT"
45
    if [[ $? != 0 ]]; then
46
        echo "Error while executing SQL script ${SCRIPT}"
47
        exit 1
48
    fi
49
}
50

  
51
# create postgis template if one doesn't already exist
52
echo "Setting up template_postgis database..."
53
sudo -u postgres createdb template_postgis
54
if [[ $? == 0 ]]; then
55
    # successfully created template_postgis, which means it didn't already exist
56
    sudo -u postgres psql -e -d template_postgis < "${SCRIPT_DIR}"/update.template_postgis.sql
57
    if [[ $? != 0 ]]; then
58
        echo "Could not create postgis extesion on template_postgis database."
59
        exit 1
60
    fi
61
fi
62

  
63
# create postgis db from template
64
echo "Creating the ${DB_NAME} database..."
65
createdb -U "$DB_USER" -h "$DB_HOST" "$DB_NAME" -T template_postgis
66
if [[ $? != 0 ]]; then
67
    echo "Could not create ${DB_NAME} database."
68
    exit 1
69
fi
70

  
71
run_sudo_sql_cmd "ALTER TABLE spatial_ref_sys OWNER TO ${DB_USER}"
72
run_sudo_sql_cmd "ALTER VIEW geography_columns OWNER TO ${DB_USER}"
73
run_sudo_sql_cmd "ALTER VIEW geometry_columns OWNER TO ${DB_USER}"
74

  
75
echo "Setting up ${DB_NAME} database tables..."
76
run_sql_script "${SCRIPT_DIR}"/setup.geonames.sql
77
run_sql_script "${SCRIPT_DIR}"/setup.geonames-to-gadm.sql
78
run_sql_script "${SCRIPT_DIR}"/setup.geoscrub.sql
79

  
80
echo "Install successfully completed."
81

  
0 82

  
derived/biengeo/README.txt
25 25

  
26 26
***** initialize the DB:
27 27
cd <svn_biengeo_root>
28
1. install.sh
28
1. setup.sh
29 29
   - creates postgis DB and all base tables
30 30

  
31 31
***** update geoscrub validation data:

Also available in: Unified diff