示例#1
0
    def setFootprint(self, polygon):
        self.clear()
        if not polygon:
            return

        lon = np.asarray([p.x() for p in polygon])
        lat = np.asarray([p.y() for p in polygon])

        mlon = lon.mean()
        mlat = lat.mean()

        delta = mlon - utils.geonormalize(mlon)
        if delta:
            lon -= delta
            mlon -= delta
            polygon.translate(-delta, 0)

        self.box = self.plot(polygon)

        points = QtGui.QPolygonF([
            QtCore.QPointF(mlon - self.bigBoxSize / 2.,
                           mlat - self.bigBoxSize / 2.),
            QtCore.QPointF(mlon + self.bigBoxSize / 2.,
                           mlat - self.bigBoxSize / 2.),
            QtCore.QPointF(mlon + self.bigBoxSize / 2.,
                           mlat + self.bigBoxSize / 2.),
            QtCore.QPointF(mlon - self.bigBoxSize / 2.,
                           mlat + self.bigBoxSize / 2.),
        ])
        self.bigbox = self.plot(points)

        self.actionZoomIn.setEnabled(True)
示例#2
0
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