
    tf                     X    d dl Z d dlZd dlZd dlZd dlZd Zd Zd Zd Z	d Z
d Zd	dZy)
    Nc                 Z    t        | ||      \  }}t        | |dz   |dz         \  }}||||fS )zbconvert Google-style Mercator tile coordinate to
    (minlat, maxlat, minlng, maxlng) bounding box   )get_lng_lat_from_tile_pos)zoomxyminlngminlatmaxlngmaxlats          [/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/tiles/geo.pyget_tile_boxr      s?     /tQ:NFF.tQUAEBNFFFFF++    c                     t         j                  dt         j                  z  |z  d| z  z  z
  }t        |      }|dz  t         j                  z  }dd|z  d| z  z  z   }||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lngs         r   r   r      sf    
 ggDGGa1955G7#G
E/DGG
#C 519T	*
*C:r   c                     |t         j                  z  dz  }t        |      }d|z  | dz   z  dz  }d|z  t         j                  |z
  z  dt         j                  z  z  }||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   s         r   get_tile_pos_from_lng_latr   "   sd     DGGme#Gw'G	T	S5[!E)A	T	TWWw&'1tww;7Aq6Mr   c                     dt        j                  t        j                  |             z  t         j                  dz  z
  S Nr   )r   atanexpr   )r   s    r   r   r   0   s+    tyy!%%!33r   c                     t        j                  t        j                  | t         j                  dz  z   dz              S r   )r   logtanr   )r   s    r   r   r   4   s+    88DHHa$''A+o2344r   c           	         t         j                  j                  |       sddiS t        j                  |       }|j                  d      j                         }|d   |d   |d   |d   |d   g|d	   |d
   g|d   d|d   z  z  d}|S )NerrorzTileset info is not available!zSELECT * FROM tileset_infor   r   r               )	zoom_step	tile_sizemax_zoommin_posmax_posmax_data_length)ospathisfilesqlite3connectexecutefetchone)filepathdbresos       r   tileset_infor;   8   s    77>>(#9::		"B
**1
2
;
;
=C VVFFCF#FCF#q6AQK/	A Hr   c                 B   t        j                  |       }|j                         }t        |||      \  }}	}
}	t        |||z   dz
  ||z   dz
        \  }	}}	}d}|j	                  |||||
|f      j                         }t        j                  t              }t        |||z         D ]  }t        |||z         D ]	  }g |||f<     |D ]  }	 |d   j                  d      }	 |d   j                  d      }t        |d   |d   |      \  }}t        |d   |d   |      \  }}	 t        j                  |d	         }	 t        j                  |d         }t        |||z         D ]]  }t        |||z         D ]I  }||dz   k  s||k\  s||dz   k  s||k\  s!|||fxx   |d   |d   |d   |d   |d   ||||d	gz  cc<   K _  |j!                          |S # t        $ r	 |d   }Y w xY w# t        $ r	 |d   }Y w xY w# t        $ r d
}Y w xY w# t        $ r d
}Y w xY w)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'   )	xStartxEndyStartyEnd
importanceuidgeometry
propertiesid)r3   r4   cursorr   r5   fetchallcoldefaultdictlistrangedecodeAttributeErrorr   jsonloads	Exceptionclose)db_filer   r   r   widthheightconnclng_from_lat_fromlng_tolat_toqueryrowsnew_rowsijrrD   rG   x_starty_startx_endy_endrE   rF   s                              r   	get_tilesrh   L   s   0 ??7#DA+D!Q7Ha1'a%i!mQZ!^LAvq&E 99UT8VXvFGPPRDt$H1a%i  "q!f*% 	"A!HaV	""  *	A$++g&C	1W%B 5QqT1Q4F01qtTBu	zz!A$'H
	AaD)J
 q!e)$ 	A1a&j) QU?uzgAo%ST*aV$&'d$%aD&'d$%aD*+A$#&(0*4"$
) $		3*V 	JJLOU  	A$C	  	1B	  	H	  	J	sH   G G*!G?:HG'&G'*G<;G<?HHHH)r   r   )rP   r   r0   r3   collectionsrJ   r   r   r   r   r   r;   rh    r   r   <module>rk      s8      	  , 45(ir   