
 X                 @   s\   d  d l  Z  d  d l m Z d  d l m Z d  d l m Z e Gd d   d e   Z d S)    N)gdal)six)python_2_unicode_compatiblec               @   sW  e  Z d  Z d Z e j d  Z e j d e j  Z e	 d d    Z
 e	 d d    Z e	 d d	    Z e	 d
 d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e d d    Z e d  d! d"   Z d# d$   Z d% S)&SpatialRefSysMixinz~
    The SpatialRefSysMixin is a class used by the database-dependent
    SpatialRefSys objects to reduce redundant code.
    zO.+SPHEROID\[\"(?P<name>.+)\",(?P<major>\d+(\.\d+)?),(?P<flattening>\d{3}\.\d+),z>.+UNIT ?\["(?P<unit_name>[\w \.\'\(\)]+)", ?(?P<unit>[^ ,\]]+)c              C   s   t  j r t |  d  r% |  j j   Sy  t  j |  j  |  _ |  j SWn+ t k
 rr } z | } WYd d } ~ Xn Xy  t  j |  j	  |  _ |  j SWn+ t k
 r } z | } WYd d } ~ Xn Xt d |  j | f   n t d   d S)zO
        Returns a GDAL SpatialReference object, if GDAL is installed.
        _srsNz9Could not get OSR SpatialReference from WKT: %s
Error:
%szGDAL is not installed.)
r   HAS_GDALhasattrr   cloneSpatialReferencewktsrs	ExceptionZ	proj4text)selfemsg r   X/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/backends/base/models.pyr      s    	zSpatialRefSysMixin.srsc             C   s^   t  j r |  j j S|  j j |  j  } | rV t | j d   t | j d   f Sd Sd S)z
        Returns a tuple of the ellipsoid parameters:
        (semimajor axis, semiminor axis, and inverse flattening).
        major
flatteningN)	r   r   r   	ellipsoidspheroid_regexmatchr   floatgroup)r   mr   r   r   r   8   s    	
(zSpatialRefSysMixin.ellipsoidc             C   s
   |  j  j S)zReturns the projection name.)r   name)r   r   r   r   r   G   s    zSpatialRefSysMixin.namec             C   s   |  j  d S)z5Returns the spheroid name for this spatial reference.spheroid)r   )r   r   r   r   r   L   s    zSpatialRefSysMixin.spheroidc             C   s   |  j  d S)z-Returns the datum for this spatial reference.datum)r   )r   r   r   r   r   Q   s    zSpatialRefSysMixin.datumc             C   s'   t  j r |  j j S|  j j d  Sd S)z$Is this Spatial Reference projected?ZPROJCSN)r   r   r   	projectedr   
startswith)r   r   r   r   r   V   s    	
zSpatialRefSysMixin.projectedc             C   s'   t  j r |  j j S|  j j d  Sd S)z Is this Spatial Reference local?ZLOCAL_CSN)r   r   r   localr   r   )r   r   r   r   r    ^   s    	
zSpatialRefSysMixin.localc             C   s'   t  j r |  j j S|  j j d  Sd S)z%Is this Spatial Reference geographic?ZGEOGCSN)r   r   r   
geographicr   r   )r   r   r   r   r!   f   s    	
zSpatialRefSysMixin.geographicc             C   sF   t  j r |  j j S|  j r  d S|  j j |  j  } | j d  Sd S)zReturns the linear units name.N	unit_name)	r   r   r   linear_namer!   units_regexr   r   r   )r   r   r   r   r   r#   n   s    	
	zSpatialRefSysMixin.linear_namec             C   sF   t  j r |  j j S|  j r  d S|  j j |  j  } | j d  Sd S)zReturns the linear units.Nunit)	r   r   r   linear_unitsr!   r$   r   r   r   )r   r   r   r   r   r&   y   s    	
	zSpatialRefSysMixin.linear_unitsc             C   sF   t  j r |  j j S|  j r  d S|  j j |  j  } | j d  Sd S)z&Returns the name of the angular units.Nr"   )	r   r   r   angular_namer   r$   r   r   r   )r   r   r   r   r   r'      s    	
	zSpatialRefSysMixin.angular_namec             C   sF   t  j r |  j j S|  j r  d S|  j j |  j  } | j d  Sd S)zReturns the angular units.Nr%   )	r   r   r   angular_unitsr   r$   r   r   r   )r   r   r   r   r   r(      s    	
	z SpatialRefSysMixin.angular_unitsc             C   sC   |  j  s |  j r" |  j |  j f S|  j r; |  j |  j f Sd Sd S)z*Returns a tuple of the units and the name.N)NN)r   r    r&   r#   r!   r(   r'   )r   r   r   r   units   s
    	zSpatialRefSysMixin.unitsc             C   sQ   t  j r t  j |  j S|  j j |  } t | j d   | j d  f Sd S)z
        Return a tuple of (unit_value, unit_name) for the given WKT without
        using any of the database fields.
        r%   r"   N)r   r   r
   r)   r$   r   r   r   )clsr   r   r   r   r   	get_units   s    	zSpatialRefSysMixin.get_unitsTc       	      C   s   t  j r. t  j |  } | j } | d } nX |  j j |  } | r t | j d   t | j d   f } | j d  } n d S| s | | f St |  d k r | d | d } } n | \ } } d	 | | | f Sd S)
z
        Class method used by GeometryField on initialization to
        retrieve the `SPHEROID[..]` parameters from the given WKT.
        r   r   r   r   N   r      zSPHEROID["%s",%s,%s])	r   r   r
   r   r   r   r   r   len)	r*   r   stringr   Zsphere_paramsZsphere_namer   Zradiusr   r   r   r   get_spheroid   s    		*
zSpatialRefSysMixin.get_spheroidc             C   s=   y t  j |  j  SWn" t k
 r8 t  j |  j  SYn Xd S)zp
        Returns the string representation.  If GDAL is installed,
        it will be 'pretty' OGC WKT.
        N)r   	text_typer   r   r   )r   r   r   r   __str__   s    zSpatialRefSysMixin.__str__N)__name__
__module____qualname____doc__recompiler   DOTALLr$   propertyr   r   r   r   r   r   r    r!   r#   r&   r'   r(   r)   classmethodr+   r0   r2   r   r   r   r   r      s(   
r   )	r7   Zdjango.contrib.gisr   Zdjango.utilsr   Zdjango.utils.encodingr   objectr   r   r   r   r   <module>   s
   