Lat/Long to Grid Square Conversion

In the process of making my WSPR beacon, I wanted the system to both use the time from GPS to sync the transmissions accordingly, but also have the system be portable in that it would generate the needed grid square information from the GPS position.

For this, I needed to write a small function that could handle that conversion for me, and I thought I would share that, at least until I’m ready to share more of the project.

void calculate_grid_square(int lat, int lon){
 if(lon < 0){
   locator[0] = 'A' + ((lon + 180) / 20);
   locator[2] = 9 - ((abs(lon) % 20) / 2);
 }else{
   locator[0] = 'J' + (lon / 20);
   locator[2] = ((lon % 20) / 2);
 }

 if(lat < 0){
   locator[1] = 'A' + ((lat + 90) / 10);
   locator[3] = 9 - (abs(lat) % 10);
 }else{
   locator[1] = 'J' + (lat / 10);
   locator[3] = (lat % 10);
 }
}

This function takes in the integer latitude and longitude coordinates (for example, -122, 47), and will update the values in a globally defined char array named “locator”. You could easily modify to assign the variable as you like.

Also note that this is only a four character locator rather than the full six, as the standard WSPR message only supports a four character locator. You can do a full locator with an extra message, but I’m not going to bother.

Also also note, this was done in the Arduino environment, so if you’re using something else, you may not have, or the names may be different, for functions like abs() (absolute value).

Anyway hope somebody finds it useful.

This entry was posted in Ham Radio, Technology. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *