Class CityPointRenderer

java.lang.Object
edu.cnu.mdi.mapping.render.CityPointRenderer

public class CityPointRenderer extends Object
Renders a collection of GeoJsonCityLoader.CityFeature instances as marker dots and optional text labels using an IMapProjection, MapTheme, and IContainer.

Coordinate conventions

City coordinates stored in GeoJsonCityLoader.CityFeature are in radians (λ = longitude, φ = latitude), as required by IMapProjection.latLonToXY(java.awt.geom.Point2D.Double, java.awt.geom.Point2D.Double). All coordinate intermediate values use the same x = λ, y = φ convention.

Filtering

Two independent filter levels control what is drawn:

  1. Point filter — controlled by setMinPopulation(long) and setMaxScalerank(int). Cities that fail this filter are not drawn at all.
  2. Label filter — an additional scalerank threshold controlled by setMaxLabelScalerank(int). Cities that pass the point filter but not the label filter still get a dot, but no text label.

Picking

pickCity(Point, IContainer) performs a brute-force linear scan over all visible cities to find the nearest one within a small pick radius. This is called on every mouse-move event via the feedback mechanism, so the city list should remain small (the default population filter achieves this in practice). Unlike CountryRenderer, city picking does not depend on a previous render call — it reprojects cities on demand.

Thread safety

This class is not thread-safe. All method calls must be made on the Event Dispatch Thread.

City list ownership

The city list passed to the constructor is stored by reference without defensive copying. Callers must ensure that the list is effectively immutable after construction (or is not modified while rendering is in progress). The list returned by GeoJsonCityLoader is already unmodifiable, so in normal usage this is safe.

  • Constructor Details

    • CityPointRenderer

      public CityPointRenderer(List<GeoJsonCityLoader.CityFeature> cities, IMapProjection projection)
      Creates a renderer for the given cities and projection.

      The city list is stored by reference; see the class-level note on list ownership for thread-safety considerations.

      Parameters:
      cities - the city features to render; must not be null; should be effectively immutable after this call
      projection - the map projection; must not be null
  • Method Details

    • setDrawLabels

      public void setDrawLabels(boolean drawLabels)
      Enables or disables drawing of city name labels.
      Parameters:
      drawLabels - true to draw labels; false to draw dots only
    • setUseAntialias

      public void setUseAntialias(boolean useAntialias)
      Enables or disables antialiased rendering.
      Parameters:
      useAntialias - true to enable antialiasing
    • setMinPopulation

      public void setMinPopulation(long minPopulation)
      Sets the minimum population required for a city to be drawn.
      Parameters:
      minPopulation - minimum population, or zero to disable filtering
    • setMaxScalerank

      public void setMaxScalerank(int maxScalerank)
      Sets the maximum scalerank for a city to be drawn.

      Cities with scalerank > maxScalerank are hidden. Set to a negative value to disable scalerank filtering. Scalerank 0 denotes the largest / most prominent cities; higher values denote progressively less prominent ones.

      Parameters:
      maxScalerank - maximum scalerank (inclusive), or negative to disable
    • setMaxLabelScalerank

      public void setMaxLabelScalerank(int maxLabelScalerank)
      Sets the maximum scalerank for which a label is drawn, in addition to the point filter (setMaxScalerank(int)).

      A city that passes the point filter but has scalerank > maxLabelScalerank still receives a dot but no label. Set to a negative value to disable this additional label filter so that every drawn city also receives a label.

      Parameters:
      maxLabelScalerank - maximum scalerank for labels (inclusive), or negative to disable label scalerank filtering
    • setPointRadius

      public void setPointRadius(double pointRadius)
      Sets the radius of city marker dots in screen pixels.
      Parameters:
      pointRadius - dot radius in pixels; clamped to a minimum of 0.5
    • setLabelFont

      public void setLabelFont(Font font)
      Sets the font used for city name labels.
      Parameters:
      font - label font; if null the default city-label font is used
    • setLabelFontSize

      public void setLabelFontSize(float size)
      Sets the city-label font size while preserving its family and style.
      Parameters:
      size - font size in points; clamped to [6, 36]
    • render

      public void render(Graphics2D g2, IContainer container)
      Renders all cities that pass the current filters.

      For each visible city that passes passesFilters(edu.cnu.mdi.mapping.loader.GeoJsonCityLoader.CityFeature):

      1. The geographic coordinates are projected to world space and then to screen space.
      2. A circular dot is drawn at the screen position using the city color from the theme.
      3. If drawLabels is true and the city also passes labelPassesScalerank(edu.cnu.mdi.mapping.loader.GeoJsonCityLoader.CityFeature), the city name is drawn to the right of the dot.

      The graphics state (color, font, antialiasing hint) is saved before rendering and restored on return, even if an exception occurs.

      Parameters:
      g2 - graphics context to draw into; must not be null
      container - container providing the world-to-local transform; must not be null
    • copyStyleFrom

      public void copyStyleFrom(CityPointRenderer source)
      Copies user-configurable rendering settings from another city renderer.

      City data and projection state are deliberately not copied.

      Parameters:
      source - renderer whose settings should be copied
    • pickCity

      public GeoJsonCityLoader.CityFeature pickCity(Point mouseLocal, IContainer container)
      Returns the closest visible city within a small picking radius of the given screen position, or null if no city is nearby.

      Unlike CountryRenderer.pickCountry(java.awt.Point, edu.cnu.mdi.container.IContainer), this method does not rely on any cached state from a previous render call. It reprojects every city from scratch on each invocation, which is acceptable because the population filter keeps the number of visible cities small.

      The pick radius is pointRadius + 2 pixels. If multiple cities fall within that radius the one closest to the cursor is returned.

      Parameters:
      mouseLocal - mouse position in the container's local (screen) coordinate space; must not be null
      container - container providing the world-to-local transform; must not be null
      Returns:
      the nearest city within the pick radius, or null
    • isDrawLabels

      public boolean isDrawLabels()
      Returns whether city labels are enabled.
      Returns:
      true when city labels are drawn
    • isUseAntialias

      public boolean isUseAntialias()
      Returns whether antialiasing is enabled.
      Returns:
      true when antialiasing is enabled
    • getMinPopulation

      public long getMinPopulation()
      Returns the minimum population threshold.
      Returns:
      minimum population, or a nonpositive value when disabled
    • getMaxScalerank

      public int getMaxScalerank()
      Returns the maximum city scalerank.
      Returns:
      maximum scalerank, or a negative value when disabled
    • getMaxLabelScalerank

      public int getMaxLabelScalerank()
      Returns the maximum scalerank for city labels.
      Returns:
      maximum label scalerank, or a negative value when disabled
    • getPointRadius

      public double getPointRadius()
      Returns the city-marker radius.
      Returns:
      marker radius in pixels
    • getLabelFont

      public Font getLabelFont()
      Returns the city-label font.
      Returns:
      label font
    • getPointColor

      public Color getPointColor()
      Returns the effective city-marker color.
      Returns:
      custom marker color when set; otherwise the theme marker color
    • setPointColor

      public void setPointColor(Color color)
      Sets a custom city-marker color.
      Parameters:
      color - custom color, or null to use the map theme
    • getLabelColor

      public Color getLabelColor()
      Returns the effective city-label color.
      Returns:
      custom label color when set; otherwise the theme label color
    • setLabelColor

      public void setLabelColor(Color color)
      Sets a custom city-label color.
      Parameters:
      color - custom color, or null to use the map theme
    • useThemeColors

      public void useThemeColors()
      Restores theme-controlled city colors.