App42 Cloud API

Ruby Cloud SDK

Geo Spatial

Geo Spatial Service on cloud provides the storage, retrieval, querying and updation of 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.

In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.

	api = App42::ServiceAPI.new("<API_KEY>","<SECRET_KEY>")

After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of GeoService, buildGeoService() method needs to be called.

	geoService = api.buildGeoService();

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The methods available under a particular API will return the domain object (like Geo in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.

geoStorageName = "geoTest";

geoList = Array.new();
gp = App42::Geo::GeoPoint.new();
gp.lat = "-74.2713";
gp.lng = "40.73137";
gp.marker = "Maplewood, NJ";
geoList.push(gp);
					
geo = geoService.create_geo_points(geoStorageName,geoList); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. */ 

The functions available under Geo Spatial API can throw some exceptions in abnormal conditions. Example of the same has been given below.

E.g. If App developer is trying to delete the storage that does not exist, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2903" and the details as "No Geo Storage exists with name '<storageName>'".

storageName = "geoTest";	
						
begin
  geo = geoService.delete_storage(storageName);
rescue App42Exception => ex
  appErrorCode = ex.app_error_code;
  httpErrorCode = ex.http_error_code;

  if(appErrorCode == 2903)
    #Handle here for Not Found (No Geo Storage exists with name '<storageName>'.)

  elsif(appErrorCode == 1401)
    #handle here for Client is not authorized

  else(appErrorCode == 1500)
    #handle here for Internal Server Error
  end

  jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/
end 
	{
	   "app42Fault": {
		   "httpErrorCode": 404,
		   "appErrorCode": 2903,
		   "message": "Not Found",
		   "details": "No Geo Storage exists with name 'geoTest'"
	   }
	}

Below are the HTTP Error Codes and their description, the function under the Geo Spatial API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/	

Below are the Application Error Codes and their description, the function under the Geo Spatial API can throw.

/*
2900 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>' and Distance In KM '<distanceInKM>'.
2901 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>' and Radius In KM '<radiusInKM>'.
2902 - NOT FOUND - No Geo Storage exists.
2903 - NOT FOUND - No Geo Storage exists with name '<storageName>'. 
2904 - NOT FOUND - No Geo Points in the storage '<storageName>' exist.
2905 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>'.
*/	

Various functions available under Geo Spatial API has been explained below.

def create_geo_points(geoStorageName, geoPointsList)

Stores the geo points with unique handler on the cloud. Geo point data contains lat, long and marker of the point.

Parameters:

geoStorageName - Unique handler for storage name
geoPointsList - List of Geo Points to be saved

Returns:

Geo object containing list of Geo Points that have been saved

Response: Geo Object

For more details click Exception
geoStorageName = "geoTest";

geoPointsList = Array.new();
gp = App42::Geo::GeoPoint.new();
gp.lat = "-74.2713";
gp.lng = "40.73137";
gp.marker = "Maplewood, NJ";
geoPointsList.push(gp);
						
geo = geoService.create_geo_points(geoStorageName,geoPointsList); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. */ 
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "points": {
                        "point": {
                            "lat": -74.2713,
                            "lng": 40.73137,
                            "marker": "Maplewood, NJ"
                        }
                    }
                }
            }
        }
    }
} 
def get_near_by_points_by_max_distance(storageName,lat,lng,distanceInKM)

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.

Parameters:

storageName - Unique handler for storage name
lat - Latitude of source point
lng - Longitude of source point
distanceInKM - Range in KM

Returns:

Geo object containing the target points in ascending order of distance from source point.

Response: Geo Object

Exception:

The method throws the following exception with the error code
/* 
2900 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>' and Distance In KM '<distanceInKM>'.
2905 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>'.
*/  
For more details click Exception
storageName = "geoTest";
lat = "-73.99171";
lng = "40.738868";
distanceInKM = "1";
						
geo = geoService.get_near_by_points_by_max_distance(storageName, lat, lng, distanceInKM); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
puts "sourceLat is #{geo.sourceLat}";
puts "distanceInKM is #{geo.distanceInKM}";
puts "sourceLng is #{geo.sourceLng}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "sourceLat": -73.99171,
                    "sourceLng": 40.738868,
                    "distanceInKM": 1,
                    "points": {
                        "point": [
                            {
                                "lat": -73.99171,
                                "lng": 40.73887,
                                "marker": "10gen Office"
                            },
                            {
                                "lat": -73.99249,
                                "lng": 40.738674,
                                "marker": "City Bakery"
                            },
                            {
                                "lat": -73.98814,
                                "lng": 40.741405,
                                "marker": "Flatiron Building"
                            },
                            {
                                "lat": -73.99781,
                                "lng": 40.73913,
                                "marker": "Players Club"
                            }
                        ]
                    }
                }
            }
        }
    }
}
def get_near_by_point(storageName,lat,lng,resultLimit)

Search the near by point from specified source point. Points to be searched should already be stored on cloud using unique storage name handler.

Parameters:

storageName - Unique handler for storage name
lat - Lattitude of source point
lng - Longitude of source point
resultLimit - Maximum number of results to be retrieved

Returns:

Geo object containing the target points in ascending order of distance from source point.

Response: Geo Object

Exception:

The method throws the following exception with the error code
/* 
2905 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>'.
*/   
For more details click Exception
storageName = "geoTest";
lat = "-73.99171";  
lng = "40.738868"; 
resultLimit	= "2";
						
