U
    d1                     @   s   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ G d	d
 d
eZG dd deZG dd deZdS )ah  
  The Spatial Reference class, represents OGR Spatial Reference objects.

  Example:
  >>> from django.contrib.gis.gdal import SpatialReference
  >>> srs = SpatialReference('WGS84')
  >>> print(srs)
  GEOGCS["WGS 84",
      DATUM["WGS_1984",
          SPHEROID["WGS 84",6378137,298.257223563,
              AUTHORITY["EPSG","7030"]],
          TOWGS84[0,0,0,0,0,0,0],
          AUTHORITY["EPSG","6326"]],
      PRIMEM["Greenwich",0,
          AUTHORITY["EPSG","8901"]],
      UNIT["degree",0.01745329251994328,
          AUTHORITY["EPSG","9122"]],
      AUTHORITY["EPSG","4326"]]
  >>> print(srs.proj)
  +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
  >>> print(srs.ellipsoid)
  (6378137.0, 6356752.3142451793, 298.25722356300003)
  >>> print(srs.projected, srs.geographic)
  False True
  >>> srs.import_epsg(32140)
  >>> print(srs.name)
  NAD83 / Texas South Central
    )byrefc_char_pc_int)IntEnum)GDALBase)SRSException)GDAL_VERSION)srs)force_bytes	force_strc                   @   s   e Zd ZdZdZdS )	AxisOrderr      N)__name__
__module____qualname__TRADITIONAL	AUTHORITY r   r   ?/tmp/pip-unpacked-wheel-7vj2equ7/django/contrib/gis/gdal/srs.pyr   '   s   r   c                   @   s  e Zd ZdZejZdLddZdd Zd	d
 Z	dMddZ
dd Zdd Zdd Zdd Zdd Zdd Z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d0d1 Zed2d3 Zed4d5 Zed6d7 Z d8d9 Z!d:d; Z"d<d= Z#d>d? Z$d@dA Z%edBdC Z&edNdDdEZ'edFdG Z(edHdI Z)edOdJdKZ*dS )PSpatialReferencez
    A wrapper for the OGRSpatialReference object. According to the GDAL web site,
    the SpatialReference object "provide[s] services to represent coordinate
    systems (projections and datums) and to transform between them."
     userNc                 C   s  t |tdtfstd|p"tj| _|dkrttd| _	| 
| | jtjkrntdkrnt| j	| j n"| jtjkrtdk rtd| j dS t |trzt|}d| }W q tk
r   Y qX n2t |trd}n"t || jr|}d	}ntd
| |d	kr|}ntd}t|}|s2td| n|| _	| jtjkrbtdkrbt| j	| j n&| jtjkrtdk rtd| j |dkr| | n|dkr| | dS )a$  
        Create a GDAL OSR Spatial Reference object from the given input.
        The input may be string of OGC Well Known Text (WKT), an integer
        EPSG code, a PROJ string, and/or a projection "well known" shorthand
        string (one of 'WGS84', 'WGS72', 'NAD27', 'NAD83').
        Nz:SpatialReference.axis_order must be an AxisOrder instance.wkt    )   r   z"%s is not supported in GDAL < 3.0.zEPSG:%depsgZogrzInvalid SRS type "%s"z+Could not create spatial reference from: %sr   )
isinstancetyper   
ValueErrorr   
axis_ordercapiZnew_srsr   ptr
import_wktr   Zset_axis_strategystrintZptr_type	TypeErrorr   import_user_inputimport_epsg)selfZ	srs_inputZsrs_typer   sridr	   bufr   r   r   __init__5   sT    






zSpatialReference.__init__c                 C   s"   t |tr| j| S | |S dS )a  
        Return the value of the given string attribute node, None if the node
        doesn't exist.  Can also take a tuple as a parameter, (target, child),
        where child is the index of the attribute in the WKT.  For example:

        >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]'
        >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326
        >>> print(srs['GEOGCS'])
        WGS 84
        >>> print(srs['DATUM'])
        WGS_1984
        >>> print(srs['AUTHORITY'])
        EPSG
        >>> print(srs['AUTHORITY', 1]) # The authority value
        4326
        >>> print(srs['TOWGS84', 4]) # the fourth value in this wkt
        0
        >>> # For the units authority, have to use the pipe symbole.
        >>> print(srs['UNIT|AUTHORITY'])
        EPSG
        >>> print(srs['UNIT|AUTHORITY', 1]) # The authority value for the units
        9122
        N)r   tuple
