Not Yet Answered # Convert Latitude and longitude to x,y in java

masijade 1,351 NormR1 563 Discussion Starter madawa123 peter_budo 2,516 Discussion Starter madawa123 peter_budo 2,516 Discussion Starter madawa123 Discussion Starter madawa123 NormR1 563 Discussion Starter madawa123 NormR1 563 Discussion Starter madawa123 NormR1 563 Discussion Starter madawa123 Discussion Starter madawa123 NormR1 563 Discussion Starter madawa123 peter_budo 2,516 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

0

Hi guys,

I hv the two coordinates of the map.

Top left : 37 44 55.49S 144 52 30.73E

bottom right: 37 47 54.43S 144 57 59.54E

my map is a rectangle with 500width 800height.

I want to get the mouse pointer in terms of lat long in degrees and minutes with x,y through out the map.Example if i click the map it should return lat, long and x, y)

How do i convert the coordinates to something mathematical?

How am i going to achieve this ?

Thanks in advance

Regards,

Nishan

0

Well, lets see. A latitude/longitude coordinate is in degrees, minutes, seconds (with decimal). 60 seconds to a minute, 60 minutes to a degree. So, first, convert your two longitudes to "absolute" numbers (i.e. (degrees * 3600) + (minutes * 60) + secs) and get the difference between the two (i.e. the higher - the lower). Now, divide this by the pixel width of the image, save these three numbers (the left, right, and interval). Now, do the same with the lattitudes and the height.

Now, when someone clicks on the map get that coordinate in pixels. Multiply the "x" by the width interval and the y be the height interval (if you want the coordinate from the "center" of the pixel, rather than the upper left corner of the pixel than add half the interval to each). Now add the "x" result to the "left" number (or subtract it if the left number was the larger). Do the same with the "y" result and the "top" number.

Now, (number / 3600) rounded down is the degrees, ((number % 3600) / 60) rounded down is the minutes, this ((number % 3600) % 60) are the seconds.

0

You need to scale the pixels to the number of degrees covered by the map.

You need the lat/long of the upper left corner and for the lower right corner.

Given those, you can convert any mouse click's x,y to a lat/long.

There may be some trigonometry required at higher latitudes because of the converging of the longitude lines as you go north.

0

hi guys,

thanks for the reply.

i'm just confused about the

first, convert your two longitudes to "absolute" numbers (i.e. (degrees * 3600) + (minutes * 60) + secs

)

shouldnt it be : degrees + (minutes * 60) + (secs*3600)

and in here

Now add the "x" result to the "left" number (or subtract it if the left number was the larger). Do the same with the "y" result and the "top" number.

what is top and what is left ?

thnx

0

masijade gave you clear formula how to convert an entry of 12degrees 34minutes 56sec down to seconds value that you can then apply to your image calculations.

Your formula "degrees + (minutes * 60) + (secs*3600)" is completely wrong. Can you explain what are you trying to calculate out of it?

0

hey,

i just got that ref from the http://www.csu.edu.au/australia/latlong/dms.html,

I just wrote a small program to test the values calculation

```
//top lat
private static double toplatd = -37;
private static double toplatm = -44;
private static double toplats = -55.49;
//top lon
private static double toplond = 144;
private static double toplonm = 52;
private static double toplons = 30.73;
//bot lat
private static double botlatd = -37;
private static double botlatm = -47;
private static double botlats = -54.43;
//bot lon
private static double botlond = 144;
private static double botlonm = 57;
private static double botlons = 59.54;
private static double toplat;
private static double toplon;
private static double botlat;
private static double botlon;
private static double decimalVal = 0;
private static double picwidth = 800;
private static double picheight = 600;
private static double clickx = 30;
private static double clicky = 30;
private static double xinterval;
private static double yinterval;
public static double getValue(double d, double m, double s){
// decimalVal = d + (m/60)+ (s/3600);
decimalVal = (d*3600) + (m/60) + s;
double myNum = decimalVal;
int precision = 1000000; //round to 6 digits
myNum= Math.floor(myNum * precision +.5)/precision;
return myNum;
}
public static double xInterval(){
double xint = botlat - toplat;
return xint/picheight;
}
public static double yInterval(){
double yint = botlon - toplon;
return yint/picwidth;
}
public static double getXCordinate(double xinterval){
double myNum = clickx*xinterval;
int precision = 1000000; //round to 6 digits
myNum= Math.floor(myNum * precision +.5)/precision;
return myNum;
}
public static double getYCordinate(double yinterval){
double myNum = clicky*yinterval;
int precision = 1000000; //round to 6 digits
myNum= Math.floor(myNum * precision +.5)/precision;
return myNum;
}
public static void main(String args[]){
toplat = getValue(toplatd,toplatm,toplats);
System.out.println("Lat of top point = "+toplat);
toplon = getValue(toplond,toplonm,toplons);
System.out.println("Lon of top point = "+toplon);
botlat = getValue(botlatd,botlatm,botlats);
System.out.println("Lat of bot point = "+botlat);
botlon = getValue(botlond,botlonm,botlons);
System.out.println("Lon of bot point = "+botlon);
xinterval = xInterval();
System.out.println("X interval = "+xinterval);
yinterval = yInterval();
System.out.println("Y interval = "+yinterval);
double xincrement = getXCordinate(xinterval);
System.out.println("X increment = "+xincrement);
double yincrement = getYCordinate(yinterval);
System.out.println("Y increment = "+yincrement);
double x = xincrement + toplon;
System.out.println("X is = "+x);
double y = yincrement + toplat;
System.out.println("Y is = "+y);
double ddd = Math.round(y/3600);
double mmm = Math.round((y%3600)/60);
double sss = Math.round((y%3600)%60);
System.out.println("LAT = "+ddd+" "+mmm+" "+sss);
double dd = Math.round(x/3600);
double mm = Math.round((x%3600)/60);
double ss = Math.round((x%3600)%60);
System.out.println("LON = "+dd+" "+mm+" "+ss);
}
```

