
v^P                 @   sP   d  d l  m Z d  d l m Z d  d l m Z m Z Gd d   d e  Z d S)    )CLOB)
WKTAdapter)GeometryCollectionPolygonc               @   sF   e  Z d  Z e Z d d   Z d d   Z d d   Z d d   Z d	 S)
OracleSpatialAdapterc             C   sW   t  | t  r |  j |  n t  | t  r; |  j |  | j |  _ | j |  _ d S)a  
        Oracle requires that polygon rings are in proper orientation. This
        affects spatial operations and an invalid orientation may cause
        failures. Correct orientations are:
         * Outer ring - counter clockwise
         * Inner ring(s) - clockwise
        N)
isinstancer   _fix_polygonr   _fix_geometry_collectionZwktZsrid)selfgeom r   O/tmp/pip-build-8lau8j11/django/django/contrib/gis/db/backends/oracle/adapter.py__init__
   s    zOracleSpatialAdapter.__init__c             C   s{   |  j  | j  r* t t | j   | _ xJ t d t |   D]3 } |  j  | |  s@ t t | |   | | <q@ W| S)z:Fix single polygon orientation as described in __init__().   )_isClockwiseZexterior_ringlistreversedrangelen)r
   polyir   r   r   r      s    z!OracleSpatialAdapter._fix_polygonc             C   sC   x< t  |  D]. \ } } t | t  r |  j |  | | <q Wd S)zf
        Fix polygon orientations in geometry collections as described in
        __init__().
        N)	enumerater   r   r   )r
   Zcollr   r   r   r   r   r	   %   s    z-OracleSpatialAdapter._fix_geometry_collectionc             C   s}   t  |  } d } x^ t |  D]P } | d | } | | | d | | d 7} | | | d | | d 8} q W| d k  S)z
        A modified shoelace algorithm to determine polygon orientation.
        See https://en.wikipedia.org/wiki/Shoelace_formula.
        g        r   r   )r   r   )r
   Zcoordsnarear   jr   r   r   r   .   s    "z!OracleSpatialAdapter._isClockwiseN)	__name__
__module____qualname__r   Z
input_sizer   r   r	   r   r   r   r   r   r      s
   	r   N)Z	cx_Oracler   Z+django.contrib.gis.db.backends.base.adapterr   Zdjango.contrib.gis.geosr   r   r   r   r   r   r   <module>   s   