
    ~iMC                        d dl mZ d dlmZ d dlmZmZ d dlmc m	Z
 d dlZd dlZd Zd Zd Zd Z	 	 	 	 	 	 	 	 	 dd	Zdd
Zddddddddi i ddddddddddddddfdZ ee eddgg dg dddgddg                    e_        d ZdS )    )build_dataframe)make_docstring)choropleth_mapscatter_mapNc                     |t           j        z  dz  }t          j        t          j        | t           j        z  dz                      }||fS )zU
    Projects lat and lon to WGS84, used to get regular hexagons on a mapbox map
       )nppiarctanhsin)latlonxys       U/home/jrussi/.local/lib/python3.11/site-packages/plotly/figure_factory/_hexbin_map.py_project_latlon_to_wgs84r   	   sA     	becA

26#++,,--Aa4K    c                     | dz  t           j        z  }dt          j        t          j        |                    z  t           j        dz  z
  dz  t           j        z  }||fS )zU
    Projects WGS84 to lat and lon, used to get regular hexagons on a mapbox map
    r      )r	   r
   arctanexp)r   r   r   r   s       r   _project_wgs84_to_latlonr      sO     c'BE/Cry###beai/3
6
>C8Or   c                 $   d}d|z  d|z  d}d}d }d }	 ||           ||          z
  t           j        z  }
|| z
  }|dk     r|dz   n|dz  } |	|d	         |d	         |
          } |	|d
         |d
         |          }t          |||          S )z
    Get the mapbox zoom level given bounds and a figure dimension
    Source: https://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calculate-the-zoom-level-for-a-given-bounds
    r      heightwidth   c                     t          j        | t           j        z  dz            }t          j        d|z   d|z
  z            dz  }t	          t          |t           j                  t           j                   dz  S )Nr      r   )r	   r   r
   logmaxmin)r   r   radX2s      r   latRadz#_getBoundsZoomLevel.<locals>.latRad'   sb    fS25[3&''CAG,--13ube$$ruf--11r   c                 f    dt          j        | |z  |z            z  t          j        d          z  S )Ngffffff?r   )r	   r!   )mapPxworldPxfractions      r   zoomz!_getBoundsZoomLevel.<locals>.zoom,   s-    bfUW_x788826!99DDr   r   ih  r   r   )r	   r
   r#   )lon_minlon_maxlat_minlat_maxmapDimscale	WORLD_DIMZOOM_MAXr%   r*   latFractionlngDifflngFractionlatZoomlngZooms                  r   _getBoundsZoomLevelr8      s     	
 
 ;u==IH2 2 2
E E E 6'??VVG__4=KG&-kkGcMMw#EKd6(#Yx%8+FFGd6'?Ig$6DDGw***r   c           
      |   |                                 }|                                }	|                                 }
|                                }d|	|z
  z  }||z  }|	|z  }	|	|z
  }||
z
  }|dk    r|dk    r||z  }n%|dk    r|dk    rt          dd          \  }}n||z  }|t          j        d          z  }t          j        ||z                                t                    }|
|
||z  z   |z
  dz  z  }
| |z
  |z  } ||
z
  |z  }t          j        |                               t                    }t          j        |                              t                    }t          j	        |                               t                    }t          j	        |                              t                    }|dz   }|dz   }|}|}||z  ||z  z   }| |z
  dz  d||z
  dz  z  z   }| |z
  dz
  dz  d||z
  dz
  dz  z  z   }||k     }|@t          j
        ||f          }t          j
        ||f          } d|k    ||k     z  d|k    z  ||k     z  |z  }!d|k    ||k     z  d|k    z  ||k     z  | z  }"t          j                            |||!         ||!         fd           t          j                            | ||"         ||"         fd           |&t          j        |||k     <   t          j        | | |k     <   t          j        |                                |                                 g          }#t          j        |#           }$n|d}t          j        ||ft$          	          }t'          |          D ]}%t'          |          D ]	}&g ||%|&f<   
