1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Refs #35058 -- Added is_3d and set_3d() to OGRGeometry.

This commit is contained in:
David Smith
2024-01-17 06:48:45 +00:00
committed by Mariusz Felisiak
parent 12c71bff83
commit cfacd69ab8
6 changed files with 65 additions and 9 deletions

View File

@@ -268,6 +268,20 @@ class OGRGeometry(GDALBase):
"Return the envelope as a 4-tuple, instead of as an Envelope object."
return self.envelope.tuple
@property
def is_3d(self):
"""Return True if the geometry has Z coordinates."""
return capi.is_3d(self.ptr)
def set_3d(self, value):
"""Set if this geometry has Z coordinates."""
if value is True:
capi.set_3d(self.ptr, 1)
elif value is False:
capi.set_3d(self.ptr, 0)
else:
raise ValueError(f"Input to 'set_3d' must be a boolean, got '{value!r}'.")
# #### SpatialReference-related Properties ####
# The SRS property
@@ -546,16 +560,15 @@ class Point(OGRGeometry):
@property
def z(self):
"Return the Z coordinate for this Point."
if self.coord_dim == 3:
if self.is_3d:
return capi.getz(self.ptr, 0)
@property
def tuple(self):
"Return the tuple of this point."
if self.coord_dim == 2:
return (self.x, self.y)
elif self.coord_dim == 3:
if self.is_3d:
return (self.x, self.y, self.z)
return self.x, self.y
coords = tuple
@@ -566,13 +579,13 @@ class LineString(OGRGeometry):
if 0 <= index < self.point_count:
x, y, z = c_double(), c_double(), c_double()
capi.get_point(self.ptr, index, byref(x), byref(y), byref(z))
if self.is_3d:
return x.value, y.value, z.value
dim = self.coord_dim
if dim == 1:
return (x.value,)
elif dim == 2:
return (x.value, y.value)
elif dim == 3:
return (x.value, y.value, z.value)
else:
raise IndexError(
"Index out of range when accessing points of a line string: %s." % index
@@ -609,7 +622,7 @@ class LineString(OGRGeometry):
@property
def z(self):
"Return the Z coordinates in a list."
if self.coord_dim == 3:
if self.is_3d:
return self._listarr(capi.getz)

View File

@@ -4,6 +4,7 @@ from django.contrib.gis.gdal.envelope import OGREnvelope
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
from django.contrib.gis.gdal.prototypes.errcheck import check_envelope
from django.contrib.gis.gdal.prototypes.generation import (
bool_output,
const_string_output,
double_output,
geom_output,
@@ -79,6 +80,8 @@ geom_diff = geom_output(lgdal.OGR_G_Difference, [c_void_p, c_void_p])
geom_intersection = geom_output(lgdal.OGR_G_Intersection, [c_void_p, c_void_p])
geom_sym_diff = geom_output(lgdal.OGR_G_SymmetricDifference, [c_void_p, c_void_p])
geom_union = geom_output(lgdal.OGR_G_Union, [c_void_p, c_void_p])
is_3d = bool_output(lgdal.OGR_G_Is3D, [c_void_p])
set_3d = void_output(lgdal.OGR_G_Set3D, [c_void_p, c_int], errcheck=False)
# Geometry modification routines.
add_geom = void_output(lgdal.OGR_G_AddGeometry, [c_void_p, c_void_p])

View File

@@ -489,8 +489,8 @@ class LayerMapping:
the mapped shapefile only contains Polygons).
"""
# Downgrade a 3D geom to a 2D one, if necessary.
if self.coord_dim != geom.coord_dim:
geom.coord_dim = self.coord_dim
if self.coord_dim == 2 and geom.is_3d:
geom.set_3d(False)
if self.make_multi(geom.geom_type, model_field):
# Constructing a multi-geometry type to contain the single geometry