Skip to content
Permalink
Browse files

Port Points From Lines to new API

  • Loading branch information
nyalldawson committed Aug 8, 2017
1 parent c6b3855 commit 47fe2a182de8224b48662c489de0c27ff86b56dc
@@ -29,28 +29,29 @@

from osgeo import gdal
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsApplication,
QgsFeature,
from qgis.core import (QgsFeature,
QgsFeatureSink,
QgsFields,
QgsField,
QgsGeometry,
QgsPointXY,
QgsWkbTypes,
QgsProcessingUtils)
from processing.tools import raster, dataobjects
QgsProcessing,
QgsFeatureRequest,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from processing.tools import raster
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools.dataobjects import exportRasterLayer


class PointsFromLines(QgisAlgorithm):

INPUT_RASTER = 'INPUT_RASTER'
RASTER_BAND = 'RASTER_BAND'
INPUT_VECTOR = 'INPUT_VECTOR'
OUTPUT_LAYER = 'OUTPUT_LAYER'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Vector analysis tools')
@@ -59,11 +60,11 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT_RASTER,
self.tr('Raster layer')))
self.addParameter(ParameterVector(self.INPUT_VECTOR,
self.tr('Vector layer'), [dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Points along line'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT_RASTER,
self.tr('Raster layer')))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_VECTOR,
self.tr('Vector layer'), [QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Points from polygons'), QgsProcessing.TypeVectorPoint))

def name(self):
return 'generatepointspixelcentroidsalongline'
@@ -72,9 +73,10 @@ def displayName(self):
return self.tr('Generate points (pixel centroids) along line')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_VECTOR), context)
source = self.parameterAsSource(parameters, self.INPUT_VECTOR, context)

rasterPath = str(self.getParameterValue(self.INPUT_RASTER))
raster_layer = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
rasterPath = exportRasterLayer(raster_layer)

rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
geoTransform = rasterDS.GetGeoTransform()
@@ -85,8 +87,8 @@ def processAlgorithm(self, parameters, context, feedback):
fields.append(QgsField('line_id', QVariant.Int, '', 10, 0))
fields.append(QgsField('point_id', QVariant.Int, '', 10, 0))

writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(fields, QgsWkbTypes.Point,
layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.Point, raster_layer.crs())

outFeature = QgsFeature()
outFeature.setFields(fields)
@@ -95,9 +97,11 @@ def processAlgorithm(self, parameters, context, feedback):
self.lineId = 0
self.pointId = 0

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(raster_layer.crs()))
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, f in enumerate(features):
if feedback.isCanceled():
break
geom = f.geometry()
if geom.isMultipart():
lines = geom.asMultiPolyline()
@@ -112,7 +116,7 @@ def processAlgorithm(self, parameters, context, feedback):
geoTransform)

self.buildLine(x1, y1, x2, y2, geoTransform,
writer, outFeature)
sink, outFeature)
else:
points = geom.asPolyline()
for i in range(len(points) - 1):
@@ -122,15 +126,15 @@ def processAlgorithm(self, parameters, context, feedback):
(x1, y1) = raster.mapToPixel(p1.x(), p1.y(), geoTransform)
(x2, y2) = raster.mapToPixel(p2.x(), p2.y(), geoTransform)

self.buildLine(x1, y1, x2, y2, geoTransform, writer,
self.buildLine(x1, y1, x2, y2, geoTransform, sink,
outFeature)

self.pointId = 0
self.lineId += 1

feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}

def buildLine(self, startX, startY, endX, endY, geoTransform, writer, feature):
if startX == endX:
@@ -87,7 +87,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields.append(QgsField('point_id', QVariant.Int, '', 10, 0))

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.PointZ, raster_layer.crs())
fields, QgsWkbTypes.Point, raster_layer.crs())

