1+ # -*- coding: utf-8 -*-
2+
3+ """
4+ ***************************************************************************
5+ ogr2ogrtopostgis.py
6+ ---------------------
7+ Date : November 2012
8+ Copyright : (C) 2012 by Victor Olaya
9+ Email : volayaf at gmail dot com
10+ ***************************************************************************
11+ * *
12+ * This program is free software; you can redistribute it and/or modify *
13+ * it under the terms of the GNU General Public License as published by *
14+ * the Free Software Foundation; either version 2 of the License, or *
15+ * (at your option) any later version. *
16+ * *
17+ ***************************************************************************
18+ """
19+
20+ __author__ = 'Victor Olaya'
21+ __date__ = 'November 2012'
22+ __copyright__ = '(C) 2012, Victor Olaya'
23+
24+ # This will get replaced with a git SHA1 when you do a git archive
25+
26+ __revision__ = '$Format:%H$'
27+
28+ import os
29+
30+ from PyQt4 .QtCore import *
31+ from PyQt4 .QtGui import *
32+
33+ from qgis .core import *
34+
35+ from processing .core .parameters import ParameterVector
36+ from processing .core .parameters import ParameterString
37+ from processing .core .parameters import ParameterCrs
38+ from processing .core .parameters import ParameterSelection
39+ from processing .core .parameters import ParameterBoolean
40+ from processing .core .parameters import ParameterExtent
41+
42+ from processing .tools .system import *
43+
44+ from processing .algs .gdal .OgrAlgorithm import OgrAlgorithm
45+ from processing .algs .gdal .GdalUtils import GdalUtils
46+
47+ class Ogr2OgrToPostGis (OgrAlgorithm ):
48+
49+ INPUT_LAYER = 'INPUT_LAYER'
50+ GTYPE = 'GTYPE'
51+ GEOMTYPE = ['' ,'NONE' ,'GEOMETRY' ,'POINT' ,'LINESTRING' ,'POLYGON' ,'GEOMETRYCOLLECTION' ,'MULTIPOINT' ,'MULTIPOLYGON' ,'MULTILINESTRING' ]
52+ S_SRS = 'S_SRS'
53+ T_SRS = 'T_SRS'
54+ HOST = 'HOST'
55+ PORT = 'PORT'
56+ USER = 'USER'
57+ DBNAME = 'DBNAME'
58+ PASSWORD = 'PASSWORD'
59+ SCHEMA = 'SCHEMA'
60+ TABLE = 'TABLE'
61+ PK = 'PK'
62+ GEOCOLUMN = 'GEOCOLUMN'
63+ DIM = 'DIM'
64+ DIMLIST = ['2' ,'3' ]
65+ SIMPLIFY = 'SIMPLIFY'
66+ SEGMENTIZE = 'SEGMENTIZE'
67+ SPAT = 'SPAT'
68+ CLIP = 'CLIP'
69+ WHERE = 'WHERE'
70+ GT = 'GT'
71+ OVERWRITE = 'OVERWRITE'
72+ APPEND = 'APPEND'
73+ ADDFIELDS = 'ADDFIELDS'
74+ LAUNDER = 'LAUNDER'
75+ INDEX = 'INDEX'
76+ SKIPFAILURES = 'SKIPFAILURES'
77+ OPTIONS = 'OPTIONS'
78+
79+ def defineCharacteristics (self ):
80+ self .name = 'Import Vector into PostGIS database (new connection)'
81+ self .group = '[OGR] Miscellaneous'
82+ self .addParameter (ParameterVector (self .INPUT_LAYER , 'Input layer' ,
83+ [ParameterVector .VECTOR_TYPE_ANY ], False ))
84+ self .addParameter (ParameterSelection (self .GTYPE , 'Output geometry type' ,self .GEOMTYPE , 5 ))
85+ self .addParameter (ParameterCrs (self .S_SRS , 'Input CRS (EPSG Code)' ,
86+ 'EPSG:4326' ))
87+ self .addParameter (ParameterCrs (self .T_SRS , 'Output CRS (EPSG Code)' ,
88+ 'EPSG:4326' ))
89+ self .addParameter (ParameterString (self .HOST , 'Host' ,
90+ 'localhost' , optional = False ))
91+ self .addParameter (ParameterString (self .PORT , 'Port' ,
92+ '5432' , optional = False ))
93+ self .addParameter (ParameterString (self .USER , 'Username' ,
94+ '' , optional = False ))
95+ self .addParameter (ParameterString (self .DBNAME , 'Database Name' ,
96+ '' , optional = False ))
97+ self .addParameter (ParameterString (self .PASSWORD , 'Password' ,
98+ '' , optional = False ))
99+ self .addParameter (ParameterString (self .SCHEMA , 'Schema name' ,
100+ 'public' , optional = True ))
101+ self .addParameter (ParameterString (self .TABLE , 'Table name, leave blank to use input name' ,
102+ '' , optional = True ))
103+ self .addParameter (ParameterString (self .PK , 'Primary Key' ,
104+ 'id' , optional = True ))
105+ self .addParameter (ParameterString (self .GEOCOLUMN , 'Geometry column name' ,
106+ 'geom' , optional = True ))
107+ self .addParameter (ParameterSelection (self .DIM , 'Vector dimensions' ,self .DIMLIST , 0 ))
108+ self .addParameter (ParameterString (self .SIMPLIFY , 'Distance tolerance for simplification' ,
109+ '' , optional = True ))
110+ self .addParameter (ParameterString (self .SEGMENTIZE , 'Maximum distance between 2 nodes (densification)' ,
111+ '' , optional = True ))
112+ self .addParameter (ParameterExtent (self .SPAT ,
113+ 'Select features by extent (defined in input layer CRS)' ))
114+ self .addParameter (ParameterBoolean (self .CLIP ,
115+ 'Clip the input layer using the above (rectangle) extent' , False ))
116+ self .addParameter (ParameterString (self .WHERE , 'Select features using a SQL "WHERE" statement (Ex: column="value")' ,
117+ '' , optional = True ))
118+ self .addParameter (ParameterString (self .GT , 'Group "n" features per transaction (Default: 20000)' ,
119+ '' , optional = True ))
120+ self .addParameter (ParameterBoolean (self .OVERWRITE ,
121+ 'Overwrite existing table?' , True ))
122+ self .addParameter (ParameterBoolean (self .APPEND ,
123+ 'Append to existing table?' , False ))
124+ self .addParameter (ParameterBoolean (self .ADDFIELDS ,
125+ 'Append and add new fields to existing table?' , False ))
126+ self .addParameter (ParameterBoolean (self .LAUNDER ,
127+ 'Do not launder columns/table name/s?' , False ))
128+ self .addParameter (ParameterBoolean (self .INDEX ,
129+ 'Do not create Spatial Index?' , False ))
130+ self .addParameter (ParameterBoolean (self .SKIPFAILURES ,
131+ 'Continue after a failure, skipping the failed feature' , False ))
132+ self .addParameter (ParameterString (self .OPTIONS , 'Additional creation options' ,
133+ '' , optional = True ))
134+
135+ def processAlgorithm (self , progress ):
136+ inLayer = self .getParameterValue (self .INPUT_LAYER )
137+ ogrLayer = self .ogrConnectionString (inLayer )
138+ ssrs = unicode (self .getParameterValue (self .S_SRS ))
139+ tsrs = unicode (self .getParameterValue (self .T_SRS ))
140+ host = unicode (self .getParameterValue (self .HOST ))
141+ port = unicode (self .getParameterValue (self .PORT ))
142+ user = unicode (self .getParameterValue (self .USER ))
143+ dbname = unicode (self .getParameterValue (self .DBNAME ))
144+ password = unicode (self .getParameterValue (self .PASSWORD ))
145+ schema = unicode (self .getParameterValue (self .SCHEMA ))
146+ schemastring = "-lco SCHEMA=" + schema
147+ table = unicode (self .getParameterValue (self .TABLE ))
148+ pk = unicode (self .getParameterValue (self .PK ))
149+ pkstring = "-lco FID=" + pk
150+ geocolumn = unicode (self .getParameterValue (self .GEOCOLUMN ))
151+ geocolumnstring = "-lco GEOMETRY_NAME=" + geocolumn
152+ dim = self .DIMLIST [self .getParameterValue (self .DIM )]
153+ dimstring = "-lco DIM=" + dim
154+ simplify = unicode (self .getParameterValue (self .SIMPLIFY ))
155+ segmentize = unicode (self .getParameterValue (self .SEGMENTIZE ))
156+ spat = self .getParameterValue (self .SPAT )
157+ ogrspat = self .ogrConnectionString (spat )
158+ clip = self .getParameterValue (self .CLIP )
159+ where = unicode (self .getParameterValue (self .WHERE ))
160+ wherestring = "-where '" + where + "'"
161+ gt = unicode (self .getParameterValue (self .GT ))
162+ overwrite = self .getParameterValue (self .OVERWRITE )
163+ append = self .getParameterValue (self .APPEND )
164+ addfields = self .getParameterValue (self .ADDFIELDS )
165+ launder = self .getParameterValue (self .LAUNDER )
166+ launderstring = "-lco LAUNDER=NO"
167+ index = self .getParameterValue (self .INDEX )
168+ indexstring = "-lco SPATIAL_INDEX=OFF"
169+ skipfailures = self .getParameterValue (self .SKIPFAILURES )
170+ options = unicode (self .getParameterValue (self .OPTIONS ))
171+
172+ arguments = []
173+ arguments .append ('-progress' )
174+ arguments .append ('--config PG_USE_COPY YES' )
175+ arguments .append ('-f' )
176+ arguments .append ('PostgreSQL' )
177+ arguments .append ('PG:"host=' )
178+ arguments .append (host )
179+ arguments .append ('port=' )
180+ arguments .append (port )
181+ arguments .append ('user=' )
182+ arguments .append (user )
183+ arguments .append ('dbname=' )
184+ arguments .append (dbname )
185+ arguments .append ('password=' )
186+ arguments .append (password )
187+ arguments .append ('"' )
188+ arguments .append (dimstring )
189+ arguments .append (ogrLayer )
190+ if index :
191+ arguments .append (indexstring )
192+ if launder :
193+ arguments .append (launderstring )
194+ if append :
195+ arguments .append ('-append' )
196+ if addfields :
197+ arguments .append ('-addfields' )
198+ if overwrite :
199+ arguments .append ('-overwrite' )
200+ if len (self .GEOMTYPE [self .getParameterValue (self .GTYPE )]) > 0 :
201+ arguments .append ('-nlt' )
202+ arguments .append (self .GEOMTYPE [self .getParameterValue (self .GTYPE )])
203+ if len (schema ) > 0 :
204+ arguments .append (schemastring )
205+ if len (geocolumn ) > 0 :
206+ arguments .append (geocolumnstring )
207+ if len (pk ) > 0 :
208+ arguments .append (pkstring )
209+ if len (table ) > 0 :
210+ arguments .append ('-nln' )
211+ arguments .append (table )
212+ if len (ssrs ) > 0 :
213+ arguments .append ('-s_srs' )
214+ arguments .append (ssrs )
215+ if len (tsrs ) > 0 :
216+ arguments .append ('-t_srs' )
217+ arguments .append (tsrs )
218+ if len (spat ) > 0 :
219+ regionCoords = ogrspat .split (',' )
220+ arguments .append ('-spat' )
221+ arguments .append (regionCoords [0 ])
222+ arguments .append (regionCoords [2 ])
223+ arguments .append (regionCoords [1 ])
224+ arguments .append (regionCoords [3 ])
225+ if clip :
226+ arguments .append ('-clipsrc spat_extent' )
227+ if skipfailures :
228+ arguments .append ('-skipfailures' )
229+ if where :
230+ arguments .append (wherestring )
231+ if len (simplify ) > 0 :
232+ arguments .append ('-simplify' )
233+ arguments .append (simplify )
234+ if len (segmentize ) > 0 :
235+ arguments .append ('-segmentize' )
236+ arguments .append (segmentize )
237+ if len (gt ) > 0 :
238+ arguments .append ('-gt' )
239+ arguments .append (gt )
240+ if len (options ) > 0 :
241+ arguments .append (options )
242+
243+ commands = []
244+ if isWindows ():
245+ commands = ['cmd.exe' , '/C ' , 'ogr2ogr.exe' ,
246+ GdalUtils .escapeAndJoin (arguments )]
247+ else :
248+ commands = ['ogr2ogr' , GdalUtils .escapeAndJoin (arguments )]
249+
250+ GdalUtils .runGdal (commands , progress )
0 commit comments