o
    þÞhÒ#  ã                   @   sf   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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ÚgeomFc                 C   sŽ   t | tƒr
t| ƒ} n
t | tƒrntdƒ‚i }| | jD ]}| ¡ }|dd… dkr-|d7 }|||< q| | j}|r=| ¡  t|ƒ ¡ ||< |S )aÛ  
    Given a DataSource, generate 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.
    ú>Data source parameter must be a string or a DataSource object.éÿÿÿÿNÚ_Úfield)	Ú
isinstanceÚstrr   Ú	TypeErrorÚfieldsÚlowerÚ	geom_typeÚto_multiÚupper)Údata_sourceÚ	geom_nameÚ	layer_keyÚ
multi_geomÚ_mappingr   ÚmfieldÚgtype© r   úc/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/gis/utils/ogrinspect.pyÚmapping   s$   


ÿ

r    c                  O   s   d  t| i |¤Ž¡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
       __str__() method (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: Call the _ogrinspect() helper to do the heavy lifting.
    Ú
)ÚjoinÚ_ogrinspect)ÚargsÚkwargsr   r   r   Ú
ogrinspect;   s   Dr&   NTc                 #   s~  � t | tƒrt| ƒ} n
t | tƒrntdƒ‚| | }|j‰‡fdd„}||
ƒ‰||	ƒ‰ ||ƒ}‡ ‡fdd„}|rCdV  dV  dV  dV  d	| V  tˆ|j|j|jƒD ]ž\}}}}| 	¡ }|d
d… dkrh|d7 }||ƒ}|t
u rŒ| 	¡ |v r€d||||f V  qRd||dd… f V  qR|tu rœd||dd… f V  qR|tu r¬d||dd… f V  qR|tu r¹d|||f V  qR|tu rÉd||dd… f V  qR|tu rÙd||dd… f V  qR|tu réd||dd… f V  qRtd||f ƒ‚|j}|rú| ¡  |j}|du �r$|jdu �rd}n|jj}|du �rd}n|dk�rd}n	d| }nd| }d|||f V  |�r=dV  d| V  dS dS )z§
    Helper routine for `ogrinspect` that generates GeoDjango models corresponding
    to the given data source.  See the `ogrinspect` docstring for more details.
    r   c                    s2   t | ttfƒrdd„ | D ƒS | rdd„ ˆ D ƒS g S )Nc                 S   ó   g | ]}|  ¡ ‘qS r   ©r   ©Ú.0Úsr   r   r   Ú
<listcomp>¦   ó    z6_ogrinspect.<locals>.process_kwarg.<locals>.<listcomp>c                 S   r'   r   r(   r)   r   r   r   r,   ¨   r-   )r   ÚlistÚtuple)Úkwarg)Ú
ogr_fieldsr   r   Úprocess_kwarg¤   s
   z"_ogrinspect.<locals>.process_kwargc                    sF   g }|   ¡ ˆv r| d¡ |   ¡ ˆ v r| d¡ |r!d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 __str__(self): return self.%s)r   r   r   r   r   ÚzipÚfield_widthsÚfield_precisionsÚfield_typesr   r   r   r   r   r   r   r	   r   r   ÚdjangoÚsrsÚsrid)r   Ú
model_namer   r   rA   r   Ú
name_fieldÚimportsÚdecimalÚblankÚnullÚlayerr2   Údecimal_fieldsr9   r5   ÚwidthÚ	precisionÚ
field_typer   Ú
kwargs_strr   Ú
geom_fieldÚsrid_strr   )r7   r8   r1   r   r#   ‚   s”   €


ÿ
ÿüþ	
ý



þr#   )r
   r   F)	r
   r   NFNTFFF)Ú__doc__Údjango.contrib.gis.gdalr   Údjango.contrib.gis.gdal.fieldr   r   r   r   r   r   r	   r    r&   r#   r   r   r   r   Ú<module>   s    $
(Jõ