
    (e=                        d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	Z
d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZmZmZ 	 	 	 	 	 	 	 	 	 	 	 dd
ededeee      deeee   f   deee      dededeeeef      deeeef      dedededee   fdZ G d de      Ze j@                  fdZ!e"dk(  r# e jF                   e!e j@                               yy)    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                 `	   d}t        |      }t        |       }|t        d|  d      t        ||      }t	        |      }|j                         }|dd|j                  |j                  f}t        |      \  }}|t        |d      }n|	rd}nt        j                  }|r|t        j                  k(  s|t        j                  k(  r$d|z   t	        |      z  j                  |      dz   }n#d|z   t	        |      z  j                  |      dz   }t!        |d         d	k  r\t!        |d
         d	k  rKt!        |j                  |d   z        d	k  r-t!        |j                  |d   z        d	k  rd|z   dz   |z   dz   }nd|z   dz   |z   dz   }t#        |t$              r|g|z  }n|t'        d |D              }d|v rd}|t)        j*                  ||      }t#        |t$              r|g|z  }|d|v s|d}|t)        j*                  ||      }|xr |du}| xr |du}|xs |}t#        |t,              r|\  }}n|x}}|\  }}}}t	        |      }||z
  |z  }||z
  |z  } || z  }!d}"d}#d}$|	rI|
r|!nd}%t)        j.                  |%      }&t)        j.                  |%      }'t)        j.                  |%|f|      }(d})t1        |||z   |      D ]  }*|
r|nd}%t)        j.                  |%|f|      }+t3        |      D ]<  \  },}-|-j5                  ||*|d      }.|
r	|.d   |+|,<   %t)        j6                  |+|.d      }+> t1        d||      D ],  }/|"dz  }"|r"|"|!z  }0t9        |0|$z        }1|1|#kD  r
|1}# ||0       |+dd|/f   }2|rt)        j:                  ||2      r|rQ|r|}2|/|z   }3|d   |3dz   |d   z  z   |*dz   |d   z  z   }4|d
   |3dz   |d   z  z   |*dz   |d   z  z   }5|r9t=        |2      z  }6t?        |4      t?        |5      |6fz  }7|jA                  |7       |	rW|
r|4&|)<   |5'|)<   |2(|)<   nEt)        j6                  &|4      }&t)        j6                  '|5      }'t)        j6                  (|2gd      }(|)dz  })/  |	r8|rdn|r|n|}8|)|"k7  r&d|) }&'d|) }'(d|)ddf   }(&'(jC                         |8f}|S )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   <   K   | ]  }|j                           y wN)GetNoDataValue).0bands     6/usr/lib/python3/dist-packages/osgeo_utils/gdal2xyz.py	<genexpr>zgdal2xyz.<locals>.<genexpr>   s     BD$--/Bs   )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   sY   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#)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#	.` $*J-!$3I!$3Ia(HIx'9'9';VCM    c                   L     e Zd Z fdZdefdZdef fdZdefdZd Z	 xZ
S )GDAL2XYZc                 d    t         |           d| _        t        j                  d      | _        y )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   zGDAL2XYZ.__init__   s+    ?
#??4
r|   r   c           
         | j                   }|j                  ddddd       |j                  ddd	t        d
d       |j                  dddddt        dd       |j                  ddddd       |j                  dddddd        |j                  d!d"d#d$dd%       |j                  d&d'd(t        d)d*+       |j                  d,d-t        d)d.+       |j                  d/d0t        d12       |j                  d3d4t        d52       |S )6Nz-skipr   
store_truer%   z-How many rows/cols to skip in each iteration.)destactiondefaulthelpz-srcwin)xoffyoffxsizeysizer   r3   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_argumentrL   rI   str)r   argvr   s      r,   
get_parserzGDAL2XYZ.get_parser   s   @ 	 	
 	6e 	 	
 	F 	 	
 	* 	 	
 	 = 	 	
 	[ 	 	
 	V 	 		
 	) 	 
	
 	!M	 	 	
 	!-	 	 	
 r|   c                    d }d }t        |      }g }d}g }g }g }	||k  r||   }
|
dv r~||dz
  k(  rt        d|
 d      |dz  } |||         st        d|
 d	      ||k  rT |||         rH|j                  t        ||                |dz  }||k  r  |||         r4n|
d
v rz||dz
  k(  rt        d|
 d      |dz  } |||         st        d|
 d      ||k  r |||         r|j                  t	        ||                |dz  }||k  r |||         r3n|
dv rz||dz
  k(  rt        d|
 d      |dz  } |||         st        d|
 d      ||k  rU |||         rJ|	j                  t	        ||                |dz  }||k  r# |||         r3n|dz  }|j                  |
       ||k  rt
        t        |   |      }|r||d<   |r||d<   |	r|	|d<   |S )Nc                 :    	 t        |        y# t        $ r Y yw xY wNTF)rI   
ValueErrorss    r,   checkIntz GDAL2XYZ.parse.<locals>.checkInt\  s#    A     	c                 :    	 t        |        y# t        $ r Y yw xY wr   )rL   r   r   s    r,   
checkFloatz"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   )r5   r4   rH   rI   rL   r   r~   parse)r   r   r   r   countnew_argvir   r   r   argkwargsr   s               r,   r   zGDAL2XYZ.parse[  so   		 D		

%iq'C//	>#&A#b$IJJQQ(#&9#>R$STT%iHT!W$5$$Sa\2FA %iHT!W$5 66	>#&A#b$IJJQ!$q'*#&9#>P$QRR%iJtAw$7%%eDGn5FA %iJtAw$7 &	>#&A#b$IJJQ!$q'*#&9#>P$QRR%iJtAw$7%%eDGn5FA %iJtAw$7 Q$? %iB x,X6"+F;#-F< #-F< r|   c                 f    |j                  d      rd |d<   n|j                  d      sd|d<   |d= |S )Nr   r   r%   )getr   r   s     r,   augment_kwargszGDAL2XYZ.augment_kwargs  s;    ::j!"&F;K("#F;:r|   c                     t        di |S )N )r{   r   s     r,   doitzGDAL2XYZ.doit  s    !&!!r|   )__name__
__module____qualname__r   r
   r   dictr   r   r   __classcell__)r   s   @r,   r~   r~      s8    
b"4 bH@T @D "r|   r~   c                 4    t               j                  |       S r(   )r~   main)r   s    r,   r   r     s    :??4  r|   __main__)NNr%   Nr   FNNFT.)$r:   r   numbersr   typingr   r   r   r   numpyrB   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   rI   r   boolr{   r~   r   r   r   exitr   r|   r,   <module>r      sm  @    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CHHT#((^ r|   