Skip to content

Commit 00e2f1f

Browse files
committedSep 13, 2013
[processing] add Points from polygons algorithm
1 parent 8481ebc commit 00e2f1f

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed
 

‎python/plugins/processing/algs/QGISAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989
from processing.algs.PointsDisplacement import PointsDisplacement
9090
from sextante.algs.ZonalStatistics import ZonalStatistics
91+
from sextante.algs.PointsFromPolygons import PointsFromPolygons
9192

9293
#from processing.algs.VectorLayerHistogram import VectorLayerHistogram
9394
#from processing.algs.VectorLayerScatterplot import VectorLayerScatterplot
@@ -143,7 +144,8 @@ def __init__(self):
143144
#MeanAndStdDevPlot(), BarPlot(), PolarPlot()
144145
# ------ vector ------
145146
PointsDisplacement(),
146-
ZonalStatistics()
147+
ZonalStatistics(),
148+
PointsFromPolygons()
147149
]
148150

149151
def initializeSettings(self):
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
PointsFromPolygons.py
6+
---------------------
7+
Date : August 2013
8+
Copyright : (C) 2013 by Alexander Bruy
9+
Email : alexander dot bruy 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__ = 'Alexander Bruy'
21+
__date__ = 'August 2013'
22+
__copyright__ = '(C) 2013, Alexander Bruy'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
from PyQt4.QtCore import *
27+
28+
from osgeo import gdal
29+
30+
from qgis.core import *
31+
32+
from sextante.core.GeoAlgorithm import GeoAlgorithm
33+
from sextante.core.QGisLayers import QGisLayers
34+
35+
from sextante.parameters.ParameterRaster import ParameterRaster
36+
from sextante.parameters.ParameterVector import ParameterVector
37+
38+
from sextante.outputs.OutputVector import OutputVector
39+
40+
from sextante.algs import QGISUtils as utils
41+
42+
class PointsFromPolygons(GeoAlgorithm):
43+
44+
INPUT_RASTER = "INPUT_RASTER"
45+
RASTER_BAND = "RASTER_BAND"
46+
INPUT_VECTOR = "INPUT_VECTOR"
47+
OUTPUT_LAYER = "OUTPUT_LAYER"
48+
49+
def defineCharacteristics(self):
50+
self.name = "Points from polygons"
51+
self.group = "Vector geometry tools"
52+
53+
self.addParameter(ParameterRaster(self.INPUT_RASTER, "Raster layer"))
54+
self.addParameter(ParameterVector(self.INPUT_VECTOR, "Vector layer", [ParameterVector.VECTOR_TYPE_POLYGON]))
55+
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
56+
57+
def processAlgorithm(self, progress):
58+
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))
59+
60+
rasterPath = unicode(self.getParameterValue(self.INPUT_RASTER))
61+
62+
rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
63+
geoTransform = rasterDS.GetGeoTransform()
64+
rasterDS = None
65+
66+
fields = QgsFields()
67+
fields.append(QgsField("id", QVariant.Int, "", 10, 0))
68+
fields.append(QgsField("poly_id", QVariant.Int, "", 10, 0))
69+
fields.append(QgsField("point_id", QVariant.Int, "", 10, 0))
70+
71+
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(fields.toList(), QGis.WKBPoint, layer.crs())
72+
73+
outFeature = QgsFeature()
74+
outFeature.setFields(fields)
75+
point = QgsPoint()
76+
77+
fid = 0
78+
polyId = 0
79+
pointId = 0
80+
81+
current = 0
82+
features = QGisLayers.features(layer)
83+
total = 100.0 / len(features)
84+
for f in features:
85+
geom = f.geometry()
86+
bbox = geom.boundingBox()
87+
88+
xMin = bbox.xMinimum()
89+
xMax = bbox.xMaximum()
90+
yMin = bbox.yMinimum()
91+
yMax = bbox.yMaximum()
92+
93+
startRow, startColumn = utils.mapToPixel(xMin, yMax, geoTransform)
94+
endRow, endColumn = utils.mapToPixel(xMax, yMin, geoTransform)
95+
96+
for row in xrange(startRow, endRow + 1):
97+
for col in xrange(startColumn, endColumn + 1):
98+
x, y = utils.pixelToMap(row, col, geoTransform)
99+
point.setX(x)
100+
point.setY(y)
101+
102+
if geom.contains(point):
103+
outFeature.setGeometry(QgsGeometry.fromPoint(point))
104+
outFeature["id"] = fid
105+
outFeature["poly_id"] = polyId
106+
outFeature["point_id"] = pointId
107+
108+
fid += 1
109+
pointId +=1
110+
111+
writer.addFeature(outFeature)
112+
113+
pointId = 0
114+
polyId += 1
115+
116+
current += 1
117+
progress.setPercentage(int(current * total))
118+
119+
del writer
120+

0 commit comments

Comments
 (0)
Please sign in to comment.