o
    Nrf#  ã                   @   s@   d dl Zd dlZd dlZdd„ Zdd„ Zddd„Zdd	d
„ZdS )é    Nc                 C   s’   t  | ¡}| ¡ }| d¡ ¡ }|d urt|ƒdkr|d }nd}|d |d |d |d |d	 |d
 |d |d dg|d g|dœ}| ¡  |S )NúSELECT * from tileset_infoé	   é   Ú r   é   é   é   é   é   é   é   )Ú	zoom_stepÚ
max_lengthÚassemblyÚchrom_namesÚchrom_sizesÚ	tile_sizeÚmax_zoomÚ	max_widthÚmin_posÚmax_posÚheader)Úsqlite3ÚconnectÚcursorÚexecuteÚfetchoneÚlenÚclose)Údb_fileÚconnÚcÚrowr   Útileset_info© r$   úY/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/db_tiles.pyÚget_tileset_info   s(   

õr&   c                 C   sv   t  | ¡}| ¡ }| d¡ ¡ }|d |d |d |d |d |d |d |d	 ddg|d |d gd
œ
}| ¡  |S )Nr   r   r   r   r   r	   r
   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r    r!   r"   r#   r$   r$   r%   Úget_2d_tileset_info"   s    
ör'   r   c                 C   sH  t | ƒ}t | ¡}| ¡ }|d d|  }|| }|||  }	d |||	¡}
| |
¡ ¡ }t t	¡}|D ]i}z	|d  
d¡}W n tyL   |d }Y nw |t |d | | ¡ }|d }|d }t||| ƒD ]3}|| }|d | }|}||k rœ||krœ||  |d |d |d |d ||d	  d
¡dœg7  < qiq4| ¡  |S )a™  
    Retrieve a contiguous set of tiles from a db tile file.

    Parameters
    ----------
    db_file: str
        The filename of the sqlite db file
    zoom: int
        The zoom level
    tile_x_pos: int
        The position of the first tile
    num_tiles: int
        The number of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    r   r   zê
    SELECT startPos, endPos, chrOffset, importance, fields, uid
    FROM intervals,position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rEndPos >= {} AND
        rStartPos <= {}
    r
   úutf-8r   r   r   r	   ú	)ÚxStartÚxEndÚ	chrOffsetÚ
importanceÚuidÚfields)r&   r   r   r   Úformatr   ÚfetchallÚcolÚdefaultdictÚlistÚdecodeÚAttributeErrorÚmathÚfloorÚrangeÚsplitr   )r   ÚzoomÚ
tile_x_posZ	num_tilesr#   r    r!   Ú
tile_widthZtile_start_posZtile_end_posÚqueryÚrowsÚnew_rowsÚrr.   Ztile_posÚx_startÚx_endÚiÚtile_x_startÚ
tile_x_endr$   r$   r%   Ú	get_tiles8   sP   
ÿø
ÿúþ€úrG   c                 C   s   t | ƒ}t | ¡}| ¡ }|d d|  }	|	| }
|
||	  }|	| }|||	  }d ||
|||¡}| |¡ ¡ }t t	¡}|D ]‰}z	|d  
d¡}W n tyX   |d }Y nw |d }|d }|d }|d }t||| ƒD ]X}t||| ƒD ]N}||	 }|d |	 }||	 }|d |	 }||k rÇ||krÇ||k rÇ||krÇ|||f  |d |d |d |d |d	 |d
 ||d |d  d¡dœ	g7  < qyqpq@| ¡  |S )a%  
    Retrieve a contiguous set of tiles from a 2D db tile file.

    Parameters
    ----------
    db_file: str
        The filename of the sqlite db file
    zoom: int
        The zoom level
    tile_x_pos: int
        The x position of the first tile
    tile_y_pos: int
        The y position of the first tile
    numx: int
        The width of the block of tiles to retrieve
    numy: int
        The height of the block of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    r   r   a9  
    SELECT
        fromX, toX, fromY, toY, chrOffset, importance, fields, uid, intervals.id
    FROM
        intervals, position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rToX >= {} AND
        rFromX <= {} AND
        rToY >= {} AND
        rFromY <= {}
    r   r(   r   r   r   r	   r
   r   r   r)   )	r*   r+   ZyStartZyEndr,   r-   r.   Úidr/   )r&   r   r   r   r0   r   r1   r2   r3   r4   r5   r6   r9   r:   r   )r   r;   r<   Z
tile_y_posZnumxZnumyr#   r    r!   r=   Ztile_x_start_posZtile_x_end_posZtile_y_start_posZtile_y_end_posr>   r?   r@   rA   r.   rB   rC   Zy_startZy_endrD   ÚjrE   rF   Ztile_y_startZ
tile_y_endr$   r$   r%   Úget_2d_tiles‡   sh   

ÿô
ÿ÷ÿ€òÿrJ   )r   )r   r   )Úcollectionsr2   r7   r   r&   r'   rG   rJ   r$   r$   r$   r%   Ú<module>   s    
O