Skip to content

Commit

Permalink
QgsGeometry::vertexAt now returns QgsPoint, not QgsPointXY
Browse files Browse the repository at this point in the history
Since it's easy to convert from a QgsPoint to a QgsPointXY,
but impossible to recover the Z/M values lost by only
returning a QgsPointXY.
  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent 620d4e0 commit ecaee1a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ empty geometry collection)
- avoidIntersections() got an extra argument: list of layers to include in the operation (previously read from active QgsProject)
- isGeosEmpty() was removed. Use isEmpty() instead.
- reshapeGeometry() expects QgsLineString as a parameter instead of a list of 2D points (so that it can support 3D geometries)
- vertexAt() now returns a QgsPoint (previously QgsPointV2) instead of a QgsPointXY (previously QgsPoint)


QgsGeometryAnalyzer {#qgis_api_break_3_0_QgsGeometryAnalyzer}
Expand Down
6 changes: 3 additions & 3 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ Returns true if WKB of the geometry is of WKBMulti* type
:rtype: bool
%End

QgsPointXY vertexAt( int atVertex ) const;
QgsPoint vertexAt( int atVertex ) const;
%Docstring
Returns coordinates of a vertex.
\param atVertex index of the vertex
:return: Coordinates of the vertex or QgsPointXY(0,0) on error
:rtype: QgsPointXY
:return: Coordinates of the vertex or QgsPoint(0,0) on error
:rtype: QgsPoint
%End

double sqrDistToVertexAt( QgsPointXY &point /In/, int atVertex ) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def processAlgorithm(self, parameters, context, feedback):
output_feature.setAttributes(attrs)

point = input_geometry.vertexAt(node_index)
output_feature.setGeometry(QgsGeometry.fromPoint(point))
output_feature.setGeometry(QgsGeometry(point))

writer.addFeature(output_feature, QgsFeatureSink.FastInsert)

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/tools/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
QgsCredentials,
QgsFeatureRequest,
QgsSettings,
QgsProcessingContext,
QgsPointXY,
QgsProcessingUtils)

from processing.tools import dataobjects
Expand Down Expand Up @@ -251,7 +251,7 @@ def snapToPrecision(geom, precision):
snapped.moveVertex(x, y, i)
i = i + 1
p = snapped.vertexAt(i)
return snapped
return QgsPointXY(snapped.x(), snapped.y())


