Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing close range GPS coordinates on a JPanel

I'm facing issue for a class where I need to convert and draw GPS coordinates dynamically into a JPanel. I have this local library data in a file that I parse. The structure is ID, County, Library Name, Latitude, Longitude.

R1,Ramsey,Maplewood,45.0327,-93.0262
R2,Ramsey,Mounds View,45.1059,-93.2104
R3,Ramsey,New Brighton,45.06604,-93.19125
R4,Ramsey,North St. Paul,45.0105,-92.9968
R5,Ramsey,Roseville,45.0072,-93.1558
R6,Ramsey,Shoreview,45.0805,-93.1345
R7,Ramsey,White Bear Lake,45.0831,-93.0092

As you can see, the locations are very close together (7 locations in 170 sq miles, Max Distance apart: 15.5 miles). My code currently can draw and link the nodes properly from hardcoded coordinates instead of the GPS data. I have found algorithms for converting the GPS lat and long to XY coordinates, but once the calculations happen, all the objects print on top of each other since the calculation leads to the same XY because of it being so close. The process needs to be dynamic because I anticipate that the test files to be use will be using 42 locations. What can I do for an equation that will give me XY coordinates that have enough variety to make a decent graphic rendering instead of using random points?

like image 453
Moobius Avatar asked Dec 29 '25 10:12

Moobius


2 Answers

What can I do for an equation that will give me XY coordinates that have enough variety to make a decent graphic rendering instead of using random points?

Find the bounding box. In other words, find the smallest x and smallest y coordinate in your list. Find the largest x and largest y coordinate in your list. Those two points define the bounding box.

Now, translate these location x, y coordinates to drawing x, y coordinates. Your smallest location x, y coordinate becomes drawing coordinate 0, 0. Your largest location x, y coordinate becomes drawing coordinate width, height.

In order to keep the scaling from distorting the image, we have to first calculate the scaling factor for the x coordinate and the y coordinate.

scaling factor x = drawing width / (location maximum x - location minimum x)
scaling factor y = drawing height / (location maximum y - location minimum y)

Then we use the smaller of the two scaling factors.

scaling factor = Math.min(scaling factor x, scaling factor y)

The equations for converting location coordinates to drawing coordinates are:

drawing x = location x * scaling factor
drawing y = location y * scaling factor

Location and scaling factor are doubles, so you don't drop any precision. You have to convert drawing x and drawing y to integers so you can plot them.

Drawing x increases from west to east. Location x probably increases from west to east.

Drawing y increases from north to south. If location y increases from south to north, then you have to take that into account when doing the conversion.

drawing y = drawing height - drawing y

You'll probably will want to add a margin to your drawing area so that none of the locations are drawn on the edge of the drawing area.

Let's say you want a margin of 10 pixels. That would make the actual drawing area 20 pixels larger in width and 20 pixels larger in height.

You then add 10 pixels to the drawing x and the drawing y before you plot your location.

like image 140
Gilbert Le Blanc Avatar answered Dec 30 '25 22:12

Gilbert Le Blanc


The ideal way to go about this is to find the min-longitude and min-latitude , max-longitude and max-latitude and map them to the [0,0] and [JPanels.width , Jpanels.Height].

this map can be done just

Point map(cure_location){
   int X = (curr_location.longitude-min_longitude)*(scalelog);
   int Y = (curr_location.latitude-min_latitude)*(scalelon);
   return new Point(X,Y);
}

and scalelog and scalelon are

scalelog = (JPanels.width)/(max_longitude-min_longitude)
scalelat = (JPanels.height)/(max_latitude-min_latitude)
like image 44
DarthCoder Avatar answered Dec 30 '25 22:12

DarthCoder