Question : Drawing a grid with gps coordinates

Hi

I'm developing a system that takes a users address and gets the GPS coordinates.
When I have the GPS coordinates I want to register this user to a 3km*3km pre-described block.

I need to build this grid.
Each block on this grid would have a GPS coordinate on every corner.
These blocks would be recorded on a  database as follow.
BlockID - KDP001
NorthLat  - x  
NorthLong  - y
EastLat  - x
EastLong  - y
SouthLat  - x
SouthLong - y
WestLat -   x
WestLong - y
 
I will then take the coordinates that I get from the users address and just do > < calculations to determine the block ID a user falls under.

My question is - how can I get all the coordinates of the blocks ??

I can calculte the distance between 2 GPS points.
So a possible solution would be that if I enter a point then I should calculate the GPS point 3km horizontaly away from that point and also vertically.



But this is where I'm getting stuck!
Any help please?


Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
The code to calculate distance between 2 GPS points.
 
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
  double theta = lon1 - lon2;
  double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
  dist = Math.acos(dist);
  dist = rad2deg(dist);
  dist = dist * 60 * 1.1515;
  if (unit == 'K') {
    dist = dist * 1.609344;
  } else if (unit == 'N') {
  	dist = dist * 0.8684;
    }
  return (dist);
}
 
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts decimal degrees to radians             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
  return (deg * Math.PI / 180.0);
}
 
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts radians to decimal degrees             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad) {
  return (rad * 180.0 / Math.PI);
}

Answer : Drawing a grid with gps coordinates

Guess the experts are very busy!

For anyone wanting this answer - I figured it out.
This is done in java!
This code will draw a grid block on a map. the blocks are 3km * 3km.

the folloing variables set the size of the grid blocks
double d1 =  - 0.027099;   // Vertical size  = 0.009033 per KM
double d2 = 0.030000;  //Horizontal size  =  0.010000 per KM

Combine coordinates  North & West to get top left corner of the block.
Combine North & east to get right top corner of block
Combine South & WEst to get bottom left coordinates of block
Combine South & East coordinates to get bottom right corner of a block.

the first 'for statent gives the mount of blocks on a horizontal line.
for(int b = 1; b<10 ; b++)

The second gives the amount of vertical blocks
 for(int i = 1; i<=10 ; i++)

Sorry, the code isn't very clean , but this would give you the basics.
Hope this helps someone!

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
double startLat = -22.073256;
    double lat = -22.073256;
    double startLon = 16.233655;
    double lon = 16.233655;
    
    double sLat;
    double sLon;
    
    double north;
    double east;
    double south;
    double west;
    
    double d1 =  - 0.027099;
    double d2 = 0.030000;
 
    int tot = 0;
    long precision = 1000000; // 2 decimal place precision
 
    
    
   for(int b = 1; b<10 ; b++) {
    
       north = startLat;
       west = startLon;
          startLon = lon; 
        
          for(int i = 1; i<=10 ; i++) 
           {
               north = startLat;
               east = startLon;      
             // extract the integer part
            long loni = (long)Math.round(startLon);
            long lond = (long)Math.round((startLon-loni)*precision);
           // extract the decimal part and make an integer value
            // based upon precision
            long d2i = (long)Math.round(d2);
            long d2d = (long)Math.round((d2-d2i)*precision);
 
           // add the two corresponding parts
            long totali = loni + d2i;
            long totald = lond + d2d;
          // calculate the floating point result
            double total = totali + ((double)totald / precision);
            startLon = total; 
                        
           //insert into database North south east & west
            
             east = startLon;         
            //Calculate other lat variables
            
            sLat = startLat;
            long lati = (long)Math.round(sLat);
            long latd = (long)Math.round((sLat-lati)*precision);
           // extract the decimal part and make an integer value
            // based upon precision
            long d1i = (long)Math.round(d1);
            long d1d = (long)Math.round((d1-d1i)*precision);
 
           // add the two corresponding parts
             totali = lati + d1i;
             totald = latd + d1d;
          // calculate the floating point result
            total = totali + ((double)totald / precision);
            south = total;
                     
                
        tot++;
        
        System.out.println("Block to DB =  North = "+north+ " South = " + south + "  East = " + east + "  West = "+west);
            
         } 
         
        long lati = (long)Math.round(startLat);
            long latd = (long)Math.round((startLat-lati)*precision);
           // extract the decimal part and make an integer value
            // based upon precision
            long d1i = (long)Math.round(d1);
            long d1d = (long)Math.round((d1-d1i)*precision);
 
           // add the two corresponding parts
            long totali = lati + d1i;
            long totald = latd + d1d;
          // calculate the floating point result
            double total = totali + ((double)totald / precision);
            startLat = total;
                     
           System.out.println("End Block = " + lat +" , "+ startLon);
          tot++;
          total =0;
        
      
      }
    
    System.out.println("Total records = " + tot);
    
    
Random Solutions  
 
programming4us programming4us