3
(h#              	   @   sl   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d	Zd
d ZdddZdS )z
This module is for inspecting OGR data sources and generating either
models for GeoDjango and/or mapping dictionaries for use with the
`LayerMapping` utility.
    )
DataSource)OFTDateOFTDateTime
OFTIntegerOFTInteger64OFTReal	OFTStringOFTTime)six)zipgeomFc             C   s   t | tjrt| } nt | tr"ntdi }x:| | jD ],}|j }|dd dkr^|d7 }|||< q:W | | j}|r|j  t	|j
 ||< |S )a  
    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.
    z>Data source parameter must be a string or a DataSource object.   N_field)
isinstancer
   string_typesr   	TypeErrorfieldslower	geom_typeto_multistrupper)data_source	geom_name	layer_key
multi_geom_mappingr   mfieldgtype r!   G/tmp/pip-install-q3hcpn_q/Django/django/contrib/gis/utils/ogrinspect.pymapping   s     


r#   c              O   s   dj dd t| |D S )a
  
    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.
    
c             s   s   | ]
}|V  qd S )Nr!   ).0sr!   r!   r"   	<genexpr>y   s    zogrinspect.<locals>.<genexpr>)join_ogrinspect)argskwargsr!   r!   r"   
ogrinspect5   s    Dr,   NTc             #   s  t | tjrt| } nt | tr"ntd| | }|jfdd}||
||	 ||} fdd}|rdV  dV  dV  d	| V  xht|j|j|j	D ]N\}}}}|j
 }|dd dkr|d7 }||}|tkr|j
 |krd||||f V  nd||dd f V  q|tkr>d||dd f V  q|tkr`d||dd f V  q|tkr|d|||f V  q|tkrd||dd f V  q|tkrd||dd f V  q|tkrd||dd f V  qtd||f qW |j}|r
|j  |j}|dkr^|jdkr,d}n0|jj}|dkrDd}n|dkrTd}nd| }nd| }d|||f V  |rdV  dtjrdnd|f V  dS ) z
    Helper routine for `ogrinspect` that generates GeoDjango models corresponding
    to the given data source.  See the `ogrinspect` docstring for more details.
    z>Data source parameter must be a string or a DataSource object.c                s6   t | ttfrdd | D S | r.dd  D S g S d S )Nc             S   s   g | ]}|j  qS r!   )r   )r%   r&   r!   r!   r"   
<listcomp>   s    z6_ogrinspect.<locals>.process_kwarg.<locals>.<listcomp>c             S   s   g | ]}|j  qS r!   )r   )r%   r&   r!   r!   r"   r-      s    )r   listtuple)kwarg)
ogr_fieldsr!   r"   process_kwarg   s
    z"_ogrinspect.<locals>.process_kwargc                sJ   g }| j  kr|jd | j   kr0|jd |rBddj| S dS d S )Nz	null=Truez
blank=Truez,  )r   appendr(   )
field_namekwlist)blank_fieldsnull_fieldsr!   r"   get_kwargs_str   s    

z#_ogrinspect.<locals>.get_kwargs_strzF# This is an auto-generated Django model module created by ogrinspect.z(from django.contrib.gis.db import modelsr3   zclass %s(models.Model):r   Nr   r   z@    %s = models.DecimalField(max_digits=%d, decimal_places=%d%s)z    %s = models.FloatField(%s)   z     %s = models.IntegerField(%s)z#    %s = models.BigIntegerField(%s)z*    %s = models.CharField(max_length=%s%s)z    %s = models.DateField(%s)z!    %s = models.DateTimeField(%s)z    %s = models.TimeField(%s)zUnknown field type %s in %szsrid=-1i  zsrid=%sz    %s = models.%s(%s)z$    def __%s__(self): return self.%sr   unicoder   )r   r
   r   r   r   r   r   Zfield_widthsZfield_precisionsZfield_typesr   r   r   r   r   r   r   r	   r   r   ZdjangoZsrssridPY3)r   Z
model_namer   r   r<   r   Z
name_fieldZimportsdecimalZblanknullZlayerr2   Zdecimal_fieldsr9   r5   widthZ	precisionZ
field_typer   Z
kwargs_strr    Z
geom_fieldZsrid_strr!   )r7   r8   r1   r"   r)   |   sx    


 










r)   )r   r   F)	r   r   NFNTFFF)__doc__Zdjango.contrib.gis.gdalr   Zdjango.contrib.gis.gdal.fieldr   r   r   r   r   r   r	   Zdjango.utilsr
   Zdjango.utils.six.movesr   r#   r,   r)   r!   r!   r!   r"   <module>   s   $
&G  