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
- Create a content type with two geofields using the PostGIS Geometry backend:
field_geometry(holding LINESTRING / POLYGON / MULTI* values) andfield_centroid(holding a POINT). - Create an ECA model that reacts to entity presave for that content type.
- Look for an action that reads
field_geometryand writes its centroid tofield_centroid. - 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_Collectbefore 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_PointOnSurfaceinstead ofST_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.
Issue fork eca_geofield-3625529
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
Comment #4
orkutmuratyilmaz