which result following values,

**Lat of top point = -133256.223333
Lon of top point = 518431.596667
Lat of bot point = -133255.213333
Lon of bot point = 518460.49
X interval = 0.0016833333333488554
Y interval = 0.03611666625001817
X increment = 0.0505
Y increment = 1.0835
X is = 518431.647167
Y is = -133255.139833
LAT = -37.0 -1.0 -55.0
LON = 144.0 1.0 32.0**

I'm not sure weather I'm doing this right or wrong.Help me out with this.

thnx

0

Well madawa123 has problems to reference properly as he asked about

degrees + (minutes * 60) + (secs*3600)

and page says

degrees + (minutes/60) + (seconds/3600) (difficult to see, but it uses divide "back slash" instead of times "*")

*Edited 6 Years Ago by peter_budo*: n/a

0

thanks for the help masijade, i think i got an idea. hopefully i can build up on it.

0

hey guys,

How to convert the lat/long (in decimal) to x,y pixel.

my map has a height and width of 800,500

thnx

regards,

Nishan

0

You need to give lat/long values for the corners of the map area to be able to determine the x,y pixel values for a given lat/long.

Are the lat/long positions like this: 12.23456 degrees North 89.44323 degrees West?

0

i made a mistake. please dont reply to this thread. use the

http://www.daniweb.com/forums/thread314734.html as the correct thread..

again sorry for the mistake

0

Do you have a response to my last post here?

I wrote an applet that plots waypoints given in lat/long:

http://www.waypoint.org/np/WayPoints.html

*Edited 6 Years Ago by NormR1*: n/a

0

hey NormR1,

i'm reading a file which contents cordinates as follows

144.676270 -38.256588

the cordinates for the map is

Top left : 37 44 55.49S 144 52 30.73E -> -37.74861, 144.875 (approx)

bottom right: 37 47 54.43S 144 57 59.54E -> -37.79833, 144.96639 (approx)

map height 800 width 500

thnx

0

Are the lat/long in degrees, minutes and seconds?

Did you convert them to decimal degrees by doing: degrees + min/60 + sec/3600

Then "normalize" the values to map to 0,0 to 800,500

Subtract the min degress from the max degress and divide by the pixel length for that direction.

Also you need to consider what latitude you are at. The longitude lines come together as you go North/South.

This will depend on whether you are North or South and East or West.

Best to **use a piece of paper** for each of the 4 cases (NE, NW, SE, SW) and write the lat/long for the upper left corner which will be 0,0 in pixels and the lower right corner: 800, 500

There will be distortion if the drawing map area does not have the same proportions as the lat/long ranges.

Is Your chart is over western Melbourne

*Edited 6 Years Ago by NormR1*: n/a

0

hey,

yep i do convert it using (degrees + min/60 + sec/3600), but i guess i was thinking about a formula to give the answer for pixel. Is there is such thing ?

Since there isnt a much difference in lat long and all of them are in same format stated above that is South , East.

*Edited 6 Years Ago by madawa123*: n/a

0

does that mean i have to divide top left from the value read from te file like x3= x2-x1, y3 = y2-y1

and divide x3 by map height and divide y3 width respectively ?

am i wrong here ?

0

Use a piece of paper to map out the pixels vs the lat/long.

The upper left corner is 0,0 in pixels and the North West corner in lat/long.

The distance from the left side to the right side is the max deg long minus the min deg long adjusted for latitude. Divide that by the width in pixels.

This is very simple math.

0

Hi guys,

latlong are given in decimal

topleft - 144.875 37.748747

bottomright - 144.966539 37.798453

have a map scale of 500*800

how can i transform this using affine transform so it will give exact pixels and lat long in image when needed.

thanks

0

@madawa123 do not create new threads on same topic! You already been given help so work with that, we are not homework solving facility.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```