Ignore:
Timestamp:
Dec 11, 2011, 8:55:04 PM (12 years ago)
Author:
rvelices
Message:

rv_gmaps can show now different markers based on number of photos at the location

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/rv_gmaps/trunk/include/cluster_maker.php

    r3447 r12719  
    33function cluster_area_compare( &$a, &$b )
    44{
    5         $aa = bounds_lat_range($a['bounds']) * bounds_lon_range($a['bounds']);
    6         $ab = bounds_lat_range($b['bounds']) * bounds_lon_range($b['bounds']);
     5        $aa = bounds_lat_range($a->bounds) * bounds_lon_range($a->bounds);
     6        $ab = bounds_lat_range($b->bounds) * bounds_lon_range($b->bounds);
    77        return -$aa+$ab; // biggest first
     8}
     9
     10final class Cluster
     11{
     12        public $bounds;
     13        public $items;
     14
     15        function __construct($b = array(), $i = array())
     16        {
     17                $this->bounds = $b;
     18                $this->items = $i;
     19        }
    820}
    921
     
    3042                        $this->bounds = bounds_add($this->bounds, $img['lat'], $img['lon'] );
    3143
    32                 $pending_split = array(
    33                                 array(
    34                                         'bounds' => $this->bounds,
    35                                         'items' => array_keys( $this->_image_map ),
    36                                 )
    37                         );
     44                $pending_split = array( new Cluster($this->bounds, array_keys($this->_image_map) ) );
    3845                $result = array();
    3946
     
    5158                                        foreach( $splitted as $k => $split )
    5259                                                echo("sub $k: " . var_export($split['bounds'],true)."\n" );*/
    53                                         $total_iterations += count($current['items']);
     60                                        $total_iterations += count($current->items);
    5461                                        $next_level_to_split = array_merge($next_level_to_split, $splitted );
    5562                                        if ( count($result)+count($pending_split)+count($next_level_to_split) >= $maxNbMarkers )
     
    6673                        usort( $pending_split, 'cluster_area_compare' );
    6774                }
     75
    6876                $merged = $this->_try_merge_clusters( $result, $maxLatPrecision, $maxLonPrecision );
    6977                $this->debug_str = get_elapsed_time($start, get_moment()) .' gen:'.$total_generations.' alg:'.count($this->_image_map).'+'.$total_iterations;
     
    7987                for ($i=0; $i<count($clusters)-1; $i++)
    8088                {
    81                         $ci = bounds_center( $clusters[$i]['bounds'] );
     89                        $ci = bounds_center( $clusters[$i]->bounds );
    8290                        for ($j=$i+1; $j<count($clusters); $j++)
    8391                        {
    84                                 $cj = bounds_center( $clusters[$j]['bounds'] );
    85                                 if ( abs($ci['lat']-$cj['lat']) < $maxLatPrecision/2
    86                                         and abs($ci['lon']-$cj['lon']) < $maxLonPrecision/2 )
     92                                $cj = bounds_center( $clusters[$j]->bounds );
     93                                $rlat = abs($ci['lat']-$cj['lat']) / $maxLatPrecision;
     94                                $rlon = abs($ci['lon']-$cj['lon']) / $maxLonPrecision;
     95                                if ( $rlat<1 && $rlon<1
     96                                                && $rlat+$rlon<1.1)
    8797                                {
    8898                                        //print_r( "i=$i; j=$j \n"); var_export( $clusters[$i]['bounds'] ); var_export( $clusters[$j]['bounds'] );
    89                                         $clusters[$i]['items'] = array_merge( $clusters[$i]['items'], $clusters[$j]['items'] );
     99                                        $clusters[$i]->items = array_merge( $clusters[$i]->items, $clusters[$j]->items );
    90100                                        if ( empty($this->_image_ranks) )
    91101                                                $this->_image_ranks = array_flip( array_keys($this->_image_map) );
    92                                         usort( $clusters[$i]['items'], array($this, '_image_rank_compare') );
    93                                         $clusters[$i]['bounds'] = bounds_union($clusters[$i]['bounds'], $clusters[$j]['bounds'] );
     102                                        usort( $clusters[$i]->items, array($this, '_image_rank_compare') );
     103                                        $clusters[$i]->bounds = bounds_union($clusters[$i]->bounds, $clusters[$j]->bounds );
    94104                                        array_splice( $clusters, $j, 1);
    95105                                        $j--;
    96                                         $ci = bounds_center( $clusters[$i]['bounds'] );
     106                                        $ci = bounds_center( $clusters[$i]->bounds );
    97107                                        $ret++;
    98108                                }
     
    109119        function _split_cluster($cluster, $maxLatPrecision, $maxLonPrecision)
    110120        {
    111                 $latRange = bounds_lat_range($cluster['bounds']);
    112                 $lonRange = bounds_lon_range($cluster['bounds']);
     121                $latRange = bounds_lat_range($cluster->bounds);
     122                $lonRange = bounds_lon_range($cluster->bounds);
    113123
    114124                $lat_nb_tiles = max( 1, $latRange/$maxLatPrecision );
     
    146156                }
    147157
    148                 if ( $cluster['bounds']['count'] > 200 )
     158                if ( $cluster->bounds['count'] > 200 )
    149159                {
    150160                        if ($lat_step>4*$maxLatPrecision) $lat_step = $lat_step/2;
     
    159169
    160170                $clusters = array();
    161                 foreach ( $cluster['items'] as $id )
     171                foreach ( $cluster->items as $id )
    162172                {
    163173                        $lon = $this->_image_map[$id]['lon'];
    164174                        $lat = $this->_image_map[$id]['lat'];
    165175
    166                         $idx_lon = floor ( ( $lon - $cluster['bounds']['w'] ) / $lon_step );
    167                         $idx_lat = floor ( ( $lat - $cluster['bounds']['s'] ) / $lat_step );
     176                        $idx_lon = floor ( ( $lon - $cluster->bounds['w'] ) / $lon_step );
     177                        $idx_lat = floor ( ( $lat - $cluster->bounds['s'] ) / $lat_step );
    168178
    169179                        $idx = $lon_nb_tiles * $idx_lat + $idx_lon;
     
    171181                        if ( !isset($clusters[$idx]) )
    172182                        {
    173                                 $clusters[$idx] = array(
    174                                         'bounds'  => array(),
    175                                         'items'   => array(),
    176                                         );
     183                                $clusters[$idx] = new Cluster();
    177184                        }
    178                         array_push( $clusters[$idx]['items'], $id );
    179                         $clusters[$idx]['bounds'] = bounds_add( $clusters[$idx]['bounds'] , $lat, $lon );
     185                        array_push( $clusters[$idx]->items, $id );
     186                        $clusters[$idx]->bounds = bounds_add( $clusters[$idx]->bounds , $lat, $lon );
    180187                }
    181188                return $clusters;
Note: See TracChangeset for help on using the changeset viewer.