
    nj=                        S SK r S SKrS SKJr  S SKJrJrJrJr  S SK	r
S SKJr  S SKJr  S SKJrJr  S SKJr  S SKJrJr  S S	KJrJrJr             SS
\S\S\\\      S\\\\   4   S\\\      S\S\S\\\\4      S\\\\4      S\S\S\S\\   4S jjr " S S\5      r\ R@                  4S jr!\"S:X  a#  \ RF                  " \!" \ R@                  5      5        gg)    N)Number)OptionalSequenceTupleUnion)gdal)PathLikeOrStr)GDALArgumentParser
GDALScript))GDALTypeCodeAndNumericTypeCodeFromDataSet)OptionalProgressCallbackget_progress_callback)PathOrDS	get_bandsopen_dssrcfiledstfilesrcwinskip	band_numsdelimskip_nodata
src_nodata
dst_nodatareturn_np_arrayspre_allocate_np_arraysprogress_callbackreturnc                 v
   Sn[        U5      n[        U 5      nUc  [        SU  S35      e[        X5      n[	        U5      nUR                  5       nUc  SSUR                  UR                  4n[        U5      u  nnUb  [        US5      nOU	(       a  SnO[        R                  nU(       a  U[        R                  :X  d  U[        R                  :X  a$  SU-   [	        U5      -  R                  U5      S-   nO#SU-   [	        U5      -  R                  U5      S-   n[!        US   5      S	:  a_  [!        US
   5      S	:  aM  [!        UR                  US   -  5      S	:  a.  [!        UR                  US   -  5      S	:  a  SU-   S-   U-   S-   nOSU-   S-   U-   S-   n[#        U[$        5      (       a  U/U-  nOUc  ['        S U 5       5      nSU;   a  SnUb  [(        R*                  " UUS9n[#        U[$        5      (       a  U/U-  nUb	  SU;   d  Uc  SnUb  [(        R*                  " UUS9nU=(       a    USLnU(       + =(       a    USLnU=(       d    Un[#        U[,        5      (       a  Uu  nnOU=nnUu  nnnn[	        U5      nUU-
  U-  nUU-
  U-  n UU -  n!Sn"Sn#Sn$U	(       aN  U
(       a  U!OSn%[(        R.                  " U%5      n&[(        R.                  " U%5      n'[(        R.                  " U%U4US9n(Sn)[1        UUU-   U5       GH  n*U
(       a  UOSn%[(        R.                  " U%U4US9n+[3        U5       HA  u  n,n-U-R5                  UU*US5      n.U
(       a
  U.S   U+U,'   M+  [(        R6                  " U+U.SS9n+MC     [1        SUU5       GHX  n/U"S-  n"U(       a#  U"U!-  n0[9        U0U$-  5      n1U1U#:  a
  U1n#U" U05        U+SS2U/4   n2U(       a.  [(        R:                  " UU25      (       a  U(       a  Mh  U(       a  Un2U/U-   n3US   U3S-   US   -  -   U*S-   US   -  -   n4US
   U3S-   US   -  -   U*S-   US   -  -   n5U(       a9  W[=        U25      -  n6W[?        U45      [?        U55      U64-  n7URA                  U75        U	(       a\  U
(       a  U4W&U)'   U5W'U)'   U2W(U)'   OE[(        R6                  " W&U45      n&[(        R6                  " W'U55      n'[(        R6                  " W(U2/SS9n(U)S-  n)GM[     GM     U	(       aC  U(       a  SO
U(       a  UOUn8U)U":w  a  W&SU) n&W'SU) n'W(SU)2SS24   n(W&W'W(RC                  5       U84nU$ )a  
translates a raster file (or dataset) into xyz format

skip - how many rows/cols to skip each iteration
srcwin (xoff, yoff, xsize, ysize) - Selects a subwindow from the source image for copying based on pixel/line location.
band_nums - selected input bands to process, None to process all.
delim - the delimiter to use between values in a line
skip_nodata - Exclude the output lines with nodata value (as determined by srcnodata)
src_nodata - The nodata value of the dataset (for skipping or replacing)
    default (`None`) - Use the dataset NoDataValue;
    `Sequence`/`Number` - use the given nodata value (per band or per dataset).
dst_nodata - Replace source nodata with a given nodata. Has an effect only if not setting `-skipnodata`
    default(`None`) - use srcnodata, no replacement;
    `Sequence`/`Number` - replace the `srcnodata` with the given nodata value (per band or per dataset).
srcfile - The source dataset filename or dataset object
dstfile - The output dataset filename; for dstfile=None - if return_np_arrays=False then output will be printed to stdout
return_np_arrays - return numpy arrays of the result, otherwise returns None
pre_allocate_np_arrays - pre-allocated result arrays.
    Should be faster unless skip_nodata and the input is very sparse thus most data points will be skipped.
progress_callback - progress callback function. use None for quiet or Ellipsis for using the default callback
NzCould not open .r   wtz%d
z%g            z%.10gz%sz%.3fc              3   @   #    U  H  oR                  5       v   M     g 7fN)GetNoDataValue).0bands     P/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/osgeo_utils/gdal2xyz.py	<genexpr>gdal2xyz.<locals>.<genexpr>   s     BED--//Es   )dtyped   )axisg      ?      )"r   r   	Exceptionr   lenGetGeoTransformRasterXSizeRasterYSizer   opensysstdoutr   	GDT_Int32
GDT_UInt32rstripabs
isinstancer   listnpasarrayr   emptyrange	enumerateReadAsArrayappendintarray_equaltuplefloatwrite	transpose)9r   r   r   r   r   r   r   r   r   r   r   r   resultdsbands
band_countgtdtnp_dtdst_fhband_formatfrmtreplace_nodataprocess_nodatax_skipy_skipx_offy_offx_sizey_sizebands_countnXBlocksnYBlocksprogress_endprogress_currprogress_prevprogress_partssize	all_geo_x	all_geo_yall_dataidxydatai_bndr+   	band_datax_iprogress_fracprogressx_i_dataxgeo_xgeo_yband_strlinenodatas9                                                            r,   gdal2xyzr|   2   sT   H F-.?@ 
	B	z/'!455b$EUJ				B ~Q79"=IB gt$	2#8 5LCJ6>>uELK 5LCJ6>>uELK 1JBqE
S BNNRU*+c1BNNRU*+c1U?W,u4t;DE>F*U2T9D*f%% \J.
		BEBB
z
ZZ
%8
*f%% \J.

 2
8J
ZZ
%8
::T#9K%oCJd,BN 2NN$!!#) E5&&e*K6)H6)Hh&LMMN5|1HHTN	HHTN	88T:.e< C5%&.&14{!xxve4$U+KE4((61=I%'lUyyyq9 , FF+CQM  - <}~=>m+$,M%m4AsF|H"..X"F"F#)HeAqEQW1--SBqE0AAEqEQW1--SBqE0AAE&x8uU|U5\8DDT")%*IcN%*IcN$,HSM "		)U ;I "		)U ;I!yyH:AFH1HCG , 2` $*J-!$3I!$3Ia(HIx'9'9';VCM    c                   ^   ^  \ rS rSrU 4S jrS\4S jrS\4U 4S jjrS\4S jr	S r
SrU =r$ )	GDAL2XYZ   c                 f   > [         TU ]  5         SU l        [        R                  " S5      U l        g )Nz(Translates a raster file into xyz formata\              The gdal2xyz utility can be used to translate a raster file into xyz format.
            It can be used as an alternative to gdal_translate of=xyz,
            But supporting other options, for example:
            * Select more then one band;
            * Skip or replace nodata value;
            * Return the output as numpy arrays.)super__init__titletextwrapdedentdescription)self	__class__s    r,   r   GDAL2XYZ.__init__   s+    ?
#??4
r}   r   c                    U R                   nUR                  SSSSSS9  UR                  SSS	[        S
SS9  UR                  SSSSS[        SSS9  UR                  SSSSSS9  UR                  SSSSSSS 9  UR                  S!S"S#S$SS%S9  UR                  S&S'S([        S)S*S+9  UR                  S,S-[        S)S.S+9  UR                  S/S0[        S1S29  UR                  S3S4[        S5S29  U$ )6Nz-skipr   
store_truer%   z-How many rows/cols to skip in each iteration.)destactiondefaulthelpz-srcwin)xoffyoffxsizeysizer   r4   zRSelects a subwindow from the source image for copying based on pixel/line location)metavarr   typenargsr   -b-band--bandr   r+   +a  Select bands from the input spectral bands for output. Bands are numbered from 1 in the order spectral bands are specified. Multiple -b switches may be used. When no -b switch is used, the first band will be used.In order to use all input bands set -allbands or -b 0..)r   r   r   r   r   z	-allbandsz
--allbandsallbandszSelect all input bands.)r   r   r   z-csvr   , store_constz*Use comma instead of space as a delimiter.)r   constr   r   r   z-skipnodataz--skipnodataz-skip_nodatar   zHExclude the output lines with nodata value (as determined by srcnodata).
-srcnodata-nodatavaluer   *zThe nodata value of the dataset (for skipping or replacing) Default (None) - Use the dataset nodata value; Sequence/Number - Use the given nodata value (per band or per dataset).)r   r   r   r   
-dstnodatar   zReplace source nodata with a given nodata. Has an effect only if not setting -skipnodata. Default(None) - Use srcnodata, no replacement; Sequence/Number - Replace the srcnodata with the given nodata value (per band or per dataset).r   src_datasetzsThe source dataset name. It can be either file name, URL of data source or subdataset name for multi-dataset files.)r   r   r   r   dst_datasetzThe destination file name.)parseradd_argumentrM   rJ   str)r   argvr   s      r,   
get_parserGDAL2XYZ.get_parser   s   @ 	 	
 	6e 	 	
 	F 	 	
 	* 	 	
 	 = 	 	
 	[ 	 	
 	V 	 		
 	) 	 
	
 	!M	 	 	
 	!-	 	 	
 r}   c                 @  > S nS n[        U5      n/ nSn/ n/ n/ n	Xd:  Ga  X   n
U
S;   a  XdS-
  :X  a  [        SU
 S35      eUS-  nU" X   5      (       d  [        SU
 S	35      eXd:  aF  U" X   5      (       a7  UR                  [        X   5      5        US-  nXd:  a  U" X   5      (       a  M7  GO.U
S
;   a  XdS-
  :X  a  [        SU
 S35      eUS-  nU" X   5      (       d  [        SU
 S35      eXd:  aF  U" X   5      (       a7  UR                  [	        X   5      5        US-  nXd:  a  U" X   5      (       a  M7  OU
S;   a  XdS-
  :X  a  [        SU
 S35      eUS-  nU" X   5      (       d  [        SU
 S35      eXd:  aF  U" X   5      (       a7  U	R                  [	        X   5      5        US-  nXd:  a  U" X   5      (       a  M7  OUS-  nUR                  U
5        Xd:  a  GM  [
        [        U ]  U5      nU(       a  X{S'   U(       a  XS'   U	(       a  XS'   U$ )Nc                 <     [        U 5        g! [         a     gf = fNTF)rJ   
ValueErrorss    r,   checkInt GDAL2XYZ.parse.<locals>.checkInt\  s#    A     
c                 <     [        U 5        g! [         a     gf = fr   )rM   r   r   s    r,   
checkFloat"GDAL2XYZ.parse.<locals>.checkFloatc  s#    a r   r   )r   r   r   r%   zMissing argument following z: zArgument following z should be a integer)r   r   z should be a floatr   r   r   r   )r6   r5   rI   rJ   rM   r   r   parse)r   r   r   r   countnew_argvir   r   r   argkwargsr   s               r,   r   GDAL2XYZ.parse[  sW   		 D		

i'C//	>#&A#b$IJJQ((#&9#>R$STTiHTW$5$5$$S\2FA iHTW$5$5 66	>#&A#b$IJJQ!$'**#&9#>P$QRRiJtw$7$7%%eDGn5FA iJtw$7$7 &	>#&A#b$IJJQ!$'**#&9#>P$QRRiJtw$7$7%%eDGn5FA iJtw$7$7 Q$? iB x,X6"+;#-< #-< r}   c                 z    UR                  S5      (       a  S US'   OUR                  S5      (       d  SUS'   US	 U$ )Nr   r   r%   )getr   r   s     r,   augment_kwargsGDAL2XYZ.augment_kwargs  sA    ::j!!"&F;K(("#F;:r}   c                     [        S0 UD6$ )N )r|   r   s     r,   doitGDAL2XYZ.doit  s    !&!!r}   )r   r   )__name__
__module____qualname____firstlineno__r   r
   r   dictr   r   r   __static_attributes____classcell__)r   s   @r,   r   r      s=    
b"4 bH@T @D " "r}   r   c                 4    [        5       R                  U 5      $ r(   )r   main)r   s    r,   r   r     s    :??4  r}   __main__)NNr%   Nr   FNNFT.)$r;   r   numbersr   typingr   r   r   r   numpyrC   osgeor   osgeo_utils.auxiliary.baser	   #osgeo_utils.auxiliary.gdal_argparser
   r    osgeo_utils.auxiliary.numpy_utilr   osgeo_utils.auxiliary.progressr   r   osgeo_utils.auxiliary.utilr   r   r   rJ   r   boolr|   r   r   r   r   exitr   r}   r,   <module>r      sk  @    3 3   4 N V D C
 "&*&')-4848"#'25ttt Xc]#t Xc]"
#	t
 &t t t x/01t x/01t t !t 0t e_tn}"z }"@ hh ! zHHT#((^ r}   