attr_valuer(   targetr   r   r   __getitem__t   s    

zSpatialReference.__getitem__c                 C   s   | j S )zUse 'pretty' WKT.)
pretty_wktr(   r   r   r   __str__   s    zSpatialReference.__str__r   c                 C   s,   t |trt |tstt| jt||S )z
        The attribute value for the given target node (e.g. 'PROJCS'). The index
        keyword specifies an index of the child node to return.
        )r   r#   r$   r%   r    Zget_attr_valuer!   r
   )r(   r/   indexr   r   r   r-      s    zSpatialReference.attr_valuec                 C   s   t | jt|S )z;Return the authority name for the given string target node.)r    Zget_auth_namer!   r
   r.   r   r   r   	auth_name   s    zSpatialReference.auth_namec                 C   s   t | jt|S )z;Return the authority code for the given string target node.)r    Zget_auth_coder!   r
   r.   r   r   r   	auth_code   s    zSpatialReference.auth_codec                 C   s   t t| j| jdS )z/Return a clone of this SpatialReference object.)r   )r   r    Z	clone_srsr!   r   r2   r   r   r   clone   s    zSpatialReference.clonec                 C   s   t | j dS )z7Morph this SpatialReference from ESRI's format to EPSG.N)r    Zmorph_from_esrir!   r2   r   r   r   	from_esri   s    zSpatialReference.from_esric                 C   s   t | j dS )z
        This method inspects the WKT of this SpatialReference, and will
        add EPSG authority nodes where an EPSG identifier is applicable.
        N)r    identify_epsgr!   r2   r   r   r   r9      s    zSpatialReference.identify_epsgc                 C   s   t | j dS )z-Morph this SpatialReference to ESRI's format.N)r    Zmorph_to_esrir!   r2   r   r   r   to_esri   s    zSpatialReference.to_esric                 C   s   t | j dS )z5Check to see if the given spatial reference is valid.N)r    Zsrs_validater!   r2   r   r   r   validate   s    zSpatialReference.validatec                 C   s8   | j r| dS | jr | dS | jr0| dS dS dS )z*Return the name of this Spatial Reference.ZPROJCSZGEOGCSZLOCAL_CSN)	projectedr-   
geographiclocalr2   r   r   r   name   s    


zSpatialReference.namec              	   C   s2   zt | ddW S  ttfk
r,   Y dS X dS )z=Return the SRID of top-level authority, or None if undefined.r   r   N)r$   r-   r%   r   r2   r   r   r   r)      s    zSpatialReference.sridc                 C   s   t | jtt \}}|S )z$Return the name of the linear units.r    linear_unitsr!   r   r   r(   unitsr?   r   r   r   linear_name   s    zSpatialReference.linear_namec                 C   s   t | jtt \}}|S )z%Return the value of the linear units.r@   rB   r   r   r   rA      s    zSpatialReference.linear_unitsc                 C   s   t | jtt \}}|S )z%Return the name of the angular units.r    angular_unitsr!   r   r   rB   r   r   r   angular_name   s    zSpatialReference.angular_namec                 C   s   t | jtt \}}|S )z&Return the value of the angular units.rE   rB   r   r   r   rF      s    zSpatialReference.angular_unitsc                 C   sd   d\}}| j s| jr.t| jtt \}}n| jrLt| jtt \}}|dk	r\t	|}||fS )z
        Return a 2-tuple of the units value and the units name. Automatically
        determine whether to return the linear or angular units.
        )NNN)
r<   r>   r    rA   r!   r   r   r=   rF   r   rB   r   r   r   rC      s    zSpatialReference.unitsc                 C   s   | j | j| jfS )z
        Return a tuple of the ellipsoid parameters:
         (semimajor axis, semiminor axis, and inverse flattening)
        )
