本文整理汇总了Python中qgis.core.QgsGeometry.fromPolygonXY方法的典型用法代码示例。如果您正苦于以下问题:Python QgsGeometry.fromPolygonXY方法的具体用法?Python QgsGeometry.fromPolygonXY怎么用?Python QgsGeometry.fromPolygonXY使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsGeometry
的用法示例。
在下文中一共展示了QgsGeometry.fromPolygonXY方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ovals
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def ovals(self, sink, source, width, height, rotation, segments, feedback):
features = source.getFeatures()
ft = QgsFeature()
xOffset = width / 2.0
yOffset = height / 2.0
total = 100.0 / source.featureCount() if source.featureCount() else 0
if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
if feedback.isCanceled():
break
if not feat.hasGeometry():
continue
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = []
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
polygon = [[QgsPointXY(i[0] * math.cos(phi) + i[1] * math.sin(phi) + x,
-i[0] * math.sin(phi) + i[1] * math.cos(phi) + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygonXY(polygon))
ft.setAttributes(feat.attributes())
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
else:
for current, feat in enumerate(features):
if feedback.isCanceled():
break
if not feat.hasGeometry():
continue
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = []
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
polygon = [[QgsPointXY(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygonXY(polygon))
ft.setAttributes(feat.attributes())
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
示例2: testQgsPolygonRepr
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def testQgsPolygonRepr(self):
p = QgsGeometry.fromPolygonXY(
[[QgsPointXY(0, 0),
QgsPointXY(2, 0),
QgsPointXY(2, 2),
QgsPointXY(0, 2),
QgsPointXY(0, 0)]])
self.assertEqual(p.constGet().__repr__(), '<QgsPolygon: Polygon ((0 0, 2 0, 2 2, 0 2, 0 0))>')
示例3: createLayerWithOnePolygon
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def createLayerWithOnePolygon():
layer = QgsVectorLayer("Polygon?crs=epsg:3111&field=pk:int", "vl", "memory")
assert layer.isValid()
f1 = QgsFeature(layer.dataProvider().fields(), 1)
f1.setAttribute("pk", 1)
f1.setGeometry(QgsGeometry.fromPolygonXY([[QgsPointXY(2484588, 2425722), QgsPointXY(2482767, 2398853), QgsPointXY(2520109, 2397715), QgsPointXY(2520792, 2425494), QgsPointXY(2484588, 2425722)]]))
assert layer.dataProvider().addFeatures([f1])
return layer
示例4: create_cell
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def create_cell(self, centroid, from_deg, to_deg, radius):
step = abs(to_deg - from_deg) / self.DEFAULT_SEGS
outer_ring = []
outer_ring.append(centroid)
# second outer
for index in range(self.DEFAULT_SEGS, -1, -1):
radian = math.radians(from_deg + (index * step))
outer_ring.append(self.create_point(centroid, radian, radius))
return QgsGeometry.fromPolygonXY([outer_ring])
示例5: _diamondGrid
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def _diamondGrid(self, sink, width, height, originX, originY,
hSpacing, vSpacing, hOverlay, vOverlay, feedback):
ft = QgsFeature()
halfHSpacing = hSpacing / 2
halfVSpacing = vSpacing / 2
halfHOverlay = hOverlay / 2
halfVOverlay = vOverlay / 2
columns = int(math.ceil(float(width) / (halfHSpacing - halfHOverlay)))
rows = int(math.ceil(float(height) / (vSpacing - halfVOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
for col in range(columns):
if feedback.isCanceled():
break
x = originX - (col * halfHOverlay)
x1 = x + ((col + 0) * halfHSpacing)
x2 = x + ((col + 1) * halfHSpacing)
x3 = x + ((col + 2) * halfHSpacing)
for row in range(rows):
y = originY + (row * halfVOverlay)
if (col % 2) == 0:
y1 = y - (((row * 2) + 0) * halfVSpacing)
y2 = y - (((row * 2) + 1) * halfVSpacing)
y3 = y - (((row * 2) + 2) * halfVSpacing)
else:
y1 = y - (((row * 2) + 1) * halfVSpacing)
y2 = y - (((row * 2) + 2) * halfVSpacing)
y3 = y - (((row * 2) + 3) * halfVSpacing)
polyline = []
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x3, y2))
polyline.append(QgsPointXY(x2, y3))
polyline.append(QgsPointXY(x1, y2))
ft.setGeometry(QgsGeometry.fromPolygonXY([polyline]))
ft.setAttributes([x1, y1, x3, y3, id])
sink.addFeature(ft, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))
示例6: rotation_test
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def rotation_test(self):
# We will create a polygon layer with a rotated rectangle.
# Then we will make it the object layer for the atlas,
# rotate the map and test that the bounding rectangle
# is smaller than the bounds without rotation.
polygonLayer = QgsVectorLayer('Polygon', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.fields())
points = [(10, 15), (15, 10), (45, 40), (40, 45)]
poly.setGeometry(QgsGeometry.fromPolygonXY([[QgsPointXY(x[0], x[1]) for x in points]]))
polygonLayer.dataProvider().addFeatures([poly])
QgsProject.instance().addMapLayer(polygonLayer)
# Recreating the layout locally
composition = QgsPrintLayout(QgsProject.instance())
composition.initializeDefaults()
# the atlas map
atlasMap = QgsLayoutItemMap(composition)
atlasMap.attemptSetSceneRect(QRectF(20, 20, 130, 130))
atlasMap.setFrameEnabled(True)
atlasMap.setLayers([polygonLayer])
atlasMap.setExtent(QgsRectangle(0, 0, 100, 50))
composition.addLayoutItem(atlasMap)
# the atlas
atlas = composition.atlas()
atlas.setCoverageLayer(polygonLayer)
atlas.setEnabled(True)
atlasMap.setAtlasDriven(True)
atlasMap.setAtlasScalingMode(QgsLayoutItemMap.Auto)
atlasMap.setAtlasMargin(0.0)
# Testing
atlasMap.setMapRotation(0.0)
atlas.beginRender()
atlas.first()
nonRotatedExtent = QgsRectangle(atlasMap.extent())
atlasMap.setMapRotation(45.0)
atlas.first()
rotatedExtent = QgsRectangle(atlasMap.extent())
self.assertLess(rotatedExtent.width(), nonRotatedExtent.width() * 0.9)
self.assertLess(rotatedExtent.height(), nonRotatedExtent.height() * 0.9)
QgsProject.instance().removeMapLayer(polygonLayer)
示例7: create_ring_cell
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def create_ring_cell(self, centroid, from_deg, to_deg, from_radius, to_radius):
step = abs(to_deg - from_deg) / self.DEFAULT_SEGS
radian = 0.0
outer_ring = []
# first interior
for index in range(self.DEFAULT_SEGS + 1 - self.GAPS):
radian = math.radians(from_deg + (index * step))
outer_ring.append(self.create_point(centroid, radian, from_radius))
# second outer
for index in range(self.DEFAULT_SEGS - self.GAPS, -1, -1):
radian = math.radians(from_deg + (index * step))
outer_ring.append(self.create_point(centroid, radian, to_radius))
return QgsGeometry.fromPolygonXY([outer_ring])
示例8: rotation_test
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def rotation_test(self):
# We will create a polygon layer with a rotated rectangle.
# Then we will make it the object layer for the atlas,
# rotate the map and test that the bounding rectangle
# is smaller than the bounds without rotation.
polygonLayer = QgsVectorLayer('Polygon', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.pendingFields())
points = [(10, 15), (15, 10), (45, 40), (40, 45)]
poly.setGeometry(QgsGeometry.fromPolygonXY([[QgsPointXY(x[0], x[1]) for x in points]]))
polygonLayer.dataProvider().addFeatures([poly])
QgsProject.instance().addMapLayer(polygonLayer)
# Recreating the composer locally
composition = QgsComposition(QgsProject.instance())
composition.setPaperSize(297, 210)
# the atlas map
atlasMap = QgsComposerMap(composition, 20, 20, 130, 130)
atlasMap.setFrameEnabled(True)
atlasMap.setLayers([polygonLayer])
atlasMap.setNewExtent(QgsRectangle(0, 0, 100, 50))
composition.addComposerMap(atlasMap)
# the atlas
atlas = composition.atlasComposition()
atlas.setCoverageLayer(polygonLayer)
atlas.setEnabled(True)
composition.setAtlasMode(QgsComposition.ExportAtlas)
atlasMap.setAtlasDriven(True)
atlasMap.setAtlasScalingMode(QgsComposerMap.Auto)
atlasMap.setAtlasMargin(0.0)
# Testing
atlasMap.setMapRotation(0.0)
atlas.firstFeature()
nonRotatedExtent = QgsRectangle(atlasMap.currentMapExtent())
atlasMap.setMapRotation(45.0)
atlas.firstFeature()
rotatedExtent = QgsRectangle(atlasMap.currentMapExtent())
assert rotatedExtent.width() < nonRotatedExtent.width() * 0.9
assert rotatedExtent.height() < nonRotatedExtent.height() * 0.9
QgsProject.instance().removeMapLayer(polygonLayer)
示例9: testMeasurePolygon
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def testMeasurePolygon(self):
# +-+-+
# | |
# + +-+
# | |
# +-+
polygon = QgsGeometry.fromPolygonXY(
[[
QgsPointXY(0, 0), QgsPointXY(1, 0), QgsPointXY(1, 1), QgsPointXY(2, 1), QgsPointXY(2, 2), QgsPointXY(0, 2), QgsPointXY(0, 0),
]]
)
da = QgsDistanceArea()
area = da.measureArea(polygon)
assert area == 3, 'Expected:\n%f\nGot:\n%f\n' % (3, area)
perimeter = da.measurePerimeter(polygon)
assert perimeter == 8, 'Expected:\n%f\nGot:\n%f\n' % (8, perimeter)
示例10: _swap_qgs_geometry
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def _swap_qgs_geometry(qgsgeom):
if qgsgeom.wkbType() == QgsWkbTypes.Point:
p = qgsgeom.asPoint()
qgsgeom = QgsGeometry.fromPointXY(QgsPointXY(p[1], p[0]))
elif qgsgeom.wkbType() == QgsWkbTypes.MultiPoint:
mp = qgsgeom.asMultiPoint()
qgsgeom = QgsGeometry.fromMultiPointXY([QgsPointXY(p[1], p[0]) for p in mp])
elif qgsgeom.wkbType() == QgsWkbTypes.LineString:
pl = qgsgeom.asPolyline()
qgsgeom = QgsGeometry.fromPolylineXY([QgsPointXY(p[1],p[0]) for p in pl])
elif qgsgeom.wkbType() == QgsWkbTypes.MultiLineString:
mls = qgsgeom.asMultiPolyline()
qgsgeom = QgsGeometry.fromMultiPolylineXY([[QgsPointXY(p[1],p[0]) for p in pl] for pl in mls])
elif qgsgeom.wkbType() == QgsWkbTypes.Polygon:
pl = qgsgeom.asPolygon()
qgsgeom = QgsGeometry.fromPolygonXY([[QgsPointXY(p[1],p[0]) for p in r] for r in pl])
elif qgsgeom.wkbType() == QgsWkbTypes.MultiPolygon:
mp = qgsgeom.asMultiPolygon()
qgsgeom = QgsGeometry.fromMultiPolygonXY([[[QgsPointXY(p[1],p[0]) for p in r] for r in pl] for pl in mp])
return qgsgeom
示例11: testMeasurePolygonWithHole
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def testMeasurePolygonWithHole(self):
# +-+-+-+
# | |
# + +-+ +
# | | | |
# + +-+ +
# | |
# +-+-+-+
polygon = QgsGeometry.fromPolygonXY(
[
[QgsPointXY(0, 0), QgsPointXY(3, 0), QgsPointXY(3, 3), QgsPointXY(0, 3), QgsPointXY(0, 0)],
[QgsPointXY(1, 1), QgsPointXY(2, 1), QgsPointXY(2, 2), QgsPointXY(1, 2), QgsPointXY(1, 1)],
]
)
da = QgsDistanceArea()
area = da.measureArea(polygon)
assert area == 8, "Expected:\n%f\nGot:\n%f\n" % (8, area)
# MH150729: Changed behavior to consider inner rings for perimeter calculation. Therefore, expected result is 16.
perimeter = da.measurePerimeter(polygon)
assert perimeter == 16, "Expected:\n%f\nGot:\n%f\n" % (16, perimeter)
示例12: _rectangleGrid
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def _rectangleGrid(self, sink, width, height, originX, originY,
hSpacing, vSpacing, hOverlay, vOverlay, feedback):
ft = QgsFeature()
columns = int(math.ceil(float(width) / (hSpacing - hOverlay)))
rows = int(math.ceil(float(height) / (vSpacing - vOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
for col in range(columns):
if feedback.isCanceled():
break
x1 = originX + (col * hSpacing - col * hOverlay)
x2 = x1 + hSpacing
for row in range(rows):
y1 = originY - (row * vSpacing - row * vOverlay)
y2 = y1 - vSpacing
polyline = []
polyline.append(QgsPointXY(x1, y1))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x2, y2))
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x1, y1))
ft.setGeometry(QgsGeometry.fromPolygonXY([polyline]))
ft.setAttributes([x1, y1, x2, y2, id])
sink.addFeature(ft, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))
示例13: ovals
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def ovals(self, sink, source, width, height, rotation, segments, feedback):
features = source.getFeatures()
ft = QgsFeature()
total = 100.0 / source.featureCount() if source.featureCount() else 0
if rotation >= 0:
for current, feat in enumerate(features):
if feedback.isCanceled():
break
if not feat.hasGeometry():
continue
w = feat[width]
h = feat[height]
angle = feat[rotation]
# block 0/NULL width or height, but allow 0 as angle value
if not w or not h:
feedback.pushInfo(QCoreApplication.translate('RectanglesOvalsDiamondsVariable', 'Feature {} has empty '
'width or height. '
'Skipping…').format(feat.id()))
continue
if angle == NULL:
feedback.pushInfo(QCoreApplication.translate('RectanglesOvalsDiamondsVariable', 'Feature {} has empty '
'angle. '
'Skipping…').format(feat.id()))
continue
xOffset = w / 2.0
yOffset = h / 2.0
phi = angle * math.pi / 180
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = []
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
polygon = [[QgsPointXY(i[0] * math.cos(phi) + i[1] * math.sin(phi) + x,
-i[0] * math.sin(phi) + i[1] * math.cos(phi) + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygonXY(polygon))
ft.setAttributes(feat.attributes())
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
else:
for current, feat in enumerate(features):
if feedback.isCanceled():
break
if not feat.hasGeometry():
continue
w = feat[width]
h = feat[height]
if not w or not h:
feedback.pushInfo(QCoreApplication.translate('RectanglesOvalsDiamondsVariable', 'Feature {} has empty '
'width or height. '
'Skipping…').format(feat.id()))
continue
xOffset = w / 2.0
yOffset = h / 2.0
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = []
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
polygon = [[QgsPointXY(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygonXY(polygon))
ft.setAttributes(feat.attributes())
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
示例14: _hexagonGrid
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def _hexagonGrid(self, sink, bbox, hSpacing, vSpacing, hOverlay, vOverlay, feedback):
feat = QgsFeature()
# To preserve symmetry, hspacing is fixed relative to vspacing
xVertexLo = 0.288675134594813 * vSpacing
xVertexHi = 0.577350269189626 * vSpacing
hSpacing = xVertexLo + xVertexHi
hOverlay = hSpacing - hOverlay
if hOverlay < 0:
raise QgsProcessingException(
self.tr('To preserve symmetry, hspacing is fixed relative to vspacing\n \
hspacing is fixed at: {0} and hoverlay is fixed at: {1}\n \
hoverlay cannot be negative. Increase hoverlay.').format(hSpacing, hOverlay)
)
halfVSpacing = vSpacing / 2.0
columns = int(math.ceil(float(bbox.width()) / hOverlay))
rows = int(math.ceil(float(bbox.height()) / (vSpacing - vOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
for col in range(columns):
if feedback.isCanceled():
break
# (column + 1) and (row + 1) calculation is used to maintain
# topology between adjacent shapes and avoid overlaps/holes
# due to rounding errors
x1 = bbox.xMinimum() + (col * hOverlay) # far left
x2 = x1 + (xVertexHi - xVertexLo) # left
x3 = bbox.xMinimum() + (col * hOverlay) + hSpacing # right
x4 = x3 + (xVertexHi - xVertexLo) # far right
for row in range(rows):
if (col % 2) == 0:
y1 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 0) * halfVSpacing) # hi
y2 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 1) * halfVSpacing) # mid
y3 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 2) * halfVSpacing) # lo
else:
y1 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 1) * halfVSpacing) # hi
y2 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 2) * halfVSpacing) # mid
y3 = bbox.yMaximum() + (row * vOverlay) - (((row * 2) + 3) * halfVSpacing) # lo
polyline = []
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x3, y1))
polyline.append(QgsPointXY(x4, y2))
polyline.append(QgsPointXY(x3, y3))
polyline.append(QgsPointXY(x2, y3))
polyline.append(QgsPointXY(x1, y2))
feat.setGeometry(QgsGeometry.fromPolygonXY([polyline]))
feat.setAttributes([x1, y1, x4, y3, id])
sink.addFeature(feat, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))
示例15: run
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygonXY [as 别名]
def run(self):
try:
inputlayer = self.inputvectorlayer
if inputlayer is None:
self.error.emit(self.tr('No input layer defined'))
self.finished.emit(False, None)
return
# Get and check the geometry type of the input layer
geometryType = self.inputvectorlayer.geometryType()
if not (geometryType == QgsWkbTypes.LineGeometry or
geometryType == QgsWkbTypes.PolygonGeometry):
self.error.emit('Only line and polygon layers are supported!')
self.finished.emit(False, None)
return
self.processed = 0
self.percentage = 0
if self.selectedfeaturesonly:
self.feature_count = inputlayer.selectedFeatureCount()
else:
self.feature_count = inputlayer.featureCount()
if self.feature_count == 0:
self.error.emit("No features in layer")
self.finished.emit(False, None)
return
self.increment = self.feature_count // 1000
# Initialise the result list
statistics = []
# Initialise the bins for the over all result
mybins = []
for i in range(self.bins):
mybins.append([0.0, 0])
# Add the over all bins
statistics.append(mybins)
# Get the features (iterator)
if self.selectedfeaturesonly:
features = inputlayer.getSelectedFeatures()
# features = inputlayer.selectedFeaturesIterator()
else:
features = inputlayer.getFeatures()
# Create a list for the (possible) tile (Polygon)
# geometries
tilegeoms = []
if self.tilelayer is not None:
self.status.emit("Using tiles!")
for tilefeat in self.tilelayer.getFeatures():
tilegeoms.append(tilefeat.geometry())
# Initialise and add bins for all the tiles
for i in range(len(tilegeoms)):
mybins = []
for j in range(self.bins):
mybins.append([0.0, 0])
statistics.append(mybins)
# Go through the features
for feat in features:
# Allow user abort
if self.abort is True:
break
# Prepare for the histogram creation by extracting
# line geometries (QgsGeometry) from the input layer
# First we do all the lines of the layer. Later we
# will do the lines per tile
# We use a list of line geometries to be able to
# handle MultiPolylines and Polygons
inputlines = []
geom = feat.geometry() # QgsGeometry
if geometryType == QgsWkbTypes.LineGeometry:
# Lines!
if geom.isMultipart():
theparts = geom.constParts()
# QgsGeometryConstPartIterator
# Go through the parts of the multigeometry
for part in theparts:
# QgsAbstractGeometry - QgsLineString
partgeom = QgsGeometry.fromPolyline(part)
inputlines.append(partgeom) # QgsGeometry
else:
inputlines.append(geom)
# There are only two possibilites for geometry type, so
# this elif: could be replaced with an else:
elif geometryType == QgsWkbTypes.PolygonGeometry:
# Polygons!
# We use a list of polygon geometries to be able to
# handle MultiPolygons
inputpolygons = []
if geom.isMultipart():
# Multi polygon
multipoly = geom.asMultiPolygon()
for geompoly in multipoly:
# list of list of QgsPointXY
# abstract geometry -> QgsGeometry polygon
polygeometry = QgsGeometry.fromPolygonXY(geompoly)
inputpolygons.append(polygeometry)
else:
# Non-multi polygon
# Make sure it is a QgsGeometry polygon
singlegeom = geom.asPolygon()
polygeometry = QgsGeometry.fromPolygonXY(singlegeom)
inputpolygons.append(polygeometry) # QgsGeometry
# Add the polygon rings
#.........这里部分代码省略.........