Debian Codegraph @

django/contrib/gis/db/backends/oracle/adapter.py

raw
1 from django.contrib.gis.db.backends.base.adapter import WKTAdapter
2 from django.contrib.gis.geos import GeometryCollection, Polygon
3 from django.db.backends.oracle.oracledb_any import oracledb
4
5
6 class OracleSpatialAdapter(WKTAdapter):
7 input_size = oracledb.CLOB
8
9 def __init__(self, geom):
10 """
11 Oracle requires that polygon rings are in proper orientation. This
12 affects spatial operations and an invalid orientation may cause
13 failures. Correct orientations are:
14 * Outer ring - counter clockwise
15 * Inner ring(s) - clockwise
16 """
17 if isinstance(geom, Polygon):
18 if self._polygon_must_be_fixed(geom):
19 geom = self._fix_polygon(geom)
20 elif isinstance(geom, GeometryCollection):
21 if any(
22 isinstance(g, Polygon) and self._polygon_must_be_fixed(g) for g in geom
23 ):
24 geom = self._fix_geometry_collection(geom)
25
26 self.wkt = geom.wkt
27 self.srid = geom.srid
28
29 @staticmethod
30 def _polygon_must_be_fixed(poly):
31 return not poly.empty and (
32 not poly.exterior_ring.is_counterclockwise
33 or any(x.is_counterclockwise for x in poly)
34 )
35
36 @classmethod
37 def _fix_polygon(cls, poly, clone=True):
38 """Fix single polygon orientation as described in __init__()."""
39 if clone:
40 poly = poly.clone()
41
42 if not poly.exterior_ring.is_counterclockwise:
43 poly.exterior_ring = list(reversed(poly.exterior_ring))
44
45 for i in range(1, len(poly)):
46 if poly[i].is_counterclockwise:
47 poly[i] = list(reversed(poly[i]))
48
49 return poly
50
51 @classmethod
52 def _fix_geometry_collection(cls, coll):
53 """
54 Fix polygon orientations in geometry collections as described in
55 __init__().
56 """
57 coll = coll.clone()
58 for i, geom in enumerate(coll):
59 if isinstance(geom, Polygon):
60 coll[i] = cls._fix_polygon(geom, clone=False)
61 return coll