geo = geoService.get_near_by_point(storageName, lat, lng, resultLimit); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
puts "sourceLat is #{geo.sourceLat}";
puts "distanceInKM is #{geo.distanceInKM}";
puts "sourceLng is #{geo.sourceLng}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. (as shown below)*/  
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "sourceLat": -73.99171,
                    "sourceLng": 40.738868,
                    "distanceInKM": null,
                    "points": {
                        "point": [
                            {
                                "lat": -73.99171,
                                "lng": 40.73887,
                                "marker": "10gen Office"
                            },
                            {
                                "lat": -73.99249,
                                "lng": 40.738674,
                                "marker": "City Bakery"
                            }
                        ]
                    }
                }
            }
        }
    }
}
def get_points_with_in_circle(storageName,lat,lng,radiusInKM,resultLimit)

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.

Parameters:

storageName - Unique handler for storage name
lat - Lattitude of source point
lng - Longitude of source point
radiusInKM - Radius in KM
resultLimit - Maximum number of results to be retrieved

Returns:

Geo object containing the target points in ascending order of distance from source point.

Response: Geo Object

Exception:

The method throws the following exception with the error code
/*  
2901 - NOT FOUND - No destination found using given parameter Lat : '<lat>'  Long : '<lng>' and Radius In KM '<radiusInKM>'.
*/ 
For more details click Exception
storageName = "geoTest";
lat = "-73.99171";  
lng = "40.738868";  
radiusInKM = "1";
resultLimit	= 1;
						
geo = geoService.get_points_with_in_circle(storageName, lat, lng, radiusInKM, resultLimit); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
puts "sourceLat is #{geo.sourceLat}";
puts "distanceInKM is #{geo.distanceInKM}";
puts "sourceLng is #{geo.sourceLng}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "sourceLat": -73.99171,
                    "sourceLng": 40.738868,
                    "distanceInKM": null,
                    "points": {
                        "point": {
                            "lat": -73.99249,
                            "lng": 40.738674,
                            "marker": "City Bakery"
                        }
                    }
                }
            }
        }
    }
} 
def get_all_storage()

Fetch the name of all storage stored on the cloud.

Parameters:

N/A

Returns:

Geo object containing list of all the storage created

Response: ArrayList<Geo> Object

Exception:

The method throws the following exception with the error code
/* 
2902 - NOT FOUND - No Geo Storage exists. 
*/
For more details click Exception
geoList = Array.new();
geoList = geoService.get_all_storage(); /* returns the list of Geo object. */
for geo in geoList do
  puts "storageName is #{geo.getStorageName}";
end
jsonResponse = geoList.to_s(); /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": [
                    {
                        "storageName": "geoTest",
                        "createdOn": "2012-05-10T06:13:39.000Z"
                    },
                    {
                        "storageName": "GEO Storage",
                        "createdOn": "2012-05-10T06:48:12.000Z"
                    }
                ]
            }
        }
    }
}
def get_all_points(storageName)

Get All Point from storage.

Parameters:

storageName - Name of the storage from which ponits have to be fetched

Returns:

Geo object containing all the stored Geo Points for the specified storage

Response: Geo Object

Exception:

The method throws the following exception with the error code
/* 
2904 - NOT FOUND - No Geo Points in the storage '<storageName>' exist.
*/  
For more details click Exception
storageName = "geoTest";
						
geo = geoService.get_all_points(storageName); /* returns the Geo object. */
puts "storageName is #{geo.getStorageName}";
pointList = Array.new();
pointList = geo.pointList();
for point in pointList do
  puts "lat is #{point.lat}";
  puts "lng is #{point.lng}";
  puts "marker is #{point.marker}";
end
jsonResponse = geo.to_s(); /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "points": {
                        "point": [
                            {
                                "lat": -73.99171,
                                "lng": 40.73887,
                                "marker": "10gen Office"
                            },
                            {
                                "lat": -73.98814,
                                "lng": 40.741405,
                                "marker": "Flatiron Building"
                            },
                            {
                                "lat": -73.99781,
                                "lng": 40.73913,
                                "marker": "Players Club"
                            },
                            {
                                "lat": -73.99249,
                                "lng": 40.738674,
                                "marker": "City Bakery"
                            },
                            {
                                "lat": -73.99249,
                                "lng": 40.738674,
                                "marker": "Splash Bar"
                            },
                            {
                                "lat": -73.98584,
                                "lng": 40.731697,
                                "marker": "Momofuku Milk Bar"
                            },
                            {
                                "lat": -73.9882,
                                "lng": 40.74164,
                                "marker": "Shake Shack"
                            },
                            {
                                "lat": -73.99408,
                                "lng": 40.75057,
                                "marker": "Penn Station"
                            },
                            {
                                "lat": -73.98602,
                                "lng": 40.74894,
                                "marker": "Empire State Building"
                            },
                            {
                                "lat": -73.99756,
                                "lng": 40.73083,
                                "marker": "Washington Square Park"
                            },
                            {
                                "lat": 106.9154,
                                "lng": 47.9245,
                                "marker": "Ulaanbaatar, Mongolia"
                            },
                            {
                                "lat": -74.2713,
                                "lng": 40.73137,
                                "marker": "Maplewood, NJ"
                            }
                        ]
                    }
                }
            }
        }
    }
} 
def delete_storage(storageName)

Delete the specifed Geo Storage from Cloud

Parameters:

StorageName - Name of the storage that has to be deleted

Returns:

Geo object containing the name of the storage that has been deleted

Response: App42Response Object

Exception:

The method throws the following exception with the error code
/* 
2903 - NOT FOUND - No Geo Storage exists with name '<storageName>'.  
*/  
For more details click Exception
storageName = "geoTest";
						
response = geoService.delete_storage(storageName); /* returns the App42Response object. */
success = response.isResponseSuccess();
jsonResponse = geo.to_s(); /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "geo": {
                "storage": {
                    "storageName": "geoTest",
                    "createdOn": "2012-05-10T06:13:39.000Z"
                }
            }
        }
    }
}