3

I am so confuse and so tired to goggling to find above trick. Actually i have a database table that have six fields id, Sratlatitude, Startlongitude, EndLatitude, EndLongitude and username. Now i want to query in the database like

SELECT * FROM LOCATIONS(table name) WHERE USERS BETWEEN STARTING POINT(InputedStartlatitude and InputedStartlongitude) AND ENDING POINT(InputedEndlatitude and InputedEndlongitude);
For example : USERXXX inputted starting point(InputedStartlatitude = 18.9647 and InputedStartlongitude = 72.8258) = Mumbai and Ending point(InputedEndlatitude = 18.9647 and InputedEndlongitude = 72.8258) = Delhi. Now he want to search how many USERS between this Inputted route(Driving Direction) using Inutedlatitudes and Inputedlongitudes. Search query will fire to the mysql database and compare from the stored starting and ending latitude and longitude which are between Mumbai to Delhi.

I have found one solution for this Like making query to the database as :

SELECT * FROM lOCATIONS(tablename) WHERE (InputStartLatitude >= StartLatitude and IntartLongitude >= StartLong) and (InputEndLatitude <= EndLatitude and InuteEndLongitude <= EndLongitude);

But i have faced some problem with above query. Below are the coordinates of some places. Please have look :

enter image description here

The starting and ending co-ordinates are in RED color. Starting position is : Porbandar and Ending is : Mumbai. Now problem is that i m facing is when i trying to searching cities between Porbandar to Mumbai then not all cities comes those are actual situated in driving direction because of my query. I have called only those cities in my query how are greater than starting Lat and Long and Less then of Ending Lat and Long. But here Mumbai's Lat and longitude almost less then of all cities. so how can i make proper search query ?
Hope I have explain well above situation.