t          j        ||ft$          	          } t'          |          D ]}%t'          |          D ]	}&g | |%|&f<   
t'          t)          |                     D ]}%||%         r\d||%         cxk    r|k     rHn  d||%         cxk    r|k     r2n 6|||%         ||%         f                             ||%                    fd||%         cxk    r|k     rHn |d||%         cxk    r|k     r2n | ||%         ||%         f                             ||%                    t'          |          D ]S}%t'          |          D ]A}&||%|&f         }'t)          |'          |k    r ||'          ||%|&f<   0t          j        ||%|&f<   BTt'          |          D ]S}%t'          |          D ]A}&| |%|&f         }'t)          |'          |k    r ||'          | |%|&f<   0t          j        | |%|&f<   BTt          j        |                    t.                                                    |                     t.                                                    f          }#t          j        |#           }$|#|$         }(t          j
        |dft.                    })t          j        t          j        |          |          |)d||z  df<   t          j        t          j        |          |          |)d||z  df<   t          j        t          j        |          dz   |          |)||z  ddf<   t          j        t          j        |          |          dz   |)||z  ddf<   |)dddfxx         |z  cc<   |)dddfxx         |z  cc<   |)dddfxx         |z  cc<   |)dddfxx         |
z  cc<   |)|$         })g d
}*dt          j        t          j        dz            z  dt          j        t          j        dz            z  dt          j        t          j        dz            z  dt          j        t          j        dz            z  dt          j        t          j        dz            z  dt          j        t          j        dz            z  g}+t)          |)          },t          j        |*g|,z            |z  t          j        |)dddf                   z   }-t          j        |+g|,z            |z  t          j        d          z  t          j        |)dddf                   z   }.|-|.|)|(fS )aQ  
    Computes the aggregation at hexagonal bin level.
    Also defines the coordinates of the hexagons for plotting.
    The binning is inspired by matplotlib's implementation.

    Parameters
    ----------
    x : np.ndarray
        Array of x values (shape N)
    y : np.ndarray
        Array of y values (shape N)
    x_range : np.ndarray
        Min and max x (shape 2)
    y_range : np.ndarray
        Min and max y (shape 2)
    color : np.ndarray
        Metric to aggregate at hexagon level (shape N)
    nx : int
        Number of hexagons horizontally
    agg_func : function
        Numpy compatible aggregator, this function must take a one-dimensional
        np.ndarray as input and output a scalar
    min_count : int
        Minimum number of points in the hexagon for the hexagon to be displayed

    Returns
    -------
    np.ndarray
        X coordinates of each hexagon (shape M x 6)
    np.ndarray
        Y coordinates of each hexagon (shape M x 6)
    np.ndarray
        Centers of the hexagons (shape M x 2)
    np.ndarray
        Aggregated value in each hexagon (shape M)

    g&.>r   r       r   g      @      ?N)dtype)r   r;   r;   r         r=   r=      ) r#   r"   r   r	   sqrtceilastypeintroundfloorzerosaddatnanconcatenateravelisnanemptyobjectrangelenappendhstackfloatrepeatarangetilecosr
   tanarrayvstack)/r   r   x_rangey_rangecolornxagg_func	min_countxminxmaxyminymaxpaddingDxDydx_dynyix1iy1ix2iy2nx1ny1nx2ny2nd1d2bdistlattice1lattice2c1c2accum	good_idxsijvalsagreggated_valuecentershxhymhxshyss/                                                  r   _compute_hexbinr   :   s   L ;;==D;;==D;;==D;;==D t$GGODGOD	B	B	Qww266"W	qR1WW(A..AA"W	bgajjB	b			 	 	%	%B 	TBG^d"a''D	
