File: set_mapset.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (84 lines) | stat: -rw-r--r-- 2,856 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 11:07:38 2012

@author: pietro

"""
from __future__ import (nested_scopes, generators, division, absolute_import,
                        with_statement, print_function, unicode_literals)

import os
import subprocess
import optparse

from grass.script import core as grasscore

def read_gisrc(gisrcpath):
    gisrc = open(gisrcpath, 'r')
    diz = {}
    for row in gisrc:
        key, val = row.split(':')
        diz[key.strip()] = val.strip()
    return diz

def main():
    # default option
    gisrc = read_gisrc(os.environ['GISRC'])
    user = os.environ['USER']
    # start optparse
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-U", "--user", dest="user", default=user,
                      help="PostgreSQL user [default=%default]")
    parser.add_option("-P", "--password", dest="passwd", default=None,
                      help="PostgreSQL password for user [default=%default]")
    parser.add_option("-D", "--database", dest="db", default='pygrassdb_doctest',
                      help="PostgreSQL database name [default=%default]")

    (opts, args) = parser.parse_args()
    #
    # Create DB
    #
    print("\n\nCreate a new DB: %s...\n" % opts.db)
    createdb = ['createdb', '--encoding=UTF-8', '--owner=%s' % opts.user,
                '--host=localhost', '--username=%s' % opts.user, opts.db]
    if opts.passwd:
        print(opts.passwd)
        createdb.append("--password=%s" % opts.passwd)
    else:
        createdb.append("--no-password")
    subprocess.Popen(createdb)

    #
    # set postgreSQL
    #
    print("\n\nSet Postgres connection...\n")
    grasscore.run_command('db.connect', driver='pg',
                          database='host=localhost,dbname=%s' % opts.db)

    grasscore.run_command('db.login', user=opts.user)
    print("\n\nCopy the map from PERMANENT to user1...\n")
    grasscore.run_command('g.copy',
                          vector="boundary_municp@PERMANENT,boundary_municp_pg",
                          overwrite=True)
    print("\n\nBuild topology...\n")
    grasscore.run_command('v.build', map='boundary_municp_pg', overwrite=True)


    #
    # set sqlite
    #
    db = [gisrc['GISDBASE'], gisrc['LOCATION_NAME'], gisrc['MAPSET'], 'sqlite.db']
    print("\n\nSet Sqlite connection...\n")
    grasscore.run_command('db.connect', driver='sqlite',
                          database=os.path.join(db))
    print("\n\nCopy the map from PERMANENT to user1...\n")
    grasscore.run_command('g.copy',
                          vector="boundary_municp@PERMANENT,boundary_municp_sqlite",
                          overwrite=True)
    print("\n\nBuild topology...\n")
    grasscore.run_command('v.build', map='boundary_municp_sqlite', overwrite=True)

if __name__ == "__main__":
    main()