Class MapCanvas
- All Implemented Interfaces:
Localized
- Direct Known Subclasses:
MapCanvasAWT
ImageView
or Canvas, which must be supplied by subclasses.
This base class provides handlers for keyboard, mouse, track pad or touch screen events
such as pans, zooms and rotations. The keyboard actions are:
| Key | Action |
|---|---|
| ⇨ | Move view to the right |
| ⇦ | Move view to the left |
| ⇧ | Move view to the top |
| ⇩ | Move view to the bottom |
| ⎇ + ⇨ | Rotate clockwise |
| ⎇ + ⇦ | Rotate anticlockwise |
| Page down | Zoom in |
| Page up | Zoom out |
| Home | Reset |
| Ctrl + above | Above actions as a smaller translation, zoom or rotation |
Subclassing
Implementations need to add at least one JavaFX node in thefloatingPane list of children.
Map rendering involves the following steps:
createRenderer()is invoked in the JavaFX thread. That method shall take a snapshot of every information needed for performing the rendering in background.MapCanvas.Renderer.render()is invoked in a background thread. That method creates or updates the nodes to show in thisMapCanvasbut without interacting with the canvas yet.MapCanvas.Renderer.commit(MapCanvas)is invoked in the JavaFX thread. The nodes prepared byrender()can be transferred tofloatingPanein that method.
- Since:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA snapshot ofMapCanvasstate to render as a map, together with rendering code.static classJavaFX nodes rendered using a snapshot of the objective to display transform. -
Property Summary
PropertiesTypePropertyDescriptionReturns a property giving the exception or error that occurred during last rendering operation.final ReadOnlyObjectProperty<Length> Returns a property which gives an estimation of positional accuracy, typically in metres.Returns a property telling whether a rendering is in progress. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StackPaneThe pane showing the map and other JavaFX nodes to keep at fixed position regardless pans, zooms or rotations applied on the map.protected final PaneThe pane showing the map and any other JavaFX nodes to scale and translate together with the map.Fields inherited from class PlanarCanvas
BIDIMENSIONAL, objectiveToDisplayModifier and TypeFieldDescriptionprotected static final intThe 2 constant for identifying code specific to bi-dimensional case.protected final AffineTransformThe conversion from objective CRS to the display coordinate system as a Java2D affine transform.Fields inherited from class Canvas
DISPLAY_BOUNDS_PROPERTY, OBJECTIVE_CRS_PROPERTY, OBJECTIVE_TO_DISPLAY_PROPERTY, POINT_OF_INTEREST_PROPERTYModifier and TypeFieldDescriptionstatic final StringThe "displayBounds" property name, used for notifications about changes in bounds of display device.static final StringThe "objectiveCRS" property name, used for notifications about changes in objective CRS.static final StringThe "objectiveToDisplay" property name, used for notifications about changes in objective to display conversion.static final StringThe "pointOfInterest" property name, used for notifications about changes in point of interest. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidclear()Removes the content shown by this canvas while keeping the listeners.protected final voidClears the error message in the status bar.protected abstract MapCanvas.RendererInvoked in JavaFX thread for creating a renderer to be executed in a background thread.protected voidSets the error property to the given value.Returns a property giving the exception or error that occurred during last rendering operation.Returns an identifier of the rendered data.Returns the data bounds to use for computing the initial "objective to display" transform.protected voidinitialize(GridGeometry visibleArea) Sets the objective bounds and/or the zoom level and objective CRS to use for the initial view of data.final ReadOnlyObjectProperty<Length> Returns a property which gives an estimation of positional accuracy, typically in metres.Returns a property telling whether a rendering is in progress.final voidRequests the map to be rendered again, possibly with new data.voidreset()Resets the map view to its default zoom level and default position with no rotation.protected booleanrunAfterRendering(Runnable task) Registers a task to execute after the background thread finished its current rendering task.voidsetObjectiveBounds(Envelope visibleArea) Sets the data bounds to use for computing the initial value ofPlanarCanvas.objectiveToDisplay.voidsetObjectiveToDisplay(LinearTransform newValue) Sets the conversion from objective CRS to display coordinate system.protected voidsetPositionalAccuracy(Quantity<Length> accuracy) Sets an estimation of the positional accuracy of the pixels or features that are rendered on the map.Returns a string representation of this canvas for debugging purposes.voidUpdates the objective to display transform with the given transform in pixel coordinates.voidUpdates the objective to display transform with the given transform in objective coordinates.Returns a pane where graphics can be added as if the objective to display transform was static.Methods inherited from class PlanarCanvas
getDisplayBoundsModifier and TypeMethodDescriptionReturns the size and location of the display device.Methods inherited from class Canvas
getDisplayCRS, getGeographicArea, getGridGeometry, getLocale, getObjectiveCRS, getObjectiveToDisplay, getPointOfInterest, getSpatialResolution, setDisplayBounds, setGridGeometry, setObjectiveCRS, setPointOfInterestModifier and TypeMethodDescriptionfinal EngineeringCRSReturns the Coordinate Reference System of the display device.Returns the geographic bounding box encompassing the area shown on the display device.Returns canvas properties (CRS, display bounds, conversion) encapsulated in a grid geometry.Returns the locale used for texts or for producing some error messages.Returns the Coordinate Reference System in which all data are transformed before displaying.Returns the (usually affine) conversion from objective CRS to display coordinate system.getPointOfInterest(boolean objective) Returns the coordinates of a point considered representative of the data.Returns an estimation of the resolution (in metres) at the point of interest.voidsetDisplayBounds(Envelope newValue) Sets the size and location of the display device.voidsetGridGeometry(GridGeometry newValue) Sets canvas properties from the given grid geometry.voidsetObjectiveCRS(CoordinateReferenceSystem newValue, DirectPosition anchor) Sets the Coordinate Reference System in which all data are transformed before displaying.voidsetPointOfInterest(DirectPosition newValue) Sets the coordinates of a representative point inside the data bounding box.Methods inherited from class Observable
addPropertyChangeListener, firePropertyChange, firePropertyChange, hasPropertyChangeListener, removePropertyChangeListenerModifier and TypeMethodDescriptionfinal voidaddPropertyChangeListener(String propertyName, PropertyChangeListener listener) Registers a listener for the property of the given name.protected voidNotifies all registered listeners that a property changed its value.protected voidfirePropertyChange(String propertyName, Object oldValue, Object newValue) Notifies all registered listeners that a property of the given name changed its value.protected final booleanhasPropertyChangeListener(String propertyName) Returnstrueif the given property has at least one listener.final voidremovePropertyChangeListener(String propertyName, PropertyChangeListener listener) Unregisters a property listener.
-
Property Details
-
rendering
Returns a property telling whether a rendering is in progress. This property becometruewhen thisMapCanvasis about to start a background thread for performing a rendering, and is reset tofalseafter thisMapCanvashas been updated with new rendering result. The reset tofalsehappens in all cases: after successful completion, failure or cancellation.- See Also:
-
positionalAccuracy
Returns a property which gives an estimation of positional accuracy, typically in metres. The positions of pixels or features rendered on the map may have limited accuracy when the positions are computed by a coordinate transformations. The position may also be inaccurate because of approximation applied for faster rendering. The property value may benullif the accuracy is not specified.Usage example
TheStatusBar.lowestAccuracyproperty can bind to this property for reporting the positional accuracy on the status bar. This is done automatically by theStatusBar.track(MapCanvas)method.- Since:
- 1.7
- See Also:
-
error
Returns a property giving the exception or error that occurred during last rendering operation. The property value is reset tonullwhen a rendering operation completed successfully.- See Also:
-
-
Field Details
-
floatingPane
The pane showing the map and any other JavaFX nodes to scale and translate together with the map. This pane is initially empty; subclasses should add nodes (canvas, images, shapes, texts, etc.) into thePane.getChildren()list. All children must specify their coordinates in units relative to the pane (absolute layout). Those coordinates can be computed from real world coordinates byPlanarCanvas.objectiveToDisplay.This pane contains an
Affinetransform which is updated by user gestures such as pans, zooms or rotations. Visual positions of all children move together in response to user's gesture, thus giving an appearance of pane floating around. Changes infloatingPaneaffine transform are temporary; they are applied for producing immediate visual feedback while the map is recomputed in a background thread. Once calculation is completed and the content of this pane has been updated, thefloatingPaneAffinetransform is reset to identity.- See Also:
-
fixedPane
The pane showing the map and other JavaFX nodes to keep at fixed position regardless pans, zooms or rotations applied on the map. This pane contains at least the floatingPane (which itself contains the map), but more children (shapes, texts, controls, etc.) can be added by subclasses into thePane.getChildren()list.
-
-
Constructor Details
-
MapCanvas
Creates a new canvas for JavaFX application.- Parameters:
locale- the locale to use for labels and some messages, ornullfor default.
-
-
Method Details
-
initialize
Sets the objective bounds and/or the zoom level and objective CRS to use for the initial view of data. ThevisibleAreaCRS defines the initial objective CRS of this canvas. ThevisibleAreaenvelope defines the (usually constant) objective bounds of this canvas. In addition, ifvisibleAreacontains a grid to CRS transform, its inverse will define the initial objective to display transform (which in turn defines the initial viewed area and zoom level).This method should be invoked only when new data have been loaded, or when the caller wants to discard any zoom or translation and reset the view to the given bounds. This method does not cause new repaint event;
requestRepaint()must be invoked by the caller if desired.- Parameters:
visibleArea- bounding box, objective CRS and or initial zoom level, ornullfor a default transform showing fully the map.- Throws:
MismatchedDimensionException- if the given grid geometry is not two-dimensional.- Since:
- 1.3
- See Also:
-
getContentIdentifier
Returns an identifier of the rendered data. The identifier should be unique on a best effort basis, but this is not a strict requirement. It can be, for example, derived from the resource identifier.- Returns:
- an identifier of the rendered data.
- Since:
- 1.7
-
usingFixedTransform
Returns a pane where graphics can be added as if the objective to display transform was static. The returned object takes a snapshot of the transform at the time when this method has been invoked. It allows to show shapes more easily than adding them directly asfloatingPanechildren, but at the expanse of quality. This compromise is better suited to animation effects such asFadeTransitionand when shapes are removed from the children list after a few seconds.- Returns:
- a pane where to add shapes without the need to track navigation events after this method call.
- Since:
- 1.7
-
reset
public void reset()Resets the map view to its default zoom level and default position with no rotation. Contrary toclear(), this method does not remove the map content.- See Also:
-
getObjectiveBounds
Returns the data bounds to use for computing the initial "objective to display" transform. This is the value specified by the last call tosetObjectiveBounds(Envelope). The coordinate reference system of the returned envelope defines also the CRS which is restored when thereset()method is invoked.- Returns:
- the data bounds to use for computing the initial "objective to display" transform,
or
nullif unspecified. - Since:
- 1.3
-
setObjectiveBounds
Sets the data bounds to use for computing the initial value ofPlanarCanvas.objectiveToDisplay. Invoking this method also sets the initial objective CRS of this canvas to the CRS of given envelope.This method should be invoked only when new data have been loaded, or when the caller wants to discard any zoom or translation and reset the view to the given bounds. This method does not cause new repaint event;
requestRepaint()must be invoked by the caller if desired.- Parameters:
visibleArea- bounding box in (new) objective CRS of the initial area to show, ornullif unknown (in which case an identity transform will be set).- Throws:
MismatchedDimensionException- if the given envelope is not two-dimensional.- See Also:
-
setObjectiveToDisplay
Sets the conversion from objective CRS to display coordinate system. Invoking this method has the effect of changing the viewed area, the zoom level or the rotation of the map. Caller needs to invokerequestRepaint()after this method call (this is not done automatically).- Overrides:
setObjectiveToDisplayin classCanvas- Parameters:
newValue- the new objective to display conversion.- Throws:
IllegalArgumentException- if given the transform does not have the expected number of dimensions or is not affine.RenderException- if the objective to display transform cannot be set to the given value for another reason.
-
transformObjectiveCoordinates
Updates the objective to display transform with the given transform in objective coordinates. This method must be invoked in the JavaFX thread. The visual is updated immediately by transforming the current image, then a more accurate image is prepared in a background thread.Transform events
This method sends immediately aTransformChangeEventwith the "objectiveToDisplay" property name andReason.INTERIM. The interim reason means that the change is reflected visually but not yet in the objective to display transform. A short time after this method call, the objective to display transform is effectively updated and another "objectiveToDisplay" event will be sent in the JavaFX thread, but withReason.DISPLAY_NAVIGATION. Note that the latter event really expresses the change in display units, not in the objective units of the transform given to this method. That event will consolidate allINTERIMevents that happened since the last non-interim event.- Overrides:
transformObjectiveCoordinatesin classPlanarCanvas- Parameters:
before- coordinate conversion to apply before the current objective to display transform.- Since:
- 1.3
- See Also:
-
transformDisplayCoordinates
Updates the objective to display transform with the given transform in pixel coordinates. This method must be invoked in the JavaFX thread. The visual is updated immediately by transforming the current image, then a more accurate image is prepared in a background thread.Transform events
This method sends immediately aTransformChangeEventwith the "objectiveToDisplay" property name andReason.INTERIM. The interim reason means that the change is reflected visually but not yet in the objective to display transform. A short time after this method call, the objective to display transform is effectively updated and another "objectiveToDisplay" event will be sent in the JavaFX thread, but withReason.DISPLAY_NAVIGATION. That event will consolidate allINTERIMevents that happened since the last non-interim event.- Overrides:
transformDisplayCoordinatesin classPlanarCanvas- Parameters:
after- coordinate conversion to apply after the current objective to display transform.- Since:
- 1.3
- See Also:
-
setPositionalAccuracy
Sets an estimation of the positional accuracy of the pixels or features that are rendered on the map. This method sets the value returned by the read-only positional accuracy property. The value should be non-zero when the positions are computed by a coordinate transformations.This method may be invoked in the
commit()method of the renderer returned bycreateRenderer().- Parameters:
accuracy- an estimation of positional accuracy, ornullif none.- Since:
- 1.7
- See Also:
-
createRenderer
Invoked in JavaFX thread for creating a renderer to be executed in a background thread. Subclasses shall copy in this method allMapCanvasproperties that the background thread will need for performing the rendering process.- Returns:
- rendering process to be executed in background thread,
or
nullif there is nothing to paint.
-
requestRepaint
public final void requestRepaint()Requests the map to be rendered again, possibly with new data. Invoking this method does not necessarily causes the repaint process to start immediately. The request will be queued and executed at an arbitrary (short) time later. -
renderingProperty
Returns a property telling whether a rendering is in progress. This property becometruewhen thisMapCanvasis about to start a background thread for performing a rendering, and is reset tofalseafter thisMapCanvashas been updated with new rendering result. The reset tofalsehappens in all cases: after successful completion, failure or cancellation.- Returns:
- a property telling whether a rendering is in progress.
-
positionalAccuracyProperty
Returns a property which gives an estimation of positional accuracy, typically in metres. The positions of pixels or features rendered on the map may have limited accuracy when the positions are computed by a coordinate transformations. The position may also be inaccurate because of approximation applied for faster rendering. The property value may benullif the accuracy is not specified.Usage example
TheStatusBar.lowestAccuracyproperty can bind to this property for reporting the positional accuracy on the status bar. This is done automatically by theStatusBar.track(MapCanvas)method.- Returns:
- a property giving an estimation of positional accuracy.
- Since:
- 1.7
- See Also:
-
errorProperty
Returns a property giving the exception or error that occurred during last rendering operation. The property value is reset tonullwhen a rendering operation completed successfully.- Returns:
- a property giving the exception or error that occurred during last rendering operation.
-
clearError
protected final void clearError()Clears the error message in the status bar.- See Also:
-
errorOccurred
Sets the error property to the given value. This method is provided for subclasses that perform processing outside theMapCanvas.Renderer. It does not need to be invoked if the error occurred during the rendering process.If the error property already has a value, then the new error will be to the current error as a suppressed exception. The error property is cleared when a rendering operation completed successfully.
- Parameters:
ex- the exception that occurred (cannot be null).
-
runAfterRendering
Registers a task to execute after the background thread finished its current rendering task. This method shall be invoked in JavaFX thread. If there is a rendering in progress at the time this method is invoked, then the given task is queued for execution in JavaFX thread after the rendering finished. Otherwise the given task is executed immediately.Exceptions are propagated if the given task has been executed immediately, or logged if execution has been deferred.
This method is useful for subclasses when modifying the
MapCanvasstate during a rendering process may cause inconsistent state.- Parameters:
task- the task to execute.- Returns:
trueif the task has been executed immediately, orfalseif it has been queued for later execution.- Since:
- 1.2
- See Also:
-
clear
protected void clear()Removes the content shown by this canvas while keeping the listeners. This method is invoked for discarding the content currently shown by the canvas, while keeping thisMapCanvasinstance for showing new content later. Invoking this method may help to release memory when the map is no longer shown.Subclasses should override this method for cleaning their fields. Implementations in subclasses shall invoke
super.clear().This method should usually not be invoked directly. Instead, a recommended usage pattern is as below. The call to
requestRepaint()can be omitted if thisMapCanvasis removed from the tree of nodes shown by JavaFX.runAfterRendering(() -> { clear(); requestRepaint(); }); -
toString
-