TRA	
TRA
(1++

S
!
!C
(1++

S
!
!C
(1++

S
!
!C
(1++

S
!
!C
q&C
q&C
C
Cc	C#IA
c'a#SQ.	.B
c'C-A	q3w}&: :	:BGE}8S#J''8S#J''3h39%c2cCi@5H3h39%c2cCi@E6I
		(SWc"g.222
		(SWc"g.222 -/VHX	)*-/VHX	)* 0 0(..2B2BCDDXe__$		I 8S#Jf555s 	$ 	$A3ZZ $ $!#A$8S#Jf555s 	$ 	$A3ZZ $ $!#A$ s1vv 	> 	>AQx >A$$$$$$$$$c!f):):):):s):):):):):SVSV^,33E!H===A$$$$$$$$$c!f):):):):s):):):):):SVSV^,33E!H===s 	, 	,A3ZZ , ,1~t99	))%-Xd^^HQTNN%'VHQTNN, s 	, 	,A3ZZ , ,1~t99	))%-Xd^^HQTNN%'VHQTNN, 	__U##))++X__U-C-C-I-I-K-KL
 
 Xe__$	Y'h1vu%%G i	#<<GKcCiKN gbinnc::GKcCiKN i	#(<cBBGC#IKKN gbinnc::S@GC#IKKNAAAqDMMMRMMMAAAqDMMMRMMMAAAqDMMMTMMMAAAqDMMMTMMMi G 
&	%	%Brvbeai   rvbeai   bfRUQYbfRUQYbfRUQYrvbeai   
B 	GA (B4!8

r
!BIgaaadm$<$<
<C
(B4!8

r
!BGAJJ
.7111a4=1I1I
ICW...r   c	           
         t          | |          \  }	}
|:t          j        |                                 |                                 g          }|:t          j        |                                |                                g          }t          ||          \  }}t          |	|
||||||          \  }}}}t          ||          \  }}|                    t                    }t          j
        |dddf         |dddf         d|                              t          j        t          j        d          t          j        d          gd	          
                              d          }||||fS )a  
    Computes the lat-lon aggregation at hexagonal bin level.
    Latitude and longitude need to be projected to WGS84 before aggregating
    in order to display regular hexagons on the map.

    Parameters
    ----------
    lat : np.ndarray
        Array of latitudes (shape N)
    lon : np.ndarray
        Array of longitudes (shape N)
    lat_range : np.ndarray
        Min and max latitudes (shape 2)
    lon_range : np.ndarray
        Min and max longitudes (shape 2)
    color : np.ndarray
        Metric to aggregate at hexagon level (shape N)
    nx : int
        Number of hexagons horizontally
    agg_func : function
        Numpy compatible aggregator, this function must take a one-dimensional
        np.ndarray as input and output a scalar
    min_count : int
        Minimum number of points in the hexagon for the hexagon to be displayed

    Returns
    -------
    np.ndarray
        Lat coordinates of each hexagon (shape M x 6)
    np.ndarray
        Lon coordinates of each hexagon (shape M x 6)
    nw.Series
        Unique id for each hexagon, to be used in the geojson data (shape M)
    np.ndarray
        Aggregated value in each hexagon (shape M)

    Nr   r    )x1x2native_namespacer   r   ,)	separator)hexagons_idsr   )r   r	   rX   r#   r"   r   r   rA   strnw	from_dictselect
concat_strcol
get_column)r   r   	lat_range	lon_ranger\   r]   r^   r_   r   r   r   rZ   r[   r   r   r   r   hexagons_latshexagons_lonsr   s                       r   _compute_wgs84_hexbinr      sg   b $C--DAqHcggii344	Hcggii344	/	9EEGW*9	1gwr8Y+ +'Cg'
 $<C#E#E M= nnS!!G
111a4=166-	
 	
 	
 
R]BF4LL"&,,+GSVWWW	X	X	N	#	#  -7GGGr   c                    g }|!t          j        t          |                     }t          | ||          D ]\  }}}t          j        ||g          j                                        }|                    |d                    |                    t          d|t          d|g                               t          d|          S )	zc
    Creates a geojson of hexagonal features based on the outputs of
    _compute_wgs84_hexbin
    Nr   FeaturePolygon)typecoordinates)r   idgeometryFeatureCollection)r   features)	r	   rT   rO   ziprX   TtolistrP   dict)r   r   idsr   r   r   idxpointss           r   _hexagons_to_geojsonr   1  s    
 H
{iM**++]M3?? 	
 	
S#3*%%'..00fQi   96(CCC  	
 	
 	
 	
 (8<<<<r      Fc                 L   t          t                      d          }t          j        |d                   }|t          j        }|d                             t          j        |d                   j        	                    d          t          j
        |d                   j        	                    d                                                                                    }|d                             t          j        |d                   j        	                    d          t          j
        |d                   j        	                    d                                                                                    }t          |d                             |d                                                   |d                             |d                                                   ||d||||	  	        \  }}}}t          |||          } |~||t!          d	d	
          }!n=||t!          d	|
          }!n'||t!          ||
          }!nt!          ||
          }!t#          |d         |d         |d         |d         |!          }|5t!          |                                |                                          }|d         Ct!          |d                             |d         d                                                    }"n
