
    hsq                     :   S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
JrJrJrJrJrJr  SSKJrJrJrJrJrJrJr  SS	KJrJr  SS
KJrJrJrJ r   SSK!J"r"   " S S\#5      r$ " S S\$5      r% " S S\$5      r& " S S\$5      r' " S S\$5      r( " S S5      r)g)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                       \ rS rSrSrg)LayerMapError&    N__name__
__module____qualname____firstlineno____static_attributes__r       W/var/www/html/env/lib/python3.13/site-packages/django/contrib/gis/utils/layermapping.pyr   r   &       r%   r   c                       \ rS rSrSrg)InvalidString*   r   Nr   r   r%   r&   r)   r)   *   r'   r%   r)   c                       \ rS rSrSrg)InvalidDecimal.   r   Nr   r   r%   r&   r,   r,   .   r'   r%   r,   c                       \ rS rSrSrg)InvalidInteger2   r   Nr   r   r%   r&   r/   r/   2   r'   r%   r/   c                       \ rS rSrSrg)MissingForeignKey6   r   Nr   r   r%   r&   r2   r2   6   r'   r%   r2   c                      \ rS rSrSrS\" S5      S\" S5      S\" S5      \" S	5      R                  \" S
5      \" S5      R                  \" S5      \" S5      R                  \" S5      0r0 \R                  \
_\R                  \_\R                  \
_\R                  \
\\4_\R"                  \
\\4_\R$                  \
\4_\R&                  \_\R*                  \_\R.                  \_\R0                  \_\R4                  \
\4_\R6                  \_\R8                  \_\R:                  \_\R<                  \_\R>                  \_\R@                  \
\\4_\RB                  \
\\4\RD                  \
\\4\RF                  \
\\4\RH                  \
\\40Er%       S S jr&S r'S r(S r)S r*S r+S r,S r-S r.S r/S r0S r1S r2SSSSS\3Rh                  S4S jr5Sr6g)!LayerMapping:   z1A class that maps OGR Layers to GeoDjango Models.   
MultiPoint   MultiLineString   MultiPolygonPoint25DMultiPoint25DLineString25DMultiLineString25D
Polygon25DMultiPolygon25DNc                 4   [        U[        [        45      (       a  [        X&S9U l        OX l        U R                  U   U l        U
b  U
O[        R                  " U5      U l        [        U R                     nUR                  U l        X0l        Xl        U R                  5         UR                  R                   (       a  U R#                  5       U l        OSnU(       a,  U R'                  U5      U l        U R+                  5       U l        OXl        U(       a  SSKJn  U" U5        X`l        OSU l        U	(       a  U R5                  U	5        SnXl        OSU l        Xpl        US:X  a  SU l        gUS:X  a  [<        R>                  U l        g[A        SU-  5      e)	z
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
autocommitcommit_on_successz!Unrecognized transaction mode: %s)!
isinstancestrr   r   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codecsrE   rD   check_uniqueuniquetransaction_modetransaction_decoratorr   atomicr   )selfrQ   datarP   rK   rX   rD   r^   rZ   r]   rM   
connectionrE   s                r&   __init__LayerMapping.__init__`   sT   ( dS$K(( 9DGGWWU^
#/UV5H5H5O
 ,
)~~ 
 	 11!002DNI
 "nnZ8DO!113DN&N  &8$M DMf%+ KDK !1|+)-D&!44)4););D& CFV VWWr%   c                     U(       a@  [        U[        [        45      (       a  [        U6 $ [        U[        5      (       a  U$ [        eg)zCheck the `fid_range` keyword.N)rH   tuplelistslice	TypeError)ra   	fid_ranges     r&   check_fid_rangeLayerMapping.check_fid_range   s<    )eT]33i((Iu--  r%   c           
      0  ^ SU l         0 U l        U R                  R                  mU R                  R                  nU4S jnU R                  R                  5        GHH  u  p4 U R                  R                  R                  U5      nUR                  R                  n[        U[        5      (       a  U R                   (       a  [        S5      eUR                  n US:X  a  [!        US-   5      nO[!        U5      n U R                  R$                  n	U	R&                  R)                  UR&                  5      (       d5  U R+                  X5      (       d  [        SU< US:X  a  S	OS
< SU	< S35      eX0l         Xpl        Un
GO[        U[.        R0                  5      (       av  [        U[2        5      (       aV  UR4                  R                  nUR                  5        H)  u  pU" U5      n UR                  R                  U5        M+     Un
O[7        S5      eUR                  U R8                  ;  a  [        SU-  5      eU" U5      nX   n[;        XR8                  UR                     5      (       d"  [        SU< SUR                  < SU< S35      eUn
XR                  U'   GMK     g! [         a    [        SU-  5      ef = f! ["         a    [        SU-  5      ef = f! [         a)    [        SU< SUR                  R                  < S35      ef = f)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.
Fc                 d   >  TR                  U 5      nU$ ! [         a    [        SU -  5      ef = f)Nz4Given mapping OGR field "%s" not found in OGR Layer.)index
ValueErrorr   )ogr_map_fldidx
ogr_fieldss     r&   check_ogr_fld/LayerMapping.check_layer.<locals>.check_ogr_fld   sE     &&{3
 J	  #J[X s    /z3Given 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".z$Invalid mapping geometry; model has z(dim=3) z, layer geometry type is .zForeignKey mapping field "z	" not in z fields.z.ForeignKey mapping must be of dictionary type.z0Django field type "%s" has no OGR mapping (yet).zOGR field "z" (of type z) cannot be mapped to Django N)
geom_fieldfieldsrK   field_typesrP   itemsrQ   _meta	get_fieldr   r   	__class__r    rH   r   dimr   r	   	geom_typename
startswith
make_multi	coord_dimr   
ForeignKeydictremote_fieldrj   FIELD_TYPES
issubclass)ra   ogr_field_typesru   
field_nameogr_namemodel_fieldfld_namer   gtypeltype
fields_val	rel_modelrel_name	ogr_fieldrs   rt   s                  @r&   rR   LayerMapping.check_layer   s      ZZ&&
**00	 %)LL$6$6$8 J"jj..88D #,,55H+}55??'!  (OO	 A~ +Hu,< = +H 5 

,,JJ))%**55u::' $)q.Yb%H%Q  #-!*(
K):):;;h-- + 8 8 > >I/7~~/?++I6%OO55h? 0@ "+J#$TUU ((0@0@@'JXU 
 $H-+0	 ")-=-=k>S>S-TUU'$i&8&8(D  )
&0KK
#m %9
 % #IJV . % 'AJN >  1 "/#+Y-@-@-I-I!K# s*   #%J*K1K*K"*KK"3Lc                 >   [        U[        5      (       a  UnOr[        XR                  R                  5       5      (       a  UR                  nO=[        U[
        [        45      (       a  [        U5      nOU R                  R                  nU(       d  [        S5      eU$ )z>Check the compatibility of the given spatial reference object.z#No source reference system defined.)	rH   r   rO   spatial_ref_syssrsintrI   rK   r   )ra   rX   srs      r&   rW   LayerMapping.check_srs4  sx     j"233B
$8$8$H$H$JKKB
S#J//!*-B B EFFIr%   c                     [        U[        [        45      (       a  U H  nX R                  ;  d  M  [        e   g[        U[
        5      (       a  XR                  ;  a  [        eg[        S5      e)zDCheck the `unique` keyword parameter -- may be a sequence or string.zBUnique keyword argument must be set with a tuple, list, or string.N)rH   rh   rg   rP   rq   rI   rj   )ra   r]   attrs      r&   r\   LayerMapping.check_uniqueF  sc    ftUm,,||+$$  $$\\)   * T r%   c                    0 nU R                   R                  5        H  u  p4U R                  U   n[        U[        5      (       a   U R                  UR                  U5      nOP[        U[        R                  R                  5      (       a  U R                  XU5      nOU R                  X   U5      nXbU'   M     U$ ! [         a    [        S5      ef = f)zc
Given an OGR Feature, return a dictionary of keyword arguments for
constructing the mapped model.
z)Could not retrieve geometry from feature.)rP   r}   r{   rH   r   verify_geomgeomr	   r   r   base	ModelBase	verify_fkverify_ogr_field)ra   featkwargsr   r   r   vals          r&   feature_kwargsLayerMapping.feature_kwargsW  s      %)LL$6$6$8 J++j1K+}55U**499kBC K)>)>?? nnTA ++DNKH "%:' %9*  % U'(STTUs   B>>Cc                     [        U R                  [        5      (       a  U R                  XR                     0$ U R                   Vs0 s H  o"X   _M	     sn$ s  snf )z
Given the feature keyword arguments (from `feature_kwargs`), construct
and return the uniqueness keyword arguments -- a subset of the feature
kwargs.
)rH   r]   rI   )ra   r   flds      r&   unique_kwargsLayerMapping.unique_kwargsx  sL     dkk3''KK!45504<$<<<s   Ac           	         [        U[        5      (       a  [        U[        R                  [        R                  45      (       a  U R
                  (       a.  UR                  b!  [        UR                  U R
                  5      nOUR                  nUR                  (       aP  UbM  [        U5      UR                  :  a4  [        UR                  < SUR                  < S[        U5      < S35      eU$ [        U[        5      (       a  [        U[        R                  5      (       a   [        [        UR                  5      5      nUR%                  5       nUS   nUS   nUR&                  UR(                  -
  nUS:  a  [        USU 5      n	O[        U5      U-   n	X:  a%  [#        S	UR&                  UR(                  U4-  5      eUnU$ [        U[        [        45      (       a7  [        U[        R*                  5      (       a   [-        UR                  5      nU$ UR                  nU$ ! [          a    [#        SUR                  -  5      ef = f! [.         a    [1        S
UR                  -  5      ef = f)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& model field maximum string length is z, given z characters.z$Could not construct decimal from: %sr7   r9   r   zeA 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)rH   r   r   	CharField	TextFieldrD   valuer   
max_lengthlenr)   r   r   DecimalFieldr   rI   DecimalInvalidOperationr,   as_tuple
max_digitsdecimal_placesIntegerFieldr   rq   r/   )
ra   r   r   r   ddtupdigitsd_idxmax_precn_precs
             r&   r   LayerMapping.verify_ogr_field  sB   
 i++
&**F,<,<=1
 1
 }}!<  	?oo&&OH{555#"'')?)?SK f 
_ 	7++
,,1
 1
C	01 ::<D!WFGE #--0J0JJH qyVFU^,Vu,  $B"--{/I/I8TU 
 C 
 	GY#788Z,,>
 >
)//* 
 //C
S + $:Y__L F  $:Y__L s   (H* I *#I#I3c                 X   0 nUR                  5        H4  u  pVU R                  X   UR                  R                  U5      5      XE'   M6      UR                  R                  U R
                  5      R                  " S0 UD6$ ! [         a    [        SUR                  < SU< 35      ef = f)z|
Given an OGR Feature, the related model and its dictionary mapping,
retrieve the related model for the ForeignKey mapping.
zNo ForeignKey z% model found with keyword arguments: r   )
r}   r   r~   r   objectsrM   getr   r2   r    )ra   r   r   rel_mapping	fk_kwargsr   r   s          r&   r   LayerMapping.verify_fk  s     	$/$5$5$7 J$($9$9	 9 9* E%I! %8	$$**4::6::GYGG! 	#%%y2 	s   4B (B)c                    UR                   (       a  UR                  S5        U R                  S:X  a"  UR                  (       a  UR	                  S5        U R                  UR                  U5      (       a@  U R                  UR                  R                     n[        U5      nUR                  U5        OUnU R                  (       a  UR                  U R                  5        UR                  $ )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).
Fr9   )is_measuredset_measuredr   is_3dset_3dr   r   MULTI_TYPESnumr
   addrZ   wkt)ra   r   r   
multi_typegs        r&   r   LayerMapping.verify_geom  s     e$ >>Q4::KK??4>>;77))$..*<*<=JJ'AEE$KA
 >>KK' uur%   c                 @   U R                   R                  5       n UR                  R                  U R                  5      R	                  U R
                  R                  S9R                  n[        U R                  U5      $ ! [         a  n[        S5      UeSnAff = f)z,Return the coordinate transformation object.)sridz?Could not translate between the data source and model geometry.N)rO   r   r   rM   r   rV   r   r   r   rX   	Exceptionr   )ra   SpatialRefSys
target_srsexcs       r&   rY   LayerMapping.coord_transform  s    ,,<<>	 %%++DJJ7$..--.  "$//:>> 	Q	s   A%B 
BBBc                 d    U R                   R                  nUR                  U R                  5      $ )zHReturn the GeometryField instance associated with the geographic column.)rQ   r~   r   rz   )ra   optss     r&   rU   LayerMapping.geometry_field  s&     zz~~doo..r%   c                     UR                   U R                  ;   =(       a&    UR                  R                  SUR                  -  :H  $ )z
Given the OGRGeomType for a geometry and its associated GeometryField,
determine whether the geometry should be turned into a GeometryCollection.
zMulti%s)r   r   r   r    django)ra   r   r   s      r&   r   LayerMapping.make_multi  s@     MMT--- O%%..)i>N>N2NN	
r%   Fc                   ^ ^^^^^^ T R                  U5      nT(       a  TSL d  [        T[        5      (       d  SmOTmUSS4UUU UUUU4S jjn	T R                  b  T R                  U	5      n	T R                  R
                  n
U(       a  [        U[        5      (       ay  X::  at  U(       a  [        S5      eSu  pn[        X:U5      n[        U5      n[        U5       H6  u  nnUS-   U:X  a  [        US5      nO[        UU5      n U	" UX5      u  pUnM8     gU	" 5         g! [         a    TR                  S	< S
U< S35        e f = f)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  r   c           	      Z  > U (       a  TR                   U    nOTR                   nU GH  nUS-  n TR                  U5      nSnTR                  (       a   TR                  U5      nTR                  R
                  R                  TR                  5      R                  " S0 UD6nSn[        UTR                  5      n	U	c  [        UTR                     5      n
O>U	R                  n
[        UTR                     5      nU H  nU
R                  U5        M     [        UTR                  U
R                  5        OTR                  " S0 UD6n UR!                  TR                  S9  US-  nT(       a"  TR#                  U(       a  SOS< SU< S35        T(       d  GMw  UT-  S:X  d  GM  TR#                  SX4-  5        GM     X!4$ ! [         a    TR                  " S0 UD6n Nf = f! [$         al  nT(       a:  T(       d2  TR#                  S	UR&                  -  5        TR#                  S
U-  5        e T(       d  TR#                  SU< SU< S35         S nANS nAff = f! [(         a>  nT(       a  e T(       d$  TR#                  SUR&                  < SU< S35         S nAGNS nAff = f)Nr7   FT)rM   UpdatedSavedz: 
zOFailed to save the feature (id: %s) into the model with the keyword arguments:
z%s
zFailed to save z:
 z
Continuing
zIgnoring Feature ID z
 because: r   z$Processed %d features, saved %d ...
r   )rK   r   r]   r   rQ   r   rM   r   getattrrz   r
   ogrr   setattrr   r   savewriter   fidr   )
feat_rangenum_feat	num_saved
layer_iterr   r   	is_updateu_kwargsm
geom_valuer   newr   msgprogressprogress_intervalra   silentstreamstrictverboses                 r&   _save LayerMapping.save.<locals>._savec  sQ   !ZZ
3
!ZZ
"A?!006F !&I{{5 (,'9'9&'AH $

 2 2 8 8 D H H T8 TA(,I *1DOO)DJ)1'26$//3J'K'1~~&1&2I&J),A$(HHQK *-#AtA
 !JJ00TZZ0!Q	""LL;Di'.QST U& 8+< < ALL?8BWWM #X &&K  2 5 $

 4V 4A5 % !#) &%JLPHH%U!" !'Vf_ =!!'"LLKQSV Wa % #FJhhPSTsD   I"C#GAG)G&%G&)
I3A"II"
J*,3J%%J*NzOThe `step` keyword may not be used in conjunction with the `fid_range` keyword.)r   r   r   r7   z(=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-z
Failed to save slice: r   )rl   rH   r   r_   rK   r   r   ranger   	enumerateri   r   r   )ra   r   rk   stepr   r   r   r   default_ranger   nfeatbegr   r   indicesn_iiend
step_slicer   s   ``  ````           @r&   r   LayerMapping.save(  sF   d ,,Y7 4z(C'@'@$(!$,!*Q! R	' R	'h %%1..u5E

##JtS))dl#+  (1$C9D.Gg,C#G,3 q5C<!&sD!1J!&sCJ*/
H*P'HC -$ G ! LL<EzR 	s   D!!%E)r   rJ   rD   r{   rV   rz   rK   rP   rQ   rX   rO   r_   r^   rZ   r]   rM   )r   Nzutf-8rG   TNN)7r    r!   r"   r#   __doc__r   r   r   r   	AutoFieldr   BigAutoFieldr   SmallAutoFieldBooleanFieldr   r   r   
FloatField	DateFieldr   DateTimeFieldr   
EmailField	TimeFieldr   r   r   	SlugFieldr   URLField	UUIDFieldBigIntegerFieldSmallIntegerFieldPositiveBigIntegerFieldPositiveIntegerFieldPositiveSmallIntegerFieldr   rd   rl   rR   rW   r\   r   r   r   r   r   rY   rU   r   sysstdoutr   r$   r   r%   r&   r5   r5   :   s   7 	
;|$	;()	;~&J##[%AO$((+6J*KL!%%{3D'EK*\ 	z 	j'9=	
 	j'9= 	J0 	' 	k 	9 	' 	j'2 	) 	) 	) 	  	)!" 	Wi @#$ 	  :w	"B&&Wi(H##j'9%E((:w	*J+K: ,PXf
s1j$"B	=FP2@$/
 zzpr%   r5   )*r  r  decimalr   r   r   pathlibr   django.contrib.gis.db.modelsr   django.contrib.gis.gdalr   r   r	   r
   r   r   django.contrib.gis.gdal.fieldr   r   r   r   r   r   r   django.core.exceptionsr   r   	django.dbr   r   r   r   django.utils.encodingr   r   r   r)   r,   r/   r2   r5   r   r%   r&   <module>r(     s      ?  6    I > > +	I 		M 		] 		] 		 	^
 ^
r%   