2012-12-02 00:03:21 +01:00
|
|
|
# -*- 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'November 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-02 00:03:21 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-02 00:03:21 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2014-07-08 20:08:31 +03:00
|
|
|
import os
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import isWindows
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2014-07-08 20:08:31 +03:00
|
|
|
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
|
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
FORMATS = [
|
|
|
|
'ESRI Shapefile',
|
|
|
|
'GeoJSON',
|
2014-05-18 10:25:05 +01:00
|
|
|
'GeoRSS',
|
2013-10-01 20:52:22 +03:00
|
|
|
'SQLite',
|
2014-05-18 10:25:05 +01:00
|
|
|
'GMT',
|
|
|
|
'MapInfo File',
|
2013-10-01 20:52:22 +03:00
|
|
|
'INTERLIS 1',
|
|
|
|
'INTERLIS 2',
|
2014-05-18 10:25:05 +01:00
|
|
|
'GML',
|
|
|
|
'Geoconcept',
|
|
|
|
'DXF',
|
|
|
|
'DGN',
|
|
|
|
'CSV',
|
|
|
|
'BNA',
|
|
|
|
'S57',
|
|
|
|
'KML',
|
|
|
|
'GPX',
|
|
|
|
'PGDump',
|
|
|
|
'GPSTrackMaker',
|
2014-05-21 21:25:18 +02:00
|
|
|
'ODS',
|
2014-05-18 10:25:05 +01:00
|
|
|
'XLSX',
|
|
|
|
'PDF',
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
EXTS = [
|
2014-05-18 10:25:05 +01:00
|
|
|
'.shp',
|
|
|
|
'.geojson',
|
2013-10-01 20:52:22 +03:00
|
|
|
'.xml',
|
|
|
|
'.sqlite',
|
|
|
|
'.gmt',
|
|
|
|
'.tab',
|
2014-05-18 10:25:05 +01:00
|
|
|
'.ili',
|
2013-10-01 20:52:22 +03:00
|
|
|
'.ili',
|
|
|
|
'.gml',
|
|
|
|
'.txt',
|
|
|
|
'.dxf',
|
|
|
|
'.dgn',
|
|
|
|
'.csv',
|
|
|
|
'.bna',
|
|
|
|
'.000',
|
|
|
|
'.kml',
|
2014-05-18 10:25:05 +01:00
|
|
|
'.gpx',
|
|
|
|
'.pgdump',
|
|
|
|
'.gtm',
|
|
|
|
'.ods',
|
|
|
|
'.xlsx',
|
|
|
|
'.pdf',
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
2012-11-10 00:31:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Ogr2Ogr(OgrAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
2014-07-08 20:08:31 +03:00
|
|
|
FORMAT = 'FORMAT'
|
|
|
|
OPTIONS = 'OPTIONS'
|
2012-11-10 00:31:01 +01:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Convert format'
|
|
|
|
self.group = '[OGR] Conversion'
|
2012-11-10 00:31:01 +01:00
|
|
|
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
|
2014-07-08 20:08:31 +03:00
|
|
|
self.addParameter(ParameterSelection(self.FORMAT,
|
2015-01-13 10:49:08 +02:00
|
|
|
self.tr('Destination Format'), FORMATS))
|
|
|
|
self.addParameter(ParameterString(self.OPTIONS,
|
|
|
|
self.tr('Creation options'), '', optional=True))
|
2012-11-10 00:31:01 +01:00
|
|
|
|
2015-05-18 09:29:41 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Converted')))
|
2013-10-24 15:26:39 +02:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
def getConsoleCommands(self):
|
2014-07-08 20:08:31 +03:00
|
|
|
inLayer = self.getParameterValue(self.INPUT_LAYER)
|
2015-01-21 19:19:45 +00:00
|
|
|
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2013-04-12 12:30:59 +02:00
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
2014-07-08 20:08:31 +03:00
|
|
|
outFile = output.value
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2014-07-08 20:08:31 +03:00
|
|
|
formatIdx = self.getParameterValue(self.FORMAT)
|
|
|
|
outFormat = FORMATS[formatIdx]
|
2013-04-12 12:30:59 +02:00
|
|
|
ext = EXTS[formatIdx]
|
2014-07-08 20:08:31 +03:00
|
|
|
if not outFile.endswith(ext):
|
|
|
|
outFile += ext
|
|
|
|
output.value = outFile
|
|
|
|
|
|
|
|
output = self.ogrConnectionString(outFile)
|
|
|
|
options = unicode(self.getParameterValue(self.OPTIONS))
|
|
|
|
|
|
|
|
if outFormat == 'SQLite' and os.path.isfile(output):
|
|
|
|
os.remove(output)
|
|
|
|
|
|
|
|
arguments = []
|
|
|
|
arguments.append('-f')
|
|
|
|
arguments.append(outFormat)
|
|
|
|
if len(options) > 0:
|
|
|
|
arguments.append(options)
|
|
|
|
|
|
|
|
arguments.append(output)
|
|
|
|
arguments.append(ogrLayer)
|
2015-01-21 19:19:45 +00:00
|
|
|
arguments.append(self.ogrLayerName(inLayer))
|
2014-07-08 20:08:31 +03:00
|
|
|
|
|
|
|
commands = []
|
|
|
|
if isWindows():
|
|
|
|
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
|
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
2012-11-10 00:31:01 +01:00
|
|
|
else:
|
2014-07-08 20:08:31 +03:00
|
|
|
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|
|
|
|
|