本文整理汇总了Python中qgis.core.QgsPoint.sqrDist方法的典型用法代码示例。如果您正苦于以下问题:Python QgsPoint.sqrDist方法的具体用法?Python QgsPoint.sqrDist怎么用?Python QgsPoint.sqrDist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsPoint
的用法示例。
在下文中一共展示了QgsPoint.sqrDist方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: snapGeometry
# 需要导入模块: from qgis.core import QgsPoint [as 别名]
# 或者: from qgis.core.QgsPoint import sqrDist [as 别名]
def snapGeometry(self, geometry, snapTolerance, mode=PreferNodes):
"""
Snaps a QgsGeometry in the reference layer
:param geometry: QgsGeometry
:param snapTolerance: float
:param mode: DsgGeometrySnapper.PreferNodes or DsgGeometrySnapper.PreferClosest
:return:
"""
center = QgsPointV2(geometry.boundingBox().center())
# Get potential reference features and construct snap index
refGeometries = []
searchBounds = geometry.boundingBox()
searchBounds.grow(snapTolerance)
# filter by bounding box to get candidates
refFeatureIds = self.index.intersects(searchBounds)
# End here in case we don't find candidates
if len(refFeatureIds) == 0:
return geometry
# speeding up the process to consider only intersecting geometries
refFeatureRequest = QgsFeatureRequest().setFilterFids(refFeatureIds)
for refFeature in self.referenceLayer.getFeatures(refFeatureRequest):
refGeometry = refFeature.geometry()
segments = self.breakQgsGeometryIntoSegments(refGeometry)
# testing intersection
for segment in segments:
if segment.intersects(searchBounds):
refGeometries.append(segment)
# End here in case we don't find geometries
if len(refGeometries) == 0:
return geometry
# building geometry index
refDict, index = self.buildReferenceIndex(refGeometries)
refSnapIndex = DsgSnapIndex(center, 10*snapTolerance)
for geom in refGeometries:
refSnapIndex.addGeometry(geom.geometry())
# Snap geometries
subjGeom = geometry.geometry().clone()
subjPointFlags = []
# Pass 1: snap vertices of subject geometry to reference vertices
for iPart in xrange(subjGeom.partCount()):
subjPointFlags.append([])
for iRing in xrange(subjGeom.ringCount(iPart)):
subjPointFlags[iPart].append([])
for iVert in xrange(self.polyLineSize(subjGeom, iPart, iRing)):
vidx = QgsVertexId(iPart, iRing, iVert, QgsVertexId.SegmentVertex)
p = QgsPointV2(subjGeom.vertexAt(vidx))
pF = QgsPoint(p.toQPointF())
snapPoint, snapSegment = refSnapIndex.getSnapItem(p, snapTolerance)
success = snapPoint or snapSegment
if not success:
subjPointFlags[iPart][iRing].append(DsgGeometrySnapper.Unsnapped )
else:
if mode == DsgGeometrySnapper.PreferNodes:
# Prefer snapping to point
if snapPoint:
subjGeom.moveVertex(vidx, snapPoint.getSnapPoint(p))
subjPointFlags[iPart][iRing].append(DsgGeometrySnapper.SnappedToRefNode)
elif snapSegment:
subjGeom.moveVertex( vidx, snapSegment.getSnapPoint(p))
subjPointFlags[iPart][iRing].append(DsgGeometrySnapper.SnappedToRefSegment)
elif mode == DsgGeometrySnapper.PreferClosest:
nodeSnap = None
segmentSnap = None
distanceNode = sys.float_info.max
distanceSegment = sys.float_info.max
if snapPoint:
nodeSnap = snapPoint.getSnapPoint(p)
nodeSnapF = QgsPoint(nodeSnap.toQPointF())
distanceNode = nodeSnapF.sqrDist(pF)
if snapSegment:
segmentSnap = snapSegment.getSnapPoint(p)
segmentSnapF = QgsPoint(segmentSnap.toQPointF())
distanceSegment = segmentSnapF.sqrDist(pF)
if snapPoint and (distanceNode < distanceSegment):
subjGeom.moveVertex( vidx, nodeSnap )
subjPointFlags[iPart][iRing].append(DsgGeometrySnapper.SnappedToRefNode)
elif snapSegment:
subjGeom.moveVertex(vidx, segmentSnap)
subjPointFlags[iPart][iRing].append(DsgGeometrySnapper.SnappedToRefSegment)
#nothing more to do for points
if isinstance(subjGeom, QgsPointV2):
return QgsGeometry(subjGeom)
# SnapIndex for subject feature
subjSnapIndex = DsgSnapIndex(center, 10*snapTolerance)
subjSnapIndex.addGeometry(subjGeom)
origSubjGeom = subjGeom.clone()
origSubjSnapIndex = DsgSnapIndex(center, 10*snapTolerance)
origSubjSnapIndex.addGeometry(origSubjGeom)
# Pass 2: add missing vertices to subject geometry
#.........这里部分代码省略.........
示例2: run
# 需要导入模块: from qgis.core import QgsPoint [as 别名]
# 或者: from qgis.core.QgsPoint import sqrDist [as 别名]
#.........这里部分代码省略.........
## BEGIN DISTANCE CODE!
distance_dictionary = {}
DList_TH_stops = defaultdict(list)
### (temporarily) set ref to TAT in case
##Enter data into Dictionary
iter = vltWM.getFeatures()
#iter = TAToutput.getFeatures()
##THstopList = []
for feature in iter:
##identifies attributes for future use
#print "Feature ID %d: " % feature.id()
trailhead_id = feature[str('FEAT_ID')]
#print trailhead_id
geom = feature.geometry()
th_x = geom.asPoint().x()
th_y = geom.asPoint().y()
th_point = QgsPoint(th_x,th_y)
#transit_stop_id = feature['stop_id']
iter_stops = TAT_layer.getFeatures() ###[new layer here?]
for stop_feature in iter_stops:
stop_id = stop_feature[str('stop_id')]
th_id = stop_feature[str('FEAT_ID')]
if th_id == trailhead_id:
stop_geom = stop_feature.geometry()
stop_id = stop_feature['stop_id']
DList_TH_stops[th_id].append(stop_id)
stop_x = stop_geom.asPoint().x()
stop_y = stop_geom.asPoint().y()
s_point = QgsPoint(stop_x,stop_y)
stop_distance = 0
a = numpy.array(th_point)
b = numpy.array(s_point)
##stop_distance = numpy.sqrt(numpy.sum(a-b)**2)
stop_distance = math.sqrt(th_point.sqrDist(s_point))
#print th_id
#print stop_id
#print stop_distance
####print math.sqrt(th_point.sqrDist(s_point))
th_stop = str(trailhead_id) + ":" + str(stop_id)
distance_dictionary[th_stop] = stop_distance
#if stop_distance < BufferDistance:
# distance_dictionary[th_stop] = stop_distance
##outputGeoJSON
vl_outTH = QgsVectorLayer("Point?crs=EPSG:4326", "stop_points", "memory")
pr_outTH = vl_outTH.dataProvider()
#
# changes are only possible when editing the layer
vl_outTH.startEditing()
# add fields
##pr_outTH.addAttributes([QgsField(th_id_field, QVariant.Int),QgsField(th_name_field, QVariant.String),QgsField("number_stops_nearby", QVariant.Int),QgsField("stops_near", QVariant.String),QgsField("stops_near_dist", QVariant.String)])
pr_outTH.addAttributes([QgsField(th_id_field, QVariant.Int),QgsField(th_name_field, QVariant.String),QgsField("number_stops_nearby", QVariant.Int),QgsField("stops_near", QVariant.String),QgsField("stops_distances", QVariant.String)])
index = 0
iter = trailhead_layer.getFeatures()
for feature in iter:
geom = feature.geometry()
feature_id = feature[th_id_field]
feature_name = feature[th_name_field]
#stops_near = str(DList_TH_stops[feature_id])
num_stops_near = 0
stops_near_in = DList_TH_stops[feature_id]
stops_near = []
for t in stops_near_in:
#print t
if not t in stops_near:
stops_near.append(t)
stops_near.sort()