def ogrConnectionString(uri):
Expand Down
6 changes: 3 additions & 3 deletions src/app/nodetool/qgsnodetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ void QgsNodeTool::startDraggingEdge( const QgsPointLocator::Match &m, const QgsP
Q_FOREACH ( const Vertex &v, movingVertices )
{
mDraggingExtraVertices << v;
mDraggingExtraVerticesOffset << ( geom.vertexAt( v.vertexId ) - layerPoint );
mDraggingExtraVerticesOffset << ( geom.vertexAt( v.vertexId ) - QgsPoint( layerPoint ) );
}

mOverrideCadPoints.clear();
Expand Down Expand Up @@ -1639,10 +1639,10 @@ void QgsNodeTool::deleteVertex()
int vertexId = vertex.vertexId;

// if next vertex is not available, use the previous one
if ( geom.vertexAt( vertexId ) == QgsPointXY() )
if ( geom.vertexAt( vertexId ) == QgsPoint() )
vertexId -= 1;

if ( geom.vertexAt( vertexId ) != QgsPointXY() )
if ( geom.vertexAt( vertexId ) != QgsPoint() )
{
QList<Vertex> nodes_new;
nodes_new << Vertex( vertex.layer, vertex.fid, vertexId );
Expand Down
9 changes: 4 additions & 5 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,20 @@ bool QgsGeometry::insertVertex( const QgsPoint &point, int beforeVertex )
return d->geometry->insertVertex( id, point );
}

QgsPointXY QgsGeometry::vertexAt( int atVertex ) const
QgsPoint QgsGeometry::vertexAt( int atVertex ) const
{
if ( !d->geometry )
{
return QgsPointXY( 0, 0 );
return QgsPoint();
}

QgsVertexId vId;
( void )vertexIdFromVertexNr( atVertex, vId );
if ( vId.vertex < 0 )
{
return QgsPointXY( 0, 0 );
return QgsPoint();
}
QgsPoint pt = d->geometry->vertexAt( vId );
return QgsPointXY( pt.x(), pt.y() );
return d->geometry->vertexAt( vId );
}

double QgsGeometry::sqrDistToVertexAt( QgsPointXY &point, int atVertex ) const
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ class CORE_EXPORT QgsGeometry
/**
* Returns coordinates of a vertex.
* \param atVertex index of the vertex
* \returns Coordinates of the vertex or QgsPointXY(0,0) on error
* \returns Coordinates of the vertex or QgsPoint(0,0) on error
*/
QgsPointXY vertexAt( int atVertex ) const;
QgsPoint vertexAt( int atVertex ) const;

/**
* Returns the squared Cartesian distance between the given point
Expand Down
30 changes: 15 additions & 15 deletions tests/src/python/test_qgsgeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def testVertexAt(self):
polyline = QgsGeometry.fromPolyline(points)

for i in range(0, len(points)):
self.assertEqual(points[i], polyline.vertexAt(i), "Mismatch at %d" % i)
self.assertEqual(QgsPoint(points[i]), polyline.vertexAt(i), "Mismatch at %d" % i)

# 2-3 6-+-7
# | | | |
Expand All @@ -773,15 +773,15 @@ def testVertexAt(self):
polyline = QgsGeometry.fromMultiPolyline(points)

p = polyline.vertexAt(-100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

p = polyline.vertexAt(100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

i = 0
for j in range(0, len(points)):
for k in range(0, len(points[j])):
self.assertEqual(points[j][k], polyline.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
self.assertEqual(QgsPoint(points[j][k]), polyline.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
i += 1

# 5---4
Expand All @@ -795,15 +795,15 @@ def testVertexAt(self):
polygon = QgsGeometry.fromPolygon(points)

p = polygon.vertexAt(-100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

p = polygon.vertexAt(100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

i = 0
for j in range(0, len(points)):
for k in range(0, len(points[j])):
self.assertEqual(points[j][k], polygon.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
self.assertEqual(QgsPoint(points[j][k]), polygon.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
i += 1

# 3-+-+-2
Expand All @@ -820,15 +820,15 @@ def testVertexAt(self):
polygon = QgsGeometry.fromPolygon(points)

p = polygon.vertexAt(-100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

p = polygon.vertexAt(100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

i = 0
for j in range(0, len(points)):
for k in range(0, len(points[j])):
self.assertEqual(points[j][k], polygon.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
self.assertEqual(QgsPoint(points[j][k]), polygon.vertexAt(i), "Mismatch at %d / %d,%d" % (i, j, k))
i += 1

# 5-+-4 0-+-9
Expand All @@ -844,17 +844,17 @@ def testVertexAt(self):
polygon = QgsGeometry.fromMultiPolygon(points)

p = polygon.vertexAt(-100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

p = polygon.vertexAt(100)
self.assertEqual(p, QgsPointXY(0, 0), "Expected 0,0, Got %s" % p.toString())
self.assertEqual(p, QgsPoint(0, 0), "Expected 0,0, Got {}.{}".format(p.x(), p.y()))

i = 0
for j in range(0, len(points)):
for k in range(0, len(points[j])):
for l in range(0, len(points[j][k])):
p = polygon.vertexAt(i)
self.assertEqual(points[j][k][l], p, "Got %s, Expected %s at %d / %d,%d,%d" % (p.toString(), points[j][k][l].toString(), i, j, k, l))
self.assertEqual(QgsPoint(points[j][k][l]), p, "Got {},{} Expected {} at {} / {},{},{}".format(p.x(), p.y(), points[j][k][l].toString(), i, j, k, l))
i += 1

def testMultipoint(self):
Expand All @@ -870,7 +870,7 @@ def testMultipoint(self):
i += 1

multipoint = QgsGeometry.fromWkt("MultiPoint ((5 5))")
self.assertEqual(multipoint.vertexAt(0), QgsPointXY(5, 5), "MULTIPOINT fromWkt failed")
self.assertEqual(multipoint.vertexAt(0), QgsPoint(5, 5), "MULTIPOINT fromWkt failed")

assert multipoint.insertVertex(4, 4, 0), "MULTIPOINT insert 4,4 at 0 failed"
expwkt = "MultiPoint ((4 4),(5 5))"
Expand Down Expand Up @@ -906,7 +906,7 @@ def testMultipoint(self):
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

multipoint = QgsGeometry.fromWkt("MultiPoint ((5 5))")
self.assertEqual(multipoint.vertexAt(0), QgsPointXY(5, 5), "MultiPoint fromWkt failed")
self.assertEqual(multipoint.vertexAt(0), QgsPoint(5, 5), "MultiPoint fromWkt failed")

def testMoveVertex(self):
multipoint = QgsGeometry.fromWkt("MultiPoint ((5 0),(0 0),(0 4),(5 4),(5 1),(1 1),(1 3),(4 3),(4 2),(2 2))")
Expand Down

0 comments on commit ecaee1a

Please sign in to comment.