mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-11-03 00:14:12 -05:00 
			
		
		
		
	Merge pull request #1649 from gioman/ogr_clip
add Processing clip using ogr2ogr to compensate for the bad Clip perform...
This commit is contained in:
		
						commit
						47f5873e86
					
				@ -66,6 +66,8 @@ from GridNearest import GridNearest
 | 
			
		||||
from GridDataMetrics import GridDataMetrics
 | 
			
		||||
 | 
			
		||||
from ogr2ogr import Ogr2Ogr
 | 
			
		||||
from ogr2ogrclip import Ogr2OgrClip
 | 
			
		||||
from ogr2ogrclipextent import Ogr2OgrClipExtent
 | 
			
		||||
from ogrinfo import OgrInfo
 | 
			
		||||
from ogrsql import OgrSql
 | 
			
		||||
 | 
			
		||||
@ -119,7 +121,7 @@ class GdalOgrAlgorithmProvider(AlgorithmProvider):
 | 
			
		||||
            ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
 | 
			
		||||
            GridDataMetrics(),
 | 
			
		||||
            # ----- OGR tools -----
 | 
			
		||||
            OgrInfo(), Ogr2Ogr(), OgrSql(),
 | 
			
		||||
            OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(), OgrSql(),
 | 
			
		||||
            ]
 | 
			
		||||
 | 
			
		||||
        # And then we add those that are created as python scripts
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										93
									
								
								python/plugins/processing/algs/gdal/ogr2ogrclip.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								python/plugins/processing/algs/gdal/ogr2ogrclip.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,93 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
***************************************************************************
 | 
			
		||||
    ogr2ogrclip.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'
 | 
			
		||||
 | 
			
		||||
# This will get replaced with a git SHA1 when you do a git archive
 | 
			
		||||
 | 
			
		||||
__revision__ = '$Format:%H$'
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from PyQt4.QtCore import *
 | 
			
		||||
from PyQt4.QtGui import *
 | 
			
		||||
 | 
			
		||||
from qgis.core import *
 | 
			
		||||
 | 
			
		||||
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.tools.system import *
 | 
			
		||||
 | 
			
		||||
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
 | 
			
		||||
from processing.algs.gdal.GdalUtils import GdalUtils
 | 
			
		||||
 | 
			
		||||
class Ogr2OgrClip(OgrAlgorithm):
 | 
			
		||||
 | 
			
		||||
    OUTPUT_LAYER = 'OUTPUT_LAYER'
 | 
			
		||||
    INPUT_LAYER = 'INPUT_LAYER'
 | 
			
		||||
    CLIP_LAYER = 'CLIP_LAYER'
 | 
			
		||||
    OPTIONS = 'OPTIONS'
 | 
			
		||||
 | 
			
		||||
    def defineCharacteristics(self):
 | 
			
		||||
        self.name = 'Clip vectors by polygon'
 | 
			
		||||
        self.group = '[OGR] Miscellaneous'
 | 
			
		||||
 | 
			
		||||
        self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
 | 
			
		||||
                          [ParameterVector.VECTOR_TYPE_ANY], False))
 | 
			
		||||
        self.addParameter(ParameterVector(self.CLIP_LAYER, 'Clip layer',
 | 
			
		||||
                          [ParameterVector.VECTOR_TYPE_POLYGON], False))	
 | 
			
		||||
        self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
 | 
			
		||||
                          '', optional=True))
 | 
			
		||||
 | 
			
		||||
        self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
 | 
			
		||||
 | 
			
		||||
    def processAlgorithm(self, progress):
 | 
			
		||||
        inLayer = self.getParameterValue(self.INPUT_LAYER)
 | 
			
		||||
        ogrLayer = self.ogrConnectionString(inLayer)
 | 
			
		||||
        clipLayer = self.getParameterValue(self.CLIP_LAYER)
 | 
			
		||||
        ogrClipLayer = self.ogrConnectionString(clipLayer)
 | 
			
		||||
 | 
			
		||||
        output = self.getOutputFromName(self.OUTPUT_LAYER)
 | 
			
		||||
        outFile = output.value
 | 
			
		||||
 | 
			
		||||
        output = self.ogrConnectionString(outFile)
 | 
			
		||||
        options = unicode(self.getParameterValue(self.OPTIONS))
 | 
			
		||||
 | 
			
		||||
        arguments = []
 | 
			
		||||
        arguments.append('-clipsrc')
 | 
			
		||||
        arguments.append(ogrClipLayer)        
 | 
			
		||||
        if len(options) > 0:
 | 
			
		||||
            arguments.append(options)
 | 
			
		||||
 | 
			
		||||
        arguments.append(output)
 | 
			
		||||
        arguments.append(ogrLayer)
 | 
			
		||||
 | 
			
		||||
        commands = []
 | 
			
		||||
        if isWindows():
 | 
			
		||||
            commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
 | 
			
		||||
                        GdalUtils.escapeAndJoin(arguments)]
 | 
			
		||||
        else:
 | 
			
		||||
            commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
 | 
			
		||||
 | 
			
		||||
        GdalUtils.runGdal(commands, progress)
 | 
			
		||||
							
								
								
									
										103
									
								
								python/plugins/processing/algs/gdal/ogr2ogrclipextent.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								python/plugins/processing/algs/gdal/ogr2ogrclipextent.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,103 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
