GPX bounds element


The GPX file has a very convenient element called bounds which defines the bounding box of all of the tracks defined in the GPX file. Here is a sample:

<gpx ...>
<time>2007-05-28T05:51:39Z</time>
<bounds
    minlat="36.954912879"
    minlon="-121.989988927"
    maxlat="37.225568471"
    maxlon="-121.976840235"/>
...
</gpx>

This elements allows you to quickly find where to center your map and also allows you to draw a bounding box around track. Without this element you would need to find the min. and the max lat. and long. of all the track points in the GPX file.

Here is a JavaScript function to create a bounding box (using Google Map API) around the track (the $ notation (Perl) means the value of these variables on the server where you’re creating the JavaScript):

function createBoundingBox() {
       var box= [];
       box.push(new GPoint($minlon, $maxlat));
       box.push(new GPoint($maxlon, $maxlat));
       box.push(new GPoint($maxlon, $minlat));
       box.push(new GPoint($minlon, $minlat));
       box.push(new GPoint($minlon, $maxlat));
       map.addOverlay(new GPolyline(box));
}

Google API also includes class GBounds that allows you to create a bounding box object and has methods to find the intersection of two bounding boxes, if a point is included in a bounding box, etc. You can create a GBounds object using the GPX bounds elements, e.g.,

var bounds = new GBounds($minlon, $minlat, $maxlon, $maxlat);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: