• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

Center point of a rectangle on a cartesian map

Hi,

This is similar to another question I had previously. I have a rectangle, which is placed on a cartesian map. I know the latitude/longitude of the 4 corner points. Something like:

  lat1 = top corner
  lon1 = left corner
  lat2 = bottom corner
  lon2 = right corner

I just want to find the center point of the box, in lat, longitude. I can do this:

  centerLat = lat1 + ((lat2-lat1) / 2.0);
  centerLon = lon1 + ((lon2-lon1) / 2.0);
       
but is that sufficient, considering that the values can "wrap" around the globe at some points?Also not sure about if the equaton for latitude is correct since it increases on the map as you go north?

Thanks
0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 6
  • 2
2 Solutions
 
phoffricCommented:
This appears to be a refinement of your previous question, where you established that you were dealing with a Mercator projection. Reviewing the answers in the previous post, there appears to be a need to work out some examples for at least one of the solutions.

Is it safe to say that your box will not contain a North or South poles (i.e., +/- 90 degrees lat)? I'll go with that for now. Certainly, it should be safe to assume that the poles are not interior to the box. (Possibly a pole could be on the horizontal edge of the box, but then we have to discuss the single point vs entire line caveat.)

Provided that both lats are in bounds (-90..+90), for the lat, you can simply use:
    centerLat =   (lat2 + lat1) / 2; (a rewrite of your function)

Even if the sum of the two lats is greater than 90, dividing by 2 brings it into bounds. So, for example, if two lats are 50 and 70, then 120 (is out of bounds), but 120/2 is 60 degrees latitude which is in bounds and is midway between the two lats. Keep in mind that in a mercator mapping, the north pole is represented by the top horizontal line; i.e. all the points on that top line represent a single point (north pole) on the sphere; so, as you get close to the north or south pole, the map will appear very distorted.

The long requires a little more explanation...
0
 
ozoCommented:
For regions that are not too large, and not to close to the poles. (so that they can be reasonably called rectangles)
you can handle wrap around with something like
 centerLon = (llon1 + (((lon2-lon1) modulo 360) / 2.0)) modulo 360

for arbitrarily distorted regions, it may be better to convert spherical coordinates to 3-d cartesian vectors,
take the 3-d vector average, then convert back
0
 
phoffricCommented:
Now, lon is in range [-180..+180].
I was looking at:
    centerLon = (lon1 + (((lon2-lon1) modulo 360) / 2.0)) modulo 360

Believe modulo

Suppose lon1 = +170, lon2= -150, then
centerlon = (170 + (((-150 - 170) modulo 360) / 2.0)) modulo 360
         = (170 + ((-320 modulo 360) / 2.0)) modulo 360
         = (170 + ((40) / 2.0)) modulo 360
         = (170 + (20)) modulo 360
         = (190) modulo 360 = 190
But correct answer should be -170 to be in range. So, let's consider subtracting 360 if answer > 180.

Also, let's consider result for case where lat1 = lat2 = +170 (i.e., box contains 180 and has extent of 360).

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
phoffricCommented:
Believe modulo is defined to return a positive number (at least in some circles). But I was thinking Zn integer arithmetic. Then again, if dealing with int, then in C, (lon2-lon1) % 360) does return negative -320 for above example.

Need your definition of modulo to make above formula work.
0
 
ozoCommented:
In C, you might define modulo(a,b) as ((a%b+b)%b)
0
 
phoffricCommented:
But % is defined for int. I think we need a float solution.

In the meantime, I tried running NovaDenizen's code:
For lon1 = +170, lon2= -150, it returns correct answer of -170.
For lon1 = +170, lon2= 169.9, it returns correct answer of -10.05, but it needs a little tweaking when the extent is 360 degrees (e.g., lon1 = lat1 = 170). Here is the original code:
double normalize_angle(double base_angle, double angle) {
    while (angle < base_angle) {
        angle = angle + 360;
    }
    while (angle > base_angle + 360) {
        angle = angle - 360;
    }
	return angle;
}

void main() {
double lon1 = 170.0, lon2=170.0;	  
double lon_extent = normalize_angle(0, lon2 - lon1);
double center_lon = normalize_angle(-180, lon1 + 0.5 * lon_extent);
printf(" center_lon = %f\n", center_lon);
}

Open in new window

0
 
phoffricCommented:
Here's a tweak - will check for other wrapped cases (if any):

double normalize_angle(double base_angle, double angle) {
	if( angle == 0.0 ) {
		return 360;
	}
    while (angle < base_angle) {
        angle = angle + 360;
    }
    while (angle > base_angle + 360) {
        angle = angle - 360;
    }
	return angle;
}

Open in new window

0
 
phoffricCommented:
Above tweak seems to cover most cases of lon1==lon2. But don't make them both either + or - 180, which probably is a reasonable thing to ask.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now