CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    What this not work??

    I am trying to find the nearest point of each point saved in a database. I think about make a query, if no results make another query with bigger limits (in retangle) and when the query have at least one result calculate the distances and search the minimum distance.

    I try it:

    Code:
    double CRangoCat::GetMinDis(double fLat, double fLon, double &faLat, double &faLon) {
      vector <double> vfDismin;
      vfDismin.clear();
      double fLatMax = fLat + faLat;
      double fLatMin = fLat - faLat;
      double fLonMax = fLon + faLon;
      double fLonMin = fLon - faLon;
    
      CString szConsulta;
    
      // SQL query
      szConsulta.Format("SELECT DISTINCT %s, %s FROM %s GROUP BY %s, %s, %s HAVING "
        "%s >= %lf AND %s < %lf AND "    // pertenezca al rango de magnitud 
        "%s <= %lf AND "  // menor o igual que fLatMax
        "%s >= %lf AND "  // mayor o igual que fLatMin
        "%s <= %lf AND "  // menor o igual que fLonMax
        "%s >= %lf AND "  // mayor o igual que fLonMin
        "(%s <> %lf OR "   // distinto que fLat
        "%s <> %lf);",     // distinto que fLon
        szCampoLat, szCampoLon, szTabla, 
        szCampoLat, szCampoLon, szCampoMag, 
        szCampoMag, fMinM, szCampoMag, fMaxM, 
        szCampoLat, fLatMax, 
        szCampoLat, fLatMin, 
        szCampoLon, fLonMax, 
        szCampoLon, fLonMin, 
        szCampoLat, fLat, 
        szCampoLon, fLon);
    
      // solutions in a vector.
      vector <CRegistroBD> vSol = bd->consultaSQL((LPCTSTR)szConsulta);
      
      if(vSol.size() == 0) {
       // if the query not have solution increase the limits and execute 
       // the funcion again
        faLat += 0.001;
        faLon += 0.001; 
       double fDistancia = GetMinDis(fLat, fLon, faLat, faLon);
       return fDistancia;
      } else {
        // calcular la distancia mÃ*nima    
        for(int i=0;i<vSol.size();i++) {
          CEarth e;
          CPolarCoordinate p1;
          CPolarCoordinate p2;
          
          p1.SetLeftRightAngleInDegrees(fLon);
          p2.SetLeftRightAngleInDegrees(vSol[i].valorCampoDouble(1));
    
          p1.SetUpDownAngleInDegrees(fLat);
          p2.SetUpDownAngleInDegrees(vSol[i].valorCampoDouble(0));
    
          p1.SetDistanceFromSurfaceInMeters(0.0);
          p2.SetDistanceFromSurfaceInMeters(0.0);
    
          vfDismin.push_back(e.GetLineOfSightDistance(p1,p2));
        }
        sort(vfDismin.begin(),vfDismin.end());
        return vfDismin[0];
      }  
    }
    But i get a infinite loop...

    Thanks in avance

  2. #2
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: What this not work??

    Since you get an infinite loop, most probably the exit condition in the for loop is never satisfied.
    Code:
     i<vSol.size()
    So, I suggest while debugging check to see the vSol.size() before the loop is executed and also put a breakpoint in the for loop and find out why the exit condition is not satisfied.
    Extreme situations require extreme measures

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: What this not work??

    panayotisk is on the right track.

    Some things I'd look into:

    - can you create query conditions where there are results from SQL? (that is, is the query wrong such that you never get results).

    - how can you determine, after you've continually increased the rectangle, that there are no good reasons to continue increasing the rectangle?

    - if there is any data that can satisfy the query, why would an infinitely increasing rectangle fail to ever find data? (That appears to be your terminating decision).

    - how many recursions can you expect? I think you'd run out of patience before you run out of stack space, but is that for certain?


    - can the data itself provide a clue as to what the next increment to the rectangle should be?


    - perhaps the query shouldn't be on a rectangle? Consider, for example, that if your data is sorted by X then Y, search for X's in the range first, then select from that result Y's that are within range.

    - alter the query so that instead of matching, you're scrolling. For example, you're searching for X = 5, Y = 5. Query for X >= 5, and see what's next, then Query for X <= 5 and see what's previous - use the results to limit allowable ranges for Y using a similar method.

    To me, this latter hints that the collected result would be 're-queried' for distance from the points, rather than just the X or Y coordinates. It's an old 'speed trick' from animation. You want distances, but that requires square roots - very slow - so you use simple subtraction in X and Y (and Z) to collect candidates, checking sqrt only on the candidates for the true nearest point.

  4. #4
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: What this not work??

    can you create query conditions where there are results from SQL? (that is, is the query wrong such that you never get results).
    First of this ask I make the query in the database console and retrive some data. I think that the query works fine.
    how can you determine, after you've continually increased the rectangle, that there are no good reasons to continue increasing the rectangle?
    I think if there are at least one result is not necesary increase the rectangle, it is a bad assert because if the point is at top-left (for exmaple) perhaps in the top can find new event nearest that the other case. Thanks for this apreciation.

    I will work in other way, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured