Skip to main content

OGRGeometry

Encapsulate an OGR geometry.

Attributes

AttributeTypeDescription
destructorcallable = capi.destroy_geomThe C API function used to destroy the OGR geometry and free its memory.
geos_supportboolean = trueA boolean flag indicating whether this geometry class supports conversion to GEOS objects.
srs[SpatialReference](../srs/spatialreference.md?sid=django_contrib_gis_gdal_srs_spatialreference)Return the Spatial Reference for this Geometry.
sridintegerThe Spatial Reference Identifier (SRID) associated with the geometry's spatial reference system.
geojsonstringReturn the GeoJSON representation of this Geometry.

Constructor

Signature

def OGRGeometry(
geom_input: str, memoryview, [OGRGeomType](../geomtype/ogrgeomtype.md?sid=django_contrib_gis_gdal_geomtype_ogrgeomtype), or c_void_p,
srs: [SpatialReference](../srs/spatialreference.md?sid=django_contrib_gis_gdal_srs_spatialreference), str, int, or None = None
) - > null

Parameters

NameTypeDescription
geom_inputstr, memoryview, [OGRGeomType](../geomtype/ogrgeomtype.md?sid=django_contrib_gis_gdal_geomtype_ogrgeomtype), or c_void_pThe input data to construct the geometry from, such as WKT, HEX, GeoJSON, WKB bytes, an existing OGR pointer, or a geometry type.
srs[SpatialReference](../srs/spatialreference.md?sid=django_contrib_gis_gdal_srs_spatialreference), str, int, or None = NoneThe spatial reference system to associate with the geometry.

Methods


from_bbox()

@classmethod
def from_bbox(
bbox: tuple
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Construct a Polygon from a bounding box (4-tuple).

Parameters

NameTypeDescription
bboxtupleA 4-tuple of (xmin, ymin, xmax, ymax) coordinates.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new Polygon geometry representing the bounding box.

from_json()

@classmethod
def from_json(
geom_input: string
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Creates an OGRGeometry instance from a GeoJSON string or bytes.

Parameters

NameTypeDescription
geom_inputstringThe GeoJSON string used to define the geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry object initialized from the GeoJSON data.

from_gml()

@classmethod
def from_gml(
gml_string: string
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Creates an OGRGeometry instance from a GML string.

Parameters

NameTypeDescription
gml_stringstringThe GML string representing the geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry object initialized from the GML data.

dimension()

@classmethod
def dimension() - > integer

Return 0 for points, 1 for lines, and 2 for surfaces.

Returns

TypeDescription
integerThe topological dimension of the geometry.

coord_dim()

@classmethod
def coord_dim() - > integer

Return the coordinate dimension of the Geometry.

Returns

TypeDescription
integerThe number of dimensions for each coordinate (e.g., 2 for XY, 3 for XYZ).

geom_count()

@classmethod
def geom_count() - > integer

Return the number of elements in this Geometry.

Returns

TypeDescription
integerThe count of sub-geometries within a collection or multi-geometry.

point_count()

@classmethod
def point_count() - > integer

Return the number of Points in this Geometry.

Returns

TypeDescription
integerThe total number of vertices in the geometry.

num_points()

@classmethod
def num_points() - > integer

Alias for point_count (same name method in GEOS API.)

Returns

TypeDescription
integerThe total number of vertices.

num_coords()

@classmethod
def num_coords() - > integer

Alias for point_count.

Returns

TypeDescription
integerThe total number of vertices.

geom_type()

@classmethod
def geom_type() - > [OGRGeomType](../geomtype/ogrgeomtype.md?sid=django_contrib_gis_gdal_geomtype_ogrgeomtype)

Return the Type for this Geometry.

Returns

TypeDescription
[OGRGeomType](../geomtype/ogrgeomtype.md?sid=django_contrib_gis_gdal_geomtype_ogrgeomtype)An object representing the OGR geometry type.

geom_name()

@classmethod
def geom_name() - > string

Return the Name of this Geometry.

Returns

TypeDescription
stringThe uppercase string name of the geometry type (e.g., 'POINT').

area()

@classmethod
def area() - > float

Return the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise.

Returns

TypeDescription
floatThe calculated area of the geometry.

envelope()

@classmethod
def envelope() - > [Envelope](../envelope/envelope.md?sid=django_contrib_gis_gdal_envelope_envelope)

Return the envelope for this Geometry.

Returns

TypeDescription
[Envelope](../envelope/envelope.md?sid=django_contrib_gis_gdal_envelope_envelope)An Envelope object representing the bounding box.

empty()

@classmethod
def empty() - > boolean

Checks if the geometry is empty.

Returns

TypeDescription
booleanTrue if the geometry has no points, False otherwise.

extent()

@classmethod
def extent() - > tuple

Return the envelope as a 4-tuple, instead of as an Envelope object.

Returns

TypeDescription
tupleA 4-tuple of (xmin, ymin, xmax, ymax).

is_3d()

@classmethod
def is_3d() - > boolean

Return True if the geometry has Z coordinates.

Returns

TypeDescription
booleanTrue if the geometry is 3D.

set_3d()

@classmethod
def set_3d(
value: boolean
)

Set if this geometry has Z coordinates.

Parameters

NameTypeDescription
valuebooleanWhether to enable or disable Z coordinates.

is_measured()

@classmethod
def is_measured() - > boolean

Return True if the geometry has M coordinates.

Returns

TypeDescription
booleanTrue if the geometry includes measurement values.

set_measured()

@classmethod
def set_measured(
value: boolean
)

Set if this geometry has M coordinates.

Parameters

NameTypeDescription
valuebooleanWhether to enable or disable M coordinates.

has_curve()

@classmethod
def has_curve() - > boolean

Return True if the geometry is or has curve geometry.

Returns

TypeDescription
booleanTrue if the geometry contains non-linear segments.

get_linear_geometry()

@classmethod
def get_linear_geometry() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a linear version of this geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry where curves are approximated by linear segments.

get_curve_geometry()

@classmethod
def get_curve_geometry() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a curve version of this geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry where linear segments are converted to curves if possible.

geos()

@classmethod
def geos() - > [GEOSGeometry](../../geos/geometry/geosgeometry.md?sid=django_contrib_gis_geos_geometry_geosgeometry)

Return a GEOSGeometry object from this OGRGeometry.

Returns

TypeDescription
[GEOSGeometry](../../geos/geometry/geosgeometry.md?sid=django_contrib_gis_geos_geometry_geosgeometry)A GEOS-compatible version of this geometry.

gml()

@classmethod
def gml() - > string

Return the GML representation of the Geometry.

Returns

TypeDescription
stringThe GML string.

hex()

@classmethod
def hex() - > string

Return the hexadecimal representation of the WKB (a string).

Returns

TypeDescription
stringThe uppercase hex-encoded WKB string.

json()

@classmethod
def json() - > string

Return the GeoJSON representation of this Geometry.

Returns

TypeDescription
stringThe GeoJSON string.

kml()

@classmethod
def kml() - > string

Return the KML representation of the Geometry.

Returns

TypeDescription
stringThe KML string.

wkb_size()

@classmethod
def wkb_size() - > integer

Return the size of the WKB buffer.

Returns

TypeDescription
integerThe number of bytes required for the WKB representation.

wkb()

@classmethod
def wkb() - > memoryview

Return the WKB representation of the Geometry.

Returns

TypeDescription
memoryviewA memoryview containing the binary WKB data.

wkt()

@classmethod
def wkt() - > string

Return the WKT representation of the Geometry.

Returns

TypeDescription
stringThe Well-Known Text string.

ewkt()

@classmethod
def ewkt() - > string

Return the EWKT representation of the Geometry.

Returns

TypeDescription
stringThe Extended WKT string including SRID if available.

clone()

@classmethod
def clone() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Clone this OGR Geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new OGRGeometry instance that is a deep copy of the original.

close_rings()

@classmethod
def close_rings()

If there are any rings within this geometry that have not been closed, this routine will do so by adding the starting point at the end.


transform()

@classmethod
def transform(
coord_trans: CoordTransform|SpatialReference|int|str,
clone: boolean = False
) - > OGRGeometry|None

Transform this geometry to a different spatial reference system.

Parameters

NameTypeDescription
coord_trans`CoordTransformSpatialReference
cloneboolean = FalseIf True, returns a transformed copy instead of transforming in-place.

Returns

TypeDescription
`OGRGeometryNone`

intersects()

@classmethod
def intersects(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry intersects with the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for intersection.

Returns

TypeDescription
booleanTrue if geometries have at least one point in common.

equals()

@classmethod
def equals(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry is equivalent to the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for equality.

Returns

TypeDescription
booleanTrue if geometries are spatially identical.

disjoint()

@classmethod
def disjoint(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry and the other are spatially disjoint.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for disjointness.

Returns

TypeDescription
booleanTrue if geometries have no points in common.

touches()

@classmethod
def touches(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry touches the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for contact.

Returns

TypeDescription
booleanTrue if geometries touch at least at one point but interiors do not intersect.

crosses()

@classmethod
def crosses(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry crosses the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for crossing.

Returns

TypeDescription
booleanTrue if geometries cross each other.

within()

@classmethod
def within(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry is within the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test as the container.

Returns

TypeDescription
booleanTrue if this geometry is completely inside the other.

contains()

@classmethod
def contains(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry contains the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test as the contained object.

Returns

TypeDescription
booleanTrue if the other geometry is completely inside this one.

overlaps()

@classmethod
def overlaps(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > boolean

Return True if this geometry overlaps the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to test for overlap.

Returns

TypeDescription
booleanTrue if geometries overlap.

boundary()

@classmethod
def boundary() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return the boundary of this geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry representing the combinatorial boundary.

convex_hull()

@classmethod
def convex_hull() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return the smallest convex Polygon that contains all the points in this Geometry.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new Polygon geometry representing the convex hull.

difference()

@classmethod
def difference(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a new geometry consisting of the region which is the difference of this geometry and the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to subtract.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry representing the point-set difference.

intersection()

@classmethod
def intersection(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a new geometry consisting of the region of intersection of this geometry and the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to intersect with.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry representing the shared region.

sym_difference()

@classmethod
def sym_difference(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a new geometry which is the symmetric difference of this geometry and the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The other geometry to compare.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry representing the symmetric difference.

union()

@classmethod
def union(
other: [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)
) - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return a new geometry consisting of the region which is the union of this geometry and the other.

Parameters

NameTypeDescription
other[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)The geometry to combine with.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A new geometry representing the combined region.

centroid()

@classmethod
def centroid() - > [OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)

Return the centroid (a Point) of this Polygon.

Returns

TypeDescription
[OGRGeometry](ogrgeometry.md?sid=django_contrib_gis_gdal_geometries_ogrgeometry)A Point geometry representing the geometric center.