Two coordinate systems are used in JMap Pro programming: the WC or World Coordinates system and the DC or Device Coordinates system. The WC system is the system used for the original data and the DC system is used for the screen that will display the map.
All the geometry coordinates in JMap are in WC. For example, if your data uses the MTM zone 8 projection, the values of the coordinates will be similar to (300 000, 5 000 000). If your data is not projected, it will be in longitude and latitude and its range will be from -180 to 180 degrees east-west and from -90 to 90 degrees north-south
The computer screen used to display the map is divided into pixels with the coordinates (0,0) showing on the top left part of the screen. This is the DC system. When you are working with mouse-clicked events (i.e., MouseClicked) in a JMap application, you are working with DC coordinates contained in the event and expressed in pixels.
In JMap programming, WC coordinates must frequently be transformed into DC and vice-versa. When the map elements of a layer are drawn on the screen, their coordinates are converted from WC to DC on the fly in order to light up the appropriate pixels on the screen. However, when a mouse click occurs on the map, the DC coordinates of the mouse cursor are transformed into WC coordinates in order to select the appropriate element on the map.
The K2DTransform class contains an affine transformation matrix used to convert data between the DC and WC systems. It provides methods to transform the coordinates in both directions. Each map in JMap (View class) has its own transformation instance.
The following source code example demonstrates how to transform WC coordinates into DC.
K2DTransform transform = ...
Point pointWC;
Point pointDC;
// Transform a point from WC to DC
pointWC = new Point.Double(100., 100.);
pointDC = transform.transform(pointWC);
// Transform a point from DC to WC
pointDC = new Point.Double(100., 100.);
pointWC = transform.transformInv(pointDC);
The following source code example demonstrates how to transform DC coordinates resulting from a mouse click event to WC. The transformation is obtained from the view.
View view = ...
K2DTransform transform = view.getTransform();
MouseEvent e = ...
Point pointWC = transform.transformInv(new Point.Double(e.getX(), e.getY()));
The following source code example demonstrates how to transform DC coordinates using a mouse click event. This simple method is only available from a tool class derived from the Tool class.
Point pointWC = Tool.toWCPoint(e);
Rectangle transformations (normal or inverted) must be performed with caution: if a rotation is applied to a view, the rectangle that is returned could be overwritten. To prevent this problem, it is preferable to make calculations on an oriented rectangle initialized from the rectangle to be transformed.
View view = ...
final ViewState viewState = view.getViewState();
// Computes the display bounding box of the selection
final Rectangle displaySelectBounds = view.getLayerManager().getDisplaySelectedBounds(viewState, true);
if (displaySelectBounds != null)
{
// Transforms the display bounding box into a WC extent.
final OrientedRectangle selectBounds = viewState.getTransform().transformInv(
new OrientedRectangle.Double(displaySelectBounds)
);
view.zoom(selectBounds);
view.refresh()
}
Main methods of the K2DTransform class
Creates a new oriented rectangle from the specified WC point and transforms it into a DC point.
Creates a new point from a WC point and transforms it into a DC point.
Creates a new rectangle from a specific WC rectangle and transforms it into DC.
Creates a new oriented rectangle from the specified DC point and transforms it into a WC point.
Creates a new point from a specific DC point and transforms it into a WC point.
Creates a new rectangle from a specific DC rectangle and transforms it into WC.