Any response will be very helpful to me.
I am using php-mysql as back-end and android at client side.

  • 2
    Welcome to SO. This is a very interesting question. Can you clarify what you mean by `BETWEEN`? Are you looking for places that lie, for example, within 30km of the great circle route path between the starting and ending point? Are you looking for places that lie within the lat-long bounding box defined by the starting and ending point? Are your starting and ending points close enough together that a path determined by linear interpolation of lat-long is an adequate approximation (likely true in India, but not in Norway)? Please clarify your question. – O. Jones Oct 31 '12 at 13:44
  • 2
    What do you mean by "between"? Are these all locations within the spherical square of `latStart<=latX<=latEnd && longStart<=longX<=longEnd'? Or do you want to find all locations in the database that a Google Maps driving direction between Start and End will touch/cross? – Jpsy Oct 31 '12 at 13:46
  • thank you Ollie Jones and Jpsy, I am updating my question please check back it , hope you will understand well this time. – Himanshu Dhakecha Oct 31 '12 at 13:56
  • Sorry Himanshu, but your edit does not make it any clearer. What I understand is, that your db holds geo location quadruples (StartLat, StartLong, EndLat, EndLong) for each user and that a user can input another quadruple of data on the fly (InputStartLat, InputStartLong, InputEndLat, InputEndLong), from which you will calculate Google Maps driving directions. But you still fail to define what you understand by "between". What mathematical or algorithmic properties must a db-quadruple have to be considered as being "between" the points of the input-quadruple? – Jpsy Oct 31 '12 at 15:08
  • "BETWEEN" means it is not any special word but consider it as English i want to tell you just that : When user – Himanshu Dhakecha Nov 01 '12 at 06:49
  • "BETWEEN" means it is not any special word but consider it as English only because i want to tell you just that : When USER-XXX when entered start and end lat and long and query database result is should like : InputStartLat, InputStartLong<=======List of cities======>InputEndLat, InputEndLong. Here "<=======List of cities =====>" is cities which startLat, startLong and endLat, eandLong between user inputted coordinates. Hope you will now understand. – Himanshu Dhakecha Nov 01 '12 at 06:58

1 Answers1

4

Your problem is probably that your search expression does not work correctly if your start values are higher than your end values. You have to sort your boundary values before you compare them to your table fields.

Here is a query that will work always - no matter whether your start location is east or west (and north or south) of your end location. For convenience I have uses MySQL's BETWEENoperator instead of >= and <=, but the major detail of this query is that the boundary values are sorted using the LEAST and GREATEST operators:

SELECT * 
  FROM `locationtable` 
  WHERE  ( `lat` BETWEEN LEAST(InputStartLatitude, InputEndLatitude) AND GREATEST(InputStartLatitude, InputEndLatitude) )  
  AND    ( `long` BETWEEN LEAST(InputStartLongitude, InputEndLongitude) AND GREATEST(InputStartLongitude, InputEndLongitude) )
;

Hope that is what you are looking for.

EDIT:

After you have now specified what you really mean by "between", it becomes clear that you first have to think about an algorithm to solve your task - long before you think about the implementation of that algorithm (in MySQL or elsewhere).

Here is a quick sketch of how I would approach a solution of your puzzle:

  1. Get Google driving directions for the requested ride as well as for all offered rides.
  2. These driving directions contain "steps" (see the Maps API) which are basically a list of lat/long points that are all crossed by the calculated trip. You will have to store the driving directions for all offered rides in your db because you will need them again and again to check against each new requested ride.
  3. The basic magic now would be, to find if the steps of a requested ride are a sub-set of the steps of any offered ride in the db. Unfortunately this will nearly never be the case as the first and last mile of the requested ride will always be very specific to the requester's location (e.g. the road from his home to the next highway). So you will have to implement some tolerance here. You could do that by multiple approaches. Let me give you two examples:

    a) Instead of checking whether a requested ride is fully contained in any offered ride (which never happens as explained above) you can try to find which steps the requested ride has in common with any offered ride. From the results take the longest one (with the biggest number of steps). This longest one can be offered to the requester "as is" - it is the best available solution for his request. Finding common sub-parts can be done by 1.) looking for the first common lat/long step coordinate in the driving directions of the requested and any offered ride and 2.) from there on counting all further steps that are identical in both rides. As you have to match a requested ride against all offered rides, the process of finding the first common lat/long coordinate can indeed be very expensive!

    b) Another tolerant algorithm (simpler and cheaper) would be to first discard all steps from the beginning and end of all driving directions (ride request and ride offers) that cover only short distances. These steps will most likely be local driving from and to the next highway. You could then match the shortened lists directly without further tolerance. If the (shortened) request is fully contained in any (shortened) offer, that offer is a hit. This is very cheap to do. You might even be able to do that in MySQL if you create a clever data representation. For example you could create a string representation of all lat/long pairs in the shortened lists and check the db for an offered ride that contains the requested ride as a full sub-string (using MySQL's LIKE '%string%' pattern matching).

I hope that this sheds some light onto your problem and puts you on the right track.

Jpsy
  • 20,077
  • 7
  • 118
  • 115
  • thanks a lot jpsy. Actually what i want to i m saying you frankly. I am making an app in which there are two option 1."Give Ride" and 2."Need Ride". For example USER-A has selected "Give Ride" option then he has to enter some info like "FROM:(StartLat and StartLong)" "TO:(EndLat and EndLong)" using textView. This info will store. – Himanshu Dhakecha Nov 02 '12 at 11:41
  • Now USER-B select other option "Need Ride". He has to enter too same info like before "FROM:(InputStartLat and InputStartLong)" "TO:(InputEndLat and InputEndLong)". Now App will start searching to find matching route for USER-B. If USER-A's route matching with USER-B's then search result will display as names of USERS who select option 1."Give Ride". – Himanshu Dhakecha Nov 02 '12 at 11:41
  • In this app i need some guidelines that i should use database(mysql) or not or any api like driving direction? What should flow and what should i use for this app? Some guidelines will very help full to me thanks in advance. – Himanshu Dhakecha Nov 02 '12 at 11:42
  • 1
    Himanshu, this finally answers the question what you really want to extract from your DB when you say "BETWEEN". Unfortunately that is a completely different "BETWEEN" than you talked about earlier. I guess you actually have to match two google driving directions against each other. You would have to check whether the requested ride is a sub-set of the offered ride. You will also need to allow some margin for mismatch as the first and last few kilometers of a requested ride will deviate nearly always from any given offer (e.g. the trip from final origin/destination to next highway). – Jpsy Nov 02 '12 at 15:53
  • All in all this goes very much beyond a simple database request. But first of all you really have to understand your problem. I have a feeling that you have not really taken that step. I will modify my answer to outline at least an algorithm for your problem. – Jpsy Nov 02 '12 at 15:53
  • Thanks for give me your great understanding. Really..what you have explain above is really really amazing. It is very very helpful to me. Now i will use Google Driving Direction API as you described above in the "a" section. I am really thankful to you. – Himanshu Dhakecha Nov 03 '12 at 06:24
  • stackoverflow allows only one Vote by one user otherwise this answer voted up for so many times. Thank you again – Himanshu Dhakecha Nov 03 '12 at 06:59
  • Glad I could finally help. :) But be warned about approach 3.a: the compute time to match each lat/long pair of a request against all pairs of all offers can be considerable when your db contains a lot of offers. Also, if a requested ride touches a major city or highway junction that is part of many offered rides, step 2 in the algorithm (compare all follow up lat/longs in all results of step 1) can explode. You have to plan your data structures very carefully to make these searches as efficient as possible. Bottomline: Approach 3.b is definitely inferior but faster and easier to implement. – Jpsy Nov 04 '12 at 00:35
  • ya i will try both and then will decide which one is preferable for me ? And i will also report you which one is good and hows my app running on. Thanks you very very much. – Himanshu Dhakecha Nov 04 '12 at 07:59
  • Hi, JPSY, i have make almost every thing for above puzzle but i steel have some little problem, can u guide me ?, so i will put my question here ? thank you – Himanshu Dhakecha Nov 29 '12 at 07:37
  • thank you Jspy..i done this : When offered_ride user enter From, To info in the database all google driving direction all steps(startLat, startLnt, endLat, endLng) will store in database. now requested ride will fire query in database like : "$sql=mysql_query("select username from userlocation where (startLat = $_POST[InputsartLat] And startLong = $_POST[InputsartLng]) And (endLat = $_POST[InputendLat] and endLong = $_POST[InputendLng])");" – Himanshu Dhakecha Nov 29 '12 at 13:04
  • But i steel not getting proper result ...any idea ? – Himanshu Dhakecha Nov 29 '12 at 13:04
  • It looks like you are still only matching the start and end coordinates of offered and requested rides against each other. This has nothing to do with the algorithm that I have proposed. Have you understood the algorithm, incl. the meaning of the term "steps" in a Google directions result? Have you understood what it means to check whether the steps of a requested ride are a sub-set of the steps of any offered ride? – Jpsy Nov 29 '12 at 22:25
  • yah, somewhat i understood and i have done some practically....I am storing steps(Google driving directions : only startLat, startLnt, endLat, endLng)from all offered rides. Now i m Confusion to how to query in the mysql database to check whether the steps of a requested ride are a sub-set of the steps of any offered ride? If i m not on the right way then please tell me.... – Himanshu Dhakecha Nov 30 '12 at 07:13
  • Hi Himanshu, it is not enough to store only the starting and ending coordinates. You need to have the possibility to find sub-sets of steps that a requested ride has in common with any offered ride. To do that you have to store the whole(!) list of steps for each offered ride in the database. When a ride is requested, you have to match its own step list against the lists of steps of all requested rides in the database - as described in point 3 of my algorithm. Quite frankly: I think that you have not understood the proposed solution. – Jpsy Nov 30 '12 at 13:25