***************************************************************************
 | 
			
		||||
    ogr2ogrclipextent.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'
 | 
			
		||||
 | 
			
		||||
# This will get replaced with a git SHA1 when you do a git archive
 | 
			
		||||
 | 
			
		||||
__revision__ = '$Format:%H$'
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from PyQt4.QtCore import *
 | 
			
		||||
from PyQt4.QtGui import *
 | 
			
		||||
 | 
			
		||||
from qgis.core import *
 | 
			
		||||
 | 
			
		||||
from processing.core.parameters import ParameterVector
 | 
			
		||||
from processing.core.parameters import ParameterString
 | 
			
		||||
from processing.core.parameters import ParameterSelection
 | 
			
		||||
from processing.core.parameters import ParameterExtent
 | 
			
		||||
from processing.core.outputs import OutputVector
 | 
			
		||||
 | 
			
		||||
from processing.tools.system import *
 | 
			
		||||
 | 
			
		||||
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
 | 
			
		||||
from processing.algs.gdal.GdalUtils import GdalUtils
 | 
			
		||||
 | 
			
		||||
class Ogr2OgrClipExtent(OgrAlgorithm):
 | 
			
		||||
 | 
			
		||||
    OUTPUT_LAYER = 'OUTPUT_LAYER'
 | 
			
		||||
    INPUT_LAYER = 'INPUT_LAYER'
 | 
			
		||||
    CLIP_EXTENT = 'CLIP_EXTENT'
 | 
			
		||||
    OPTIONS = 'OPTIONS'
 | 
			
		||||
 | 
			
		||||
    def defineCharacteristics(self):
 | 
			
		||||
        self.name = 'Clip vectors by extent'
 | 
			
		||||
        self.group = '[OGR] Miscellaneous'
 | 
			
		||||
 | 
			
		||||
        self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
 | 
			
		||||
                          [ParameterVector.VECTOR_TYPE_ANY], False))
 | 
			
		||||
        self.addParameter(ParameterExtent(self.CLIP_EXTENT,
 | 
			
		||||
                          'Clip extent'))
 | 
			
		||||
	#self.addParameter(ParameterString(self.CLIP_EXTENT, 'Clip extent',
 | 
			
		||||
        #                  '', optional=False))
 | 
			
		||||
        self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
 | 
			
		||||
                          '', optional=True))
 | 
			
		||||
 | 
			
		||||
        self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
 | 
			
		||||
 | 
			
		||||
    def processAlgorithm(self, progress):
 | 
			
		||||
        inLayer = self.getParameterValue(self.INPUT_LAYER)
 | 
			
		||||
        ogrLayer = self.ogrConnectionString(inLayer)
 | 
			
		||||
        clipExtent = self.getParameterValue(self.CLIP_EXTENT)
 | 
			
		||||
        ogrclipExtent = self.ogrConnectionString(clipExtent)
 | 
			
		||||
 | 
			
		||||
        output = self.getOutputFromName(self.OUTPUT_LAYER)
 | 
			
		||||
        outFile = output.value
 | 
			
		||||
 | 
			
		||||
        output = self.ogrConnectionString(outFile)
 | 
			
		||||
        options = unicode(self.getParameterValue(self.OPTIONS))
 | 
			
		||||
 | 
			
		||||
        arguments = []
 | 
			
		||||
        regionCoords = ogrclipExtent.split(',')
 | 
			
		||||
        arguments.append('-spat')
 | 
			
		||||
        arguments.append(regionCoords[0])
 | 
			
		||||
        arguments.append(regionCoords[2])
 | 
			
		||||
        arguments.append(regionCoords[1])
 | 
			
		||||
        arguments.append(regionCoords[3])        
 | 
			
		||||
        #arguments.append('-spat')
 | 
			
		||||
        #arguments.append(ogrclipExtent)        
 | 
			
		||||
 | 
			
		||||
        if len(options) > 0:
 | 
			
		||||
            arguments.append(options)
 | 
			
		||||
 | 
			
		||||
        arguments.append(output)
 | 
			
		||||
        arguments.append(ogrLayer)
 | 
			
		||||
 | 
			
		||||
        commands = []
 | 
			
		||||
        if isWindows():
 | 
			
		||||
            commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
 | 
			
		||||
                        GdalUtils.escapeAndJoin(arguments)]
 | 
			
		||||
        else:
 | 
			
		||||
            commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
 | 
			
		||||
 | 
			
		||||
        GdalUtils.runGdal(commands, progress)
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user