hive - Finding the closest location to a lat and long value -
i have 2 tables t1
, t2
(t1
has 1/10th of size of t2
). each table has 2 columns <lat, long>
contain latitude , longitude of points. each row in t1
i'd find row in t2
closest it. efficient query doing this? hive have sort of libraries geospatial search?
you'll need bit of trig.
please refer article on database journal
the last routine believe looking (you'll need modify use):
create definer=`root`@`localhost` procedure closest_restaurants_optimized` (in units varchar(5), in lat decimal(9,6), in lon decimal(9,6), in max_distance smallint, in limit_rows mediumint) begin declare one_degree_constant tinyint; declare earth_radius_constant smallint; declare lon1, lon2, lat1, lat2 float; if units = 'miles' set one_degree_constant = 69; set earth_radius_constant = 3959; else -- default kilometers set one_degree_constant = 111; set earth_radius_constant = 6371; end if; set lon1 = lon-max_distance/abs(cos(radians(lat))*one_degree_constant); set lon2 = lon+max_distance/abs(cos(radians(lat))*one_degree_constant); set lat1 = lat-(max_distance/one_degree_constant); set lat2 = lat+(max_distance/one_degree_constant); select pm1.post_id, p.post_title, round((earth_radius_constant * acos( cos( radians(lat) ) * cos( radians(pm1.meta_value) ) * cos( radians(pm2.meta_value) - radians(lon)) + sin(radians(lat)) * sin( radians(pm1.meta_value))) ), 3) distance goodfood_wp_md20m_postmeta pm1, goodfood_wp_md20m_postmeta pm2, goodfood_wp_md20m_posts p pm1.meta_key = 'latitude' , pm2.meta_key = 'longitude' , pm1.post_id = pm2.post_id , pm1.post_id = p.id , p.post_status = 'publish' , pm2.meta_value between lon1 , lon2 , pm1.meta_value between lat1 , lat2 order distance asc limit limit_rows; end
Comments
Post a Comment