ó
i4Vdc        
   @   s­   d  Z  d d l m Z d d l m Z m Z m Z m Z m Z m	 Z	 m
 Z
 d d l m Z d d l m Z d d e d  Z d	   Z d d d e d e e e e d
 	 Z d S(   s    
This module is for inspecting OGR data sources and generating either
models for GeoDjango and/or mapping dictionaries for use with the
`LayerMapping` utility.
i’’’’(   t
   DataSource(   t   OFTDatet   OFTDateTimet
   OFTIntegert   OFTInteger64t   OFTRealt	   OFTStringt   OFTTime(   t   six(   t   zipt   geomi    c         C   sŹ   t  |  t j  r! t |   }  n t  |  t  r3 n t d   i  } xH |  | j D]9 } | j   } | d d k r | d 7} n  | | | <qS W|  | j } | r° | j   n  t	 |  j
   | | <| S(   sÜ  
    Given a DataSource, generates a dictionary that may be used
    for invoking the LayerMapping utility.

    Keyword Arguments:
     `geom_name` => The name of the geometry field to use for the model.

     `layer_key` => The key for specifying which layer in the DataSource to use;
       defaults to 0 (the first layer).  May be an integer index or a string
       identifier for the layer.

     `multi_geom` => Boolean (default: False) - specify as multigeometry.
    s>   Data source parameter must be a string or a DataSource object.i’’’’t   _t   field(   t
   isinstanceR   t   string_typesR    t	   TypeErrort   fieldst   lowert	   geom_typet   to_multit   strt   upper(   t   data_sourcet	   geom_namet	   layer_keyt
   multi_geomt   _mappingR   t   mfieldt   gtype(    (    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyt   mapping   s     c          O   s    d j  d   t |  |   D  S(   s¢
  
    Given a data source (either a string or a DataSource object) and a string
    model name this function will generate a GeoDjango model.

    Usage:

    >>> from django.contrib.gis.utils import ogrinspect
    >>> ogrinspect('/path/to/shapefile.shp','NewModel')

    ...will print model definition to stout

    or put this in a python script and use to redirect the output to a new
    model like:

    $ python generate_model.py > myapp/models.py

    # generate_model.py
    from django.contrib.gis.utils import ogrinspect
    shp_file = 'data/mapping_hacks/world_borders.shp'
    model_name = 'WorldBorders'

    print(ogrinspect(shp_file, model_name, multi_geom=True, srid=4326,
                     geom_name='shapes', blank=True))

    Required Arguments
     `datasource` => string or DataSource object to file pointer

     `model name` => string of name of new model class to create

    Optional Keyword Arguments
     `geom_name` => For specifying the model name for the Geometry Field.
       Otherwise will default to `geom`

     `layer_key` => The key for specifying which layer in the DataSource to use;
       defaults to 0 (the first layer).  May be an integer index or a string
       identifier for the layer.

     `srid` => The SRID to use for the Geometry Field.  If it can be determined,
       the SRID of the datasource is used.

     `multi_geom` => Boolean (default: False) - specify as multigeometry.

     `name_field` => String - specifies a field name to return for the
       `__unicode__`/`__str__` function (which will be generated if specified).

     `imports` => Boolean (default: True) - set to False to omit the
       `from django.contrib.gis.db import models` code from the
       autogenerated models thus avoiding duplicated imports when building
       more than one model by batching ogrinspect()

     `decimal` => Boolean or sequence (default: False).  When set to True
       all generated model fields corresponding to the `OFTReal` type will
       be `DecimalField` instead of `FloatField`.  A sequence of specific
       field names to generate as `DecimalField` may also be used.

     `blank` => Boolean or sequence (default: False).  When set to True all
       generated model fields will have `blank=True`.  If the user wants to
       give specific fields to have blank, then a list/tuple of OGR field
       names may be used.

     `null` => Boolean (default: False) - When set to True all generated
       model fields will have `null=True`.  If the user wants to specify
       give specific fields to have null, then a list/tuple of OGR field
       names may be used.

    Note: This routine calls the _ogrinspect() helper to do the heavy lifting.
    s   
c         s   s   |  ] } | Vq d  S(   N(    (   t   .0t   s(    (    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pys	   <genexpr>y   s    (   t   joint   _ogrinspect(   t   argst   kwargs(    (    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyt
   ogrinspect5   s    Dc         #   s'  t  |  t j  r! t |   }  n t  |  t  r3 n t d   |  | } | j   f d   } | |
   | |	    | |  }    f d   } | rÆ d Vd Vd Vn  d | Vxt  | j | j | j	  D]t\ } } } } | j
   } | d d	 k r| d
 7} n  | |  } | t k rj| j
   | k rTd | | | | f VqKd | | d f Vq× | t k rd | | d f Vq× | t k r®d | | d f Vq× | t k rĻd | | | f Vq× | t k rńd | | d f Vq× | t k rd | | d f Vq× | t k r5d | | d f Vq× t d | | f   q× W| j } | rk| j   n  | j } | d k rŪ| j d k rd } qå| j j } | d k r¹d } qå| d k rĪd } qåd | } n
 d | } d | | | f V| r#d Vd t j rd n d | f Vn  d S(   s§   
    Helper routine for `ogrinspect` that generates GeoDjango models corresponding
    to the given data source.  See the `ogrinspect` docstring for more details.
    s>   Data source parameter must be a string or a DataSource object.c            s]   t  |  t t f  r2 g  |  D] } | j   ^ q S|  rU g    D] } | j   ^ q? Sg  Sd  S(   N(   R   t   listt   tupleR   (   t   kwargR   (   t
   ogr_fields(    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyt   process_kwarg   s
    c            si   g  } |  j     k r( | j d  n  |  j      k rJ | j d  n  | ra d d j |  Sd Sd  S(   Ns	   null=Trues
   blank=Trues   , t    (   R   t   appendR    (   t
   field_namet   kwlist(   t   blank_fieldst   null_fields(    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyt   get_kwargs_str   s    sF   # This is an auto-generated Django model module created by ogrinspect.s(   from django.contrib.gis.db import modelsR*   s   class %s(models.Model):i’’’’R   R   s@       %s = models.DecimalField(max_digits=%d, decimal_places=%d%s)s       %s = models.FloatField(%s)i   s        %s = models.IntegerField(%s)s#       %s = models.BigIntegerField(%s)s*       %s = models.CharField(max_length=%s%s)s       %s = models.DateField(%s)s!       %s = models.DateTimeField(%s)s       %s = models.TimeField(%s)s   Unknown field type %s in %ss   srid=-1ię  s   srid=%ss       %s = models.%s(%s)s$       def __%s__(self): return self.%sR   t   unicodeN(   R   R   R   R    R   R   R	   t   field_widthst   field_precisionst   field_typesR   R   R   R   R   R   R   R   R   R   t   djangot   Nonet   srst   sridt   PY3(   R   t
   model_nameR   R   R8   R   t
   name_fieldt   importst   decimalt   blankt   nullt   layerR)   t   decimal_fieldsR0   R,   t   widtht	   precisiont
   field_typeR   t
   kwargs_strR   t
   geom_fieldt   srid_str(    (   R.   R/   R(   sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyR!   |   sx    
		+					
N(   t   __doc__t   django.contrib.gis.gdalR    t   django.contrib.gis.gdal.fieldR   R   R   R   R   R   R   t   django.utilsR   t   django.utils.six.movesR	   t   FalseR   R$   R6   t   TrueR!   (    (    (    sq   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/ogrinspect.pyt   <module>   s   4&	G		