semi_major
semi_minorinverse_flatteningr2   r   r   r   	ellipsoid   s    zSpatialReference.ellipsoidc                 C   s   t | jtt S )z6Return the Semi Major Axis for this Spatial Reference.)r    rH   r!   r   r   r2   r   r   r   rH     s    zSpatialReference.semi_majorc                 C   s   t | jtt S )z6Return the Semi Minor Axis for this Spatial Reference.)r    rI   r!   r   r   r2   r   r   r   rI   	  s    zSpatialReference.semi_minorc                 C   s   t | jtt S )z9Return the Inverse Flattening for this Spatial Reference.)r    Zinvflatteningr!   r   r   r2   r   r   r   rJ     s    z#SpatialReference.inverse_flatteningc                 C   s   t t| jS )zd
        Return True if this SpatialReference is geographic
         (root node is GEOGCS).
        )boolr    Zisgeographicr!   r2   r   r   r   r=     s    zSpatialReference.geographicc                 C   s   t t| jS )zFReturn True if this SpatialReference is local (root node is LOCAL_CS).)rL   r    Zislocalr!   r2   r   r   r   r>     s    zSpatialReference.localc                 C   s   t t| jS )zw
        Return True if this SpatialReference is a projected coordinate system
         (root node is PROJCS).
        )rL   r    Zisprojectedr!   r2   r   r   r   r<   !  s    zSpatialReference.projectedc                 C   s   t | j| dS )z=Import the Spatial Reference from the EPSG code (an integer).N)r    Z	from_epsgr!   )r(   r   r   r   r   r'   *  s    zSpatialReference.import_epsgc                 C   s   t | j| dS )z0Import the Spatial Reference from a PROJ string.N)r    Z	from_projr!   )r(   projr   r   r   import_proj.  s    zSpatialReference.import_projc                 C   s   t | jt| dS )z>Import the Spatial Reference from the given user input string.N)r    Zfrom_user_inputr!   r
   )r(   Z
user_inputr   r   r   r&   2  s    z"SpatialReference.import_user_inputc                 C   s   t | jttt| dS )z2Import the Spatial Reference from OGC WKT (string)N)r    Zfrom_wktr!   r   r   r
   )r(   r   r   r   r   r"   6  s    zSpatialReference.import_wktc                 C   s   t | j| dS )z0Import the Spatial Reference from an XML string.N)r    Zfrom_xmlr!   )r(   xmlr   r   r   
import_xml:  s    zSpatialReference.import_xmlc                 C   s   t | jtt S )z8Return the WKT representation of this Spatial Reference.)r    Zto_wktr!   r   r   r2   r   r   r   r   ?  s    zSpatialReference.wktc                 C   s   t | jtt |S )z.Return the 'pretty' representation of the WKT.)r    Zto_pretty_wktr!   r   r   )r(   Zsimplifyr   r   r   r1   D  s    zSpatialReference.pretty_wktc                 C   s   t | jtt S )z:Return the PROJ representation for this Spatial Reference.)r    Zto_projr!   r   r   r2   r   r   r   rM   I  s    zSpatialReference.projc                 C   s   | j S )zAlias for proj().)rM   r2   r   r   r   proj4N  s    zSpatialReference.proj4c                 C   s   t | jtt t|S )z8Return the XML representation of this Spatial Reference.)r    Zto_xmlr!   r   r   r
   )r(   dialectr   r   r   rO   S  s    zSpatialReference.xml)r   r   N)r   )r   )r   )+r   r   r   __doc__r    Zrelease_srs
destructorr+   r0   r3   r-   r5   r6   r7   r8   r9   r:   r;   propertyr?   r)   rD   rA   rG   rF   rC   rK   rH   rI   rJ   r=   r>   r<   r'   rN   r&   r"   rP   r   r1   rM   rQ   rO   r   r   r   r   r   ,   sp   
?
	
















r   c                   @   s&   e Zd ZdZejZdd Zdd ZdS )CoordTransformz,The coordinate system transformation object.c                 C   sB   t |trt |tstdt|j|j| _|j| _|j| _	dS )z;Initialize on a source and target SpatialReference objects.z2source and target must be of type SpatialReferenceN)
r   r   r%   r    Znew_ctZ_ptrr!   r?   
_srs1_name
_srs2_name)r(   sourcer/   r   r   r   r+   ]  s     zCoordTransform.__init__c                 C   s   d| j | jf S )NzTransform from "%s" to "%s")rW   rX   r2   r   r   r   r3   g  s    zCoordTransform.__str__N)	r   r   r   rS   r    Z
destroy_ctrT   r+   r3   r   r   r   r   rV   Y  s   
rV   N)rS   ctypesr   r   r   enumr   Zdjango.contrib.gis.gdal.baser   Zdjango.contrib.gis.gdal.errorr   Zdjango.contrib.gis.gdal.libgdalr   Z"django.contrib.gis.gdal.prototypesr	   r    Zdjango.utils.encodingr
   r   r   r   rV   r   r   r   r   <module>   s     /