o a"@sZdZddlmZddlmZmZmZmZmZm Z m Z dddZ dd Z dd 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 OFTInteger64OFTReal OFTStringOFTTimegeomFcCst|tr t|}n t|trntdi}||jD]}|}|dddkr-|d7}|||<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) isinstancestrr TypeErrorfieldslower geom_typeto_multiupper) data_source geom_name layer_key multi_geom_mappingrmfieldgtyperE/usr/lib/python3/dist-packages/django/contrib/gis/utils/ogrinspect.pymapping s     r cOsdt|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)argskwargsrrr ogrinspect3sDr&NTc #s~t|tr t|}n t|trntd||} | jfdd} | | | | | |} fdd}|rCdVdVdVdVd |Vt| j| j| jD]\}}}}| }|d d d krh|d 7}||}|t ur| | vrd||||fVqRd||dd fVqR|t urd||dd fVqR|t urd||dd fVqR|t urd|||fVqR|turd||dd fVqR|turd||dd fVqR|turd||dd fVqRtd||f| j}|r||j}|d ur$| jd ur d}n| jj}|d urd}n|dkrd}n d|}nd|}d|||fV|r=dVd|Vd Sd S)z Helper routine for `ogrinspect` that generates GeoDjango models corresponding to the given data source. See the `ogrinspect` docstring for more details. r cs2t|ttfrdd|DS|rddDSgS)NcSg|]}|qSrr.0srrr z6_ogrinspect..process_kwarg..cSr'rr(r)rrrr,r-)rlisttuple)kwarg) ogr_fieldsrr process_kwargs z"_ogrinspect..process_kwargcsFg}|vr |d|vr|d|r!dd|SdS)Nz null=Truez blank=Truez, )rappendr") field_namekwlist) blank_fields null_fieldsrrget_kwargs_strs    z#_ogrinspect..get_kwargs_strzF# This is an auto-generated Django model module created by ogrinspect.z(from django.contrib.gis.db import modelsr3zclass %s(models.Model):r Nr rz@ %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)zUnknown field type %s in %szsrid=-1izsrid=%sz %s = models.%s(%s)z% def __str__(self): return self.%s)rrrrrzip field_widthsfield_precisions field_typesrrrrrrrr rrdjangosrssrid)r model_namerrrAr name_fieldimportsdecimalblanknulllayerr2decimal_fieldsr9r5width precision field_typer kwargs_strr geom_fieldsrid_strr)r7r8r1rr#zs             r#)r rF) r rNFNTFFF)__doc__django.contrib.gis.gdalrdjango.contrib.gis.gdal.fieldrrrrrrr r r&r#rrrrs $ &G