Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.cooliris.media;
     18 
     19 public class LocationMediaFilter extends MediaFilter {
     20     private double mRadius;
     21     private double mCenterLat;
     22     private double mCenterLon;
     23     public static final int EARTH_RADIUS_METERS = 6378137;
     24     public static final int LAT_MIN = -90;
     25     public static final int LAT_MAX = 90;
     26     public static final int LON_MIN = -180;
     27     public static final int LON_MAX = 180;
     28 
     29     LocationMediaFilter(double centerLatitude, double centerLongitude, double thresholdRadius) {
     30         mCenterLat = centerLatitude;
     31         mCenterLon = centerLongitude;
     32         mRadius = thresholdRadius;
     33     }
     34 
     35     LocationMediaFilter(double latitude1, double longitude1, double latitude2, double longitude2) {
     36         mCenterLat = centerLat(latitude1, latitude2);
     37         mCenterLon = centerLon(longitude1, longitude2);
     38         mRadius = distanceBetween(latitude1, longitude1, latitude2, longitude2);
     39     }
     40 
     41     public static final double centerLat(double lat1, double lat2) {
     42         return (centerOfAngles(lat1, lat2, LAT_MAX));
     43     }
     44 
     45     public static final double centerLon(double lon1, double lon2) {
     46         return (centerOfAngles(lon1, lon2, LON_MAX));
     47     }
     48 
     49     private static final double centerOfAngles(double ang1, double ang2, int wrapAroundThreshold) {
     50         boolean wrapAround = false;
     51         if (Math.abs(ang1 - ang2) > wrapAroundThreshold) {
     52             wrapAround = true;
     53         }
     54         double center = (ang1 + ang2) * 0.5;
     55         if (wrapAround) {
     56             center += wrapAroundThreshold;
     57             center %= wrapAroundThreshold;
     58         }
     59         return center;
     60     }
     61 
     62     public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) {
     63         double dLat = Math.toRadians(lat2 - lat1);
     64         double dLon = Math.toRadians(lon2 - lon1);
     65         double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
     66                 * Math.sin(dLon / 2) * Math.sin(dLon / 2);
     67         double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
     68         return EARTH_RADIUS_METERS * c;
     69     }
     70 
     71     public static final double toKm(double meter) {
     72         return meter / 1000;
     73     }
     74 
     75     public static final double toMile(double meter) {
     76         return meter / 1609;
     77     }
     78 
     79     @Override
     80     public boolean pass(MediaItem item) {
     81         double radius = distanceBetween(mCenterLat, mCenterLon, item.mLatitude, item.mLongitude);
     82         if (radius <= mRadius) {
     83             return true;
     84         }
     85         return false;
     86     }
     87 }
     88