Package wx :: Package lib :: Package floatcanvas :: Module FloatCanvas :: Class FloatCanvas
[frames | no frames]

Type FloatCanvas

object --+                
         |                
    Object --+            
             |            
    EvtHandler --+        
                 |        
            Window --+    
                     |    
                 Panel --+
                         |
                        FloatCanvas


FloatCanvas.py

This is a high level window for drawing maps and anything else in an arbitrary coordinate system.

The goal is to provide a convenient way to draw stuff on the screen without having to deal with handling OnPaint events, converting to pixel coordinates, knowing about wxWindows brushes, pens, and colors, etc. It also provides virtually unlimited zooming and scrolling

I am using it for two things: 1) general purpose drawing in floating point coordinates 2) displaying map data in Lat-long coordinates

If the projection is set to None, it will draw in general purpose floating point coordinates. If the projection is set to 'FlatEarth', it will draw a FlatEarth projection, centered on the part of the map that you are viewing. You can also pass in your own projection function.

It is double buffered, so re-draws after the window is uncovered by something else are very quick.

It relies on NumPy, which is needed for speed (maybe, I havn't profiled it)

Bugs and Limitations:
Lots: patches, fixes welcome

For Map drawing: It ignores the fact that the world is, in fact, a sphere, so it will do strange things if you are looking at stuff near the poles or the date line. so far I don't have a need to do that, so I havn't bothered to add any checks for that yet.

Zooming: I have set no zoom limits. What this means is that if you zoom in really far, you can get integer overflows, and get wierd results. It doesn't seem to actually cause any problems other than wierd output, at least when I have run it.

Speed: I have done a couple of things to improve speed in this app. The one thing I have done is used NumPy Arrays to store the coordinates of the points of the objects. This allowed me to use array oriented functions when doing transformations, and should provide some speed improvement for objects with a lot of points (big polygons, polylines, pointsets).

The real slowdown comes when you have to draw a lot of objects, because you have to call the wx.DC.DrawSomething call each time. This is plenty fast for tens of objects, OK for hundreds of objects, but pretty darn slow for thousands of objects.

The solution is to be able to pass some sort of object set to the DC directly. I've used DC.DrawPointList(Points), and it helped a lot with drawing lots of points. I havn't got a LineSet type object, so I havn't used DC.DrawLineList yet. I'd like to get a full set of DrawStuffList() methods implimented, and then I'd also have a full set of Object sets that could take advantage of them. I hope to get to it some day.

Mouse Events:

At this point, there are a full set of custom mouse events. They are just like the regular mouse events, but include an extra attribute: Event.GetCoords(), that returns the (x,y) position in world coordinates, as a length-2 NumPy vector of Floats.

Copyright: Christopher Barker

License: Same as the version of wxPython you are using it with

Please let me know if you're using this!!!

Contact me at:

Chris.Barker@noaa.gov


Method Summary
  __init__(self, parent, id, size, ProjectionFun, BackgroundColor, Debug)
  AddArc(self, *args, **kwargs)
Creates Arc and adds its reference to the canvas.
  AddArrow(self, *args, **kwargs)
Creates Arrow and adds its reference to the canvas.
  AddArrowLine(self, *args, **kwargs)
Creates ArrowLine and adds its reference to the canvas.
  AddBitmap(self, *args, **kwargs)
Creates Bitmap and adds its reference to the canvas.
  AddCircle(self, *args, **kwargs)
Creates Circle and adds its reference to the canvas.
  AddEllipse(self, *args, **kwargs)
Creates Ellipse and adds its reference to the canvas.
  AddGroup(self, *args, **kwargs)
Creates Group and adds its reference to the canvas.
  AddLine(self, *args, **kwargs)
Creates Line and adds its reference to the canvas.
  AddObject(self, obj)
  AddObjects(self, Objects)
  AddPoint(self, *args, **kwargs)
Creates Point and adds its reference to the canvas.
  AddPointSet(self, *args, **kwargs)
Creates PointSet and adds its reference to the canvas.
  AddPolygon(self, *args, **kwargs)
Creates Polygon and adds its reference to the canvas.
  AddRectangle(self, *args, **kwargs)
Creates Rectangle and adds its reference to the canvas.
  AddScaledBitmap(self, *args, **kwargs)
Creates ScaledBitmap and adds its reference to the canvas.
  AddScaledText(self, *args, **kwargs)
Creates ScaledText and adds its reference to the canvas.
  AddScaledTextBox(self, *args, **kwargs)
Creates ScaledTextBox and adds its reference to the canvas.
  AddSpline(self, *args, **kwargs)
Creates Spline and adds its reference to the canvas.
  AddSquarePoint(self, *args, **kwargs)
Creates SquarePoint and adds its reference to the canvas.
  AddText(self, *args, **kwargs)
Creates Text and adds its reference to the canvas.
  ClearAll(ResetBB)
Removes all DrawObjects from the Canvas
  ComputeFontScale(self)
  Draw(Canvas, Force)
Re-draws the canvas.
  FlatEarthProjection(self, CenterPoint)
  GetHitTestColor(self, xy)
  HitTest(self, event, HitEvent)
  InitAll(self)
InitAll() sets everything in the Canvas to default state.
  InitializePanel(self)
  KeyDownEvent(self, event)
  KeyUpEvent(self, event)
  LeftDoubleClickEvent(self, event)
  LeftDownEvent(self, event)
  LeftUpEvent(self, event)
  MakeHitDict(self)
  MakeNewBuffers(self)
  MakeNewForegroundHTBitmap(self)
Off screen Bitmap used for Hit tests on foreground objects
  MakeNewHTBitmap(self)
Off screen Bitmap used for Hit tests on background objects
  MiddleDoubleClickEvent(self, event)
  MiddleDownEvent(self, event)
  MiddleUpEvent(self, event)
  MotionEvent(self, event)
  MouseOverTest(self, event)
  MoveImage(self, shift, CoordType, ReDraw)
move the image in the window.
  OnPaint(self, event)
  OnSize(self, event)
  OnSizeTimer(self, event)
  PixelToWorld(self, Points)
Converts coordinates from Pixel coordinates to world coordinates.
  RemoveObject(self, Object, ResetBB)
  RemoveObjects(self, Objects)
  RightDoubleCLickEvent(self, event)
  RightDownEvent(self, event)
  RightUpEvent(self, event)
  SaveAsImage(self, filename, ImageType)
Saves the current image as an image file.
  ScalePixelToWorld(self, Lengths)
This function computes a pair of x.y lengths, to change then from pixel to world coordinates.
  ScaleWorldToPixel(self, Lengths)
This function will get passed to the drawing functions of the objects, to Change a length from world to pixel coordinates.
  SetMode(self, Mode)
Set the GUImode to any of the availble mode.
  SetProjectionFun(self, ProjectionFun)
  SetToNewScale(self, DrawFlag)
  UnBindAll(self)
Removes all bindings to Objects
  WheelEvent(self, event)
  WorldToPixel(self, Coordinates)
This function will get passed to the drawing functions of the objects, to transform from world to pixel coordinates.
  Zoom(self, factor, center, centerCoords)
Zoom(factor, center) changes the amount of zoom of the image by factor.
  ZoomToBB(self, NewBB, DrawFlag)
Zooms the image to the bounding box given, or to the bounding box of all the objects on the canvas, if none is given.

Property Summary

Class Variable Summary
int HitTestBitmapDepth = 32                                                                    

Instance Method Details

AddArc(self, *args, **kwargs)

Creates Arc and adds its reference to the canvas. Argument protocol same as Arc class

AddArrow(self, *args, **kwargs)

Creates Arrow and adds its reference to the canvas.
Argument protocol same as Arrow class, whose docstring is:


    Arrow(XY, # coords of origin of arrow (x,y)
          Length, # length of arrow in pixels
          theta, # angle of arrow in degrees: zero is straight up
                 # +angle is to the right
          LineColor = "Black",
          LineStyle = "Solid",
          LineWidth    = 1,
          ArrowHeadSize = 4, # size of arrowhead in pixels
          ArrowHeadAngle = 45, # angle of arrow head in degrees
          InForeground = False):

    It will draw an arrow , starting at the point, (X,Y) pointing in
    direction, theta.

AddArrowLine(self, *args, **kwargs)

Creates ArrowLine and adds its reference to the canvas. Argument protocol same as ArrowLine class, whose docstring is:

ArrowLine(Points, # coords of points
LineColor = "Black", LineStyle = "Solid", LineWidth = 1, ArrowHeadSize = 4, # in pixels ArrowHeadAngle = 45, InForeground = False):

It will draw a set of arrows from point to point.

It takes a list of 2-tuples, or a NX2 NumPy Float array of point coordinates.

AddBitmap(self, *args, **kwargs)

Creates Bitmap and adds its reference to the canvas. Argument protocol same as Bitmap class, whose docstring is:

This class creates a bitmap object, placed at the coordinates, x,y. the "Position" argument is a two charactor string, indicating where in relation to the coordinates the bitmap should be oriented.

The first letter is: t, c, or b, for top, center and bottom The second letter is: l, c, or r, for left, center and right The position refers to the position relative to the text itself. It defaults to "tl" (top left).

The size is fixed, and does not scale with the drawing.

AddCircle(self, *args, **kwargs)

Creates Circle and adds its reference to the canvas. Argument protocol same as Circle class

AddEllipse(self, *args, **kwargs)

Creates Ellipse and adds its reference to the canvas. Argument protocol same as Ellipse class

AddGroup(self, *args, **kwargs)

Creates Group and adds its reference to the canvas. Argument protocol same as Group class, whose docstring is:

A group of other FloatCanvas Objects

Not all DrawObject methods may apply here. In particular, you can't Bind events to a group.

Note that if an object is in more than one group, it will get drawn more than once.

AddLine(self, *args, **kwargs)

Creates Line and adds its reference to the canvas. Argument protocol same as Line class, whose docstring is:

The Line class takes a list of 2-tuples, or a NX2 NumPy Float array of point coordinates.

It will draw a straight line if there are two points, and a polyline if there are more than two.

AddPoint(self, *args, **kwargs)

Creates Point and adds its reference to the canvas. Argument protocol same as Point class, whose docstring is:

The Point class takes a 2-tuple, or a (2,) NumPy array of point coordinates.

The Diameter is in screen points, not world coordinates, So the Bounding box is just the point, and doesn't include the Diameter.

The HitLineWidth is used as diameter for the Hit Test.

AddPointSet(self, *args, **kwargs)

Creates PointSet and adds its reference to the canvas. Argument protocol same as PointSet class, whose docstring is:

The PointSet class takes a list of 2-tuples, or a NX2 NumPy array of point coordinates.

If Points is a sequence of tuples: Points[N][0] is the x-coordinate of point N and Points[N][1] is the y-coordinate.

If Points is a NumPy array: Points[N,0] is the x-coordinate of point N and Points[N,1] is the y-coordinate for arrays.

Each point will be drawn the same color and Diameter. The Diameter is in screen pixels, not world coordinates.

The hit-test code does not distingish between the points, you will only know that one of the points got hit, not which one. You can use PointSet.FindClosestPoint(WorldPoint) to find out which one

In the case of points, the HitLineWidth is used as diameter.

AddPolygon(self, *args, **kwargs)

Creates Polygon and adds its reference to the canvas. Argument protocol same as Polygon class, whose docstring is:

The Polygon class takes a list of 2-tuples, or a NX2 NumPy array of point coordinates. so that Points[N][0] is the x-coordinate of point N and Points[N][1] is the y-coordinate or Points[N,0] is the x-coordinate of point N and Points[N,1] is the y-coordinate for arrays.

The other parameters specify various properties of the Polygon, and should be self explanatory.

AddRectangle(self, *args, **kwargs)

Creates Rectangle and adds its reference to the canvas. Argument protocol same as Rectangle class

AddScaledBitmap(self, *args, **kwargs)

Creates ScaledBitmap and adds its reference to the canvas. Argument protocol same as ScaledBitmap class, whose docstring is:

This class creates a bitmap object, placed at the coordinates, XY, of Height, H, in World coorsinates. The width is calculated from the aspect ratio of the bitmap.

the "Position" argument is a two charactor string, indicating where in relation to the coordinates the bitmap should be oriented.

The first letter is: t, c, or b, for top, center and bottom The second letter is: l, c, or r, for left, center and right The position refers to the position relative to the text itself. It defaults to "tl" (top left).

The size scales with the drawing

AddScaledText(self, *args, **kwargs)

Creates ScaledText and adds its reference to the canvas.
Argument protocol same as ScaledText class, whose docstring is:

    This class creates a text object that is scaled when zoomed.  It is
    placed at the coordinates, x,y. the "Position" argument is a two
    charactor string, indicating where in relation to the coordinates
    the string should be oriented.

    The first letter is: t, c, or b, for top, center and bottom The
    second letter is: l, c, or r, for left, center and right The
    position refers to the position relative to the text itself. It
    defaults to "tl" (top left).

    Size is the size of the font in world coordinates.

    Family:
        Font family, a generic way of referring to fonts without
        specifying actual facename. One of:
            wx.DEFAULT:  Chooses a default font.
            wx.DECORATI: A decorative font.
            wx.ROMAN: A formal, serif font.
            wx.SCRIPT: A handwriting font.
            wx.SWISS: A sans-serif font.
            wx.MODERN: A fixed pitch font.
        NOTE: these are only as good as the wxWindows defaults, which aren't so good.
    Style:
        One of wx.NORMAL, wx.SLANT and wx.ITALIC.
    Weight:
        One of wx.NORMAL, wx.LIGHT and wx.BOLD.
    Underlined:
        The value can be True or False. At present this may have an an
        effect on Windows only.

    Alternatively, you can set the kw arg: Font, to a wx.Font, and the
    above will be ignored. The size of the font you specify will be
    ignored, but the rest of its attributes will be preserved.

    The size will scale as the drawing is zoomed.

    Bugs/Limitations:

    As fonts are scaled, the do end up a little different, so you don't
    get exactly the same picture as you scale up and doen, but it's
    pretty darn close.

    On wxGTK1 on my Linux system, at least, using a font of over about
    3000 pts. brings the system to a halt. It's the Font Server using
    huge amounts of memory. My work around is to max the font size to
    3000 points, so it won't scale past there. GTK2 uses smarter font
    drawing, so that may not be an issue in future versions, so feel
    free to test. Another smarter way to do it would be to set a global
    zoom limit at that point.

    The hit-test is done on the entire text extent. This could be made
    optional, but I haven't gotten around to it.

AddScaledTextBox(self, *args, **kwargs)

Creates ScaledTextBox and adds its reference to the canvas.
Argument protocol same as ScaledTextBox class, whose docstring is:

    This class creates a TextBox object that is scaled when zoomed.  It is
    placed at the coordinates, x,y.

    If the Width parameter is defined, the text will be wrapped to the width given.

    A Box can be drawn around the text, be specifying:
    LineWidth and/or  FillColor

    A space(margin) can be put all the way around the text, be specifying:
    the PadSize argument in world coordinates.

    The spacing between lines can be adjusted with the:
    LineSpacing argument.

    The "Position" argument is a two character string, indicating where
    in relation to the coordinates the Box should be oriented.
    -The first letter is: t, c, or b, for top, center and bottom.
    -The second letter is: l, c, or r, for left, center and right The
    position refers to the position relative to the text itself. It
    defaults to "tl" (top left).

    Size is the size of the font in world coordinates.

    Family:
        Font family, a generic way of referring to fonts without
        specifying actual facename. One of:
            wx.DEFAULT:  Chooses a default font.
            wx.DECORATIVE: A decorative font.
            wx.ROMAN: A formal, serif font.
            wx.SCRIPT: A handwriting font.
            wx.SWISS: A sans-serif font.
            wx.MODERN: A fixed pitch font.
        NOTE: these are only as good as the wxWindows defaults, which aren't so good.
    Style:
        One of wx.NORMAL, wx.SLANT and wx.ITALIC.
    Weight:
        One of wx.NORMAL, wx.LIGHT and wx.BOLD.
    Underlined:
        The value can be True or False. At present this may have an an
        effect on Windows only.

    Alternatively, you can set the kw arg: Font, to a wx.Font, and the
    above will be ignored. The size of the font you specify will be
    ignored, but the rest of its attributes will be preserved.

    The size will scale as the drawing is zoomed.

    Bugs/Limitations:

    As fonts are scaled, they do end up a little different, so you don't
    get exactly the same picture as you scale up and down, but it's
    pretty darn close.

    On wxGTK1 on my Linux system, at least, using a font of over about
    1000 pts. brings the system to a halt. It's the Font Server using
    huge amounts of memory. My work around is to max the font size to
    1000 points, so it won't scale past there. GTK2 uses smarter font
    drawing, so that may not be an issue in future versions, so feel
    free to test. Another smarter way to do it would be to set a global
    zoom limit at that point.

    The hit-test is done on the entire box. This could be made
    optional, but I haven't gotten around to it.

AddSpline(self, *args, **kwargs)

Creates Spline and adds its reference to the canvas. Argument protocol same as Spline class

AddSquarePoint(self, *args, **kwargs)

Creates SquarePoint and adds its reference to the canvas. Argument protocol same as SquarePoint class, whose docstring is:

The SquarePoint class takes a 2-tuple, or a (2,) NumPy array of point coordinates. It produces a square dot, centered on Point

The Size is in screen points, not world coordinates, so the Bounding box is just the point, and doesn't include the Size.

The HitLineWidth is used as diameter for the Hit Test.

AddText(self, *args, **kwargs)

Creates Text and adds its reference to the canvas.
Argument protocol same as Text class, whose docstring is:

    This class creates a text object, placed at the coordinates,
    x,y. the "Position" argument is a two charactor string, indicating
    where in relation to the coordinates the string should be oriented.

    The first letter is: t, c, or b, for top, center and bottom The
    second letter is: l, c, or r, for left, center and right The
    position refers to the position relative to the text itself. It
    defaults to "tl" (top left).

    Size is the size of the font in pixels, or in points for printing
    (if it ever gets implimented). Those will be the same, If you assume
    72 PPI.

    Family:
        Font family, a generic way of referring to fonts without
        specifying actual facename. One of:
            wx.DEFAULT:  Chooses a default font.
            wx.DECORATIVE: A decorative font.
            wx.ROMAN: A formal, serif font.
            wx.SCRIPT: A handwriting font.
            wx.SWISS: A sans-serif font.
            wx.MODERN: A fixed pitch font.
        NOTE: these are only as good as the wxWindows defaults, which aren't so good.
    Style:
        One of wx.NORMAL, wx.SLANT and wx.ITALIC.
    Weight:
        One of wx.NORMAL, wx.LIGHT and wx.BOLD.
    Underlined:
        The value can be True or False. At present this may have an an
        effect on Windows only.

    Alternatively, you can set the kw arg: Font, to a wx.Font, and the
    above will be ignored.

    The size is fixed, and does not scale with the drawing.

    The hit-test is done on the entire text extent

ClearAll(ResetBB=True)

Removes all DrawObjects from the Canvas

If ResetBB is set to False, the original bounding box will remain

Draw(Canvas, Force=False)

Re-draws the canvas.

Note that the buffer will not be re-drawn unless something has changed. If you change a DrawObject directly, then the canvas will not know anything has changed. In this case, you can force a re-draw by passing int True for the Force flag:

Canvas.Draw(Force=True)

There is a main buffer set up to double buffer the screen, so you can get quick re-draws when the window gets uncovered.

If there are any objects in self._ForeDrawList, then the background gets drawn to a new buffer, and the foreground objects get drawn on top of it. The final result if blitted to the screen, and stored for future Paint events. This is done so that you can have a complicated background, but have something changing on the foreground, without having to wait for the background to get re-drawn. This can be used to support simple animation, for instance.

InitAll(self)

InitAll() sets everything in the Canvas to default state.

It can be used to reset the Canvas

MakeNewForegroundHTBitmap(self)

Off screen Bitmap used for Hit tests on foreground objects

MakeNewHTBitmap(self)

Off screen Bitmap used for Hit tests on background objects

MoveImage(self, shift, CoordType, ReDraw=True)

move the image in the window.

shift is an (x,y) tuple, specifying the amount to shift in each direction

It can be in any of three coordinates: Panel, Pixel, World, specified by the CoordType parameter

Panel coordinates means you want to shift the image by some fraction of the size of the displaed image

Pixel coordinates means you want to shift the image by some number of pixels

World coordinates mean you want to shift the image by an amount in Floating point world coordinates

PixelToWorld(self, Points)

Converts coordinates from Pixel coordinates to world coordinates.

Points is a tuple of (x,y) coordinates, or a list of such tuples, or a NX2 Numpy array of x,y coordinates.

SaveAsImage(self, filename, ImageType=15)

Saves the current image as an image file. The default is in the PNG format. Other formats can be specified using the wx flags:

wx.BITMAP_TYPE_PNG wx.BITMAP_TYPE_JPG wx.BITMAP_TYPE_BMP wx.BITMAP_TYPE_XBM wx.BITMAP_TYPE_XPM etc. (see the wx docs for the complete list)

ScalePixelToWorld(self, Lengths)

This function computes a pair of x.y lengths, to change then from pixel to world coordinates.

Lengths should be a NX2 array of (x,y) coordinates, or a 2-tuple, or sequence of 2-tuples.

ScaleWorldToPixel(self, Lengths)

This function will get passed to the drawing functions of the objects, to Change a length from world to pixel coordinates.

Lengths should be a NX2 array of (x,y) coordinates, or a 2-tuple, or sequence of 2-tuples.

SetMode(self, Mode)

Set the GUImode to any of the availble mode.

UnBindAll(self)

Removes all bindings to Objects

WorldToPixel(self, Coordinates)

This function will get passed to the drawing functions of the objects, to transform from world to pixel coordinates. Coordinates should be a NX2 array of (x,y) coordinates, or a 2-tuple, or sequence of 2-tuples.

Zoom(self, factor, center=None, centerCoords='world')

Zoom(factor, center) changes the amount of zoom of the image by factor. If factor is greater than one, the image gets larger. If factor is less than one, the image gets smaller.

center is a tuple of (x,y) coordinates of the center of the viewport, after zooming. If center is not given, the center will stay the same.

centerCoords is a flag indicating whether the center given is in pixel or world coords. Options are: "world" or "pixel"

ZoomToBB(self, NewBB=None, DrawFlag=True)

Zooms the image to the bounding box given, or to the bounding box of all the objects on the canvas, if none is given.


Class Variable Details

HitTestBitmapDepth

Type:
int
Value:
32                                                                    

Generated by Epydoc 2.1.20050511.rpd on Mon Feb 16 12:53:02 2009 http://epydoc.sf.net