Problem/Motivation

Many spatial workflows keep a line or polygon geometry in one geofield (a road segment, a parcel, a building footprint, a service area) and also need a single representative point for the same entity. That point is typically used for map markers, clustering, proximity queries, or exports to systems that only accept point data.

Right now there is no ECA action in this module that derives a point from an existing line or polygon geometry. Site builders have to maintain the point field by hand or write custom code, which means the two fields can easily drift apart when the source geometry is edited.

Since the module already relies on the PostGIS backend, the centroid can be calculated reliably on the database side with ST_Centroid and written into a target geofield as part of an ECA model.

Steps to reproduce

  1. Create a content type with two geofields using the PostGIS Geometry backend: field_geometry (holding LINESTRING / POLYGON / MULTI* values) and field_centroid (holding a POINT).
  2. Create an ECA model that reacts to entity presave for that content type.
  3. Look for an action that reads field_geometry and writes its centroid to field_centroid.
  4. No such action is available.

Proposed resolution

Add a new ECA Action plugin (for example eca_geofield_centroid, label "Geofield: calculate centroid") with the following configuration:

  • Entity: the entity to operate on (token, as in the other actions of this module).
  • Source field: the geofield holding the line or polygon geometry.
  • Target field: the geofield that will receive the resulting point.

Behaviour:

  • Read the source field value and validate its geometry type. Supported types: LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON (and GEOMETRYCOLLECTION containing these). If the source field has multiple values, collect them with ST_Collect before the calculation.
  • Calculate the centroid with ST_Centroid, keeping the SRID of the source geometry.
  • Write the result as a POINT into the target field.
  • If the source field is empty, or contains an unsupported geometry type, leave the target field unchanged and log a message.
  • Optionally, a checkbox to use ST_PointOnSurface instead of ST_Centroid, for cases where the point must lie inside the polygon (the centroid of a concave or multi-part polygon can fall outside of it).

Remaining tasks

  • Implement the action plugin.
  • Add kernel tests covering line, polygon, multi-geometry, multi-value, empty and unsupported input.
  • Document the action in the README.
  • Review.

User interface changes

A new action, "Geofield: calculate centroid", becomes available in the ECA modeller, with the configuration fields listed above.

API changes

None. A new plugin is added; existing plugins are not changed.

Data model changes

None.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

orkutmuratyilmaz created an issue. See original summary.

orkutmuratyilmaz’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.