Skip to content
Permalink
Browse files

Merge pull request #1503 from alexbruy/processing-ogr

Processing ogr
  • Loading branch information
volaya committed Jul 10, 2014
2 parents 6097e58 + c33007f commit 66e4895ad70f07c12f5ff56d3792a8e06ffb0114
@@ -1,5 +1,3 @@
FILE(GLOB PY_FILES *.py)

ADD_SUBDIRECTORY(pyogr)

PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})
PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})
@@ -138,10 +138,10 @@ def getFormatShortNameFromFilename(filename):
def escapeAndJoin(strList):
joined = ''
for s in strList:
if s[0]!='-' and ' ' in s:
if s[0] != '-' and ' ' in s:
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \
+ '"'
else:
escaped = s
joined += escaped + ' '
return joined.strip()
return joined.strip()
@@ -44,8 +44,6 @@

class OgrAlgorithm(GdalAlgorithm):

DB = 'DB'

def ogrConnectionString(self, uri):
ogrstr = None

@@ -68,16 +66,3 @@ def ogrConnectionString(self, uri):
else:
ogrstr = str(layer.source())
return ogrstr

def drivers(self):
list = []
if ogrAvailable:
for iDriver in range(ogr.GetDriverCount()):
list.append('%s' % ogr.GetDriver(iDriver).GetName())
return list

def failure(self, pszDataSource):
out = 'FAILURE: Unable to open datasource %s with the following \
drivers.' % pszDataSource
out = out + string.join(map(lambda d: '->' + d, self.drivers()), '\n')
return out
@@ -64,6 +64,8 @@ def processAlgorithm(self, progress):
progress)
output = self.getOutputValue(information.OUTPUT)
f = open(output, 'w')
f.write('<pre>')
for s in GdalUtils.getConsoleOutput()[1:]:
f.write('<p>' + str(s) + '</p>')
f.write(unicode(s))
f.write('</pre>')
f.close()
@@ -25,27 +25,22 @@

__revision__ = '$Format:%H$'


try:
from osgeo import gdal, ogr, osr
gdalAvailable = True
except:
gdalAvailable = False
import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterSelection import ParameterSelection
from processing.outputs.OutputVector import OutputVector

from OgrAlgorithm import OgrAlgorithm
from pyogr.ogr2ogr import *
from processing.tools.system import *

GeomOperation = Enum(['NONE', 'SEGMENTIZE', 'SIMPLIFY_PRESERVE_TOPOLOGY'])
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

FORMATS = [
'ESRI Shapefile',
@@ -101,147 +96,57 @@ class Ogr2Ogr(OgrAlgorithm):

OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
DEST_DS = 'DEST_DS'
DEST_FORMAT = 'DEST_FORMAT'
DEST_DSCO = 'DEST_DSCO'
FORMAT = 'FORMAT'
OPTIONS = 'OPTIONS'

def defineCharacteristics(self):
self.name = 'Convert format'
self.group = '[OGR] Conversion'

self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
[ParameterVector.VECTOR_TYPE_ANY], False))
self.addParameter(ParameterSelection(self.DEST_FORMAT,
self.addParameter(ParameterSelection(self.FORMAT,
'Destination Format', FORMATS))
self.addParameter(ParameterString(self.DEST_DSCO, 'Creation Options',
''))
self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
'', optional=True))

self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))

def commandLineName(self):
return "gdalogr:ogr2ogr"
def processAlgorithm(self, progress):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(inLayer)

output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value

def processAlgorithm(self, progress):
if not gdalAvailable:
raise GeoAlgorithmExecutionException(
'GDAL bindings not installed.')
formatIdx = self.getParameterValue(self.FORMAT)
outFormat = FORMATS[formatIdx]
ext = EXTS[formatIdx]
if not outFile.endswith(ext):
outFile += ext
output.value = outFile

input = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(input)
output = self.ogrConnectionString(outFile)
options = unicode(self.getParameterValue(self.OPTIONS))

output = self.getOutputFromName(self.OUTPUT_LAYER)
outfile = output.value
if outFormat == 'SQLite' and os.path.isfile(output):
os.remove(output)

formatIdx = self.getParameterValue(self.DEST_FORMAT)
arguments = []
arguments.append('-f')
arguments.append(outFormat)
if len(options) > 0:
arguments.append(options)

ext = EXTS[formatIdx]
if not outfile.endswith(ext):
outfile = outfile + ext
output.value = outfile

dst_ds = self.ogrConnectionString(outfile)
dst_format = FORMATS[formatIdx]
ogr_dsco = [self.getParameterValue(self.DEST_DSCO)]

poDS = ogr.Open(ogrLayer, False)
if poDS is None:
raise GeoAlgorithmExecutionException(self.failure(ogrLayer))

if dst_format == 'SQLite' and os.path.isfile(dst_ds):
os.remove(dst_ds)
driver = ogr.GetDriverByName(str(dst_format))
poDstDS = driver.CreateDataSource(dst_ds, options=ogr_dsco)
if poDstDS is None:
raise GeoAlgorithmExecutionException('Error creating %s' % dst_ds)
return
self.ogrtransform(poDS, poDstDS, bOverwrite=True)

def ogrtransform(
self,
poSrcDS,
poDstDS,
papszLayers=[],
papszLCO=[],
bTransform=False,
bAppend=False,
bUpdate=False,
bOverwrite=False,
poOutputSRS=None,
poSourceSRS=None,
pszNewLayerName=None,
pszWHERE=None,
papszSelFields=None,
eGType=-2,
eGeomOp=GeomOperation.NONE,
dfGeomOpParam=0,
papszFieldTypesToString=[],
pfnProgress=None,
pProgressData=None,
nCountLayerFeatures=0,
poClipSrc=None,
poClipDst=None,
bExplodeCollections=False,
pszZField=None,
):

# Process each data source layer
if len(papszLayers) == 0:
nLayerCount = poSrcDS.GetLayerCount()
papoLayers = [None for i in range(nLayerCount)]
iLayer = 0

for iLayer in range(nLayerCount):
poLayer = poSrcDS.GetLayer(iLayer)

if poLayer is None:
raise GeoAlgorithmExecutionException(
"FAILURE: Couldn't fetch advertised layer %d!"
% iLayer)

papoLayers[iLayer] = poLayer
iLayer = iLayer + 1
arguments.append(output)
arguments.append(ogrLayer)

commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
# Process specified data source layers
nLayerCount = len(papszLayers)
papoLayers = [None for i in range(nLayerCount)]
iLayer = 0

for layername in papszLayers:
poLayer = poSrcDS.GetLayerByName(layername)

if poLayer is None:
raise GeoAlgorithmExecutionException(
"FAILURE: Couldn't fetch advertised layer %s!"
% layername)

papoLayers[iLayer] = poLayer
iLayer = iLayer + 1

for poSrcLayer in papoLayers:
ok = TranslateLayer(
poSrcDS,
poSrcLayer,
poDstDS,
papszLCO,
pszNewLayerName,
bTransform,
poOutputSRS,
poSourceSRS,
papszSelFields,
bAppend,
eGType,
bOverwrite,
eGeomOp,
dfGeomOpParam,
papszFieldTypesToString,
nCountLayerFeatures,
poClipSrc,
poClipDst,
bExplodeCollections,
pszZField,
pszWHERE,
pfnProgress,
pProgressData,
)
return True
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)

0 comments on commit 66e4895

Please sign in to comment.
You can’t perform that action at this time.