o
    s,i                  	   @   s2  d dl mZmZmZmZ d dlmZ d dlmZ d dl	Z	d dl
Z
d dlZd dlZd dlZe Zejdedddd	 d
edefddZdedefddZdededefddZdedefddZdedefddZededfd
efddZeddefd d!Zed"ded#ed$ed%efd&d'ZdS )(    )FastAPI
UploadFileFileHTTPException)FileResponse)StaticFilesNz/staticz$/var/www/html/qgis-automation/static)	directorystatic)nameimagereturnc                 C   sX   t jddd d| j }t|d}t| j| W d    |S 1 s%w   Y  |S )NuploadsTexist_okzuploads/wb)osmakedirsfilenameopenshutilcopyfileobjfile)r   
image_pathf r   %/var/www/html/qgis-automation/main.pysave_uploaded_image   s   
r   r   c                 C   sN   |  ddd d }t|d}|d W d    |S 1 s w   Y  |S )N.   r   z_points.txtwz"0,0,88.0,22.0
3487,4620,88.1,22.1
)rsplitr   write)r   points_pathr   r   r   r   generate_points_file   s   
r#   r"   c                 C   s   t j| std|  ztjd| gdd W n tjy'   td|  w | ddd d	 }d
ddddddddd| |g}tj|dd |S )NzImage not found: gdalinfoTcheckzGDAL cannot read image: r   r   r   z_georef.tifgdal_translatez-ofGTiffz-a_ullrz88.0z22.1z88.1z22.0z-a_srsz	EPSG:4326)	r   pathexistsFileNotFoundError
subprocessrunCalledProcessErrorRuntimeErrorr    )r   r"   geotiff_pathcommandr   r   r   georeference_image   s"   
r2   r0   c              
   C   s   t j| std|  t jd d }t jd|}t j|dd dddd	d
| |g}z	t	j
|dd W n t	jyJ } ztd| d }~ww t |sWtd| |S )NzGeoTIFF not found:    tiles_outputTr   zgdal2tiles.pyz-zz0-5z-wnoner%   zTile generation failed: zNo tiles generated in: )r   r)   r*   r+   uuiduuid4hexjoinr   r,   r-   r.   r/   listdir)r0   tile_id
output_dirr1   er   r   r   generate_tiles4   s(   
r>   tile_dirc           	      C   s   |  d}t |dt j-}t| D ]\}}}|D ]}tj||}tj|| }||| qqW d    |S 1 s=w   Y  |S )N.zipr   )	zipfileZipFileZIP_DEFLATEDr   walkr)   r9   relpathr!   )	r?   zip_pathzipfroot_filesr   	full_patharcnamer   r   r   	zip_tilesN   s   

rM   z/georef/.c              
      sx   zt | }t|}t||}t|}t|}tj|}W n ty2 } zt	dt
|dd }~ww dd| |dS )Ni  status_codedetailzGeoreferencing completez/tiles/download/)message	tiles_zipr;   )r   r#   r2   r>   rM   r   r)   basename	Exceptionr   str)r   r   r"   geotiff	tiles_dirrF   r;   r=   r   r   r   georef_mouza_mapY   s    
rX   z/tiles/download/{tile_id}r;   c                 C   s8   d|  d}t j|stdddt|d|  ddS )Nztiles_output/r@     zZip not foundrN   zapplication/zip)
media_typer   )r   r)   r*   r   r   )r;   rF   r   r   r   download_tilesm   s   r[   z /tiles/{tile_id}/{z}/{x}/{y}.pngzxyc              	   C   sP   d|  d| d| d| d	}t j|s"td|  tdddt|dd	S )
Nz+/var/www/html/qgis-automation/tiles_output//z.pngzMissing tile: rY   zTile not foundrN   z	image/png)rZ   )r   r)   r*   printr   r   )r;   r\   r]   r^   	tile_pathr   r   r   
serve_tilet   s
   rb   )fastapir   r   r   r   fastapi.responsesr   fastapi.staticfilesr   r   r   r,   r6   rA   appmountrU   r   r#   r2   r>   rM   postrX   getr[   intrb   r   r   r   r   <module>   s*     