Skip to content

Commit 2005530

Browse files
smithdc1felixxm
authored andcommitted
Refs #35058 -- Made centroid available on all geometry types.
Centroid is available on all geometry types since GDAL 1.8.0. Previously it was restricted to Polygon. https://gdal.org/doxygen/classOGRGeometry.html#a91787f669b2a148169667e270e7e40df
1 parent 9c6d7b4 commit 2005530

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

django/contrib/gis/gdal/geometries.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,14 @@ def union(self, other):
565565
"""
566566
return self._geomgen(capi.geom_union, other)
567567

568+
@property
569+
def centroid(self):
570+
"""Return the centroid (a Point) of this Polygon."""
571+
# The centroid is a Point, create a geometry for this.
572+
p = OGRGeometry(OGRGeomType("Point"))
573+
capi.get_centroid(self.ptr, p.ptr)
574+
return p
575+
568576

569577
# The subclasses for OGR Geometry.
570578
class Point(OGRGeometry):
@@ -708,14 +716,6 @@ def point_count(self):
708716
# Summing up the number of points in each ring of the Polygon.
709717
return sum(self[i].point_count for i in range(self.geom_count))
710718

711-
@property
712-
def centroid(self):
713-
"Return the centroid (a Point) of this Polygon."
714-
# The centroid is a Point, create a geometry for this.
715-
p = OGRGeometry(OGRGeomType("Point"))
716-
capi.get_centroid(self.ptr, p.ptr)
717-
return p
718-
719719

720720
# Geometry Collection base class.
721721
class GeometryCollection(OGRGeometry):

docs/ref/contrib/gis/gdal.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,15 @@ coordinate transformation:
839839
Returns the region consisting of the union of this geometry and
840840
the other, as a new :class:`OGRGeometry` object.
841841

842+
.. attribute:: centroid
843+
844+
Returns a :class:`Point` representing the centroid of this geometry.
845+
846+
.. versionchanged:: 5.1
847+
848+
``centroid`` was promoted from a :class:`.Polygon` only attribute to
849+
being available on all geometry types.
850+
842851
.. attribute:: tuple
843852

844853
Returns the coordinates of a point geometry as a tuple, the
@@ -939,10 +948,6 @@ coordinate transformation:
939948

940949
An alias for :attr:`shell`.
941950

942-
.. attribute:: centroid
943-
944-
Returns a :class:`Point` representing the centroid of this polygon.
945-
946951
.. class:: GeometryCollection
947952

948953
.. method:: add(geom)

docs/releases/5.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Minor features
8383
via the new :attr:`.OGRGeometry.is_measured` and :attr:`.Point.m` properties,
8484
and the :meth:`.OGRGeometry.set_measured` method.
8585

86+
* :attr:`.OGRGeometry.centroid` is now available on all supported geometry
87+
types.
88+
8689
:mod:`django.contrib.messages`
8790
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8891

tests/gis_tests/gdal_tests/test_geom.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,29 @@ def test_point_m_dimension_geos(self):
877877
geom = OGRGeometry("POINT M (1 2 3)")
878878
self.assertEqual(geom.geos.wkt, "POINT (1 2)")
879879

880+
def test_centroid(self):
881+
point = OGRGeometry("POINT (1 2 3)")
882+
self.assertEqual(point.centroid.wkt, "POINT (1 2)")
883+
linestring = OGRGeometry("LINESTRING (0 0 0, 1 1 1, 2 2 2)")
884+
self.assertEqual(linestring.centroid.wkt, "POINT (1 1)")
885+
polygon = OGRGeometry("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")
886+
self.assertEqual(polygon.centroid.wkt, "POINT (5 5)")
887+
multipoint = OGRGeometry("MULTIPOINT (0 0,10 10)")
888+
self.assertEqual(multipoint.centroid.wkt, "POINT (5 5)")
889+
multilinestring = OGRGeometry(
890+
"MULTILINESTRING ((0 0,0 10,0 20),(10 0,10 10,10 20))"
891+
)
892+
self.assertEqual(multilinestring.centroid.wkt, "POINT (5 10)")
893+
multipolygon = OGRGeometry(
894+
"MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),"
895+
"((20 20, 20 30, 30 30, 30 20, 20 20)))"
896+
)
897+
self.assertEqual(multipolygon.centroid.wkt, "POINT (15 15)")
898+
geometrycollection = OGRGeometry(
899+
"GEOMETRYCOLLECTION (POINT (110 260),LINESTRING (110 0,110 60))"
900+
)
901+
self.assertEqual(geometrycollection.centroid.wkt, "POINT (110 30)")
902+
880903

881904
class DeprecationTests(SimpleTestCase):
882905
def test_coord_setter_deprecation(self):

0 commit comments

Comments
 (0)