1
|
|
2
|
|
3
|
"""
|
4
|
***************************************************************************
|
5
|
ogr2ogrdistance.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
|
|
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 ParameterNumber
|
38
|
from processing.core.parameters import ParameterBoolean
|
39
|
from processing.core.parameters import ParameterTableField
|
40
|
from processing.core.outputs import OutputVector
|
41
|
|
42
|
from processing.tools.system import *
|
43
|
from processing.tools import dataobjects
|
44
|
|
45
|
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
|
46
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
47
|
|
48
|
class Ogr2OgrDistance(OgrAlgorithm):
|
49
|
|
50
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
51
|
INPUT_LAYER_A = 'INPUT_LAYER_A'
|
52
|
INPUT_LAYER_B = 'INPUT_LAYER_B'
|
53
|
FIELD_A = 'FIELD_A'
|
54
|
FIELD_B = 'FIELD_B'
|
55
|
TABLE = 'TABLE'
|
56
|
OPTIONS = 'OPTIONS'
|
57
|
|
58
|
def defineCharacteristics(self):
|
59
|
self.name = 'Distance between features'
|
60
|
self.group = '[OGR] PostGIS Geoprocessing'
|
61
|
|
62
|
self.addParameter(ParameterVector(self.INPUT_LAYER_A, 'First input layer',
|
63
|
[ParameterVector.VECTOR_TYPE_ANY], False))
|
64
|
self.addParameter(ParameterTableField(self.FIELD_A, 'First input layer ID',
|
65
|
self.INPUT_LAYER_A, optional=False))
|
66
|
self.addParameter(ParameterVector(self.INPUT_LAYER_B, 'Second input layer',
|
67
|
[ParameterVector.VECTOR_TYPE_ANY], False))
|
68
|
self.addParameter(ParameterTableField(self.FIELD_B, 'Second input layer ID',
|
69
|
self.INPUT_LAYER_B, optional=False))
|
70
|
self.addParameter(ParameterString(self.TABLE, 'Table name',
|
71
|
'distance_analysis', optional=False))
|
72
|
self.addParameter(ParameterString(self.OPTIONS, 'Additional creation options (see ogr2ogr manual)',
|
73
|
'', optional=True))
|
74
|
|
75
|
def processAlgorithm(self, progress):
|
76
|
inLayerA = self.getParameterValue(self.INPUT_LAYER_A)
|
77
|
ogrLayerA = self.ogrConnectionString(inLayerA)
|
78
|
layernameA = self.ogrLayerName(inLayerA)
|
79
|
inLayerB = self.getParameterValue(self.INPUT_LAYER_B)
|
80
|
ogrLayerB = self.ogrConnectionString(inLayerB)
|
81
|
layernameB = self.ogrLayerName(inLayerB)
|
82
|
fieldA = unicode(self.getParameterValue(self.FIELD_A))
|
83
|
fieldB = unicode(self.getParameterValue(self.FIELD_B))
|
84
|
dsUriA = QgsDataSourceURI(self.getParameterValue(self.INPUT_LAYER_A))
|
85
|
geomColumnA = dsUriA.geometryColumn()
|
86
|
dsUriB = QgsDataSourceURI(self.getParameterValue(self.INPUT_LAYER_B))
|
87
|
geomColumnB = dsUriB.geometryColumn()
|
88
|
table = unicode(self.getParameterValue(self.TABLE))
|
89
|
sqlstring = "-sql \"SELECT ST_ShortestLine(g1." + geomColumnA + ",g2." + geomColumnB + ") AS geom, ST_Distance(g1." + geomColumnA + ",g2." + geomColumnB + ") AS distance, g1. " + fieldA + " AS id_from, g2. " + fieldB + " AS id_to FROM " + layernameA + " AS g1, " + layernameB + " AS g2\" -nln " + table + " -lco FID=gid -lco GEOMETRY_NAME=geom -nlt LINESTRING --config PG_USE_COPY YES"
|
90
|
options = unicode(self.getParameterValue(self.OPTIONS))
|
91
|
|
92
|
arguments = []
|
93
|
arguments.append('-f')
|
94
|
arguments.append('PostgreSQL')
|
95
|
arguments.append(ogrLayerA)
|
96
|
arguments.append(ogrLayerA)
|
97
|
arguments.append(sqlstring)
|
98
|
arguments.append('-overwrite')
|
99
|
|
100
|
if len(options) > 0:
|
101
|
arguments.append(options)
|
102
|
|
103
|
commands = []
|
104
|
if isWindows():
|
105
|
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
|
106
|
GdalUtils.escapeAndJoin(arguments)]
|
107
|
else:
|
108
|
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
109
|
|
110
|
GdalUtils.runGdal(commands, progress)
|