mirror of
https://github.com/django/django.git
synced 2025-01-03 06:55:47 +00:00
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
This commit is contained in:
parent
9c6d7b4a67
commit
2005530920
@ -565,6 +565,14 @@ class OGRGeometry(GDALBase):
|
||||
"""
|
||||
return self._geomgen(capi.geom_union, other)
|
||||
|
||||
@property
|
||||
def centroid(self):
|
||||
"""Return the centroid (a Point) of this Polygon."""
|
||||
# The centroid is a Point, create a geometry for this.
|
||||
p = OGRGeometry(OGRGeomType("Point"))
|
||||
capi.get_centroid(self.ptr, p.ptr)
|
||||
return p
|
||||
|
||||
|
||||
# The subclasses for OGR Geometry.
|
||||
class Point(OGRGeometry):
|
||||
@ -708,14 +716,6 @@ class Polygon(OGRGeometry):
|
||||
# Summing up the number of points in each ring of the Polygon.
|
||||
return sum(self[i].point_count for i in range(self.geom_count))
|
||||
|
||||
@property
|
||||
def centroid(self):
|
||||
"Return the centroid (a Point) of this Polygon."
|
||||
# The centroid is a Point, create a geometry for this.
|
||||
p = OGRGeometry(OGRGeomType("Point"))
|
||||
capi.get_centroid(self.ptr, p.ptr)
|
||||
return p
|
||||
|
||||
|
||||
# Geometry Collection base class.
|
||||
class GeometryCollection(OGRGeometry):
|
||||
|
@ -839,6 +839,15 @@ coordinate transformation:
|
||||
Returns the region consisting of the union of this geometry and
|
||||
the other, as a new :class:`OGRGeometry` object.
|
||||
|
||||
.. attribute:: centroid
|
||||
|
||||
Returns a :class:`Point` representing the centroid of this geometry.
|
||||
|
||||
.. versionchanged:: 5.1
|
||||
|
||||
``centroid`` was promoted from a :class:`.Polygon` only attribute to
|
||||
being available on all geometry types.
|
||||
|
||||
.. attribute:: tuple
|
||||
|
||||
Returns the coordinates of a point geometry as a tuple, the
|
||||
@ -939,10 +948,6 @@ coordinate transformation:
|
||||
|
||||
An alias for :attr:`shell`.
|
||||
|
||||
.. attribute:: centroid
|
||||
|
||||
Returns a :class:`Point` representing the centroid of this polygon.
|
||||
|
||||
.. class:: GeometryCollection
|
||||
|
||||
.. method:: add(geom)
|
||||
|
@ -83,6 +83,9 @@ Minor features
|
||||
via the new :attr:`.OGRGeometry.is_measured` and :attr:`.Point.m` properties,
|
||||
and the :meth:`.OGRGeometry.set_measured` method.
|
||||
|
||||
* :attr:`.OGRGeometry.centroid` is now available on all supported geometry
|
||||
types.
|
||||
|
||||
:mod:`django.contrib.messages`
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
@ -877,6 +877,29 @@ class OGRGeomTest(SimpleTestCase, TestDataMixin):
|
||||
geom = OGRGeometry("POINT M (1 2 3)")
|
||||
self.assertEqual(geom.geos.wkt, "POINT (1 2)")
|
||||
|
||||
def test_centroid(self):
|
||||
point = OGRGeometry("POINT (1 2 3)")
|
||||
self.assertEqual(point.centroid.wkt, "POINT (1 2)")
|
||||
linestring = OGRGeometry("LINESTRING (0 0 0, 1 1 1, 2 2 2)")
|
||||
self.assertEqual(linestring.centroid.wkt, "POINT (1 1)")
|
||||
polygon = OGRGeometry("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")
|
||||
self.assertEqual(polygon.centroid.wkt, "POINT (5 5)")
|
||||
multipoint = OGRGeometry("MULTIPOINT (0 0,10 10)")
|
||||
self.assertEqual(multipoint.centroid.wkt, "POINT (5 5)")
|
||||
multilinestring = OGRGeometry(
|
||||
"MULTILINESTRING ((0 0,0 10,0 20),(10 0,10 10,10 20))"
|
||||
)
|
||||
self.assertEqual(multilinestring.centroid.wkt, "POINT (5 10)")
|
||||
multipolygon = OGRGeometry(
|
||||
"MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),"
|
||||
"((20 20, 20 30, 30 30, 30 20, 20 20)))"
|
||||
)
|
||||
self.assertEqual(multipolygon.centroid.wkt, "POINT (15 15)")
|
||||
geometrycollection = OGRGeometry(
|
||||
"GEOMETRYCOLLECTION (POINT (110 260),LINESTRING (110 0,110 60))"
|
||||
)
|
||||
self.assertEqual(geometrycollection.centroid.wkt, "POINT (110 30)")
|
||||
|
||||
|
||||
class DeprecationTests(SimpleTestCase):
|
||||
def test_coord_setter_deprecation(self):
|
||||
|
Loading…
Reference in New Issue
Block a user