Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Terra Server/Client

consists of three files


CityView.aspx

<html>
  <body>
    <h1>CityView</h1>
    <hr>
    <form runat="server">
      <table cellpadding="8">
        <tr>
          <td>
            City
          </td>
          <td>
            <asp:TextBox ID="City" Width="100%" RunAt="server" />
          </td>
          <td>
            <asp:RequiredFieldValidator
              ControlToValidate="City"
              ErrorMessage="*"
              Display="static"
              Color="red"
              RunAt="server"
            />
          </td>
        </tr>
        <tr>
          <td>
            State
          </td>
          <td>
            <asp:DropDownList ID="State" Width="100%" RunAt="server">
              <asp:ListItem Text="AL" RunAt="server" />
//...
            </asp:DropDownList>
          </td>
          <td>
          </td>
        </tr>
        <tr>
          <td>
          </td>
          <td>
            <fieldset>
              <legend>Scale</legend>
              <asp:RadioButtonList ID="Scale" RunAt="server"
                RepeatColumns="2" RepeatDirection="Horizontal">
                <asp:ListItem Text="1 meter" RunAt="server" />
                <asp:ListItem Text="2 meters" RunAt="server" />
                <asp:ListItem Text="4 meters" RunAt="server" />
                <asp:ListItem Text="8 meters" Selected="true"
                  RunAt="server" />
                <asp:ListItem Text="16 meters" RunAt="server" />
                <asp:ListItem Text="32 meters" RunAt="server" />
              </asp:RadioButtonList>
            </fieldset>
          </td>
          <td>
          </td>
        </tr>
        <tr>
          <td>
          </td>
          <td>
            <asp:Button Text="Show Image" OnClick="OnShowImage"
              Width="100%" RunAt="server" />
          </td>
          <td>
          </td>
        </tr>
      </table>
    </form>
    <hr>
    <asp:Image ID="MyImage" RunAt="server" />
  </body>
</html>

<script language="C#" runat="server">
  void OnShowImage (Object sender, EventArgs e)
  {
      StringBuilder builder = new StringBuilder ();
      builder.Append ("CityView.ashx?city=");
      builder.Append (City.Text);
      builder.Append ("&state=");
      builder.Append (State.SelectedItem.Text);
      builder.Append ("&scale=");

      switch (Scale.SelectedIndex) {
      case 0:
          builder.Append ("1");
    //...
      }

      MyImage.ImageUrl = builder.ToString ();
  }
</script>

CityView.ashx

<%@ WebHandler Language="C#" Class="CityViewImageGen" %>


public class CityViewImageGen : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
        // Extract user input from the query string
        string city = context.Request["City"];
        string state = context.Request["State"];
        string scale = context.Request["Scale"];

        if (city != null && state != null) {
            // Determine the scale
            TS.Scale res = TS.Scale.Scale8m;
// . . .
            // Generate the requested image
            string type = "image/jpeg";
            ImageFormat format = ImageFormat.Jpeg;
            Bitmap bitmap = GetTiledImage (city, state, res, 600, 400);

// . . .
            // Write the image to the HTTP response
            bitmap.Save (context.Response.OutputStream, format);

        }
    }

    Bitmap GetTiledImage (string City, string State, TS.Scale Scale,
        int cx, int cy)
    {
        Bitmap bitmap = null;
        Graphics g = null;

        try {
            // Instantiate the TerraService proxy
            TS.TerraService ts = new TS.TerraService ();

            // Get the latitude and longitude of the requested city
            TS.Place place = new TS.Place ();
            place.City = City;
            place.State = State;
            place.Country = "USA";
            TS.LonLatPt point = ts.ConvertPlaceToLonLatPt  (place);

            // Compute the parameters for a bounding box
// . . .
                    Image tile = Image.FromStream
                        (new MemoryStream (ts.GetTile  (tid)));
                    g.DrawImage (tile,
// . . .
            }

            // Return the image
            return bitmap;
        }
// . . .
    }

GetTiledImage uses three TerraService Web methods:

A “tile” is a 200-pixel-square image of a particular geographic location. To build larger images, a TerraService client must fetch multiple tiles and stitch them together to form a composite. That’s how GetTiledImage generates the 600 x 400 images that it returns. It starts by creating a Bitmap object to represent the image. Then it uses Graphics.DrawImage to draw each tile onto the image. The logic is wholly independent of the image size, so if you’d like to modify CityView to show larger (or smaller) images, find the statement

Bitmap bitmap = GetTiledImage (city, state, res, 600, 400);

in CityView.ashx and change the 600 and 400 to the desired width and height.

TerraService.dll was compiled from TerraService.cs, which I generated with the following command:

wsdl /namespace:TS http://terraservice.net/terraservice.asmx

The namespace was necessary to prevent certain data types defined in TerraService’s WSDL contract from conflicting with data types defined in the .NET Framework Class Library. Once TerraService.cs was created, the command

csc /t:library terraservice.cs

compiled it into a DLL.


 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.