|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +############################################################################ |
| 4 | +# |
| 5 | +# MODULE: qgis.v.out.ogr.pg.py |
| 6 | +# AUTHOR(S): Radim Blazek |
| 7 | +# |
| 8 | +# PURPOSE: Export a vectore to PostGIS (PostgreSQL) database table |
| 9 | +# COPYRIGHT: (C) 2009 by Radim Blazek |
| 10 | +# |
| 11 | +# This program is free software under the GNU General Public |
| 12 | +# License (>=v2). Read the file COPYING that comes with GRASS |
| 13 | +# for details. |
| 14 | +# |
| 15 | +############################################################################# |
| 16 | + |
| 17 | +#%Module |
| 18 | +#% description: Export vector to PostGIS (PostgreSQL) database table. |
| 19 | +#% keywords: vector, export, database |
| 20 | +#%End |
| 21 | + |
| 22 | +#%option |
| 23 | +#% key: input |
| 24 | +#% type: string |
| 25 | +#% gisprompt: old,vector,vector |
| 26 | +#% key_desc : name |
| 27 | +#% description: Name of input vector map |
| 28 | +#% required : yes |
| 29 | +#%end |
| 30 | + |
| 31 | +#%option |
| 32 | +#% key: layer |
| 33 | +#% type: integer |
| 34 | +#% description: Number of input layer |
| 35 | +#% answer: 1 |
| 36 | +#% required : yes |
| 37 | +#%end |
| 38 | + |
| 39 | +#%option |
| 40 | +#% key: type |
| 41 | +#% type: string |
| 42 | +#% description: Feature type(s) |
| 43 | +#% options: point,kernel,centroid,line,boundary,area,face |
| 44 | +#% multiple: yes |
| 45 | +#% required : yes |
| 46 | +#%end |
| 47 | + |
| 48 | +#%option |
| 49 | +#% key: olayer |
| 50 | +#% type: string |
| 51 | +#% description: Name of output database table |
| 52 | +#% required : yes |
| 53 | +#%end |
| 54 | + |
| 55 | +#%option |
| 56 | +#% key: host |
| 57 | +#% type: string |
| 58 | +#% label: Host |
| 59 | +#% description: Host name of the machine on which the server is running. |
| 60 | +#% required : no |
| 61 | +#%end |
| 62 | + |
| 63 | +#%option |
| 64 | +#% key: port |
| 65 | +#% type: integer |
| 66 | +#% label: Port |
| 67 | +#% description: TCP port on which the server is listening, usually 5432. |
| 68 | +#% required : no |
| 69 | +#%end |
| 70 | + |
| 71 | +#%option |
| 72 | +#% key: database |
| 73 | +#% type: string |
| 74 | +#% key_desc : name |
| 75 | +#% gisprompt: old_dbname,dbname,dbname |
| 76 | +#% label: Database |
| 77 | +#% description: Database name |
| 78 | +#% required : yes |
| 79 | +#%end |
| 80 | + |
| 81 | +# AFAIK scheme is not supported well by OGR |
| 82 | +##%option |
| 83 | +##% key: schema |
| 84 | +##% type: string |
| 85 | +##% label: Schema |
| 86 | +##% description: Database schema. |
| 87 | +##% required : no |
| 88 | +##%end |
| 89 | + |
| 90 | +#%option |
| 91 | +#% key: user |
| 92 | +#% type: string |
| 93 | +#% label: User |
| 94 | +#% description: Connect to the database as the user username instead of the default. |
| 95 | +#% required : no |
| 96 | +#%end |
| 97 | + |
| 98 | +#%option |
| 99 | +#% key: password |
| 100 | +#% type: string |
| 101 | +#% label: Password |
| 102 | +#% description: Password will be stored in file! |
| 103 | +#% required : no |
| 104 | +#%end |
| 105 | + |
| 106 | +import sys |
| 107 | +import os |
| 108 | +import string |
| 109 | +try: |
| 110 | + import grass |
| 111 | +except: |
| 112 | + raise Exception ("Cannot find 'grass' Python module. Python is supported by GRASS from version 6.4" ) |
| 113 | + |
| 114 | +def main(): |
| 115 | + input = options['input'] |
| 116 | + layer = options['layer'] |
| 117 | + type = options['type'] |
| 118 | + olayer = options['olayer'] |
| 119 | + host = options['host'] |
| 120 | + port = options['port'] |
| 121 | + database = options['database'] |
| 122 | + #schema = options['schema'] |
| 123 | + user = options['user'] |
| 124 | + password = options['password'] |
| 125 | + |
| 126 | + # Construct dsn string |
| 127 | + dsn = "PG:dbname=" + database |
| 128 | + if host: dsn += " host=" + host |
| 129 | + if port: dsn += " port=" + port |
| 130 | + if user: dsn += " user=" + user |
| 131 | + if password: dsn += " password=" + password |
| 132 | + |
| 133 | + if grass.run_command('v.out.ogr', input=input, layer=layer, type=type, format="PostgreSQL", dsn=dsn, olayer=olayer ) != 0: |
| 134 | + grass.fatal("Cannot export vector to database.") |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + options, flags = grass.parser() |
| 138 | + main() |
0 commit comments