d|d         i}"g }#|"                                D ]\  }$}%t          |%                    |d                                                   |%                    |d                                                   |||d         r-|%                    |d                                                   nd||||	  	        \  }&}&}}'|#                    t          j        |$d         gt/          |          z  ||'d|                     t          j        |#d                              t          j        d                              t          j                            }(|4|(d                                         |(d         
                                g}t;          d/i d|(                                d| dddddddddd|d         dndd|d|d |	d!|
d"|d#|d$|d%|d&|d'|d(|d)|d*|d+|})|rt?          |d         $|d                              |d         dd,          n|d                                         |d         |d         |d         -          }*d.|*j!        d         _"        d|*j!        d         _#        ||*j!        d         _$        |)%                    |*j!        d                    |d         tM          t/          |*j'                            D ]}+d.|*j'        |+         j!        d         _"        d|*j'        |+         j!        d         _#        ||*j'        |+         j!        d         _$        |)j'        |+         j!        d         |*j'        |+         j!        d         g|)j'        |+         _!        |)S )0zO
    Returns a figure aggregating scattered points into connected hexagons
    N)argsconstructor
data_framer   _min_maxr   )	r   r   r   r   r\   r]   r^   r_   r   i  r   r   r    )r   r   animation_frameT)drop_null_keys)r   r\   )frame	locationsr\   r   vertical)how)r\   geojsonr   
hover_dataF)r\   r   r   r   color_discrete_sequencecolor_discrete_maplabelscolor_continuous_scalerange_colorcolor_continuous_midpointopacityr*   center	map_styletitletemplater   r   )by
descending
nulls_last)r   r   r   r   skip )(r   localsr   get_native_namespacer	   meanr   r#   namesuffixr"   to_numpysqueezer   r   r   r   r8   group_by__iter__itemsrP   r   rO   concatwith_columnsr   castInt64r   	to_nativer   sortdata	hoverinfohovertemplatemarker	add_tracerN   frames),r   r   r   r\   
nx_hexagonr^   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r_   show_original_dataoriginal_data_markerr   r   r   r   r   r   r   countr   r/   groupsagg_data_frame_listkeydfrh   aggregated_valueagg_data_framefigoriginal_figr}   s,                                               r   create_hexbin_mapr   F  s+   : d;;;D.tL/ABB7 	\	F4;$++F33F4;$++F33

 

 
	  	\	F4;$++F33F4;$++F33

 

 
	  9N))$u+66??AA))$u+66??AA)
9 
9 
95M=, #=-NNG|>emC000FF^ 1E222FFEMv666FFu555F"aL)A,	!ilF
 
 ~)..**	0@0@AAA*Xd,-dXCCXZZ
 
 \*+<<>> 
 
R/Dd5k**3355d5k**3355=A']T"--W..77999PT-
0
 
0
 
0
,1l, 	""L!!fXL(9(99!-- 
 "2  		
 		
 		
 		
 Y2
CCCPPfWoo""28,, Q  N 7#''))7#''))

    !++--- + g	
 "FFF %)):$;$GT !8 7 .- v  65  K #<";  T v  )!" e#$ %& e'( v)C.  " )*6 \"''-.$# (    ,'ikkUU !23
 
 
 *0!&-1!*&:!#l'*+++!".3|23344  ;A#A&+A.8?C#A&+A.<8L#A&+A.5 JqM&q) '*/2&
1""
 Jr   rB   z/Number of hexagons (horizontally) to be created)functionz8Numpy array aggregator, it must take as input a 1D arrayzand output a scalar value.)rB   z=Minimum number of points in a hexagon for it to be displayed.z3If None and color is not set, display all hexagons.zDIf None and color is set, only display hexagons that contain points.boolzCWhether to show the original data on top of the hexbin aggregation.r   zScattermap marker options.)r   r^   r_   r   r   )override_dictc                      t          j        ddt                     d|v r|                    d          |d<   t	          | i |S )Nzcreate_hexbin_mapbox() is deprecated and will be removed in the next major version. Please use create_hexbin_map() instead. Learn more at: https://plotly.com/python/mapbox-to-maplibre/r   )
stacklevelcategorymapbox_styler   )warningswarnDeprecationWarningpopr   )r   kwargss     r   create_hexbin_mapboxr     s_    M	I #    $jj88{d-f---r   )	NNNNNNNNN)N)plotly.express._corer   plotly.express._docr   plotly.express._chart_typesr   r   narwhals.stable.v1stablev1r   numpyr	   r   r   r   r8   r   r   r   r   r   __doc__r   r   r   r   <module>r     s   0 0 0 0 0 0 . . . . . . C C C C C C C C                  + + +>e/ e/ e/R 	
LH LH LH LH^= = = =, 
 "	

1v v v vr +N$LM
 
 


 
 
 Q
 %&BC#      0. . . . .r   