Skip to content

Commit 5789eec

Browse files
author
wonder
committedApr 18, 2007
- more sane function prototypes for searching for closest vertex/segment
- fixed some problems in python bindings git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6896 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

10 files changed

+130
-139
lines changed

10 files changed

+130
-139
lines changed
 

‎python/core/qgsgeometry.sip

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class QgsGeometry
206206
*/
207207
// SIP: buffer will be transferred from python to C++
208208
// TODO: create pythonic interface that will receive wkb as a string
209-
void setWkbAndOwnership(unsigned char * wkb /Transfer/, size_t length);
209+
void setWkbAndOwnership(unsigned char * wkb /Transfer, Array/, size_t length /ArraySize/);
210210

211211
/**
212212
Returns the buffer containing this geometry in WKB format.
@@ -298,36 +298,34 @@ class QgsGeometry
298298

299299
*/
300300
double sqrDistToVertexAt(QgsPoint& point,
301-
QgsGeometryVertexIndex& atVertex);
301+
QgsGeometryVertexIndex& atVertex /Out/);
302302

303303
/**
304-
* Modifies x and y to indicate the location of
305-
* the vertex at the given position number,
306-
* ring and item (first number is index 0)
307-
* to the given coordinates
308-
*
309-
* Returns TRUE if atVertex is a valid position on
310-
* the geometry, otherwise FALSE.
311-
*
312-
* If FALSE, x and y are not modified.
304+
* Returns coordinates of a vertex.
305+
* @param atVertex index of the vertex
306+
* @return Coordinates of the vertex or QgsPoint(0,0) on error
313307
*/
314-
bool vertexAt(double &x, double &y, QgsGeometryVertexIndex atVertex);
308+
QgsPoint vertexAt(const QgsGeometryVertexIndex& atVertex);
315309

316310
/**
317-
Returns, in atVertex, the closest vertex in this geometry to the given point.
318-
The squared cartesian distance is also returned in sqrDist.
311+
* Searches for the the closest vertex in this geometry to the given point.
312+
* @param point Specifiest the point for search
313+
* @param atVertex Receives index of the closest vertex
314+
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
319315
*/
320-
QgsPoint closestVertexWithContext(QgsPoint& point,
321-
QgsGeometryVertexIndex& atVertex,
322-
double& sqrDist);
316+
double closestVertexWithContext(const QgsPoint& point,
317+
QgsGeometryVertexIndex& atVertex /Out/);
323318

324319
/**
325-
Returns, in beforeVertex, the closest segment in this geometry to the given point.
326-
The squared cartesian distance is also returned in sqrDist.
320+
* Searches for the closest segment of geometry to the given point
321+
* @param point Specifies the point for search
322+
* @param minDistPoint Receives the nearest point on the segment
323+
* @param beforeVertex Receives index of the vertex before the closest segment
324+
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
327325
*/
328-
QgsPoint closestSegmentWithContext(QgsPoint& point,
329-
QgsGeometryVertexIndex& beforeVertex,
330-
double& sqrDist);
326+
double closestSegmentWithContext(const QgsPoint& point,
327+
QgsPoint& minDistPoint /Out/,
328+
QgsGeometryVertexIndex& beforeVertex /Out/);
331329

332330
/**Returns the bounding box of this feature*/
333331
QgsRect boundingBox();

‎python/core/qgspoint.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public:
6262
/**Returns the squared distance between this point and x,y*/
6363
double sqrDist(double x, double y) const;
6464

65+
/**Returns the squared distance between this and other point*/
66+
double sqrDist(const QgsPoint& other);
67+
6568
//! equality operator
6669
bool operator==(const QgsPoint &other);
6770

‎python/core/qgsvectorlayer.sip

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public:
185185
If there is no point within this tolerance, point is left unchanged.
186186
@param tolerance The snapping tolerance
187187
@return true if the position of point has been changed, and false otherwise */
188-
bool snapPoint(QgsPoint& point, double tolerance);
188+
bool snapPoint(QgsPoint& point /In, Out/, double tolerance);
189189

