Mapscript and XNA (1)

June 24th, 2007

We’ve demonstrated a number of smaller projects over the past couple of months showcasing some simple mapping and XNA applications. Now things are starting to get more interesting as we build upon what we’ve learned in an effort to create more interesting products. This next series of posts combines aspects from several of our projects, including World Mapper, Using Mapserver, Mapscript Demo, and the Image Swapping example. Basically, we now have the opportunity to incorporate Mapserver-generated images into an XNA program.

Quick Review

Mapserver is a map-rendering program mainly used for web mapping applications. It can also be used for desktop applications via it’s Mapscript API.

In our previous Mapserver and Mapscript posts, we used a sample dataset of Canada that can be downloaded here. Since it is freely available and easy to use, we will continue to use it in this project. We will cover making your own datasets for Mapserver in a future post.

Adding XNA

We already created a standard forms-based application for interacting with Mapserver. There were some buttons and the map image just switches upon refreshes. If the project were scored on presentation, it would receive low marks. Nothing stands out, the visual aspects are relatively limited and fail to draw the user in. Fortunately XNA, in addition to being a game development platform, also offers a great set of features for producing mapping applications with some graphical enhancements.

Using Mapserver in XNA

It is actually very easy to use Mapserver map images in XNA. Before we begin coding our project, let’s think about the steps needed to do so:

  1. Our XNA Program requests a new map image. This request will occur upon startup and any time the user interacts with the map via zooming or panning.
  2. Using the Mapscript API, a new map image needs to be generated (see Mapscript Demo 2 for more info). The map image can be returned as a memory stream or saved to a temporary image file in JPEG or PNG format.
  3. The XNA program then loads the map image as a 2D texture and displays it on-screen (see XNA and GIS 2 for more info). Our image swapping example shows one way to produce fade in/out transitions we could use when switching map images.

The map image is displayed until another request is made and the process is repeated. Both the speed of Mapserver and XNA enable this process to take less than a second to complete.

We will start coding in our next post and rely on an object oriented design to create a C# class that completes the steps above automatically. Adhering to this type of approach ensures we can easily add Mapserver functionality to other future XNA projects without much hassle.

Continue to part 2