/
ogr2ogr.py
155 lines (127 loc) · 4.22 KB
/
ogr2ogr.py
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
# -*- coding: utf-8 -*-
"""
***************************************************************************
ogr2ogr.py
---------------------
Date : November 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf 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. *
* *
***************************************************************************
"""
from builtins import str
__author__ = 'Victor Olaya'
__date__ = 'November 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools.system import isWindows
from processing.tools.vector import ogrConnectionString, ogrLayerName
FORMATS = [
'ESRI Shapefile',
'GeoJSON',
'GeoRSS',
'SQLite',
'GMT',
'MapInfo File',
'INTERLIS 1',
'INTERLIS 2',
'GML',
'Geoconcept',
'DXF',
'DGN',
'CSV',
'BNA',
'S57',
'KML',
'GPX',
'PGDump',
'GPSTrackMaker',
'ODS',
'XLSX',
'PDF',
]
EXTS = [
'.shp',
'.geojson',
'.xml',
'.sqlite',
'.gmt',
'.tab',
'.ili',
'.ili',
'.gml',
'.txt',
'.dxf',
'.dgn',
'.csv',
'.bna',
'.000',
'.kml',
'.gpx',
'.pgdump',
'.gtm',
'.ods',
'.xlsx',
'.pdf',
]
class Ogr2Ogr(GdalAlgorithm):
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
FORMAT = 'FORMAT'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Convert format')
self.group, self.i18n_group = self.trAlgorithm('[OGR] Conversion')
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(ParameterSelection(self.FORMAT,
self.tr('Destination Format'), FORMATS))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Creation options'), '', optional=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Converted')))
def getConsoleCommands(self):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
formatIdx = self.getParameterValue(self.FORMAT)
outFormat = FORMATS[formatIdx]
ext = EXTS[formatIdx]
if not outFile.endswith(ext):
outFile += ext
output.value = outFile
output = ogrConnectionString(outFile)
options = str(self.getParameterValue(self.OPTIONS))
if outFormat == 'SQLite' and os.path.isfile(output):
os.remove(output)
arguments = []
arguments.append('-f')
arguments.append(outFormat)
if options is not None and len(options.strip()) > 0:
arguments.append(options)
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
return commands
def commandName(self):
return "ogr2ogr"