Determine if two geolocation cycles intersect

This forum contains topics that were moved from Get Satisfaction
Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

Hi

Suppose I have an app with a geoloaction service "Point1", which gives me lat1,lng1

And I have another point "Point2" in the map with lat2,lng2.

I want a function to be able to determine if a cycle with center Point1 and radius radius1, INTERSECTS with a cycle with center Point2 and radius radius2.

If this condition is satisfied, then I will return the related records from DB.

I have used to nearShpere to see if a point is within a cycle.

Thanks team!

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

Or if the distance between the two points is less than the sum of the radius1 + radius2, then cycles intersect.

So another option is if there is function that returns the distance between two geopoints.
Is there such a function ?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Determine if two geolocation cycles intersect

Post by Evgene Karachevtsev »

Panagiotis,

The basic formula of this function should be such
radius1+radius2 D
Where D is the distance between these two points. The distance can be calculated as shown here:
http://www.movable-type.co.uk/scripts...

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

Thanks!

The below optimized code that I found, works fine. Is the below.

=====================================
var R = 6371; // Radius of the earth in km
var dist;
var lat1=38.003934;
var lon1=23.793436;

var lat2=39.003934;
var lon2=22.793436;

var dLat = (lat2 - lat1) * Math.PI / 180; // deg2rad below
var dLon = (lon2 - lon1) * Math.PI / 180;
var a = 0.5 - Math.cos(dLat)/2 + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * (1 - Math.cos(dLon))/2;

dist=R * 2 * Math.asin(Math.sqrt(a));
alert (dist);
=====================================

I know have to figure out how to use it in my query to construct a where clause ...

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Determine if two geolocation cycles intersect

Post by Evgene Karachevtsev »

Panagiotis,

Thank you for the update! Glad it works!

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

Thanks aslo

I have this constructed dynamically

=================
{ "$and":
[

{"$or": [{"Type": "Food"},{"Type": "Dance"}]},
{ "Location": {"$nearSphere": [23.791376,38.002788], "$maxDistanceInKilometers": 0.5} }

]
}
====================

How can I use tha javascript function in the where clause?
For example:

=================
{ "$and":
[

{"$or": [{"Type": "Food"},{"Type": "Dance"}]},
{ "Location": {function(lat,lang,rad) } <----- ????

]
}


Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

I have created a new Javascript in the project, gave a name and inside I created the function

function distance(lat1, lon1, lat2, lon2, rad1, rad2)
which returns rad2, based on some criteria

Then I want use and test it in the Test tab of my query service.
The below does not work (I put example values)
============================
{ "$and": [ {"$or": [{"Type": "Food"},{"Type": "Dance"}]}, { "ValidRadius": $distance(38.003934, 23.793436, 39.003934, 22.793436, 1, 5) } ] }


Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Determine if two geolocation cycles intersect

Post by Evgene Karachevtsev »

Panagiotis,

Please use JS when for a mapping.
http://devcenter.appery.io/documentat...

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Determine if two geolocation cycles intersect

Post by Takis »

Hi Evgene

Thanks, I know how to do this, I have done it already.
My problem is how to return records where (for example)

type is Food or Drink
and the function distance(lat1, lon1, lat2, lon2, rad1, rad2) returns a value.
Input values are taken from DB table.

So does this query must be done in more steps ?

  1. get records with type food or drink

  2. run function distance(lat1, lon1, lat2, lon2, rad1, rad2) for the above result set
    and if it returns a value then keep the record, otherwise remove it from list
    3 produce the final result set

    Hope the problem is clear....

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Determine if two geolocation cycles intersect

Post by Evgene Karachevtsev »

Panagiotis,

Sorry for misunderstanding.
Here are all the types of queries, that work with location: http://devcenter.appery.io/documentat...
Unfortunately you can't use your own function as the selection criteria from the database. I.e., it should be done in a few steps.

Post Reply