本文整理汇总了Python中qgis.core.QgsGeometry.fromPolygon方法的典型用法代码示例。如果您正苦于以下问题:Python QgsGeometry.fromPolygon方法的具体用法?Python QgsGeometry.fromPolygon怎么用?Python QgsGeometry.fromPolygon使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsGeometry
的用法示例。
在下文中一共展示了QgsGeometry.fromPolygon方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fillHoles
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def fillHoles(self, layer, minWidth, minHeight, layerName):
provider = layer.dataProvider()
fields = provider.fields()
writer = QgsVectorLayer("Polygon?crs=EPSG:2180", layerName, "memory")
writer.startEditing()
layer.startEditing()
for feat in layer.getFeatures():
geometry = feat.geometry()
if geometry.isMultipart():
multi_polygon = geometry.asMultiPolygon()
for polygon in multi_polygon:
for ring in polygon[1:]:
geometry, area, perim, angle, width, height = self.OMBBox(QgsGeometry.fromPolygon([ring]))
if width <= minWidth or height <= minHeight or area <=minWidth*minHeight:
polygon.remove(ring)
geometry = QgsGeometry.fromMultiPolygon(multi_polygon)
else:
polygon = geometry.asPolygon()
for ring in polygon[1:]:
geometry, area, perim, angle, width, height = self.OMBBox(QgsGeometry.fromPolygon([ring]))
if width <= minWidth or height <= minHeight or area <= minWidth * minHeight:
polygon.remove(ring)
geometry = QgsGeometry.fromPolygon(polygon)
outFeat = QgsFeature()
outFeat.setGeometry(geometry)
writer.addFeature(feat)
writer.commitChanges()
layer.commitChanges()
return writer
示例2: testOverlaps
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def testOverlaps(self):
myPolyA = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 3),QgsPoint(2, 0),QgsPoint(0, 0)]])
myPolyB = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
overlapsGeom = QgsGeometry.overlaps(myPolyA, myPolyB)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", overlapsGeom))
assert overlapsGeom == True, myMessage
示例3: qgsgeom_from_mpl_collec
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def qgsgeom_from_mpl_collec(collections):
polygons = []
for i, polygon in enumerate(collections):
mpoly = []
for path in polygon.get_paths():
path.should_simplify = False
poly = path.to_polygons()
if len(poly) > 0 and len(poly[0]) > 3:
exterior = [QgsPoint(*p.tolist()) for p in poly[0]]
holes = [[QgsPoint(*p.tolist()) for p in h]
for h in poly[1:] if len(h) > 3]
if len(holes) == 1:
mpoly.append([exterior, holes[0]])
elif len(holes) > 1:
mpoly.append([exterior] + [h for h in holes])
else:
mpoly.append([exterior])
if len(mpoly) == 1:
polygons.append(QgsGeometry.fromPolygon(mpoly[0]))
elif len(mpoly) > 1:
polygons.append(QgsGeometry.fromMultiPolygon(mpoly))
else:
polygons.append(QgsGeometry.fromPolygon([]))
return polygons
示例4: diamonds
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def diamonds(self, writer, features, width, height, rotation):
ft = QgsFeature()
xOffset = width / 2.0
yOffset = height / 2.0
if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
polygon = [[QgsPoint(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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
else:
for current, feat in enumerate(features):
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
polygon = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
示例5: ovals
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def ovals(self, writer, features, width, height, rotation, segments):
ft = QgsFeature()
xOffset = width / 2.0
yOffset = height / 2.0
if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
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 = [[QgsPoint(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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
else:
for current, feat in enumerate(features):
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 = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
示例6: __init__
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def __init__(self, xmin, ymin, xmax, ymax, x_segments, y_segments):
self.vbands = []
self.hbands = []
self.vidx = QgsSpatialIndex()
self.hidx = QgsSpatialIndex()
xres = (xmax - xmin) / x_segments
yres = (ymax - ymin) / y_segments
self.xmin, self.ymax, self.xres, self.yres = xmin, ymax, xres, yres
def addVBand(idx, geom):
f = QgsFeature(idx)
f.setGeometry(geom)
self.vbands.append(f)
self.vidx.insertFeature(f)
def addHBand(idx, geom):
f = QgsFeature(idx)
f.setGeometry(geom)
self.hbands.append(f)
self.hidx.insertFeature(f)
for x in range(x_segments):
pt0 = QgsPoint(xmin + x * xres, ymax)
pt1 = QgsPoint(xmin + x * xres, ymin)
pt2 = QgsPoint(xmin + (x + 1) * xres, ymin)
pt3 = QgsPoint(xmin + (x + 1) * xres, ymax)
addVBand(x, QgsGeometry.fromPolygon([[pt0, pt1, pt2, pt3, pt0]]))
for y in range(y_segments):
pt0 = QgsPoint(xmin, ymax - y * yres)
pt1 = QgsPoint(xmin, ymax - (y + 1) * yres)
pt2 = QgsPoint(xmax, ymax - (y + 1) * yres)
pt3 = QgsPoint(xmax, ymax - y * yres)
addHBand(y, QgsGeometry.fromPolygon([[pt0, pt1, pt2, pt3, pt0]]))
示例7: diamonds
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def diamonds(self, writer, features, width, height, rotation):
ft = QgsFeature()
if rotation is not None:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
angle = feat[rotation]
if not w or not h or not angle:
ProcessingLog.addToLog(
ProcessingLog.LOG_WARNING,
self.tr("Feature {} has empty " "width, height or 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 = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
polygon = [
[
QgsPoint(
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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
else:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
if not w or not h:
ProcessingLog.addToLog(
ProcessingLog.LOG_WARNING,
self.tr("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 = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
polygon = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
示例8: ovals
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def ovals(self, writer, features, width, height, rotation, segments):
ft = QgsFeature()
if rotation is not None:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
angle = feat[rotation]
if not w or not h or not angle:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature {} has empty '
'width, height or 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 = [[QgsPoint(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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
else:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
if not w or not h:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('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 = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft)
示例9: completePolygon
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def completePolygon(self,geom, p4):
if (len(geom)>=2) and (len(geom) % 2 == 0):
p1 = geom[1]
p2 = geom[0]
p3 = geom[-1]
pf = self.lineIntersection(p1, p2, p3, p4)
new_geom = QgsGeometry.fromPolygon([self.geometry+[p4, pf]])
else:
new_geom = QgsGeometry.fromPolygon([self.geometry+[QgsPoint(p4.x(), p4.y())]])
pf = p4
return new_geom, pf
示例10: diamonds
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def diamonds(self, writer, features, width, height, rotation):
ft = QgsFeature()
if rotation is not None:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
angle = feat[rotation]
if not w or not h or not angle:
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
'width, height or angle. '
'Skipping...'.format(feat.id())),
self.tr('Processing'), QgsMessageLog.WARNING)
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 = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
else:
for current, feat in enumerate(features):
w = feat[width]
h = feat[height]
if not w or not h:
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
'width or height. '
'Skipping...'.format(feat.id())),
self.tr('Processing'), QgsMessageLog.WARNING)
continue
xOffset = w / 2.0
yOffset = h / 2.0
point = feat.geometry().asPoint()
x = point.x()
y = point.y()
points = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
polygon = [[QgsPointXY(i[0] + x, i[1] + y) for i in points]]
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
示例11: ovals
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [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.fromPolygon(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.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
示例12: _rectangleGridPoly
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def _rectangleGridPoly(self, writer, width, height, originX, originY,
hSpacing, vSpacing):
ft = QgsFeature()
columns = int(math.floor(float(width) / hSpacing))
rows = int(math.floor(float(height) / vSpacing))
for col in xrange(0, columns):
# (column + 1) and (row + 1) calculation is used to maintain
# topology between adjacent shapes and avoid overlaps/holes
# due to rounding errors
x1 = originX + (col * hSpacing)
x2 = originX + ((col + 1) * hSpacing)
for row in xrange(0, rows):
y1 = originY + (row * vSpacing)
y2 = originY + ((row + 1) * vSpacing)
polyline = []
polyline.append(QgsPoint(x1, y1))
polyline.append(QgsPoint(x2, y1))
polyline.append(QgsPoint(x2, y2))
polyline.append(QgsPoint(x1, y2))
polyline.append(QgsPoint(x1, y1))
ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x2, y2])
writer.addFeature(ft)
示例13: testContains
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def testContains(self):
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
containsGeom = QgsGeometry.contains(myPoly, myPoint)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", containsGeom))
assert containsGeom == True, myMessage
示例14: testTouches
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def testTouches(self):
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 2)])
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 0),QgsPoint(0, 0)]])
touchesGeom = QgsGeometry.touches(myLine, myPoly)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", touchesGeom))
assert touchesGeom == True, myMessage
示例15: canvasMoveEvent
# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import fromPolygon [as 别名]
def canvasMoveEvent(self, event):
if self.snapCursorRubberBand:
self.snapCursorRubberBand.hide()
self.snapCursorRubberBand.reset(geometryType=QGis.Point)
self.snapCursorRubberBand = None
oldPoint = QgsPoint(event.mapPoint())
event.snapPoint(QgsMapMouseEvent.SnapProjectConfig)
point = QgsPoint(event.mapPoint())
if oldPoint != point:
self.createSnapCursor(point)
point = QgsPoint(event.mapPoint())
if self.qntPoint == 1:
self.distanceToolTip.canvasMoveEvent(self.geometry[0], point)
geom = QgsGeometry.fromPolyline([self.geometry[0], point])
self.rubberBand.setToGeometry(geom, None)
elif self.qntPoint >= 2:
self.distanceToolTip.canvasMoveEvent(self.geometry[-1], point)
if self.free:
geom = QgsGeometry.fromPolygon([self.geometry+[QgsPoint(point.x(), point.y())]])
self.rubberBand.setToGeometry(geom, None)
else:
if (self.qntPoint % 2 == 1):
self.setAvoidStyleSnapRubberBand()
else:
self.setAllowedStyleSnapRubberBand()
projectedMousePoint = self.projectPoint(self.geometry[-2], self.geometry[-1], point)
if projectedMousePoint:
geom, pf = self.completePolygon(self.geometry, projectedMousePoint)
self.rubberBand.setToGeometry(geom, None)