def _levelOfDetailFromTransform(worldTransform): # @COMPATIBILITY: since Qt v. 4.6.0 the levelOfDetail attribute of # QStyleOptionGraphicsItem is deprecated # @SEEALSO: ItemUsesExtendedStyleOption item at # http://doc.qt.nokia.com/4.6/qgraphicsitem.html#GraphicsItemFlag-enum # # From qt/src/gui/styles/qstyleoption.cpp:5130 if worldTransform.type() <= QtGui.QTransform.TxTranslate: return 1 # Translation only? The LOD is 1. # Two unit vectors. v1 = QtCore.QLineF(0, 0, 1, 0) v2 = QtCore.QLineF(0, 0, 0, 1) # LOD is the transformed area of a 1x1 rectangle. return np.sqrt( worldTransform.map(v1).length() * worldTransform.map(v2).length())
def singleGeometryToGraphicsItem(geom, transform=None): '''Convert a single OGR geometry into a Qt4 graphics item. A "single geometry" is an OGR gemetry that don't include other geometries (GetGeometryCount() == 0). If the *transform* callable is provided then each point in the geometry is converted using the `transform(x, y, z)` call before genereting the graphics item path. .. note: for 2.5D geometries the *z* value is ignored. :param geom: a single OGR geometry :param transform: callable object for arbitrary coordinate conversion :returns: a Qt4 graphics item representing the geometry .. seealso:: :func:`geometryToGraphicsItem` ''' assert geom.GetGeometryCount() == 0 gtype = geom.GetGeometryType() if gtype in (ogr.wkbPoint, ogr.wkbPoint25D): # @TODO: check RADIUS = 3 point = geom.GetPoint() if transform: point = transform(*point) qitem = qtdraw.GraphicsPointItem(point[0], point[1], RADIUS) # @TODO: style options should be set in a more general way. # Probably it is better to set them in an external function. # Red point pen = qitem.pen() pen.setColor(QtCore.Qt.red) #pen.setWidth(15) qitem.setPen(pen) brush = qitem.brush() brush.setColor(QtCore.Qt.red) #brush.setStyle(QtCore.Qt.SolidPattern) qitem.setBrush(brush) elif (gtype in (ogr.wkbLineString, ogr.wkbLineString25D) and geom.GetPointCount() == 2): p0 = geom.GetPoint(0) p1 = geom.GetPoint(1) if transform: p0 = transform(*p0) p1 = transform(*p1) qline = QtCore.QLineF(p0[0], p0[1], p1[0], p1[1]) qitem = QtWidgets.QGraphicsLineItem(qline) elif gtype in (ogr.wkbLinearRing, ogr.wkbPolygon, ogr.wkbPolygon25D, ogr.wkbLineString, ogr.wkbLineString25D): # @NOTE: use only if geometry is a ring if geom.IsRing(): qpoly = QtGui.QPolygonF(geom.GetPointCount()) for index in range(geom.GetPointCount()): point = geom.GetPoint(index) if transform: point = transform(*point) qpoly[index] = QtCore.QPointF(point[0], point[1]) qitem = QtWidgets.QGraphicsPolygonItem(qpoly) #qitem.setFillRule(QtCore.Qt.WindingFill) # @TODO: check else: qpath = QtGui.QPainterPath() #qpath.setFillRule(QtCore.Qt.WindingFill) # @TODO: check point = geom.GetPoint(0) if transform: point = transform(*point) qpath.moveTo(point[0], point[1]) for index in range(1, geom.GetPointCount()): point = geom.GetPoint(index) if transform: point = transform(*point) qpath.lineTo(point[0], point[1]) qitem = QtWidgets.QGraphicsPathItem(qpath) elif gtype in (ogr.wkbMultiPoint, ogr.wkbMultiPoint25D, ogr.wkbMultiLineString, ogr.wkbMultiLineString25D, ogr.wkbMultiPolygon, ogr.wkbMultiPolygon25D, ogr.wkbGeometryCollection, ogr.wkbGeometryCollection25D): raise ValueError('should not happen.') elif gtype in (ogr.wkbUnknown, ogr.wkbNone): raise ValueError('invalid geopetry type: ' '"%s"' % geom.GetGeometryName()) else: raise ValueError('invalid geopetry type: "%d"' % gtype) return qitem