def iterateFeatures(self): """Iterates over features with vertices in the input layer. For each consecutive group of points with the same value for the given attribute, yields a "WKBPolygon" (Circle, Ellipse, Rectangle or general Polygon) using those points.""" for key, pts in self.iterationGroups(): try: # calls the specific feature creation method (using a dictionary) depending on # the output geometry choice (circle, ellipse etc.) provider = self.layer.dataProvider() geomAttrIdx = provider.fieldNameIndex(self.geoAttr) if geomAttrIdx < 0: raise UnknownAttributeError(self.layer.name, self.groupAttr) points = [p for p in pts] attributes = points[0][1] geom = str(attributes[geomAttrIdx]).upper() if self.sortAttr and GeoEnum.determineGeom(geom) == GeoEnum.Poly: sortAttrIdx = provider.fieldNameIndex(self.sortAttr) if sortAttrIdx < 0: raise UnknownAttributeError(self.layer.name, self.sortAttr) points = sorted(points, key = lambda p : (p[1][sortAttrIdx])) pointList = [point[0] for point in points] if self.rectOrient is not None: # we are in the Rectangle Dialog case if GeoEnum.determineGeom(geom) == GeoEnum.Rect: feature = self.createRectangle(pointList, attributes) elif GeoEnum.determineGeom(geom) in [GeoEnum.Poly, GeoEnum.Cercle, GeoEnum.Ellipse]: continue else: msg = QtGui.QApplication.translate("Engine","At least one group of point contains an invalid output geometry: {}", None, QtGui.QApplication.UnicodeUTF8) raise ValueError(msg.format(geom)) else: if GeoEnum.determineGeom(geom) == GeoEnum.Rect: continue else: feature = self.CreateMethodDict[GeoEnum.determineGeom(geom)](pointList, attributes) except ValueError as e: msg = QtGui.QApplication.translate("Engine","key: {0}-value: {1}\n", None, QtGui.QApplication.UnicodeUTF8) self.logWarning(msg.format(key, e.message)) except KeyError as e: msg = QtGui.QApplication.translate("Engine","At least one group of point contains an invalid output geometry: {}", None, QtGui.QApplication.UnicodeUTF8) self.logWarning(msg.format(geom)) else: yield feature
def iterationGroups(self): """Iterates over the input layer grouping by attribute. Returns an iterator of (key, points) pairs where key is the attribute value and points is an iterator of (QgsPoint, attributes) pairs.""" points = self.iteratePoints() provider = self.layer.dataProvider() geomAttrIdx = provider.fieldNameIndex(self.geoAttr) if geomAttrIdx < 0: raise UnknownAttributeError(self.layer.name, self.geoAttr) if self.groupAttr: grpAttrIdx = provider.fieldNameIndex(self.groupAttr) if grpAttrIdx < 0: raise UnknownAttributeError(self.layer.name, self.groupAttr) points = sorted(points, key = lambda p : (GeoEnum.determineGeom(p[1][geomAttrIdx]), p[1][grpAttrIdx])) return groupby(points, lambda p: (GeoEnum.determineGeom(p[1][geomAttrIdx]), p[1][grpAttrIdx])) else: points = sorted(points, key = lambda p : GeoEnum.determineGeom(p[1][geomAttrIdx])) return groupby(points, lambda p: GeoEnum.determineGeom(p[1][geomAttrIdx]))