On 06/03/2011 11:46 AM, uk52rob wrote:
> I have long hoped for a plugin to create ovals based on a point layer.
> It would ideally have the ability to select orientation in degrees,
> width and length (with values contained in fields also available).
> 
> I notice that Pavol Kapusta created an excellent plugin called
> ‘Rectangles’ in the contributed repo to do exactly what it describes,
> but has anyone tried ovals?
> 
> I am not a python developer myself, so I am not used to the language.
> However, if anyone is able to come up with a ‘simple’ (dare I say it)
> plugin, or has created one before and it has not yet reached a repo, I
> would be very grateful to hear from you.

In Martin's excellent python cookbook there is a part about the
appearance symbology of vector layers and how to do this in python:

http://www.qgis.org/pyqgis-cookbook/vector.html#appearance-symbology-of-vector-layers

The part of 'FooSymbolLayer' is an example for a red oval symbol. It's
not what you want, but it is a start for a developer.

Attached is the example from the cookbook, a little changed (apperently
the constructor for the Metadata has changed) always drawing a red oval
with a height which is two times the width.

To use it (only usable with New Symbology! tested in trunk here) is:
- put it in your python path somewhere (eg in your .qgis/python/plugins
directory)
- after starting qgis, open het python console and do (given the file is
still called foosymbol.py):
    from foosymbol import *
- now if you open your point layer in new symbology, clicking on the
'Change...' button below the
- symbol preview you will find a new 'symbol layer type': FooMarker
- this will draw a red elipse with a height of two times the width

Anyway: you will need a (I think pretty experienced) python programmer
to make it work exactly the way you want it.
Hopefully there is somebody with some free time...

Regards,

Richard Duivenvoorde
# little changed example from i
#   http://www.qgis.org/pyqgis-cookbook/vector.html#appearance-symbology-of-vector-layers
# to use it (only usable with New Symbology! tested in trunk here) is:
# put it in your python path somewhere (eg in your .qgis/python/plugins directory)
# after starting qgis, open het python console and do (given the file is still called foosymbol.py):
# from foosymbol import *
# now if you open your point layer in new symbology, clicking on the 'Change...' button below the
# symbol preview you will find a new 'symbol layer type': FooMarker
# this will draw a red elipse with a height of two times the width
# please adjust this example further for your needs


from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class FooSymbolLayer(QgsMarkerSymbolLayerV2):

  def __init__(self, radius=4.0):
    QgsMarkerSymbolLayerV2.__init__(self)
    self.radius = radius
    self.color = QColor(255,0,0)

  def layerType(self):
    return "FooMarker"

  def properties(self):
    return { "radius" : str(self.radius) }

  def startRender(self, context):
    pass

  def stopRender(self, context):
    pass

  def renderPoint(self, point, context):
    # Rendering depends on whether the symbol is selected (Qgis >= 1.5)
    color = context.selectionColor() if context.selected() else self.color
    p = context.renderContext().painter()
    p.setPen(color)
    p.drawEllipse(point, self.radius, self.radius*2)

  def clone(self):
    return FooSymbolLayer(self.radius)


class FooSymbolLayerWidget(QgsSymbolLayerV2Widget):
  def __init__(self, parent=None):
    QgsSymbolLayerV2Widget.__init__(self, parent)

    self.layer = None

    # setup a simple UI
    self.label = QLabel("Radius:")
    self.spinRadius = QDoubleSpinBox()
    self.hbox = QHBoxLayout()
    self.hbox.addWidget(self.label)
    self.hbox.addWidget(self.spinRadius)
    self.setLayout(self.hbox)
    self.connect( self.spinRadius, SIGNAL("valueChanged(double)"), self.radiusChanged)

  def setSymbolLayer(self, layer):
    if layer.layerType() != "FooMarker":
      return
    self.layer = layer
    self.spinRadius.setValue(layer.radius)

  def symbolLayer(self):
    return self.layer

  def radiusChanged(self, value):
    self.layer.radius = value
    self.emit(SIGNAL("changed()"))



class FooSymbolLayerMetadata(QgsSymbolLayerV2AbstractMetadata):

  def __init__(self):
    QgsSymbolLayerV2AbstractMetadata.__init__(self, QString("FooMarker"), QString("FooMarker"), QgsSymbolV2.Marker)

  def createSymbolLayer(self, props):
    radius = float(props[QString("radius")]) if QString("radius") in props else 4.0
    return FooSymbolLayer(radius)

  def createSymbolLayerWidget(self):
    return FooSymbolLayerWidget()

QgsSymbolLayerV2Registry.instance().addSymbolLayerType( FooSymbolLayerMetadata() )


_______________________________________________
Qgis-developer mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to