pktools  2.6.7
Processing Kernel for geospatial data
pkextract.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pkextract.py
6  ---------------------
7  Date : April 2015
8  Copyright : (C) 2015 by Pieter Kempeneers
9  Email : kempenep 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__ = 'Pieter Kempeneers'
21 __date__ = 'April 2015'
22 __copyright__ = '(C) 2015, Pieter Kempeneers'
23 # This will get replaced with a git SHA1 when you do a git archive
24 __revision__ = '$Format:%H$'
25 
26 import os
27 from pktoolsUtils import pktoolsUtils
28 from pktoolsAlgorithm import pktoolsAlgorithm
29 from processing.core.parameters import ParameterRaster
30 from processing.core.parameters import ParameterVector
31 from processing.core.outputs import OutputVector
32 from processing.core.parameters import ParameterSelection
33 from processing.core.parameters import ParameterNumber
34 from processing.core.parameters import ParameterString
35 from processing.core.parameters import ParameterBoolean
36 from processing.core.parameters import ParameterExtent
37 
38 FORMATS = [
39  'ESRI Shapefile',
40  'GeoJSON',
41  'GeoRSS',
42  'SQLite',
43  'GMT',
44  'MapInfo File',
45  'INTERLIS 1',
46  'INTERLIS 2',
47  'GML',
48  'Geoconcept',
49  'DXF',
50  'DGN',
51  'CSV',
52  'BNA',
53  'S57',
54  'KML',
55  'GPX',
56  'PGDump',
57  'GPSTrackMaker',
58  'ODS',
59  'XLSX',
60  'PDF',
61 ]
62 EXTS = [
63  '.shp',
64  '.geojson',
65  '.xml',
66  '.sqlite',
67  '.gmt',
68  '.tab',
69  '.ili',
70  '.ili',
71  '.gml',
72  '.txt',
73  '.dxf',
74  '.dgn',
75  '.csv',
76  '.bna',
77  '.000',
78  '.kml',
79  '.gpx',
80  '.pgdump',
81  '.gtm',
82  '.ods',
83  '.xlsx',
84  '.pdf',
85 ]
86 
88 
89  INPUT = "INPUT"
90  SAMPLE = "SAMPLE"
91  ITERATE = "ITERATE"
92  OUTPUT = "OUTPUT"
93 
94  RULE_OPTIONS = ['centroid', 'point', 'mean', 'proportion', 'count', 'min', 'max', 'mode', 'sum', 'median', 'stdev', 'percentile']
95 
96  RULE = "RULE"
97  POLYGON = "POLYGON"
98  BUFFER = "BUFFER"
99  SRCNODATA = "SRCNODATA"
100  BNDNODATA = "BNDNODATA"
101 
102  EXTRA = 'EXTRA'
103 
104  FORMAT = "FORMAT"
105 
106  def cliName(self):
107  return "pkextractogr"
108 
109  def defineCharacteristics(self):
110  self.name = "extract vector sample from raster"
111  self.group = "[pktools] raster/vector"
112  self.addParameter(ParameterRaster(self.INPUT, 'Input raster data set'))
113  self.addParameter(ParameterVector(self.SAMPLE, 'Sample vector data set'))
114  self.addParameter(ParameterBoolean(self.ITERATE, "Iterate over all layers",True))
115  self.addParameter(ParameterSelection(self.RULE,"extraction rule",self.RULE_OPTIONS, 0))
116 
117  self.addOutput(OutputVector(self.OUTPUT, 'Output vector data set'))
118  self.addParameter(ParameterSelection(self.FORMAT,
119  'Destination Format', FORMATS))
120 
121  self.addParameter(ParameterBoolean(self.POLYGON, "Create OGRPolygon as geometry instead of OGRPoint",False))
122  self.addParameter(ParameterNumber(self.BUFFER, "Buffer for calculating statistics for point features",0,19,0))
123  self.addParameter(ParameterString(self.SRCNODATA, "invalid value(s) for input raster dataset (e.g., 0;255)","none"))
124  self.addParameter(ParameterString(self.BNDNODATA, "Band(s) in input image to check if pixel is valid (e.g., 0;1)","0"))
125 
126  self.addParameter(ParameterString(self.EXTRA,
127  'Additional parameters', '', optional=True))
128 
129  def processAlgorithm(self, progress):
130  cliPath = '"' + os.path.join(pktoolsUtils.pktoolsPath(), self.cliName()) + '"'
131  commands = [cliPath]
132 
133  input=self.getParameterValue(self.INPUT)
134  commands.append('-i')
135  commands.append('"' + input + '"')
136 
137  sample=self.getParameterValue(self.SAMPLE)
138  if self.getParameterValue(self.ITERATE):
139  if str(sample).find('|')>0:
140  samplename=str(sample)[:str(sample).find('|')]
141  else:
142  samplename=str(sample)
143  else:
144  samplename=str(sample).replace("|layername"," -ln")
145  commands.append('-s')
146  commands.append(samplename)
147 
148  if self.getParameterValue(self.POLYGON):
149  commands.append("-polygon")
150 
151  commands.append("-r")
152  commands.append(self.RULE_OPTIONS[self.getParameterValue(self.RULE)])
153 
154  output = self.getOutputFromName(self.OUTPUT)
155  outFile = output.value
156  formatIdx = self.getParameterValue(self.FORMAT)
157  outFormat = '"' + FORMATS[formatIdx] + '"'
158  commands.append('-f')
159  commands.append(outFormat)
160  ext = EXTS[formatIdx]
161  if not outFile.endswith(ext):
162  outFile += ext
163  output.value = outFile
164  commands.append('-o')
165  commands.append('"' + outFile + '"')
166 
167  buffer=self.getParameterValue(self.BUFFER)
168  if buffer > 1:
169  commands.append("-buf")
170  commands.append(str(buffer))
171 
172  srcnodata=self.getParameterValue(self.SRCNODATA)
173  if srcnodata != "none":
174  srcnodataValues = srcnodata.split(';')
175  for srcnodataValue in srcnodataValues:
176  commands.append('-srcnodata')
177  commands.append(srcnodataValue)
178  bndnodata=self.getParameterValue(self.BNDNODATA)
179  bndnodataValues = bndnodata.split(';')
180  for bndnodataValue in bndnodataValues:
181  commands.append('-bndnodata')
182  commands.append(bndnodataValue)
183 
184  extra = str(self.getParameterValue(self.EXTRA))
185  if len(extra) > 0:
186  commands.append(extra)
187 
188  pktoolsUtils.runpktools(commands, progress)