outFeature = QgsFeature()
outFeature.setFields(fields)
@@ -95,6 +95,7 @@
from .PointOnSurface import PointOnSurface
from .PointsAlongGeometry import PointsAlongGeometry
from .PointsDisplacement import PointsDisplacement
from .PointsFromLines import PointsFromLines
from .PointsFromPolygons import PointsFromPolygons
from .PointsInPolygon import PointsInPolygon
from .PointsLayerFromTable import PointsLayerFromTable
@@ -155,7 +156,6 @@
# from .GeometryConvert import GeometryConvert
# from .FieldsCalculator import FieldsCalculator
# from .FieldPyculator import FieldsPyculator
# from .PointsFromLines import PointsFromLines
# from .SetVectorStyle import SetVectorStyle
# from .SetRasterStyle import SetRasterStyle
# from .SelectByAttributeSum import SelectByAttributeSum
@@ -191,7 +191,6 @@ def getAlgs(self):
# SpatialJoin(),
# GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(),
# PointsFromLines(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
@@ -259,6 +258,7 @@ def getAlgs(self):
PointOnSurface(),
PointsAlongGeometry(),
PointsDisplacement(),
PointsFromLines(),
PointsFromPolygons(),
PointsInPolygon(),
PointsLayerFromTable(),
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>pixel_lines</Name>
<ElementPath>pixel_lines</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>3</FeatureCount>
<ExtentXMin>18.69765</ExtentXMin>
<ExtentXMax>18.69836</ExtentXMax>
<ExtentYMin>45.79745</ExtentYMin>
<ExtentYMax>45.79816</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ pixel_lines.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>18.69764779785013</gml:X><gml:Y>45.79744720550632</gml:Y></gml:coord>
<gml:coord><gml:X>18.69836033831157</gml:X><gml:Y>45.79815749820605</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:pixel_lines fid="pixel_lines.0">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>18.6976477978501,45.798157498206 18.697964732251,45.7981507549209</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:pixel_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_lines fid="pixel_lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>18.6983603383116,45.7974472055063 18.6983603383116,45.7977618921454</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:pixel_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_lines fid="pixel_lines.2">
</ogr:pixel_lines>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,30 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>pixel_polygons</Name>
<ElementPath>pixel_polygons</ElementPath>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>3</FeatureCount>
<ExtentXMin>18.69514</ExtentXMin>
<ExtentXMax>18.69889</ExtentXMax>
<ExtentYMin>45.79821</ExtentYMin>
<ExtentYMax>45.80722</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>5</Width>
</PropertyDefn>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ pixel_polygons.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>18.69513929578401</gml:X><gml:Y>45.79820863478491</gml:Y></gml:coord>
<gml:coord><gml:X>18.69889193395552</gml:X><gml:Y>45.80721878759138</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:pixel_polygons fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>18.6987166085423,45.7984862333558 18.6987132368997,45.7982086347849 18.6988919339555,45.7982086347849 18.6988874384321,45.798485109475 18.6987166085423,45.7984862333558</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
</ogr:pixel_polygons>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_polygons fid="polys.0">
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:4326"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>18.695139295784,45.8072159778892 18.695139295784,45.8070440241186 18.695359014491,45.8070429002377 18.6953595764314,45.8072187875914 18.695139295784,45.8072159778892</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:pixel_polygons>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_polygons fid="polys.2">
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123</ogr:floatval>
</ogr:pixel_polygons>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,31 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>pixel_centroids_lines</Name>
<ElementPath>pixel_centroids_lines</ElementPath>
<!--POINT-->
<GeometryType>1</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>8</FeatureCount>
<ExtentXMin>18.69765</ExtentXMin>
<ExtentXMax>18.69835</ExtentXMax>
<ExtentYMin>45.79745</ExtentYMin>
<ExtentYMax>45.79815</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>id</Name>
<ElementPath>id</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>line_id</Name>
<ElementPath>line_id</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>point_id</Name>
<ElementPath>point_id</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>18.6976479442</gml:X><gml:Y>45.7974514376</gml:Y></gml:coord>
<gml:coord><gml:X>18.6983479442</gml:X><gml:Y>45.7981514376</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6976479442,45.7981514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>0</ogr:id>
<ogr:line_id>0</ogr:line_id>
<ogr:point_id>0</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6977479442,45.7981514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>1</ogr:id>
<ogr:line_id>0</ogr:line_id>
<ogr:point_id>1</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6978479442,45.7981514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>2</ogr:id>
<ogr:line_id>0</ogr:line_id>
<ogr:point_id>2</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6979479442,45.7981514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>3</ogr:id>
<ogr:line_id>0</ogr:line_id>
<ogr:point_id>3</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.4">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6983479442,45.7977514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>4</ogr:id>
<ogr:line_id>1</ogr:line_id>
<ogr:point_id>0</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6983479442,45.7976514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>5</ogr:id>
<ogr:line_id>1</ogr:line_id>
<ogr:point_id>1</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6983479442,45.7975514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>6</ogr:id>
<ogr:line_id>1</ogr:line_id>
<ogr:point_id>2</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:pixel_centroids_lines fid="pixel_centroids_lines.7">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>18.6983479442,45.7974514376</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>7</ogr:id>
<ogr:line_id>1</ogr:line_id>
<ogr:point_id>3</ogr:point_id>
</ogr:pixel_centroids_lines>
</gml:featureMember>
</ogr:FeatureCollection>

0 comments on commit 47fe2a1

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