us.zuercher.gpx2map.plotter
Class AbstractPlotter

java.lang.Object
  extended by us.zuercher.gpx2map.plotter.AbstractPlotter
Direct Known Subclasses:
AbstractTextPlotter

abstract class AbstractPlotter
extends Object

AbstractPlotter renders arbitrary shapes defined by the caller onto an existing Netpbm file without reading the entire file into memory. Each AbstractShape is capable of indicating which image row it starts on. By sorting Shapes by their starting row, AbstractPlotter can read one row of the input NetpbmImage at a time, modify it according to the Shapes that appear on that line and write the row out to a WritablePpmImage.

Furthermore, the pixel-row iterators used to obtain rows of pixels from each shape are sorting by Z-ordering.

Author:
Stephan Zuercher

Field Summary
private  Rectangle cropTo
          Rectangle used for cropping the final image.
private static int LINE_THICKNESS
          Thickness, in pixels, of a plotter line.
private  MapSource mapSource
          MapSource used for mapping coordinates to the image.
private  File netpbmFile
          Netpbm input file.
private  List<AbstractShape> shapes
          List of AbstractShapes to plot.
private static Comparator<AbstractShapePixelRowIterator> Z_COMPARATOR
          Comparator for ordering rows of Pixels by their Z-ordering.
 
Constructor Summary
protected AbstractPlotter(File netpbmFile, MapSource mapSource)
          Constructs a new AbstractPlotter based on the given Netpbm file and map source.
 
Method Summary
protected  void addLine(WayPoint start, WayPoint end, Color color, double minLength, int initialZOrdering)
          Add a Line to the list of shapes to be plotted.
protected  void addShape(AbstractShape shape)
          Add an AbstractShape to the list of shapes to be plotted.
private  void applyZOrdering()
          Apply Z-ordering for all shapes based on their initial Z-ordering.
 void execute(ProgressMeter meter)
          Plot the configured AbstractShapes on the image.
protected  Rectangle getCroppingRectangle()
          Returns the cropping rectangle for the output image.
protected  Point mapLocationToPixel(LatLon location)
          Maps a LatLon coordinate onto the image.
 void setCropping(Rectangle cropTo)
          Sets the rectangle within the image boundaries that will be stored in the final output file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_THICKNESS

private static final int LINE_THICKNESS
Thickness, in pixels, of a plotter line.

See Also:
Constant Field Values

Z_COMPARATOR

private static final Comparator<AbstractShapePixelRowIterator> Z_COMPARATOR
Comparator for ordering rows of Pixels by their Z-ordering. Smaller Z values are closer to the viewer and are sorted later in the list so that the pixel rows may be drawn in sort order to achieve the correct Z-ordering.


netpbmFile

private final File netpbmFile
Netpbm input file.


mapSource

private final MapSource mapSource
MapSource used for mapping coordinates to the image.


shapes

private List<AbstractShape> shapes
List of AbstractShapes to plot.


cropTo

private Rectangle cropTo
Rectangle used for cropping the final image. Right-edge and bottom-edge coordinates are not included in the output image. That is, a Rectangle(1, 1, 10, 10) is 9 pixels by 9 pixels.

Constructor Detail

AbstractPlotter

protected AbstractPlotter(File netpbmFile,
                          MapSource mapSource)
Constructs a new AbstractPlotter based on the given Netpbm file and map source.

Parameters:
netpbmFile - the Netpbm file to plot on
mapSource - the MapSource that generated the file
Method Detail

setCropping

public void setCropping(Rectangle cropTo)
Sets the rectangle within the image boundaries that will be stored in the final output file. If this method is not called, no cropping occurs.

Parameters:
cropTo - rectangle to crop to.
Throws:
IllegalArgumentException - if the rectangle's left or top boundaries are less than 0, or if the rectangle's right or bottom boundaries are greater than the image size.

execute

public void execute(ProgressMeter meter)
             throws IOException
Plot the configured AbstractShapes on the image.

Parameters:
meter - progress indicator
Throws:
IOException - if there's an error reading the source image or writing the target image.

getCroppingRectangle

protected Rectangle getCroppingRectangle()
Returns the cropping rectangle for the output image.

Returns:
the cropping rectangle for the output image.

mapLocationToPixel

protected Point mapLocationToPixel(LatLon location)
Maps a LatLon coordinate onto the image.

Parameters:
location - the coordinate to locate on the image
Returns:
a Point representing the coordinate on the image.

addShape

protected void addShape(AbstractShape shape)
Add an AbstractShape to the list of shapes to be plotted.

Parameters:
shape - the shape to add

addLine

protected void addLine(WayPoint start,
                       WayPoint end,
                       Color color,
                       double minLength,
                       int initialZOrdering)
Add a Line to the list of shapes to be plotted. Lines are assumed to be drawn from one marker to the next. Markers have a size, and therefore may be close enough together to overlap. Pass a value of minLength where minLength is the maximum distance between two markers such that they still obscure the line drawn between them. If the line is shorter than this length, it will not be drawn.

Parameters:
start - starting coordinates of the line
end - ending coordinates of the line
color - the line's color
minLength - the minimum line length to plot.
initialZOrdering - initial Z-ordering

applyZOrdering

private void applyZOrdering()
Apply Z-ordering for all shapes based on their initial Z-ordering. All shapes with identical initial Z-orderings maintain their relative order to other Z values, but are given unique Z values that Z-order them by order added to the plotter. In other words, 5 shapes (a, b, c, d, and e) with Z-orders (1, 2, 1, 3, and 2) are added in this order: a(1), b(2), c(1), d(3), e(2). Their final Z-ordering will be: a(1), b(3), c(2), d(5), e(4).