J/Link Tutorial

Visualizing Flight Routes with Mathematica\(TraditionalForm\`\^®\)

In aviation, a map of the planned flight route helps the pilot to increase situational awareness by giving a clear picture of where the flight path is going.  This tutorial uses three of the tools for flight planning available on the web.  Using the J/Link functionality in Mathematica 4.2, you can obtain the coordinates for a given airport from a web server and use them to get small maps of the airport environment from a second web server.  Flight routes can be depicted by passing the airport identifiers of the routing to a third web server.  The java.net.URL class allows you to retrieve several types of data from various URLs.  In this example you will access the contents of a URL with some of Mathematica's J/Link commands.

The procedure is as follows.  First, you set up the java.net.URL class, then open a stream and obtain the contents of the URL, which are stored in a string.  In a separate step, you have to determine the MIME type of the contents received from the URL so Mathematica knows how to process it.  The following Mathematica program segments demonstrate how to implement these steps.

First, load J/Link.

<< JLink`

Off[General::spell1];

This function takes a URL as a string and performs the steps mentioned above to retrieve the contents from the URL.

\(GetURLAsString[url_String] := 
      JavaBlock[
        Module[{u, stream, numRead, outFile, buf, s}, 
          InstallJava[]; 
u = 
            JavaNew["java.net.URL", url]; 
stream = 
            u@openStream[]; 
If[stream === $Failed, 
            Return[$Failed]]; 
buf = 
            JavaNew["[B", 
              50000]; 
s = ""; 
While[\((numRead = stream@read[buf])\) > 0, 
            s = \((s <> 
                  FromCharacterCode[\((If[# < 0, # + 256, #] &)\) /@ 
                      Take[Val[buf], numRead]])\)]; 
stream@
            close[]; 
s]]\)

Mathematica uses the following function to determine the MIME content type of the data received from the URL.

GetURLType[url_String] := 
      JavaBlock[
        Module[{u, conn, 
            type}, 
InstallJava[]; 
u = 
            JavaNew["java.net.URL", url]; 
stream = 
            u@openStream[]; 
conn = 
            u@openConnection[]; 
type = 
            conn@getContentType[]; 
stream@
            close[]; 
type]]

The flight route is specified as a string consisting of FAA (U.S. only), ICAO, or IATA airport identifiers separated by dashes. This specifies a short flight from Champaign, Illinois to Pittsburgh, Pennsylvania.

routestring = "kcmi-kpit";

In Mathematica 4.2 the function ImportString now has a second argument that specifies the MIME type of the data to be imported. With the definitions above, you can now depict a map of a direct route for the given airports.

url = "http://gc.kls2.com/cgi-bin/gcmap?PATH=" <> 
        routestring <> "&MARKER=1&SPEED-UNITS="; 
    Show@ImportString[GetURLAsString[url], GetURLType[url]];

The routing can involve airports between the two endpoints.

\(\(routestring = "kbdr-kbvi-9g2-kcgx-khnr-klmo-klvs-kffz-kprc-kffz-0v7-kgcn-kffz-ksez-kbce-kvgt-kmod-khaf-klgu-khnr-kbvi-kbdr";\)\)

url = "http://gc.kls2.com/cgi-bin/gcmap?PATH=" <> 
        routestring <> "&MARKER=1&SPEED-UNITS="; 
    Show@ImportString[GetURLAsString[url], GetURLType[url]];

Map scaling is done automatically. The following depicts a direct route from Chicago to Reykjavik.

routestring = "kord-birk";

url = "http://gc.kls2.com/cgi-bin/gcmap?PATH=" <> 
        routestring <> "&MARKER=1&SPEED-UNITS="; 
    Show@ImportString[GetURLAsString[url], GetURLType[url]];

The J/Link functions used in this example are extremely broad in scope. The same functions can be used unmodified to retrieve small maps of the areas surrounding the given airports. As before, you can specify the airports using routestring and then display small airport maps.

routestring = "kbdr-kmtp-kack-ksez-khaf-kmyf";

airportlist = 
        ReadList[
          StringToStream[
            StringReplace[
              ToUpperCase[routestring], "-" -> " "]], 
          Word];
\(\(Do[airportid = airportlist[\([i]\)]; 

        mapurl = "http://www.airnav.com/airport/" <> airportid; 

        htmllist = 
          ReadList[
            StringToStream[
              StringReplace[
                GetURLAsString[
                  mapurl], {"<BR>" -> " ", "</TD>" -> " "}]], Word]; 

        latstring = \(htmllist[\([Flatten[
                    Position[htmllist, "(estimated)"]] - 
                  3]\)]\)[\([1]\)]; 

        lonstring = \(htmllist[\([Flatten[
                    Position[htmllist, "(estimated)"]] - 
                  1]\)]\)[\([1]\)]; 

        map = "http://tiger.census.gov/cgi-bin/mapgen?lon=" <> 
            lonstring <> "&lat=" <> 
            latstring <> "&wid=0.4&iwd=200&iht=200&mark=" <> 
            lonstring <> "," <> latstring <> ",bluestar," <> 
            airportid <> "&on=GRID,CITIES,states,majroads,water,shorelin&off=counties,streets,railroads,miscell"; 

        Show@ImportString[GetURLAsString[map], GetURLType[map]], {i, 
          Length[airportlist]}];\)\)

Credits: Maps provided by Karl Swartz's Great Circle Mapper (gc.kls2.com). Airport location data provided by Paulo Santos' Airnav.com (www.airnav.com). City maps provided by U.S. Census Tiger Maps (tiger.census.gov).

© 2002 Wolfram Research, Inc. All rights reserved. Mathematica is a registered trademark of Wolfram Research, Inc. Mathematica is not associated with Mathematica Policy Research, Inc. or MathTech, Inc.