Skip to content

Add getCenter() and getRadius() to Polygon#197

Open
Copilot wants to merge 2 commits intomasterfrom
copilot/calculate-center-and-radius-polygon
Open

Add getCenter() and getRadius() to Polygon#197
Copilot wants to merge 2 commits intomasterfrom
copilot/calculate-center-and-radius-polygon

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 18, 2026

Polygons had no way to compute their geographic centroid or bounding radius, making sliding-window workflows (add coords until radius exceeds limit, then trim from the front) impossible without external computation.

Changes

New: PolygonCenterTrait

Shared trait providing coordinateToXYZ() and haversineDistance() helpers, used by both Polygon and MultiPolygon.

PolygonInterface

  • getCenter(): ?CoordinateInterface — geographic centroid; null on empty polygon
  • getRadius(): float — max haversine distance (metres) from centroid to any vertex

Polygon

  • Maintains incremental unit-sphere sums ($sumX/Y/Z) updated in O(1) for add() and remove() — the hot path for sliding-window use
  • Bulk mutations (set(), setCoordinates(), offsetSet(), offsetUnset()) recalculate sums from scratch
  • Centroid uses 3D unit-sphere averaging (correct near poles and the ±180 meridian; avoids simple lat/lng mean pitfalls)

MultiPolygon

Implements both methods by iterating all coordinates across sub-polygons via the inherited getCoordinates().

Usage

$polygon = new \League\Geotools\Polygon\Polygon([
    [48.9675969, 1.7440796],
    [48.4711003, 2.5268555],
    [48.9279131, 3.1448364],
    [49.3895245, 2.6119995],
]);

$center = $polygon->getCenter(); // CoordinateInterface — geographic centroid
$radius = $polygon->getRadius(); // float — metres from centroid to farthest vertex

// Sliding-window: add to tail, remove from head
$polygon->add($newCoord);
if ($polygon->getRadius() > $limit) {
    $polygon->remove(0);
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/doctrine/instantiator/zipball/c6222283fa3f4ac679f8b9ced9a4e23f163e80d0
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/geocoder-php/php-common/zipball/a749e00819d2929bd0ec40fb79853956332970ba
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/nikic/PHP-Parser/zipball/dca41cd15c2ac9d055ad70dbfd011130757d1f82
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-cache/adapter-common/zipball/8788309be72aa7be69b88cdc0687549c74a7d479
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-cache/array-adapter/zipball/7658acf46b35a23b7be13e50a2792049e1c678c4
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-cache/hierarchical-cache/zipball/dedffd0a74f72c1db76e57ce29885836944e27f3
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-cache/tag-interop/zipball/b062b1d735357da50edf8387f7a8696f3027d328
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-fig/cache/zipball/213f9dbc5b9bfbc4f8db86d2838dc968752ce13b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/php-http/discovery/zipball/82fe4c73ef3363caed49ff8dd1539ba06044910d
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/IFsND5 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/reactphp/event-loop/zipball/ba276bda6083df7e0050fd9b33f66ad7a4ac747a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/reactphp/promise/zipball/23444f53a813a3296c1368bb104793ce8d88f04a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/ac91f01ccec49fb77bdc6fd1e548bc70f7faa3e5
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/code-unit/zipball/1fc9f64c0927627ef78ba436c9b17d967e68e120
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/comparator/zipball/e4df00b9b3571187db2831ae9aada2c6efbd715d
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/diff/zipball/ba01945089c3a293b01ba9badc29ad55b106b0bc
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/environment/zipball/830c43a844f1f8d5b7a1f6d6076b784454d8b7ed
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/exporter/zipball/14c6ba52f95a36c3d27c835d65efc7123c446e8c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/global-state/zipball/b6781316bdcd28260904e7cc18ec983d0d2ef4f6
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/5c9eeac41b290a3712d88851518825ad78f45c71
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/b4f479ebdbf63ac605d183ece17d8d7fe49c15c7
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b36f02317466907a230d3aa1d34467041271ef4a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/539c6691e0623af6dc6f9c20384c120f963465a0
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/type/zipball/75e2c2a32f5e0b3aef905b9ed0b179b953b3d7c7
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/sebastianbergmann/version/zipball/c6c1022351a901512170118436c764e473f6de8c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/console/zipball/e1e6770440fb9c9b0cf725f81d1361ad1835329d
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/380872130d3a5dd3ace2f4010d95125fde5d5c70
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/polyfill-php84/zipball/d8ced4d875142b6a7426000426b8abc631d6b191
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/property-access/zipball/fa49bf1ca8fce1ba0e2dba4e4658554cfb9364b1
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/property-info/zipball/02501d75fd834345da3ecdd8e3200ced39e370f8
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/serializer/zipball/bd395bbc6fabd136a48e1a6f91b09f88b5050b0b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/service-contracts/zipball/45112560a3ba2d715666a509a0bc9521d10b6c43
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/string/zipball/9f209231affa85aa930a5e46e6eb03381424b30b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/symfony/type-info/zipball/31f1e40cbf7851c7354281c90eb1b352c4cb8269
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)
  • https://api.github.com/repos/theseer/tokenizer/zipball/b7489ce515e168639d17feec34b8847c326b0b3c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/dccXdY /usr/bin/composer install --no-progress (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>calculate center and radius of polygon?</issue_title>
<issue_description>Is it possible / are there plans to keep track of the center and radius of a polygon?

My use case:

  1. first: keep adding coordinates to end of polygon until certain radius is exceeded
  2. then: keep removing coordinates from start of polygon until radius is within defined limit

For this I need to calculate the weighted center of the current polygon (not: average of the bounding box) and the radius of the coordinates.

Bonus points when average is calculated by keeping track of the sums of x/y/z (and dividing by number of coordinates) instead of calculating/adding all x/y/z's when center is calculated.</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI changed the title [WIP] Add calculation for center and radius of polygon Add getCenter() and getRadius() to Polygon Mar 18, 2026
Copilot AI requested a review from Surfoo March 18, 2026 22:47
@Surfoo Surfoo marked this pull request as ready for review March 18, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calculate center and radius of polygon?

2 participants