Commit f908ca8d authored by Mohammed Zia's avatar Mohammed Zia
Browse files

Initial Commit

parent 4c8f97a4
{
"PG_HOST": "localhost",
"PG_PORT": 5432,
"PG_BASE_DB": "postgres",
"PG_BASE_USER": "postgres",
"PROJ_DB": "missingosm",
"PROJ_USER": "admin_osm",
"PROJ_USER_PASS": "pass_osm",
"PROJ_REF_SCHEMA": "reference",
"PROJ_WORLD_SCHEMA": "world",
"PROJ_WORLD_COUNTRIES_GEOM_JSON": "geom_json",
"PROJ_SCANNED_TBL": "scanned_locations",
"PROJ_SCANNED_GEOM": "geom",
"PROJ_SCANNED_GEOM_JSON": "geom_json",
"PROJ_WORLD_COUNTRIES_TBL": "cntry92_world_countries",
"PROJ_WORLD_UNION_VIEW": "world_union_view",
"PROJ_WORLD_INDIVIDUAL_GEOM_JSON": "geom_json",
"PROJ_WORLD_INDIVIDUAL_GEOM": "geom"
}
\ No newline at end of file
import os, psycopg2, json
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
root_conf = open('root.conf')
conf_var_str = root_conf.read()
conf_var = json.loads(conf_var_str)
conn_base = psycopg2.connect(host=conf_var["PG_HOST"],port=conf_var["PG_PORT"], database=conf_var["PG_BASE_DB"], user=conf_var["PG_BASE_USER"])
conn_base.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur_base = conn_base.cursor()
cur_base.execute("CREATE DATABASE {0};".format(conf_var["PROJ_DB"]))
cur_base.execute("CREATE USER {0} WITH ENCRYPTED PASSWORD '{1}' SUPERUSER;".format(conf_var["PROJ_USER"], conf_var["PROJ_USER_PASS"]))
cur_base.execute("GRANT ALL PRIVILEGES ON DATABASE {0} TO {1};".format(conf_var["PROJ_DB"], conf_var["PROJ_USER"]))
cur_base.close()
conn_proj = psycopg2.connect(host=conf_var["PG_HOST"],port=conf_var["PG_PORT"],database=conf_var["PROJ_DB"], user=conf_var["PROJ_USER"], password=conf_var["PROJ_USER_PASS"])
conn_proj.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur_proj = conn_proj.cursor()
cur_insert = conn_proj.cursor()
cur_proj.execute("CREATE SCHEMA IF NOT EXISTS {0} AUTHORIZATION {1};".format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_USER"]))
cur_proj.execute("CREATE SCHEMA IF NOT EXISTS {0} AUTHORIZATION {1};".format(conf_var["PROJ_WORLD_SCHEMA"], conf_var["PROJ_USER"]))
cur_proj.execute("CREATE EXTENSION postgis;")
cur_proj.execute("CREATE TABLE {0}.{1} (id SERIAL PRIMARY KEY, weightage DOUBLE PRECISION);".format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_SCANNED_TBL"]))
cur_proj.execute("SELECT ADDGEOMETRYCOLUMN ('{0}','{1}','{2}',4326,'POLYGON',2, false); ALTER TABLE {0}.{1} ADD COLUMN {3} JSONB".format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_SCANNED_TBL"], conf_var["PROJ_SCANNED_GEOM"], conf_var["PROJ_SCANNED_GEOM_JSON"]))
os.system('shp2pgsql -G -I -W "latin1" /tmp/worldshapefile/CNTRY92.shp {0}.{1} | psql -h {2} -p {3} -U {4} -d {5}'.format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_WORLD_COUNTRIES_TBL"], conf_var["PG_HOST"], conf_var["PG_PORT"], conf_var["PROJ_USER"], conf_var["PROJ_DB"]))
cur_proj.execute("ALTER TABLE {0}.{1} ADD COLUMN {2} JSONB; UPDATE {0}.{1} SET {2} = CAST(ST_AsGeoJSON(geog) AS JSON)::JSON->'coordinates';".format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_WORLD_COUNTRIES_TBL"], conf_var["PROJ_WORLD_COUNTRIES_GEOM_JSON"]))
cur_proj.execute("SELECT * FROM {0}.{1};".format(conf_var["PROJ_REF_SCHEMA"], conf_var["PROJ_WORLD_COUNTRIES_TBL"]))
view_str_array = []
for row in cur_proj:
cur_insert.execute("CREATE TABLE {0}.{1} (id SERIAL PRIMARY KEY, scanned_bbox_id INTEGER, deepvgi_count INTEGER, deepvgi_probability DOUBLE PRECISION); SELECT ADDGEOMETRYCOLUMN ('{0}','{1}','{3}',4326,'POLYGON',3, false); ALTER TABLE {0}.{1} ADD COLUMN {2} JSONB;".format(conf_var["PROJ_WORLD_SCHEMA"], row[4].lower(), conf_var["PROJ_WORLD_INDIVIDUAL_GEOM_JSON"], conf_var["PROJ_WORLD_INDIVIDUAL_GEOM"]))
view_str_array.append("SELECT * FROM {0}.{1}".format(conf_var["PROJ_WORLD_SCHEMA"], row[4].lower()))
view_str = " UNION ".join(view_str_array)
view_str = "create view {0}.{1} as ( {2} );".format(conf_var["PROJ_WORLD_SCHEMA"], conf_var["PROJ_WORLD_UNION_VIEW"], view_str)
cur_proj.execute(view_str)
conn_proj.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment