# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'geo/GeoResponseBuilder.rb' require 'geo/Geo.rb' require 'geo/GeoPoint.rb' require 'bigdecimal' module App42 module Geo # # Geo Spatial Service on cloud provides the storage, retrieval, querying and updating geo data. # One can store the geo data by unique handler on the cloud and can apply search, update and query on it. # Geo spatial query includes finding nearby/In circle target point from given point using geo points stored on the cloud. # # @see Geo # class GeoService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Geo->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "geo" @version = "1.0" end # # Stores the geo points with unique handler on the cloud. Geo point data contains lat, long and marker of the point. # # @param geoStorageName # - Unique handler for storage name # @param geoPointsList # - List of Geo Points to be saved # # @return Geo object containing list of Geo Points that have been saved # # @raise App42Exception # def create_geo_points(geoStorageName, geoPointsList) puts "Create GeoPoints Called " puts "Base url #{@base_url}" response = nil; geoObj = nil; geoObj = Geo.new util = Util.new util.throwExceptionIfNullOrBlank(geoStorageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(geoPointsList, "Geo Points List"); begin connection = App42::Connection::RESTConnection.new(@base_url) geoArray = Array.new for geoPoint in geoPointsList do geoArray.push(geoPoint) end body = {'app42' => {"geo"=> { "storage"=> { "storageName" => geoStorageName, "points" =>{ "point" => geoArray.to_json }}}}}.to_json puts "Body #{body}" params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/createGeoPoints" response = connection.post(signature, resource_url, query_params, body) geo = GeoResponseBuilder.new() geoObj = geo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObj end # # Search the near by point in given range(In KM) from specified source point. # Points to be searched should already be stored on cloud using unique storage name handler. # # @param storageName # - Unique handler for storage name # @param lat # - Latitude of source point # @param lng # - Longitude of source point # @param distanceInKM # - Range in KM # # @return Geo object containing the target points in ascending order of distance from source point. # # @raise App42Exception # def get_near_by_points_by_max_distance(storageName,lat,lng,distanceInKM) puts "Get Near By Points By Max Distance Called" puts "Base url #{@base_url}" response = nil; geoObj = nil; geoObj = Geo.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(lat, "Latitute"); util.throwExceptionIfNullOrBlank(lng, "langitude"); util.throwExceptionIfNullOrBlank(distanceInKM, "Distance In KM"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName) params.store("lat", lat.to_s + "") params.store("lng",lng.to_s + "") params.store("distanceInKM", distanceInKM.to_s + "") signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/getNearByPoints/storageName/#{storageName}/lat/#{lat}/lng/#{lng}/distanceInKM/#{distanceInKM}" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new geoObj = geo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObj end # # Search the near by point from specified source point. # Points to be searched should already be stored on cloud using unique storage name handler. # # @param storageName # - Unique handler for storage name # @param lat # - Lattitude of source point # @param lng # - Longitude of source point # @param resultLimit # - Maximum number of results to be retrieved # # @return Geo object containing the target points in ascending order of distance from source point. # # @raise App42Exception # def get_near_by_point(storageName,lat,lng,resultLimit) puts "Get Near By Points By Point Called" puts "Base url #{@base_url}" response = nil; geoObj = nil; geoObj = Geo.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(lat, "Latitute"); util.throwExceptionIfNullOrBlank(lng, "langitude"); util.throwExceptionIfNullOrBlank(resultLimit, "resultLimit"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName) params.store("lat", lat.to_s + "") params.store("lng", lng.to_s + "") params.store("resultLimit", resultLimit.to_s+ "") signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/getNearByPoint/storageName/#{storageName}/lat/#{lat}/lng/#{lng}/limit/#{resultLimit}" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new geoObj = geo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObj end # # Search the near by point from specified source point with in specified radius. # Points to be searched should already be stored on cloud using unique storage name handler. # # @param storageName # - Unique handler for storage name # @param lat # - Lattitude of source point # @param lng # - Longitude of source point # @param radiusInKM # - Radius in KM # @param resultLimit # - Maximum number of results to be retrieved # # @return Geo object containing the target points in ascending order of distance from source point. # # @raise App42Exception # def get_points_with_in_circle(storageName,lat,lng,radiusInKM,resultLimit) puts "Get Near By Points By Max Distance Called" puts "Base url #{@base_url}" response = nil; geoObj = nil; geoObj = Geo.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(lat, "Latitute"); util.throwExceptionIfNullOrBlank(lng, "langitude"); util.throwExceptionIfNullOrBlank(radiusInKM, "Radius In KM"); util.throwExceptionIfNullOrBlank(resultLimit, "Result Limit"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName) params.store("lat", lat.to_s + "") params.store("lng", lng.to_s + "") params.store("resultLimit", resultLimit.to_s + "") params.store("radiusInKM", radiusInKM.to_s + "") signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/getPointsWithInCircle/storageName/#{storageName}/lat/#{lat}/lng/#{lng}/radiusInKM/#{radiusInKM}/limit/#{resultLimit}" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new geoObj = geo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObj end # # Fetch the name of all storage stored on the cloud # # @return Geo object containing list of all the storage created # # @raise App42Exception # def get_all_storage() puts "Get All Storage Called " puts "Base url #{@base_url}" response = nil geoObj = nil; geoObjList = Array.new util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new geoObjList = geo.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end puts geoObjList[0].createdOn return geoObjList end # # Delete the specifed Geo Storage from Cloud. # # @return Geo object containing the name of the storage that has been deleted # # @raise App42Exception # def delete_storage(storageName) puts "Delete Storage Called" puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage/#{storageName}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Get All Point from storage. # # @return Geo object containing all the stored Geo Points for the specified storage # # @raise App42Exception # def get_all_points(storageName) puts "Get All Points" puts "Base url #{@base_url}" response = nil; geoObjList = nil; geoObjList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/points/#{storageName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" geo = GeoResponseBuilder.new() geoObjList = geo.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObjList end # # NEW METHOD # def get_all_storage_by_paging(max, offset) puts "get_all_storage_by_paging Called " puts "Base url #{@base_url}" response = nil; geoObjList = nil; geoObjList = Array.new(); util = Util.new util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new() geoObjList = geo.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoObjList end # # NEW METHOD # def delete_geo_points(geoStorageName, geoPointsList) puts "deleteGeoPoints Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(geoStorageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(geoPointsList, "Geo Points List"); begin connection = App42::Connection::RESTConnection.new(@base_url) geoArray = Array.new for geoPoint in geoPointsList do geoArray.push(geoPoint) end body = {'app42' => {"geo"=> { "storage"=> { "points" =>{ "point" => geoArray.to_json }}}}}.to_json puts "Body #{body}" params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params.store("geoPoints", body) query_params = params.clone signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/points/#{geoStorageName}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # NEW METHOD # def get_all_points_by_paging(storageName, max, offset) puts "get_all_points_by_paging Called " puts "Base url #{@base_url}" response = nil; geoList = nil; geoList = Array.new(); util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Geo Storage Name"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("storageName", storageName); params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/points/#{storageName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) geo = GeoResponseBuilder.new() geoList = geo.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return geoList end end end end