本文整理汇总了Python中qgis.core.QgsPointXY.distanceSquared方法的典型用法代码示例。如果您正苦于以下问题:Python QgsPointXY.distanceSquared方法的具体用法?Python QgsPointXY.distanceSquared怎么用?Python QgsPointXY.distanceSquared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsPointXY
的用法示例。
在下文中一共展示了QgsPointXY.distanceSquared方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: balanced
# 需要导入模块: from qgis.core import QgsPointXY [as 别名]
# 或者: from qgis.core.QgsPointXY import distanceSquared [as 别名]
def balanced(features, graph, feedback, balance=0, min_colors=4):
feature_colors = {}
# start with minimum number of colors in pool
color_pool = set(range(1, min_colors + 1))
# calculate count of neighbours
neighbour_count = defaultdict(int)
for feature_id, neighbours in graph.node_edge.items():
neighbour_count[feature_id] += len(neighbours)
# sort features by neighbour count - we want to handle those with more neighbours first
sorted_by_count = [feature_id for feature_id in sorted(neighbour_count.items(),
key=operator.itemgetter(1),
reverse=True)]
# counts for each color already assigned
color_counts = defaultdict(int)
color_areas = defaultdict(float)
for c in color_pool:
color_counts[c] = 0
color_areas[c] = 0
total = 10.0 / len(sorted_by_count)
i = 0
for (feature_id, n) in sorted_by_count:
if feedback.isCanceled():
break
# first work out which already assigned colors are adjacent to this feature
adjacent_colors = set()
for neighbour in graph.node_edge[feature_id]:
if neighbour in feature_colors:
adjacent_colors.add(feature_colors[neighbour])
# from the existing colors, work out which are available (ie non-adjacent)
available_colors = color_pool.difference(adjacent_colors)
feature_color = -1
if len(available_colors) == 0:
# no existing colors available for this feature, so add new color to pool and repeat
min_colors += 1
return ColoringAlgorithm.balanced(features, graph, feedback, balance, min_colors)
else:
if balance == 0:
# choose least used available color
counts = [(c, v) for c, v in color_counts.items() if c in available_colors]
feature_color = sorted(counts, key=operator.itemgetter(1))[0][0]
color_counts[feature_color] += 1
elif balance == 1:
areas = [(c, v) for c, v in color_areas.items() if c in available_colors]
feature_color = sorted(areas, key=operator.itemgetter(1))[0][0]
color_areas[feature_color] += features[feature_id].geometry().area()
elif balance == 2:
min_distances = {c: sys.float_info.max for c in available_colors}
this_feature_centroid = QgsPointXY(features[feature_id].geometry().centroid().geometry())
# find features for all available colors
other_features = {f_id: c for (f_id, c) in feature_colors.items() if c in available_colors}
# loop through these, and calculate the minimum distance from this feature to the nearest
# feature with each assigned color
for other_feature_id, c in other_features.items():
if feedback.isCanceled():
break
other_geometry = features[other_feature_id].geometry()
other_centroid = QgsPointXY(other_geometry.centroid().geometry())
distance = this_feature_centroid.distanceSquared(other_centroid)
if distance < min_distances[c]:
min_distances[c] = distance
# choose color such that minimum distance is maximised! ie we want MAXIMAL separation between
# features with the same color
feature_color = sorted(min_distances, key=min_distances.__getitem__, reverse=True)[0]
feature_colors[feature_id] = feature_color
i += 1
feedback.setProgress(70 + int(i * total))
return feature_colors