o
    Nrf                     @   sf   d dl Z d dlZd dlZd dlZd dlZdd Zdd Zdd Zdd	 Z	d
d Z
dd ZdddZdS )    Nc                 C   s4   t | ||\}}t | |d |d \}}||||fS )zbconvert Google-style Mercator tile coordinate to
    (minlat, maxlat, minlng, maxlng) bounding box   )get_lng_lat_from_tile_pos)zoomxyZminlngZminlatZmaxlngZmaxlat r   Z/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/geo.pyget_tile_box   s   r	   c                 C   sN   t jdt j | d|    }t|}|d t j }dd| d|    }||fS )zZconvert Google-style Mercator tile coordinate to
    (lng, lat) of top-left corner of tile        f@g     f     v@)mathpigudermannian)r   r   r   lat_radlatlngr   r   r   r      s
   r   c                 C   sN   |t j d }t|}d| | d  d }d| t j|  dt j  }||fS )z[convert lng/lat to Google-style Mercator tile coordinate (x, y)
    at the given zoom levelr   r
   r   )r   r   inv_gudermannian)r   r   r   r   r   r   r   r   r   get_tile_pos_from_lng_lat"   s
   r   c                 C   s   dt t |  t jd  S Nr
   )r   atanexpr   )r   r   r   r   r   0      r   c                 C   s   t t | t jd  d S r   )r   logtanr   )r   r   r   r   r   4   r   r   c              	   C   sv   t j| s
ddiS t| }|d }|d |d |d |d |d g|d	 |d
 g|d d|d   d}|S )NerrorzTileset info is not available!zSELECT * FROM tileset_infor   r   r
               )Z	zoom_stepZ	tile_sizeZmax_zoomZmin_posZmax_posZmax_data_length)ospathisfilesqlite3connectexecuteZfetchone)filepathdbresor   r   r   tileset_info8   s   
	r*   r   c                 C   s2  t | }| }t|||\}}	}
}	t||| d || d \}	}}	}d}||||||
|f }tt}t	||| D ]}t	||| D ]}g |||f< qIq@|D ]}z	|d 
d}W n tym   |d }Y nw z	|d 
d}W n ty   |d }Y nw t|d |d |\}}t|d |d |\}}z	t|d	 }W n ty   d
}Y nw z	t|d }W n ty   d
}Y nw t	||| D ]C}t	||| D ]9}||d k r||kr||d k r||kr|||f  |d |d |d |d |d ||||d	g7  < qqqU|  |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
    x: int
        The x position of the first tile
    y: int
        The y position of the first tile
    width: int
        The width of the block of tiles to retrieve
    height: int
        The height of the block of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    r   aO  
    SELECT
        minLng, maxLng, maxLat, minLat, uid, importance, geometry, properties,
        intervals.id
    FROM
        intervals, position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= ? AND
        rMaxLng >= ? AND
        rMinLng <= ? AND
        rMinLat <= ? AND
        rMaxLat >= ?
    r   zutf-8   r   r
   r   r   N   r   )	ZxStartZxEndZyStartZyEndZ
importanceuidgeometry
propertiesid)r#   r$   cursorr	   r%   ZfetchallcoldefaultdictlistrangedecodeAttributeErrorr   jsonloads	Exceptionclose)Zdb_filer   r   r   widthheightconncZlng_from_Zlat_fromZlng_toZlat_toqueryrowsZnew_rowsijrr-   r0   Zx_startZy_startZx_endZy_endr.   r/   r   r   r   	get_tilesL   sp   
$
0rF   )r   r   )r8   r   r    r#   collectionsr2   r	   r   r   r   r   r*   rF   r   r   r   r   <module>   s    