190190
/**Snaps a point to the closest vertex if there is one within the snapping tolerance
191191
@param atVertex Set to a vertex index of the snapped-to vertex
@@ -199,11 +199,11 @@ public:
199199
TODO: Handle returning multiple verticies if they are coincident
200200
*/
201201
bool snapVertexWithContext(QgsPoint& point,
202-
QgsGeometryVertexIndex& atVertex,
203-
int& beforeVertexIndex,
204-
int& afterVertexIndex,
205-
int& snappedFeatureId,
206-
QgsGeometry& snappedGeometry,
202+
QgsGeometryVertexIndex& atVertex /Out/,
203+
int& beforeVertexIndex /Out/,
204+
int& afterVertexIndex /Out/,
205+
int& snappedFeatureId /Out/,
206+
QgsGeometry& snappedGeometry /Out/,
207207
double tolerance);
208208

209209
/**Snaps a point to the closest line segment if there is one within the snapping tolerance
@@ -215,10 +215,10 @@ public:
215215

216216
TODO: Handle returning multiple lineFeatures if they are coincident
217217
*/
218-
bool snapSegmentWithContext(QgsPoint& point,
219-
QgsGeometryVertexIndex& beforeVertex,
220-
int& snappedFeatureId,
221-
QgsGeometry& snappedGeometry,
218+
bool snapSegmentWithContext(QgsPoint& point /In, Out/,
219+
QgsGeometryVertexIndex& beforeVertex /Out/,
220+
int& snappedFeatureId /Out/,
221+
QgsGeometry& snappedGeometry /Out/,
222222
double tolerance);
223223

224224
/**

‎src/app/qgsmaptoolvertexedit.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
9999
{
100100
QgsPoint point = toMapCoords(e->pos());
101101

102-
double x1, y1;
103-
double x2, y2;
104102
QgsGeometryVertexIndex index, rb1Index, rb2Index; //rb1Index/rb2Index is for rubberbanding
105103

106104
if (mTool == AddVertex)
@@ -122,20 +120,20 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
122120

123121
index = mSnappedBeforeVertex;
124122
// Get the endpoint of the snapped-to segment
125-
mSnappedAtGeometry.vertexAt(x2, y2, index);
123+
QgsPoint pnt2 = mSnappedAtGeometry.vertexAt(index);
126124

127125
// Get the startpoint of the snapped-to segment
128126
index.decrement_back();
129-
mStartPointValid = mSnappedAtGeometry.vertexAt(x1, y1, index);
127+
QgsPoint pnt1 = mSnappedAtGeometry.vertexAt(index);
130128

131129
createRubberBand();
132130

133-
if (mStartPointValid)
131+
if (pnt1 != QgsPoint(0,0))
134132
{
135-
mRubberBand->addPoint(QgsPoint(x1,y1));
133+
mRubberBand->addPoint(pnt1);
136134
}
137135
mRubberBand->addPoint(toMapCoords(e->pos()));
138-
mRubberBand->addPoint(QgsPoint(x2,y2));
136+
mRubberBand->addPoint(pnt2);
139137
}
140138
else if (mTool == MoveVertex)
141139
{
@@ -190,8 +188,8 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
190188
if(mRubberBandIndex1 != -1)
191189
{
192190
rb1Index.push_back(mRubberBandIndex1);
193-
mSnappedAtGeometry.vertexAt(x1, y1, rb1Index);
194-
mRubberBand->addPoint(QgsPoint(x1,y1));
191+
QgsPoint pnt1 = mSnappedAtGeometry.vertexAt(rb1Index);
192+
mRubberBand->addPoint(pnt1);
195193
mStartPointValid = true;
196194
}
197195
else
@@ -205,8 +203,8 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
205203
if(mRubberBandIndex2 != -1)
206204
{
207205
rb2Index.push_back(mRubberBandIndex2);
208-
mSnappedAtGeometry.vertexAt(x2, y2, rb2Index);
209-
mRubberBand->addPoint(QgsPoint(x2,y2));
206+
QgsPoint pnt2 = mSnappedAtGeometry.vertexAt(rb2Index);
207+
mRubberBand->addPoint(pnt2);
210208
}
211209
#ifdef QGISDEBUG
212210
qWarning("Creating rubber band for moveVertex");
@@ -230,11 +228,11 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
230228
}
231229

232230
// Get the point of the snapped-to vertex
233-
mSnappedAtGeometry.vertexAt(x1, y1, mSnappedAtVertex);
231+
QgsPoint pnt = mSnappedAtGeometry.vertexAt(mSnappedAtVertex);
234232

235233
mCross = new QgsVertexMarker(mCanvas);
236234
mCross->setIconType(QgsVertexMarker::ICON_X);
237-
mCross->setCenter(QgsPoint(x1,y1));
235+
mCross->setCenter(pnt);
238236
}
239237

240238
}

‎src/core/qgsgeometry.cpp

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,10 +1727,11 @@ bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex
17271727
}
17281728
}
17291729

1730-
bool QgsGeometry::vertexAt(double &x, double &y,
1731-
QgsGeometryVertexIndex atVertex)
1730+
QgsPoint QgsGeometry::vertexAt(const QgsGeometryVertexIndex& atVertex)
17321731
{
1733-
if(mGeos)//try to find the vertex from the Geos geometry (it present)
1732+
double x,y;
1733+
1734+
if (mGeos) //try to find the vertex from the Geos geometry (it present)
17341735
{
17351736
GEOS_GEOM::CoordinateSequence* cs = mGeos->getCoordinates();
17361737
if(cs)
@@ -1739,7 +1740,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
17391740
x = coord.x;
17401741
y = coord.y;
17411742
delete cs;
1742-
return true;
1743+
return QgsPoint(x,y);
17431744
}
17441745
}
17451746
else if(mGeometry)
@@ -1760,11 +1761,11 @@ bool QgsGeometry::vertexAt(double &x, double &y,
17601761
memcpy(&x, ptr, sizeof(double));
17611762
ptr += sizeof(double);
17621763
memcpy(&y, ptr, sizeof(double));
1763-
return true;
1764+
return QgsPoint(x,y);
17641765
}
17651766
else
17661767
{
1767-
return FALSE;
1768+
return QgsPoint(0,0);
17681769
}
17691770
}
17701771
case QGis::WKBLineString25D:
@@ -1779,7 +1780,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
17791780
// return error if underflow
17801781
if (0 > atVertex.back() || *nPoints <= atVertex.back())
17811782
{
1782-
return FALSE;
1783+
return QgsPoint(0,0);
17831784
}
17841785

17851786
// copy the vertex coordinates
@@ -1794,7 +1795,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
17941795
memcpy(&x, ptr, sizeof(double));
17951796
ptr += sizeof(double);
17961797
memcpy(&y, ptr, sizeof(double));
1797-
return true;
1798+
return QgsPoint(x,y);
17981799
}
17991800
case QGis::WKBPolygon25D:
18001801
hasZValue = true;
@@ -1817,7 +1818,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18171818
memcpy(&x, ptr, sizeof(double));
18181819
ptr += sizeof(double);
18191820
memcpy(&y, ptr, sizeof(double));
1820-
return true;
1821+
return QgsPoint(x,y);
18211822
}
18221823
ptr += 2*sizeof(double);
18231824
if(hasZValue)
@@ -1827,7 +1828,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18271828
++pointindex;
18281829
}
18291830
}
1830-
return false;
1831+
return QgsPoint(0,0);
18311832
}
18321833
case QGis::WKBMultiPoint25D:
18331834
hasZValue = true;
@@ -1837,7 +1838,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18371838
int* nPoints = (int*)ptr;
18381839
if(atVertex.back() < 0 || atVertex.back() >= *nPoints)
18391840
{
1840-
return false;
1841+
return QgsPoint(0,0);
18411842
}
18421843
if(hasZValue)
18431844
{
@@ -1851,7 +1852,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18511852
memcpy(&x, ptr, sizeof(double));
18521853
ptr += sizeof(double);
18531854
memcpy(&y, ptr, sizeof(double));
1854-
return true;
1855+
return QgsPoint(x,y);
18551856
}
18561857
case QGis::WKBMultiLineString25D:
18571858
hasZValue = true;
@@ -1874,7 +1875,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18741875
memcpy(&x, ptr, sizeof(double));
18751876
ptr += sizeof(double);
18761877
memcpy(&y, ptr, sizeof(double));
1877-
return true;
1878+
return QgsPoint(x,y);
18781879
}
18791880
ptr += 2*sizeof(double);
18801881
if(hasZValue)
@@ -1884,7 +1885,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
18841885
++pointindex;
18851886
}
18861887
}
1887-
return false;
1888+
return QgsPoint(0,0);
18881889
}
18891890
case QGis::WKBMultiPolygon25D:
18901891
hasZValue = true;
@@ -1912,7 +1913,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
19121913
memcpy(&x, ptr, sizeof(double));
19131914
ptr += sizeof(double);
19141915
memcpy(&y, ptr, sizeof(double));
1915-
return true;
1916+
return QgsPoint(x,y);
19161917
}
19171918
++pointindex;
19181919
ptr += 2*sizeof(double);
@@ -1923,60 +1924,46 @@ bool QgsGeometry::vertexAt(double &x, double &y,
19231924
}
19241925
}
19251926
}
1926-
return false;
1927+
return QgsPoint(0,0);
19271928
}
1928-
default:
1929-
#ifdef QGISDEBUG
1930-
qWarning("error: mGeometry type not recognized in QgsGeometry::vertexAt");
1931-
#endif
1932-
return false;
1933-
break;
1929+
default:
1930+
QgsDebugMsg("error: mGeometry type not recognized");
1931+
return QgsPoint(0,0);
19341932
}
19351933
}
1936-
else
1937-
{
1938-
#ifdef QGISDEBUG
1939-
qWarning("error: no mGeometry pointer in QgsGeometry::vertexAt");
1940-
#endif
1934+
else
1935+
{
1936+
QgsDebugMsg("error: no mGeometry pointer");
19411937
}
19421938

1943-
return false;
1939+
return QgsPoint(0,0);
19441940
}
19451941

19461942

19471943
double QgsGeometry::sqrDistToVertexAt(QgsPoint& point,
19481944
QgsGeometryVertexIndex& atVertex)
19491945
{
1950-
double x;
1951-
double y;
1952-
1953-
if (vertexAt(x, y, atVertex))
1946+
QgsPoint pnt = vertexAt(atVertex);
1947+
if (pnt != QgsPoint(0,0))
19541948
{
1955-
#ifdef QGISDEBUG
1956-
std::cout << "QgsGeometry::sqrDistToVertexAt: Exiting with distance to " << x << " " << y << "." << std::endl;
1957-
#endif
1958-
return point.sqrDist(x, y);
1949+
QgsDebugMsg("Exiting with distance to " + pnt.stringRep());
1950+
return point.sqrDist(pnt);
19591951
}
19601952
else
19611953
{
1962-
#ifdef QGISDEBUG
1963-
std::cout << "QgsGeometry::sqrDistToVertexAt: Exiting with std::numeric_limits<double>::max()." << std::endl;
1964-
#endif
1954+
QgsDebugMsg("Exiting with std::numeric_limits<double>::max().");
19651955
// probably safest to bail out with a very large number
19661956
return std::numeric_limits<double>::max();
19671957
}
19681958
}
19691959

19701960

1971-
QgsPoint QgsGeometry::closestVertexWithContext(QgsPoint& point,
1972-
QgsGeometryVertexIndex& atVertex,
1973-
double& sqrDist)
1961+
double QgsGeometry::closestVertexWithContext(const QgsPoint& point,
1962+
QgsGeometryVertexIndex& atVertex)
19741963
{
1975-
QgsPoint minDistPoint;
1976-
19771964
// Initialise some stuff
19781965
atVertex.clear();
1979-
sqrDist = std::numeric_limits<double>::max();
1966+
double sqrDist = std::numeric_limits<double>::max();
19801967
int closestVertexIndex = 0;
19811968

19821969
// set up the GEOS geometry
@@ -1985,7 +1972,7 @@ QgsPoint QgsGeometry::closestVertexWithContext(QgsPoint& point,
19851972
if (!mGeos)
19861973
{
19871974
QgsDebugMsg("GEOS geometry not available!");
1988-
return QgsPoint(0,0);
1975+
return -1;
19891976
}
19901977

19911978
GEOS_GEOM::CoordinateSequence* sequence = mGeos->getCoordinates();
@@ -2003,15 +1990,15 @@ QgsPoint QgsGeometry::closestVertexWithContext(QgsPoint& point,
20031990
}
20041991
atVertex.push_back(closestVertexIndex);
20051992

2006-
return minDistPoint;
1993+
return sqrDist;
20071994
}
20081995

20091996

2010-
QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
2011-
QgsGeometryVertexIndex& beforeVertex,
2012-
double& sqrDist)
1997+
double QgsGeometry::closestSegmentWithContext(const QgsPoint& point,
1998+
QgsPoint& minDistPoint,
1999+
QgsGeometryVertexIndex& beforeVertex)
20132000
{
2014-
QgsPoint minDistPoint;
2001+
QgsPoint distPoint;
20152002

20162003
QGis::WKBTYPE wkbType;
20172004
bool hasZValue = false;
@@ -2022,7 +2009,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
20222009

20232010
// Initialise some stuff
20242011
beforeVertex.clear();
2025-
sqrDist = std::numeric_limits<double>::max();
2012+
double sqrDist = std::numeric_limits<double>::max();
20262013

20272014
// TODO: implement with GEOS
20282015
if(mDirtyWkb) //convert latest geos to mGeometry
@@ -2033,7 +2020,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
20332020
if (!mGeometry)
20342021
{
20352022
QgsDebugMsg("WKB geometry not available!");
2036-
return QgsPoint(0,0);
2023+
return -1;
20372024
}
20382025

20392026
memcpy(&wkbType, (mGeometry+1), sizeof(int));
@@ -2046,7 +2033,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
20462033
case QGis::WKBMultiPoint:
20472034
{
20482035
// Points have no lines
2049-
return QgsPoint(0,0);
2036+
return -1;
20502037
}
20512038
case QGis::WKBLineString25D:
20522039
hasZValue = true;
@@ -2068,10 +2055,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
20682055

20692056
if (index > 0)
20702057
{
2071-
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
2058+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, distPoint)) < sqrDist )
20722059
{
20732060
closestSegmentIndex = index;
20742061
sqrDist = testdist;
2062+
minDistPoint = distPoint;
20752063
}
20762064
}
20772065
ptr += sizeof(double);
@@ -2111,10 +2099,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
21112099
}
21122100
if(prevx && prevy)
21132101
{
2114-
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
2102+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, distPoint)) < sqrDist )
21152103
{
21162104
closestSegmentIndex = pointindex;
21172105
sqrDist = testdist;
2106+
minDistPoint = distPoint;
21182107
}
21192108
}
21202109
prevx = thisx;
@@ -2152,10 +2141,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
21522141
}
21532142
if(prevx && prevy)
21542143
{
2155-
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
2144+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, distPoint)) < sqrDist )
21562145
{
21572146
closestSegmentIndex = index;
21582147
sqrDist = testdist;
2148+
minDistPoint = distPoint;
21592149
}
21602150
}
21612151
prevx = thisx;
@@ -2199,10 +2189,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
21992189
}
22002190
if(prevx && prevy)
22012191
{
2202-
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
2192+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, distPoint)) < sqrDist )
22032193
{
22042194
closestSegmentIndex = pointindex;
22052195
sqrDist = testdist;
2196+
minDistPoint = distPoint;
22062197
}
22072198
}
22082199
prevx = thisx;
@@ -2215,20 +2206,16 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
22152206
break;
22162207
}
22172208
case QGis::WKBUnknown:
2209+
default:
2210+
return -1;
22182211
break;
22192212
} // switch (wkbType)
22202213

22212214

2222-
#ifdef QGISDEBUG
2223-
std::cout << "QgsGeometry::closestSegment: Exiting with beforeVertex "
2224-
// << beforeVertex << ", sqrDist from "
2225-
<< point.stringRep().toLocal8Bit().data() << " is "
2226-
<< sqrDist
2227-
<< "." << std::endl;
2228-
#endif
2229-
2230-
return minDistPoint; // TODO: Is this meaningful?
2231-
2215+
QgsDebugMsg("Exiting with nearest point " + point.stringRep() +
2216+
", dist: " + QString::number(sqrDist) + ".");
2217+
2218+
return sqrDist;
22322219
}
22332220

22342221

@@ -3353,7 +3340,7 @@ bool QgsGeometry::exportGeosToWkb()
33533340

33543341

33553342

3356-
double QgsGeometry::distanceSquaredPointToSegment(QgsPoint& point,
3343+
double QgsGeometry::distanceSquaredPointToSegment(const QgsPoint& point,
33573344
double *x1, double *y1,
33583345
double *x2, double *y2,
33593346
QgsPoint& minDistPoint)

‎src/core/qgsgeometry.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,11 @@ class CORE_EXPORT QgsGeometry {
199199
bool deleteVertexAt(QgsGeometryVertexIndex atVertex);
200200

201201
/**
202-
* Modifies x and y to indicate the location of
203-
* the vertex at the given position number,
204-
* ring and item (first number is index 0)
205-
* to the given coordinates
206-
*
207-
* Returns TRUE if atVertex is a valid position on
208-
* the geometry, otherwise FALSE.
209-
*
210-
* If FALSE, x and y are not modified.
202+
* Returns coordinates of a vertex.
203+
* @param atVertex index of the vertex
204+
* @return Coordinates of the vertex or QgsPoint(0,0) on error
211205
*/
212-
bool vertexAt(double &x, double &y, QgsGeometryVertexIndex atVertex);
206+
QgsPoint vertexAt(const QgsGeometryVertexIndex& atVertex);
213207

214208
/**
215209
Returns the squared cartesian distance between the given point
@@ -221,20 +215,24 @@ class CORE_EXPORT QgsGeometry {
221215
QgsGeometryVertexIndex& atVertex);
222216

223217
/**
224-
Returns, in atVertex, the closest vertex in this geometry to the given point.
225-
The squared cartesian distance is also returned in sqrDist.
218+
* Searches for the the closest vertex in this geometry to the given point.
219+
* @param point Specifiest the point for search
220+
* @param atVertex Receives index of the closest vertex
221+
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
226222
*/
227-
QgsPoint closestVertexWithContext(QgsPoint& point,
228-
QgsGeometryVertexIndex& atVertex,
229-
double& sqrDist);
223+
double closestVertexWithContext(const QgsPoint& point,
224+
QgsGeometryVertexIndex& atVertex);
230225

231226
/**
232-
Returns, in beforeVertex, the closest segment in this geometry to the given point.
233-
The squared cartesian distance is also returned in sqrDist.
227+
* Searches for the closest segment of geometry to the given point
228+
* @param point Specifies the point for search
229+
* @param minDistPoint Receives the nearest point on the segment
230+
* @param beforeVertex Receives index of the vertex before the closest segment
231+
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
234232
*/
235-
QgsPoint closestSegmentWithContext(QgsPoint& point,
236-
QgsGeometryVertexIndex& beforeVertex,
237-
double& sqrDist);
233+
double closestSegmentWithContext(const QgsPoint& point,
234+
QgsPoint& minDistPoint,
235+
QgsGeometryVertexIndex& beforeVertex);
238236

239237
/**Returns the bounding box of this feature*/
240238
QgsRect boundingBox();
@@ -317,7 +315,7 @@ class CORE_EXPORT QgsGeometry {
317315
/** Squared distance from point to the given line segment
318316
* TODO: Perhaps move this to QgsPoint
319317
*/
320-
double distanceSquaredPointToSegment(QgsPoint& point,
318+
double distanceSquaredPointToSegment(const QgsPoint& point,
321319
double *x1, double *y1,
322320
double *x2, double *y2,
323321
QgsPoint& minDistPoint);

‎src/core/qgspoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ double QgsPoint::sqrDist(double x, double y) const
5555
return (m_x-x)*(m_x-x)+(m_y-y)*(m_y-y);
5656
}
5757

58+
double QgsPoint::sqrDist(const QgsPoint& other)
59+
{
60+
return sqrDist(other.x(), other.y());
61+
}
62+
5863
// operators
5964
bool QgsPoint::operator==(const QgsPoint & other)
6065
{

‎src/core/qgspoint.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class CORE_EXPORT QgsPoint
9797

9898
/**Returns the squared distance between this point and x,y*/
9999
double sqrDist(double x, double y) const;
100+
101+
/**Returns the squared distance between this and other point*/
102+
double sqrDist(const QgsPoint& other);
100103

101104
//! equality operator
102105
bool operator==(const QgsPoint &other);

‎src/core/qgsvectorlayer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ bool QgsVectorLayer::snapSegmentWithContext(QgsPoint& point, QgsGeometryVertexIn
24172417
// feature->setGeometry( mChangedGeometries[ feature->featureId() ] );
24182418
}
24192419

2420-
minDistSegPoint = feature.geometry()->closestSegmentWithContext(origPoint, beforeVertexTemp, testSqrDist);
2420+
testSqrDist = feature.geometry()->closestSegmentWithContext(origPoint, minDistSegPoint, beforeVertexTemp);
24212421

24222422
if (
24232423
(testSqrDist < minSqrDist) ||
@@ -2453,7 +2453,7 @@ bool QgsVectorLayer::snapSegmentWithContext(QgsPoint& point, QgsGeometryVertexIn
24532453
}
24542454
else
24552455
{
2456-
minDistSegPoint = (*iter).geometry()->closestSegmentWithContext(origPoint, beforeVertexTemp, testSqrDist);
2456+
testSqrDist = (*iter).geometry()->closestSegmentWithContext(origPoint, minDistSegPoint, beforeVertexTemp);
24572457
}
24582458

24592459
if (
@@ -2478,7 +2478,7 @@ bool QgsVectorLayer::snapSegmentWithContext(QgsPoint& point, QgsGeometryVertexIn
24782478
//and also go through the changed geometries, because the spatial filter of the provider did not consider feature changes
24792479
for(QgsGeometryMap::iterator it = mChangedGeometries.begin(); it != mChangedGeometries.end(); ++it)
24802480
{
2481-
minDistSegPoint = it.value().closestSegmentWithContext(origPoint, beforeVertexTemp, testSqrDist);
2481+
testSqrDist = it.value().closestSegmentWithContext(origPoint, minDistSegPoint, beforeVertexTemp);
24822482
if (
24832483
(testSqrDist < minSqrDist) ||
24842484
(

‎src/gui/qgsvertexmarker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void QgsVertexMarker::setCenter(const QgsPoint& point)
4141
mCenter = point;
4242
QPointF pt = toCanvasCoords(mCenter);
4343
setPos(pt);
44-
std::cout << "at " << (int) pt.x() << " " << (int) pt.y() << std::endl;
4544
}
4645

4746

0 commit comments

Comments
 (0)
Please sign in to comment.