File: v.out.ogr.pg.py

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (164 lines) | stat: -rw-r--r-- 4,259 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

"""
***************************************************************************
    v.out.ogr.pg.py
    ---------------------
    Date                 : July 2009
    Copyright            : (C) 2009 by Radim Blazek
    Email                : radim dot blazek at gmail dot com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'Radim Blazek'
__date__ = 'July 2009'
__copyright__ = '(C) 2009, Radim Blazek'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


############################################################################
#
# MODULE:       qgis.v.out.ogr.pg.py
# PURPOSE:      Export a vector to PostGIS (PostgreSQL) database table
#
#############################################################################

#%Module
#% description: Export vector to PostGIS (PostgreSQL) database table.
#% keywords: vector, export, database
#%End

#%option
#% key: input
#% type: string
#% gisprompt: old,vector,vector
#% key_desc : name
#% description: Name of input vector map
#% required : yes
#%end

#%option
#% key: layer
#% type: integer
#% description: Number of input layer
#% answer: 1
#% required : yes
#%end

#%option
#% key: type
#% type: string
#% description: Feature type(s)
#% options: point,kernel,centroid,line,boundary,area,face
#% multiple: yes
#% required : yes
#%end

#%option
#% key: olayer
#% type: string
#% description: Name of output database table
#% required : yes
#%end

#%option
#% key: host
#% type: string
#% label: Host
#% description: Host name of the machine on which the server is running.
#% required : no
#%end

#%option
#% key: port
#% type: integer
#% label: Port
#% description: TCP port on which the server is listening, usually 5432.
#% required : no
#%end

#%option
#% key: database
#% type: string
#% key_desc : name
#% gisprompt: old_dbname,dbname,dbname
#% label: Database
#% description: Database name
#% required : yes
#%end

# AFAIK scheme is not supported well by OGR
##%option
##% key: schema
##% type: string
##% label: Schema
##% description: Database schema.
##% required : no
##%end

#%option
#% key: user
#% type: string
#% label: User
#% description: Connect to the database as the user username instead of the  default.
#% required : no
#%end

#%option
#% key: password
#% type: string
#% label: Password
#% description: Password will be stored in file!
#% required : no
#%end

#%flag
#% key: c
#% description: to export features with category (labeled) only. Otherwise all features are exported
#%end

import sys
import os
import string
try:
    from grass.script import core as grass
except ImportError:
    import grass
except:
    raise Exception ("Cannot find 'grass' Python module. Python is supported by GRASS from version >= 6.4" )

def main():
    input = options['input']
    layer = options['layer']
    type = options['type']
    olayer = options['olayer']
    host = options['host']
    port = options['port']
    database = options['database']
    #schema = options['schema']
    user = options['user']
    password = options['password']

    # Construct dsn string
    dsn = "PG:dbname=" + database
    if host: dsn += " host=" + host
    if port: dsn += " port=" + port
    if user: dsn += " user=" + user
    if password: dsn += " password=" + password

    if grass.run_command('v.out.ogr', flags=flags_string, input=input, layer=layer, type=type, format="PostgreSQL", dsn=dsn, olayer=olayer ) != 0:
         grass.fatal("Cannot export vector to database.")

if __name__ == "__main__":
    options, flags = grass.parser()
    flags_string = "".join([k for k in flags.keys() if flags[k] and k != 'r'])
    main()