Creating a Spider Visual Effect in QGIS

This tutorial explains how to create a dynamic visual effect in QGIS that simulates a “spider” (ragnetto) that automatically connects the 8 nearest points to the mouse cursor. This functionality allows interactive visualization of spatial relationships and is especially useful for proximity analysis.

The tutorial includes:

  • Dynamic graphic connections from cursor to nearby points
  • Automatic name labeling (optional)
  • Distance visualization in meters (optional)

Requirements

  • QGIS version 3.x
  • Vector point layer (cities, facilities, points of interest, etc.)

Step 1: Geometry Generator Configuration

The first step involves configuring the lines that dynamically connect the cursor with the nearest points.

  1. Load a point layer and right-click on it to access layer properties (right-click → Properties)
  2. Navigate to the “Symbology” tab, locate “Simple marker” and on the right side click the + button to add another marker
  3. Select a simple marker, then go to “Symbol layer type” and select “Geometry generator”. In geometry type select “LineString”
  4. Enter the following expression in the corresponding field:
with_variable(
  'center',
  transform(@canvas_cursor_point, @map_crs, @layer_crs),
  if(
    array_contains(
      array_slice(
        array_agg(
          @id,
          order_by := distance(@geometry, @center)
        ), 0, 7
      ),
      @id
    ),
    make_line(@geometry, @center),
    NULL
  )
)

After accepting the changes and clicking anywhere on the map, the nearest points will be displayed. If you want the cursor to follow them automatically, perform the following configuration: in the next level of the Geometry generator select “Line”, then at the bottom below the styles find the “Advanced” button. There select “Animation Setting” and activate with “Is Animated”.

If you want the expression without movement, simply disable “Animation Setting” in the previous step and use this expression:

with_variable(
  'center',
  transform(@map_extent_center, @map_crs, @layer_crs),
  if(
    array_contains(
      array_slice(
        array_agg(
          @id,
          order_by := distance(@geometry, @center)
        ), 0, 7
      ),
      @id
    ),
    make_line(@geometry, @center),
    NULL
  )
)

These expressions calculate the cursor position, determine the 8 nearest points and generate lines from each selected point to the cursor position.

  1. Configure line and point style:

    • Type: depending on the symbol for point or line, select preferred colors. Manual customization is recommended to avoid losing the geometry generator expressions

    • Color: black or according to preferences

    • Width: 0.5 mm

Step 2: Conditional Labeling

To display only the labels of connected points, rule-based labeling must be configured.

  1. Access layer properties in the “Labels” tab
  2. Select “Rule-based labeling” from the dropdown menu
  3. Create a new rule, enter the rule name and in the “Filter” field (next to Test) click the expression icon (ε) to add the following filter:
with_variable(
  'center',
  transform(@canvas_cursor_point, @map_crs, @layer_crs),
  if(
    array_contains(
      array_slice(
        array_agg(
          @id,
          order_by := distance(@geometry, @center)
        ), 0, 7
      ),
      @id
    ),
    true,
    NULL
  )
) is true
  1. Configure label text. In the lower section you can select the field you want to display the text and format it with font type, text color, etc.:
    • Select the field containing the names
    • Adjust font, size and position according to needs
    • Apply colors that provide good contrast

Step 3: Distance Visualization

If you want to show calculated distances in real time as an additional feature, this can also be done, but you must consider that the layer is projected in a planar coordinate system and the project as well. In geographic coordinates it will calculate other values.

  1. Like the previous step, create a new additional labeling rule or modify the existing one (the previous one can be disabled to keep both)
  2. Use the following expression for the label text, but not in the “Filter” field but in the “Value” field by clicking the expression icon (ε):
with_variable(
  'center',
  transform(@canvas_cursor_point, @map_crs, @layer_crs),
  if(
    array_contains(
      array_slice(
        array_agg(
          @id,
          order_by := distance(@geometry, @center)
        ), 0, 7
      ),
      @id
    ),
    round(distance(@geometry, @center), 2) || ' m',
    NULL
  )
)
  1. Recommended configuration for distance labels:
    • Distinctive color (red or orange)
    • Reduced font size (8-9 points)
    • Position close to connection lines

Advanced Customization

Once the basic functionality is established, additional improvements can be implemented:

  • Application of variable colors according to distance using ramp_color
  • Recording video sequences to demonstrate functionality
  • Export to web formats using QGIS2Web for online visualization
  • Customize according to each user’s preference

Common Troubleshooting

The effect does not update when moving the cursor

Verify that dynamic rendering is activated using the corresponding button (▶️) in the toolbar. If too many points are placed, consider using a powerful computer.

Calculated distances are incorrect

Use an appropriate projected coordinate system (for example, EPSG:3857 or the UTM system corresponding to the work area) instead of geographic systems.

Modify the number of connected points

In the provided expressions, modify the “0, 7” values according to the desired number of connections (example: “0, 4” for 5 points, “0, 9” for 10 points).

Performance Considerations

For layers with large numbers of entities (over 1000 points), it is recommended to apply previous filters to optimize system performance and maintain the fluidity of interactive visualization.


Reference: Based on documentation available at https://hfcqgis.opendatasicilia.it/esempi/ragnetto/

Scroll to Top

Discover more from GIS Tuto

Subscribe now to keep reading and get access to the full archive.

Continue reading