Class MapGridPaintable
- java.lang.Object
-
- org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable
-
- org.openstreetmap.josm.gui.layer.MapGridPaintable
-
- All Implemented Interfaces:
MapViewPaintable,PreferenceChangedListener,Destroyable
public class MapGridPaintable extends AbstractMapViewPaintable implements PreferenceChangedListener, Destroyable
A grid drawn over the whole map view, on top of all layers.The grid is a pure visual aid (there is no snapping to it). It is either aligned to latitude/longitude, with a spacing in degrees, or to the projected coordinates, with a spacing in metres (true distance, measured at the grid origin), an optional rotation and an origin offset. When the grid cells would become smaller than a minimal size on screen, the spacing is multiplied by 10 until the cells are large enough, so the grid stays readable at every zoom level while remaining aligned to the configured one.
All settings are preferences (prefix
draw.grid.), seeGridPreference.An instance registers itself as a preference listener, so
destroy()must be called when it is no longer used (MapFramedoes this), otherwise the listener is leaked.- Since:
- 19640
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMapGridPaintable.GridTypeThe kind of coordinates a grid is aligned to.static classMapGridPaintable.LatLonGridLineA (curved) line of a latitude/longitude grid.static classMapGridPaintable.ProjectedGridLineA straight line of a projected grid.-
Nested classes/interfaces inherited from class org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable
AbstractMapViewPaintable.CompatibilityModeLayerPainter
-
Nested classes/interfaces inherited from interface org.openstreetmap.josm.gui.layer.MapViewPaintable
MapViewPaintable.LayerPainter, MapViewPaintable.MapViewEvent, MapViewPaintable.PaintableInvalidationEvent, MapViewPaintable.PaintableInvalidationListener
-
-
Field Summary
Fields Modifier and Type Field Description static EnumProperty<BlendComposite.Mode>BLEND_MODEThe blend mode used to draw the linesstatic NamedColorPropertyCOLOR_EASTColor of the lines of constant east coordinate resp.static NamedColorPropertyCOLOR_NORTHColor of the lines of constant north coordinate resp.private static intCURVE_SEGMENTSnumber of segments used to draw a curved (lat/lon) grid line across the viewstatic BooleanPropertyENABLEDWhether the grid is shownstatic DoublePropertyLINE_WIDTHWidth of the lines in pixelsprivate MapViewmapViewwritten bypaint(java.awt.Graphics2D, org.openstreetmap.josm.gui.MapView, org.openstreetmap.josm.data.Bounds)and read bypreferenceChanged(org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent), which may run on another threadprivate static intMAX_LINEShard limit for the number of lines in one direction, whatever the settings arestatic DoublePropertyMIN_PIXEL_SPACINGBelow this distance between lines (in pixels) the spacing is multiplied by 10static DoublePropertyORIGIN_XOrigin of the grid: a grid line passes through this coordinate (longitude resp.static DoublePropertyORIGIN_YOrigin of the grid: a grid line passes through this coordinate (latitude resp.private static java.lang.StringPREFIXstatic DoublePropertyROTATIONRotation of a projected grid, in degrees counter clockwisestatic DoublePropertySPACING_XSpacing of the vertical lines (longitude resp.static DoublePropertySPACING_YSpacing of the horizontal lines (latitude resp.static EnumProperty<MapGridPaintable.GridType>TYPEThe kind of grid
-
Constructor Summary
Constructors Constructor Description MapGridPaintable()Constructs a newMapGridPaintable.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidaddParallel(java.util.List<MapGridPaintable.LatLonGridLine> lines, double lat, double minLon, double maxLon, int segments)Adds a parallel running from one longitude to another.voiddestroy()Called when the object has been destroyed.static java.util.List<MapGridPaintable.LatLonGridLine>getLatLonGridLines(Bounds area, Bounds world, double spacingLon, double spacingLat, double originLon, double originLat, int segments)Computes the lines of a latitude/longitude grid which cross the given area.static java.util.List<MapGridPaintable.ProjectedGridLine>getProjectedGridLines(ProjectionBounds area, double spacingX, double spacingY, double rotationDegrees, double originX, double originY)Computes the lines of a (possibly rotated) grid in projected coordinates which cross the given area.(package private) static BoundsgetVisibleLatLonBounds(MapView mv)Computes the latitude/longitude bounds of the part of the world which is visible in the given view.private static EastNorthgridToEastNorth(double u, double v, double c, double s, double originX, double originY)voidpaint(java.awt.Graphics2D g, MapView mv, Bounds bbox)Paint the dataset using the engine set.private static voidpaintLatLonGrid(java.awt.Graphics2D g, MapView mv)private static voidpaintProjectedGrid(java.awt.Graphics2D g, MapView mv)voidpreferenceChanged(PreferenceChangeEvent e)Triggered when a preference entry value changes.private static doubleprobePixels(MapView mv, java.awt.geom.Point2D center, LatLon at, double spacing, boolean lon)Measures the distance on screen which corresponds to one grid spacing at the center of the view.static doubleprojectionUnitsPerMetre(Projection projection, EastNorth at)Computes how many projection units correspond to one metre on the ground at the given position.(package private) static doublethinningFactor(double pixelSpacing)Computes the factor (a power of 10) by which the spacing must be multiplied so that the lines are at leastMIN_PIXEL_SPACINGapart.-
Methods inherited from class org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable
addInvalidationListener, attachToMapView, createMapViewPainter, invalidate, removeInvalidationListener
-
-
-
-
Field Detail
-
PREFIX
private static final java.lang.String PREFIX
- See Also:
- Constant Field Values
-
ENABLED
public static final BooleanProperty ENABLED
Whether the grid is shown
-
TYPE
public static final EnumProperty<MapGridPaintable.GridType> TYPE
The kind of grid
-
SPACING_X
public static final DoubleProperty SPACING_X
Spacing of the vertical lines (longitude resp. east), in degrees resp. metres (true distance at the origin)
-
SPACING_Y
public static final DoubleProperty SPACING_Y
Spacing of the horizontal lines (latitude resp. north), in degrees resp. metres (true distance at the origin)
-
ROTATION
public static final DoubleProperty ROTATION
Rotation of a projected grid, in degrees counter clockwise
-
ORIGIN_X
public static final DoubleProperty ORIGIN_X
Origin of the grid: a grid line passes through this coordinate (longitude resp. east)
-
ORIGIN_Y
public static final DoubleProperty ORIGIN_Y
Origin of the grid: a grid line passes through this coordinate (latitude resp. north)
-
MIN_PIXEL_SPACING
public static final DoubleProperty MIN_PIXEL_SPACING
Below this distance between lines (in pixels) the spacing is multiplied by 10
-
BLEND_MODE
public static final EnumProperty<BlendComposite.Mode> BLEND_MODE
The blend mode used to draw the lines
-
LINE_WIDTH
public static final DoubleProperty LINE_WIDTH
Width of the lines in pixels
-
COLOR_EAST
public static final NamedColorProperty COLOR_EAST
Color of the lines of constant east coordinate resp. longitude (the lines running north-south)
-
COLOR_NORTH
public static final NamedColorProperty COLOR_NORTH
Color of the lines of constant north coordinate resp. latitude (the lines running east-west)
-
CURVE_SEGMENTS
private static final int CURVE_SEGMENTS
number of segments used to draw a curved (lat/lon) grid line across the view- See Also:
- Constant Field Values
-
MAX_LINES
private static final int MAX_LINES
hard limit for the number of lines in one direction, whatever the settings are- See Also:
- Constant Field Values
-
mapView
private volatile MapView mapView
written bypaint(java.awt.Graphics2D, org.openstreetmap.josm.gui.MapView, org.openstreetmap.josm.data.Bounds)and read bypreferenceChanged(org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent), which may run on another thread
-
-
Constructor Detail
-
MapGridPaintable
public MapGridPaintable()
Constructs a newMapGridPaintable.
-
-
Method Detail
-
paint
public void paint(java.awt.Graphics2D g, MapView mv, Bounds bbox)
Description copied from interface:MapViewPaintablePaint the dataset using the engine set.- Specified by:
paintin interfaceMapViewPaintable- Parameters:
g- Graphicsmv- The object that can translate GeoPoints to screen coordinates.bbox- Bounding box
-
paintProjectedGrid
private static void paintProjectedGrid(java.awt.Graphics2D g, MapView mv)
-
paintLatLonGrid
private static void paintLatLonGrid(java.awt.Graphics2D g, MapView mv)
-
getVisibleLatLonBounds
static Bounds getVisibleLatLonBounds(MapView mv)
Computes the latitude/longitude bounds of the part of the world which is visible in the given view.Contrary to
NavigatableComponent.getRealBounds()this does not simply convert the corners of the view: as soon as the view is larger than the world, those lie outside the world and their longitude wraps around, which yields a range much narrower than what is really visible (and one which jumps around while zooming or panning). The view is therefore first clipped to the world in projected coordinates.The clipped area is kept a hair inside the world, because a point exactly on the antimeridian converts to an ambiguous longitude:
Projection.eastNorth2latlon(org.openstreetmap.josm.data.coor.EastNorth)normalizes it to -180, which would turn the visible range of a view showing e.g. 60° E to 180° into 180° W to 60° E, i.e. the other half of the world.- Parameters:
mv- the map view- Returns:
- the visible bounds, or
nullif no part of the world is visible
-
probePixels
private static double probePixels(MapView mv, java.awt.geom.Point2D center, LatLon at, double spacing, boolean lon)
Measures the distance on screen which corresponds to one grid spacing at the center of the view. The probe is placed on whichever side of the center stays inside the valid coordinate range, and it is shortened if the spacing itself does not fit, so that the result is a usable length at every zoom level.- Parameters:
mv- the map viewcenter- the center of the view, on screenat- the center of the viewspacing- the grid spacing, in degreeslon-trueto probe along the longitude,falsealong the latitude- Returns:
- the distance in pixels; 0 if it cannot be measured
-
projectionUnitsPerMetre
public static double projectionUnitsPerMetre(Projection projection, EastNorth at)
Computes how many projection units correspond to one metre on the ground at the given position. For conformal projections (e.g. Mercator) this is the local scale factor, for a Mercator grid at 60° latitude one metre is two projection units.- Parameters:
projection- the projectionat- the position (projected coordinates)- Returns:
- projection units per metre; 1 if it cannot be determined (position outside the world)
-
thinningFactor
static double thinningFactor(double pixelSpacing)
Computes the factor (a power of 10) by which the spacing must be multiplied so that the lines are at leastMIN_PIXEL_SPACINGapart.- Parameters:
pixelSpacing- the distance between two lines on screen, in pixels- Returns:
- the factor (at least 1)
-
getProjectedGridLines
public static java.util.List<MapGridPaintable.ProjectedGridLine> getProjectedGridLines(ProjectionBounds area, double spacingX, double spacingY, double rotationDegrees, double originX, double originY)
Computes the lines of a (possibly rotated) grid in projected coordinates which cross the given area.- Parameters:
area- the area to coverspacingX- distance between the lines running in the "north" direction of the grid (before rotation)spacingY- distance between the lines running in the "east" direction of the grid (before rotation)rotationDegrees- rotation of the grid, counter clockwiseoriginX- east coordinate of the grid originoriginY- north coordinate of the grid origin- Returns:
- the lines; empty if the spacing is invalid or there would be too many lines
-
gridToEastNorth
private static EastNorth gridToEastNorth(double u, double v, double c, double s, double originX, double originY)
-
getLatLonGridLines
public static java.util.List<MapGridPaintable.LatLonGridLine> getLatLonGridLines(Bounds area, Bounds world, double spacingLon, double spacingLat, double originLon, double originLat, int segments)
Computes the lines of a latitude/longitude grid which cross the given area. Since these lines are curves in most projections, each line is returned as a polyline.- Parameters:
area- the area to coverworld- the bounds of the world in the current projection, the lines are clamped to itspacingLon- distance between the meridians, in degreesspacingLat- distance between the parallels, in degreesoriginLon- longitude of a meridian of the gridoriginLat- latitude of a parallel of the gridsegments- number of segments of each polyline- Returns:
- the lines; empty if the spacing is invalid or there would be too many lines
-
addParallel
private static void addParallel(java.util.List<MapGridPaintable.LatLonGridLine> lines, double lat, double minLon, double maxLon, int segments)
Adds a parallel running from one longitude to another. A parallel which crosses the antimeridian is added as two lines, one on each side of it: wrapping the longitudes of a single polyline would instead make it jump right across the view.- Parameters:
lines- the list to add tolat- the latitude of the parallelminLon- the longitude to start at, in [-180, 180]maxLon- the longitude to end at, may be larger than 180 if the area crosses the antimeridiansegments- number of segments of each polyline
-
preferenceChanged
public void preferenceChanged(PreferenceChangeEvent e)
Description copied from interface:PreferenceChangedListenerTriggered when a preference entry value changes.- Specified by:
preferenceChangedin interfacePreferenceChangedListener- Parameters:
e- the preference change event
-
destroy
public void destroy()
Description copied from interface:DestroyableCalled when the object has been destroyed.- Specified by:
destroyin interfaceDestroyable
-
-