getCenter on a Polygon

945 views
Skip to first unread message

Oraxia

unread,
Nov 9, 2009, 3:53:34 PM11/9/09
to KML Developer Support - Google Earth Plug-in
I'm sorry if this is really basic or answered somewhere that I could
not find--I know little to know KML and am essentially trying to
modify another person's plugin to do something for me.

I need to be able to pan/fly-to a dynamically generated polygon which
is already on the map, but is not currently in the view. I have the
polygon, so I can getOuterBoundary and all that, but is there a
function I can call on some part of this to compute the center (so I
can create a lookAt) or some way of setting a lookAt to encompass the
outer boundary of the polygon?

Thank you!

Markw65

unread,
Nov 9, 2009, 4:46:23 PM11/9/09
to KML Developer Support - Google Earth Plug-in
Here's what I use to set the view to contain a polygon (or rather, the
bounds of the polygon). map_div is the div containing the earth, s,w,n
and e are the south, west, north and east coords of the bounding box:

var sw = new google.maps.LatLng(s,w);
var ne = new google.maps.LatLng(n,e);
var b = new google.maps.LatLngBounds(sw, ne);
var c = b.getCenter();

var d1 = sw.distanceFrom(ne);
var wid = map_div.offsetWidth;
var hig = map_div.offsetHeight;
if (wid > hig) {
d1 = d1*wid/hig;
} else {
d1 = d1 * hig/wid;
}
var cam = ge.createCamera('');
cam.set(c.lat(), c.lng(), d1*.9,
ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 0);
ge.getView().setAbstractView(cam);

It relies on the google maps api, which you may not have loaded. Thats
mainly because I already need it, and it makes it takes care of
wrapping around the dateline, and computing the distance between two
points.

The former is straightforward (and depending on your data, you may not
have to worry about it). You could replace the distance calculation
with something like (note this is an approximation, but should be fine
when used in the above):

var d2r = Math.atan2(1,1)/45;
var r = 6378100;
var h = (n-s);
var w = (e-w)* cos((n+s)*d2r/2);
var d1 = Math.sqrt(h*h+w*w)*d2r * r;

Mark

Oraxia

unread,
Nov 9, 2009, 5:17:17 PM11/9/09
to KML Developer Support - Google Earth Plug-in
Thank you so much for answering :)

I don't have the Maps API in this project, and I'm not sure if I
should load it just for this, but if worst comes to worst, I figure
will have to average the latitudes and longitudes of my polygon to
approximate a center (basically like your second suggestion). I'd used
getCenter in the Maps API before, and had figured there would surely
be a similar function in Earth's API that would do a better job :/
Sounds like there may not be.

Thanks,
X

jmatthews

unread,
Nov 10, 2009, 3:39:42 PM11/10/09
to KML Developer Support - Google Earth Plug-in
You can't average them. What if the polygon is an L-shape?

The formula is a bit more complex. Look at the Centroid calculation
here (the 2nd calculation on the page): http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

I have seen a few easier-to-read ways of showing the formula. You can
just Google "formula calculate polygon centroid".

Markw65

unread,
Nov 10, 2009, 4:40:32 PM11/10/09
to KML Developer Support - Google Earth Plug-in
Sure you can average them - but if you're trying to center the view to
look at the entire polygon, its not the right thing to do.

But neither is the centroid, and for exactly the same reason...

Think about it - if 100 points are along the base of a long thin
triangle, and one is at the opposite vertex, the centroid (or the
average of the lat/lngs) is going to be very near to the base. But you
want to be looking at the center of the bounding box of the triangle
(because you want all the points to be visible, and you want the
polygon to more-or-less fill the screen).

The point is that you dont really care about the distribution of the
vertices, just the boundary.

So like I said, the correct operation is to compute the bounding box,
and then get the center of that (which is trivial, leaving out the
problem of wrapping the date line).

Mark

jmatthews

unread,
Nov 10, 2009, 5:48:11 PM11/10/09
to KML Developer Support - Google Earth Plug-in
True, if you don't want a centroid. "Centroid" defines a point within
the polygon. It's not necessary to have centroid if all you are
trying to do is compute a decent bounding box. Some people might want
a centroid to place a point INSIDE the boundaries of the polygon.
> > > > > Thank you!- Hide quoted text -
>
> - Show quoted text -

Markw65

unread,
Nov 10, 2009, 6:55:01 PM11/10/09
to KML Developer Support - Google Earth Plug-in


On Nov 10, 2:48 pm, jmatthews wrote:
> True, if you don't want a centroid.  "Centroid" defines a point within
> the polygon.  It's not necessary to have centroid if all you are
> trying to do is compute a decent bounding box.  Some people might want
> a centroid to place a point INSIDE the boundaries of the polygon.

Why do you think the centroid is going to be inside the polygon? Think
about your "L" shape again.

And again - the goal was to center the polygon within the view.
Putting the centroid of the polygon (or the mean of the vertices) at
the center of the view isnt going do that in general. But centering
the bounding box of the polygon within the bounds of the view /is/.

Its not a question of using the bounding box as a cheap alternative to
the centroid. The bounding box is much better than the centroid for
this application.

Mark
Reply all
Reply to author
Forward
0 new messages