public void Test_0002_GeoBox_null_key()
        {
            KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100);
            KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102);

            KiiClause.GeoBox(null, northEast, southWest);
        }
        public void Test_0001_GeoDistanceQuery()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myLoc", point);
            obj.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc");
            KiiQuery    query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myLoc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);
            JsonObject jObj             = retObj.GetJsonObject("_calculated");
            double     retDistance      = jObj.GetDouble("distanceToMyLoc");
            double     expectedDistance = distanceInMeter(point, center);

            Assert.IsTrue(approximateEqual(expectedDistance, retDistance, 0.00001));
        }
        public void Test_0005_GeoBoxQuery()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myloc", point);
            obj.Save();
            //not in the box
            KiiObject   obj2   = bucket.NewKiiObject();
            KiiGeoPoint point2 = new KiiGeoPoint();

            obj2.SetGeoPoint("myloc", point2);
            obj2.Save();

            KiiGeoPoint sw = new KiiGeoPoint(35.52105, 139.699402);
            KiiGeoPoint ne = new KiiGeoPoint(36.069082, 140.07843);

            KiiClause clause = KiiClause.GeoBox("myloc", ne, sw);
            KiiQuery  query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(result.Count, 1);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myloc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);
        }
        public void Test_0003_GeoBox_empty_key()
        {
            KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100);
            KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102);

            KiiClause.GeoBox("", northEast, southWest);
        }
        public void Test_0008_GeoDistance_empty_key()
        {
            KiiGeoPoint center          = new KiiGeoPoint(70.00, 100);
            double      radius          = 10.0;
            string      key             = "";
            string      putDistanceInto = "calculatedDistance";

            KiiClause.GeoDistance(key, center, radius, putDistanceInto);
        }
        public void Test_0010_GeoBox_Valid_key_Valid_Center_Invalid_radius()
        {
            KiiGeoPoint center          = new KiiGeoPoint(70.00, 100);
            double      radius          = -1;
            string      key             = "currentLocation";
            string      putDistanceInto = "calculatedDistance";

            KiiClause.GeoDistance(key, center, radius, putDistanceInto);
        }
        public void Test_0004_GeoDistanceQuery_sort_asc()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj1   = bucket.NewKiiObject();
            KiiGeoPoint point1 = new KiiGeoPoint(35.672568, 139.723606);

            obj1.SetGeoPoint("myLoc", point1);
            obj1.Save();

            KiiObject   obj2   = bucket.NewKiiObject();
            KiiGeoPoint point2 = new KiiGeoPoint(35.667983, 139.739356);

            obj2.SetGeoPoint("myLoc", point2);
            obj2.Save();
            // not in radius
            KiiObject   obj3   = bucket.NewKiiObject();
            KiiGeoPoint point3 = new KiiGeoPoint();

            obj3.SetGeoPoint("myLoc", point3);
            obj3.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc");
            KiiQuery    query  = new KiiQuery(clause);

            query.SortByAsc("_calculated.distanceToMyLoc");

            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(result.Count, 2);
            KiiObject   retObj1   = result [0];
            KiiGeoPoint retPoint1 = retObj1.GetGeoPoint("myLoc");

            Assert.AreEqual(point1.Latitude, retPoint1.Latitude);
            Assert.AreEqual(point1.Longitude, retPoint1.Longitude);
            JsonObject jObj1 = retObj1.GetJsonObject("_calculated");

            KiiObject   retObj2   = result [1];
            KiiGeoPoint retPoint2 = retObj2.GetGeoPoint("myLoc");

            Assert.AreEqual(point2.Latitude, retPoint2.Latitude);
            Assert.AreEqual(point2.Longitude, retPoint2.Longitude);
            JsonObject jObj2 = retObj2.GetJsonObject("_calculated");

            double retDistance1      = jObj1.GetDouble("distanceToMyLoc");
            double retDistance2      = jObj2.GetDouble("distanceToMyLoc");
            double expectedDistance1 = distanceInMeter(point1, center);
            double expectedDistance2 = distanceInMeter(point2, center);

            Assert.IsTrue(approximateEqual(expectedDistance1, retDistance1, 0.00001));
            Assert.IsTrue(approximateEqual(expectedDistance2, retDistance2, 0.00001));
        }
        /// <summary>
        /// Create a clause of geo box.
        /// </summary>
        /// <remarks>
        /// This clause inquires objects in the specified rectangle.
        /// Rectangle would be placed parallel to the equator with specified coordinates of the corner.
        /// </remarks>
        /// <returns>
        /// KiiClause instance.
        /// </returns>
        /// <exception cref='ArgumentException'>
        /// Is thrown when an argument is invalid. Please see parameter explanation.
        /// </exception>
        /// <param name='key'>
        /// Name of the key to inquire, which holds geo point. Must not null or empty string.
        /// </param>
        /// <param name='northEast'>
        /// North east corner of the rectangle. Must not null.
        /// </param>
        /// <param name='southWest'>
        /// South west corner of the rectangle. Must not null.
        /// </param>
        public static KiiClause GeoBox(string key, KiiGeoPoint northEast, KiiGeoPoint southWest)
        {
            if (Utils.IsEmpty(key))
            {
                throw new ArgumentException("key must not null or empty string");
            }

            JsonObject box = new JsonObject();

            box.Put("ne", northEast.ToJson());
            box.Put("sw", southWest.ToJson());

            return(GeoBoxInternal(key, box));
        }
        /// <summary>
        /// Create a clause of geo distance.
        /// </summary>
        /// <returns>
        /// KiiClause instance.
        /// </returns>
        /// <exception cref='ArgumentException'>
        /// Is thrown when an argument is invalid. Please see parameter explanation.
        /// </exception>
        /// <remarks>
        /// This clause inquires objects in the specified circle.<br/>
        /// <b>Note:</b> You can get the results in ascending order of distances from center.
        /// To do so, build the orderBy field  by "_calculated.{specified value of calculatedDistance}" and pass it in <see cref="KiiQuery.SortByAsc()"/>.<br/>
        /// Note that, descending order of distances is not supported. The unit of distance is meter.
        /// <see cref="KiiObject"/>
        /// <code>
        /// // example
        /// string calculatedDistance = "distanceFromCurrentLoc";
        /// KiiGeoPoint currentLoc = KiiGeoPoint(120.000,77.000); //dummy location
        /// KiiClause geoDist = KiiClause("location",currentLoc, 7.0,calculatedDistance);
        /// KiiQuery query = KiiQuery.QueryWithClause(geoDist);
        /// // sort distance ny ascending order.
        /// string orderByKey = "_calculated."+ calculatedDistance;
        /// query.SortByAsc(orderByKey);
        /// KiiBucket bucket = Kii.Bucket("MyBucket");
        /// KiiQueryResult&lt;KiiObject&gt; result = bucket.Query(query);
        /// if(result.Size &gt; 0)
        /// {
        ///     KiiObject object = result[0];
        ///     double distanceInMeter = object.GetJsonObject("_calculated").GetDouble(calculatedDistance);
        /// }
        /// </code>
        /// </remarks>
        /// <param name='key'>
        /// Name of the key to inquire, which holds geo point. Must not null or empty string.
        /// </param>
        /// <param name='center'>
        /// Geo point which specify center of the circle. Mus not null.
        /// </param>
        /// <param name='radius'>
        /// Radius of the circle. unit is meter. value should be in range of 0-20000000.
        /// </param>
        /// <param name='calculatedDistance'>
        /// Calculated distance is used for retrieve distance from the center from the query result. If the specified value is null, query result will not contain the distance.
        /// </param>
        ///
        public static KiiClause GeoDistance(string key, KiiGeoPoint center, double radius, string calculatedDistance)
        {
            if (Utils.IsEmpty(key))
            {
                throw new ArgumentException("key must not null or empty string");
            }

            if (radius <= 0 || radius > 20000000)
            {
                throw new ArgumentException("radius value should be in range of 0-20000000");
            }

            return(GeoDistanceInternal(key, center.ToJson(), radius, calculatedDistance));
        }
        public void Test_0001_GeoBox_Valid_Parameters()
        {
            KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100);
            KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102);

            KiiClause  c      = KiiClause.GeoBox("box", northEast, southWest);
            JsonObject clause = c.ToJson();
            JsonObject box    = clause.GetJsonObject("box");
            JsonObject ne     = box.GetJsonObject("ne");
            JsonObject sw     = box.GetJsonObject("sw");

            Assert.AreEqual(clause.GetString("type"), "geobox");
            Assert.AreEqual(clause.GetString("field"), "box");
            Assert.AreEqual(ne.GetDouble("lat"), northEast.Latitude);
            Assert.AreEqual(ne.GetDouble("lon"), northEast.Longitude);
            Assert.AreEqual(sw.GetDouble("lat"), southWest.Latitude);
            Assert.AreEqual(sw.GetDouble("lon"), southWest.Longitude);
        }
        public void Test_0006_GeoDistance_Valid_Parameters()
        {
            KiiGeoPoint center          = new KiiGeoPoint(70.00, 100);
            double      radius          = 10.0;
            string      key             = "currentLocation";
            string      putDistanceInto = "calculatedDistance";

            KiiClause  c      = KiiClause.GeoDistance(key, center, radius, putDistanceInto);
            JsonObject clause = c.ToJson();

            Assert.AreEqual(clause.GetString("type"), "geodistance");
            Assert.AreEqual(clause.GetString("field"), key);
            Assert.AreEqual(clause.GetString("putDistanceInto"), putDistanceInto);
            Assert.AreEqual(clause.GetDouble("radius"), radius);

            JsonObject centerJson = clause.GetJsonObject("center");

            Assert.AreEqual(centerJson.GetDouble("lat"), center.Latitude);
            Assert.AreEqual(centerJson.GetDouble("lon"), center.Longitude);
        }
        public void Test_0002_GeoDistanceQuery_calculatedDistance_nil()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myLoc", point);
            obj.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "");
            KiiQuery    query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myLoc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);

            Assert.IsFalse(retObj.Has("_calculated"));
        }
        public void Test_0001_GetGeoPoint_valid_param()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            this.SetGeoRefreshResponse(client);

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/abcd"));

            obj.Refresh();
            KiiGeoPoint expectedResult = new KiiGeoPoint(72.00, 100.00);

            Assert.AreEqual("abcd", obj.ID);
            Assert.AreEqual(2345, obj.CreatedTime);
            Assert.AreEqual(6789, obj.ModifedTime);
            Assert.AreEqual(expectedResult, obj.GetGeoPoint("location"));
        }
        private double distanceInMeter(KiiGeoPoint start, KiiGeoPoint end)
        {
            double lat1 = start.Latitude;
            double lat2 = end.Latitude;
            double lon1 = start.Longitude;
            double lon2 = end.Longitude;
            //degrees to radians
            double lat1rad = lat1 * Math.PI / 180;
            double lon1rad = lon1 * Math.PI / 180;
            double lat2rad = lat2 * Math.PI / 180;
            double lon2rad = lon2 * Math.PI / 180;

            //deltas
            double dLat = lat2rad - lat1rad;
            double dLon = lon2rad - lon1rad;

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1rad) * Math.Cos(lat2rad);
            double c = 2 * Math.Asin(Math.Sqrt(a));
            double R = 6371 * 1000;

            return(R * c);
        }
        public void Test_0008_GetGeoPoint_fallback_notfound_key()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            this.SetGeoRefreshResponse(client);

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/abcd"));

            obj.Refresh();

            KiiGeoPoint fallBack = new KiiGeoPoint(50.00, 100.00);

            Assert.AreEqual("abcd", obj.ID);
            Assert.AreEqual(2345, obj.CreatedTime);
            Assert.AreEqual(6789, obj.ModifedTime);
            Assert.AreEqual(fallBack, obj.GetGeoPoint("notfound", fallBack));
        }
 /// <summary>
 /// Determines whether the specified <see cref="KiiGeoPoint"/> is equal to the current <see cref="KiiCorp.Cloud.Storage.KiiGeoPoint"/>.
 /// </summary>
 /// <param name='other'>
 /// The <see cref="KiiGeoPoint"/> to compare with the current <see cref="KiiCorp.Cloud.Storage.KiiGeoPoint"/>.
 /// </param>
 /// <returns>
 /// <c>true</c> if the specified <see cref="KiiGeoPoint"/> is equal to the current
 /// <see cref="KiiCorp.Cloud.Storage.KiiGeoPoint"/>; otherwise, <c>false</c>.
 /// </returns>
 public bool Equals(KiiGeoPoint other)
 {
     return(other.Longitude == this.longitude && other.Latitude == this.latitude);
 }
        public void Test_0001_GeoPoint_Valid_Parameters()
        {
            KiiGeoPoint location = new KiiGeoPoint(71.00, 102);

            Assert.AreEqual("{\"lat\":71,\"lon\":102,\"_type\":\"point\"}", location.ToJson().ToString());
        }
        public void Test_0002_GeoPoint_Default_Constructor()
        {
            KiiGeoPoint location = new KiiGeoPoint();

            Assert.AreEqual("{\"lat\":-0,\"lon\":-0,\"_type\":\"point\"}", location.ToJson().ToString());
        }