<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spatial Horizons &#187; Mapserver</title>
	<atom:link href="http://spatialhorizons.com/category/mapserver/feed/" rel="self" type="application/rss+xml" />
	<link>http://spatialhorizons.com</link>
	<description>Exploring Geographic Technologies</description>
	<lastBuildDate>Tue, 16 Aug 2011 23:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Using MapServer (3) &#8211; CGI MapServer</title>
		<link>http://spatialhorizons.com/2007/12/09/using-mapserver-3-cgi-mapserver/</link>
		<comments>http://spatialhorizons.com/2007/12/09/using-mapserver-3-cgi-mapserver/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 15:15:44 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/12/09/using-mapserver-3-cgi-mapserver/</guid>
		<description><![CDATA[This post is the third in a series on using MapServer. First-time visitors are encouraged to read part 1 to learn how to download and install MapServer on their computer. Additionally, the data layers used in this tutorial are available to download in this post. We&#8217;ve already introduced MapServer and showed how to create a [...]]]></description>
			<content:encoded><![CDATA[<p class="moreinfo">This post is the third in a series on using <a href="http://mapserver.gis.umn.edu/">MapServer</a>. First-time visitors are encouraged to read <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">part 1</a> to learn how to download and install MapServer on their computer. Additionally, the data layers used in this tutorial are available to download in this <a href="http://spatialhorizons.com/2007/12/09/sample-dataset-world-map/">post</a>.</p>
<p>We&#8217;ve already <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">introduced MapServer</a> and <a href="http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/">showed how to create a mapfile</a> using <a href="http://spatialhorizons.com/faqs#gis">GIS</a> data.  We will now introduce MapServer&#8217;s <a href="http://en.wikipedia.org/wiki/Common_Gateway_Interface">CGI</a> functionality as a way to generate a map image. Later we will build on what we&#8217;ve learned to create a map using <a href="http://www.openlayers.org/">OpenLayers</a>, a web mapping interface.</p>
<p><strong>Introducing Mapserv.exe</strong></p>
<p>At the heart of MapServer is a single file referred to as &#8220;mapserv&#8221;. With the <a href="http://www.maptools.org/ms4w/">MS4W package</a>, it is a single executable found at <strong>C:\ms4w\Apache\cgi-bin\mapserv.exe</strong>. This file is the map production engine that uses a mapfile to generate a map image. </p>
<p>Mapserv.exe is usually accessed internally by MapServer-based packages such as <a href="http://ka-map.maptools.org/">ka-map</a> or <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">Mapscript</a>. But it can also be called directly from a web browser as long as some required parameters are included.<br />
<span id="more-140"></span><br />
<strong>Mapping from the Web Browser Address Bar</strong></p>
<p>In order to demonstrate Mapserv.exe, we already need to have some data and a mapfile ready to use.  We will be using the data and mapfile from our <a href="http://spatialhorizons.com/2007/12/09/sample-dataset-world-map/">World Map dataset</a>.</p>
<p>Open a web browser and enter the following: <strong>http://localhost/cgi-bin/mapserv.exe</strong>. If MapServer is correctly configured, you should see the following warning message:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/12/mapservercgi_noinfo.jpg" alt="CGI MapServer" /></p>
<p>This means mapserv.exe is working correctly so we can next tell it that we want to draw a map. Add <strong>?mode=map</strong> to the address so the full address is <strong>http://localhost/cgi-bin/mapserv.exe?mode=map</strong>. Pressing enter again returns another message:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/12/mapservercgi_nomap.jpg" alt="CGI MapServer" / ></p>
<p>This is expected because we have not provided a mapfile to display. The next variable to add is named &#8220;map&#8221; and it&#8217;s value is the file path to our mapfile. For example, it could be: <strong>http://localhost/cgi-bin/mapserv.exe?mode=map&#038;map=C:\world_mapfile.map</strong>.  If a valid mapfile is entered the browser will generate an image such as this:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/12/mapservercgi_map1.jpg" alt="CGI MapServer Map" /></p>
<p>A full list of CGI MapServer parameters can be found <a href="http://mapserver.gis.umn.edu/docs/reference/cgi/referencemanual-all-pages">here</a>.  For example, to generate an image of a specific map extent, the MAPEXT parameter can be used.  In our example, we added MAPEXT -20 -20 20 20 in the browser to get an address like this: <strong>http://localhost/cgi-bin/mapserv.exe?mode=map&#038; <br /> map=C:\James\GIS\Data\World\world_mapfile.map&#038;mapext=-20 -20 20 20</strong>. The image returned reflects the new extent.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/12/mapservercgi_map2.jpg" alt="CGI MapServer Map" /></p>
<p><strong>Conclusion</strong></p>
<p>Understanding how MapServer uses mapserv.exe to generate images will become more useful in our next post where we will setup a basic web mapping site using <a href="http://www.openlayers.org/">OpenLayers</a> that will need to directly access the CGI functionality of MapServer.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/12/09/using-mapserver-3-cgi-mapserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MapServer (2) &#8211; Generating Map Tiles</title>
		<link>http://spatialhorizons.com/2007/11/14/using-mapserver-2-generating-map-tiles/</link>
		<comments>http://spatialhorizons.com/2007/11/14/using-mapserver-2-generating-map-tiles/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 23:44:06 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Map Tiles]]></category>
		<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/11/14/using-mapserver-2-generating-map-tiles/</guid>
		<description><![CDATA[This post is the second in a series on using MapServer. First-time visitors are encouraged to read part 1 which details how to download, setup, and create a web mapping site using MapServer. Additionally, the topic of map tiles has already been introduced in a previous series. Once the MapServer for Windows (MS4W) package with [...]]]></description>
			<content:encoded><![CDATA[<p class="moreinfo">This post is the second in a series on using <a href="http://mapserver.gis.umn.edu/">MapServer</a>. First-time visitors are encouraged to read <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">part 1</a> which details how to download, setup, and create a web mapping site using MapServer. Additionally, the topic of map tiles has already been introduced in a <a href="http://spatialhorizons.com/category/map-tiles/">previous series</a>.</p>
<p>Once the <a href="http://www.maptools.org/ms4w/">MapServer for Windows (MS4W)</a> package with <a href="http://ka-map.maptools.org/">ka-Map</a> is installed on your local machine, some new mapping functionality becomes available. One area that interests us is the ability to automatically generate a set of map tiles.</p>
<p><strong>ka-Map Configuration</strong></p>
<p>The first step in creating a set of map tiles is to configure ka-Map to point to a valid MapServer mapfile (see <a href="http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/">here</a> for more on a mapfile). Navigate to this directory: <strong>C:\ms4w\apps\ka-map-1.0\include</strong> and open the file named <strong>config.php</strong> in any text editor.</p>
<p>This configuration file contains several parameters which you should be familiar with before creating our map tiles. We won&#8217;t change any of their values yet but it is important to understand what they do. First, on lines 73 and 74, the pixel size of each tile to be generated is defined:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">$tileWidth <span style="color: #008000;">=</span> <span style="color: #FF0000;">256</span><span style="color: #008000;">;</span>
$tileHeight <span style="color: #008000;">=</span> <span style="color: #FF0000;">256</span><span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>Lines 106 through 110 define the most important parameters:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">$aszGMap <span style="color: #008000;">=</span> array <span style="color: #008000;">&#40;</span>
         <span style="color: #666666;">'title'</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #666666;">'GMap 75'</span>,
         <span style="color: #666666;">'path'</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #666666;">'../../gmap/htdocs/gmap75.map'</span>,
         <span style="color: #666666;">'scales'</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> array<span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">40000000</span>, <span style="color: #FF0000;">25000000</span>, <span style="color: #FF0000;">12000000</span>,
                                 <span style="color: #FF0000;">7500000</span>, <span style="color: #FF0000;">3000000</span>, <span style="color: #FF0000;">1000000</span> <span style="color: #008000;">&#41;</span>,
         <span style="color: #666666;">'format'</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #666666;">'PNG'</span>
 <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p><span id="more-134"></span><br />
By default, ka-Map is setup to use the GMap application that we downloaded and setup in the <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">first post</a>. So the values for the title, path, and scales reflect that.</p>
<p>The path variable is relative based on the location of config.php (config.php is in C:\ms4w\apps\ka-map-1.0\include and the gmap75.map file is in C:\ms4w\apps\gmap\htdocs).</p>
<p>The scales array contains all the map scales at which the tiles will be generated. Determining which scales and how many scale levels is completely configurable based on the user&#8217;s preference. For now we will leave them as-is.</p>
<p>Lastly, the output image format is defined as PNG.</p>
<p><strong>Precache.php</strong></p>
<p>ka-Map is a web-based interface for viewing MapServer-based maps using map tiles. Normally, ka-Map generates only the necessary map tiles needed to show in the browser&#8217;s window. But there is a separate file, named precache.php, that also comes with the ka-Map that does generate all the map tiles for every scale of the map.</p>
<p>Navigate to the following directory: <strong>C:\ms4w\apps\ka-map-1.0\htdocs</strong>. Scroll through the list of files to find the file <strong>precache.php</strong>. The good news is this file does not need to be modified in any way. It relies solely on the config.php settings described above to determine what mapfile and map scales to use when creating the map tiles.</p>
<p><strong>Running Precache.php</strong></p>
<p>Readers are encouraged to first read over <a href="http://ka-map.ominiverdi.org/wiki/index.php/Pre-caching_system" target="_blank">this article</a> in the ka-Map wiki about precache.php. You don&#8217;t have to understand everything it discusses &#8211; just glance over the parameters needed to run precache.php.</p>
<p>Running precache.php requires returning to the world of DOS prompts and command line. It is important to follow these steps exactly as listed otherwise it may not work correctly.</p>
<ol>
<li>Open a command prompt. On Windows, go to Start&#8230;Run and type <strong>cmd</strong>.</li>
<li>Type <strong>cd C:\ms4w\apps\ka-map-1.0\htdocs</strong> to go to the directory with precache.php.</li>
<li>Since precache.php is a PHP file, we need to run it using the PHP executable (php.exe). Fortunately this is already included with the MS4W package. Type <strong>C:\ms4w\Apache\cgi-bin\php.exe precache.php</strong> in the command prompt. We left out the required parameters on purpose here so you can see the usage instructions:</li>
</ol>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/11/precache_usage.jpg" alt="Precache.php Usage" /></p>
<p>We won&#8217;t need to use -f, -m, or -s options but we do need to add the tile_url parameter. This is the web address (hence &#8220;_url&#8221;) to ka-Map&#8217;s <strong>tile.php</strong> file. Even though the file is stored locally at <strong>C:\ms4w\apps\ka-map-1.0\htdocs\tile.php</strong>, we need to enter a valid URL. The usage instructions suggest using http://localhost/ka-map/htdocs/tile.php but that didn&#8217;t work for us. Instead we will use <strong>http://localhost/kamap/tile.php</strong>.</p>
<ol>
<li>Type <strong>C:\ms4w\Apache\cgi-bin\php.exe precache.php http://localhost/kamap/tile.php</strong>. Press enter the map tiles will be generated.</li>
</ol>
<p>This process will take several minutes as all the tiles at each of the scale levels are generated. The command prompt will display information on each scale level. This information on meta tiles and tile counts is informative but not necessary to understand right now. In short, precache.php requests larger tiles (&#8220;meta tiles&#8221;) and then cuts them up to arrive at the final set of tiles.</p>
<p><strong>Viewing the Map Tile Cache</strong></p>
<p>Navigate to <strong>C:\ms4w\tmp\ms_tmp\kacache\gmap</strong>. Inside this folder should be a folder named for each scale level in the map. Inside each scale level folder our folders named __base__ and then a sub-folder named def. This is where the tile images can be found.</p>
<p>Because ka-Map tiles are generally only used internally and not designed for end-user interaction, there is a potentially confusing way to name tiles and tile folders. Anytime a folder or file contains the letters &#8220;t&#8221; or &#8220;l&#8221;, it means top or left. So instead of naming tiles with X/Ys or Row/Columns, ka-Map uses the &#8220;Left/Top&#8221; naming convention.</p>
<p>It also names each tile based on it&#8217;s pixel location and tile size. Since the config.php was set to output 256&#215;256 tiles, each tile image will include top and left numbers based on multiples of 256. So if one tile is named t0l0.png, the adjacent tiles on each side would be named t0l256.png and t0l-256.png. Glance through the folders and view some of the individual tiles to get a sense of the tiling structure.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/11/kamap_tile_t-256l0.png" alt="kaMap Tile Example" /></p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/11/kamap_tile_t-256l-256.png" alt="kaMap Tile Example" /></p>
<p><strong>What&#8217;s the Benefit?</strong></p>
<p>Traditional usage of precache.php is for improving the load times on a web mapping site by caching all the tiles ahead of time.  For our objectives, having a complete set of pre-compiled map tiles means we can create a desktop application without including a GIS code library or GIS data. It also eliminates the extra processing time and would make deploying our applications easier. Instead of including extra DLLs and shapefiles, we simply need to include a folder of map tiles to run the application.</p>
<p><strong>Conclusion</strong></p>
<p>This post is probably the most technical topic we&#8217;ve covered to date. For now, simply understanding how to generate map tiles using precache.php is a what&#8217;s important. In the next post we will discuss the map tiles in some more depth, including how we can use them in other applications.</p>
<p><a title="Using MapServer (3) – CGI MapServer" href="http://spatialhorizons.com/2007/12/09/using-mapserver-3-cgi-mapserver/">Continue to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/11/14/using-mapserver-2-generating-map-tiles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using QGIS (5) &#8211; Export to MapServer</title>
		<link>http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/</link>
		<comments>http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 22:42:10 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[QGIS]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/</guid>
		<description><![CDATA[Our last post for now on QGIS will cover an interesting feature added in the recent versions. A new &#8220;Export to MapServer Map&#8221; command creates a MapServer configuration file (simply called a &#8220;mapfile&#8221;) from an existing QGIS map. That mapfile can be used to then display the data on a website or use it in [...]]]></description>
			<content:encoded><![CDATA[<p>Our last post for now on <a href="http://www.qgis.org/">QGIS</a> will cover an interesting feature added in the recent versions. A new &#8220;Export to MapServer Map&#8221; command creates a MapServer configuration file (simply called a &#8220;mapfile&#8221;) from an existing QGIS map. That mapfile can be used to then display the data on a website or use it in a desktop application. The desktop option is what we will take a look at because we will now have an easy way to create a mapfile that we can use with some of our <a href="http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/">MapServer/XNA tutorials</a> we demonstrated in the past.</p>
<p><strong>Python Installation</strong></p>
<p>The Export to MapServer tool will only work if the Python programming language is installed on your computer.  The latest version (2.5.1) of Python is required so we first need to download and  install it <a href="http://www.python.org/download/releases/2.5.1/">here</a>.</p>
<p><strong>Open a QGIS Map</strong></p>
<p>After the installation, go ahead and open a QGIS map. We will continue to use the world map project created in the <a href="http://spatialhorizons.com/2007/09/23/using-qgis-3-more-layer-symbology/">previous posts</a>.  As a reminder, we have two layers in our map &#8211; a vector layer showing country outlines and a raster background image.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/09/qgis_raster_aligned.jpg" alt="QGIS Map" /></p>
<p><span id="more-111"></span><br />
<strong>Export to MapServer</strong></p>
<p>Under the File menu option, select the &#8220;Export to MapServer Map&#8221; item:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/qgis_export2mapserver.jpg" alt="Export to MapServer" /></p>
<p>The window that opens requires you to input some information before creating the mapfile. First you need to select a location for the new mapfile. Then pick the QGIS project file that you will be creating it from (it does not automatically use the currently open project in QGIS).  Provide a map name, enter the size, and make sure the units drop-down box matches the map&#8217;s units. Our map is only in longitude/latitude units so we chose dd (decimal degrees). We will skip the box covering the Web Interface Definition so our filled out menu looks like this:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/qgis_export2mapserver2.jpg" alt="Export to MapServer" /></p>
<p>Press Ok to run the export and the mapfile will be created.</p>
<p><strong>Viewing the Mapfile</strong></p>
<p>Find the created mapfile and open it in your favorite text editor. The mapfile is a structured document that contains groups of elements that begin with a name (e.g., MAP, PROJECTION, LAYER) and end with END.  It also contains single-line attributes using the key-value syntax (e.g., NAME &#8216;World Map&#8217;, TRANSPARENCY 100, OUTLINECOLOR 0 0 0).  It is very similar to an XML file but without the brackets and slashes.</p>
<p>For a complete reference on the syntax of mapfiles, check out their <a href="http://mapserver.gis.umn.edu/docs/reference/mapfile">help</a> page on the web. Even without reading the help file, most of the mapfile can be understood by simply glancing at the text. For example, our world raster image appears in the mapfile like this:</p>
<pre>LAYER
    NAME 'world_raster'
    TYPE RASTER
    DATA 'M:\\World\\world_raster.tif'
    METADATA
      'wms_title' 'world_raster'
    END
    STATUS DEFAULT
    TRANSPARENCY 100
    PROJECTION
    'proj=longlat'
    'ellps=WGS84'
    'datum=WGS84'
    'no_defs'
    END
  END</pre>
<p>It is easy to determine the between the LAYER and END tags, the raster is described with it&#8217;s name, type, file location, and projection information.</p>
<p>Our shapefile of world countries is slightly longer but similar:</p>
<pre>LAYER
    NAME 'world_adm0'
    TYPE POLYGON
    DATA 'M:\\World\\world_adm0.shp'
    METADATA
      'wms_title' 'world_adm0'
    END
    STATUS DEFAULT
    TRANSPARENCY 100
    PROJECTION
    'proj=longlat'
    'ellps=WGS84'
    'datum=WGS84'
    'no_defs'
    END
    CLASS
       NAME 'world_adm0'
       STYLE
         SYMBOL 0
         SIZE 6
         OUTLINECOLOR 0 0 0
         COLOR 144 50 207
       END
    END
  END</pre>
<p>The major difference between this and the raster file is the addition of a CLASS and STYLE element. They define the coloring of the countries using the OUTLINECOLOR and COLOR attributes. </p>
<p>Sometimes our mapfile doesn&#8217;t exactly match how QGIS had it displayed. In our QGIS map, we displayed the countries with a transparent fill so only the black outlines showed on the map.  To make our exported mapfile match that, we need to remove the &#8220;COLOR 144 50 207&#8243; line and save the mapfile.</p>
<p><strong>Testing the Mapfile</strong></p>
<p>There are various ways our mapfile could be tested in another application. The coolest example would be to go back to our <a href="http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/">Mapscript/XNA tutorials</a>. The result of these tutorials was a C# program that uses <a href="http://msdn2.microsoft.com/en-us/xna/default.aspx">XNA</a> as the graphics engine and Mapscript (Mapserver&#8217;s API) to generate map images from a mapfile (download the project&#8217;s source code in this <a href="http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/">post</a>).</p>
<p>In our program, we simply need to change the line of code where our mapfile path is defined to point to our new mapfile created from the QGIS export.</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">mapserver <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> shMapserver<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;M:\World\world_mapfile.map&quot;</span>, <span style="color: #FF0000;">600</span>, <span style="color: #FF0000;">600</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</p>
<p>Running the program will prove that our new mapfile works and allow us to zoom around the world.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/xna_with_world_mapfile.jpg" alt="World Mapfile in XNA" /></p>
<p><strong>Conclusion</strong></p>
<p>QGIS is a free, easy-to-use, software program for viewing and exploring geographic data.  The reason for discussing it in some depth is that if we want to start developing our own mapping applications, we need to begin acquiring GIS data and making our own maps. QGIS also gives us an easy way to produce MapServer mapfiles as we demonstrated above.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/10/06/using-qgis-5-export-to-mapserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript and XNA (3)</title>
		<link>http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/</link>
		<comments>http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/#comments</comments>
		<pubDate>Sun, 08 Jul 2007 11:46:37 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/</guid>
		<description><![CDATA[Continuing with our program that combines Mapserver (via the Mapscript API) and XNA, we next added functionality to the update method our of XNA program. We check for a mouse click to initiate a map pan and also for key presses to zoom in, out and return to full extent. At the end, if any [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing with our <a href="http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/">program</a> that combines <a href="http://mapserver.gis.umn.edu/">Mapserver</a> (via the <a href="http://mapserver.gis.umn.edu/docs/reference/mapscript">Mapscript API</a>) and <a href="http://msdn2.microsoft.com/en-us/xna/default.aspx">XNA</a>, we next added functionality to the update method our of XNA program. We check for a mouse click to initiate a map pan and also for key presses to zoom in, out and return to full extent. At the end, if any map event has occurred, the map will be updated. It is in the updating that a new Mapserver image is generated and prepared for displaying on-screen. </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// check if a mouse click has occured</span>
    mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">LeftButton</span> <span style="color: #008000;">==</span> ButtonState<span style="color: #008000;">.</span><span style="color: #0000FF;">Pressed</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">ZoomMap</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// check if a key has been pressed</span>
    KeyboardState keyState <span style="color: #008000;">=</span> Keyboard<span style="color: #008000;">.</span><span style="color: #0000FF;">GetState</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Keys<span style="color: #008000;">.</span><span style="color: #0000FF;">F</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">ToggleFullScreen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Keys<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">ZoomMap</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPixelCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, 
                      mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPixelCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Keys<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">ZoomMap</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">2</span>, mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPixelCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, 
                      mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPixelCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Keys<span style="color: #008000;">.</span><span style="color: #0000FF;">C</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">ZoomMapFullExtent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Keys<span style="color: #008000;">.</span><span style="color: #0000FF;">Escape</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Exit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// check if a new mapserver image needs to be generated</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">RefreshMap</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">UpdateMap</span><span style="color: #008000;">&#40;</span>graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">GraphicsDevice</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</p>
<p><span id="more-78"></span><br />
Each option relies on our MapserverXNA class discussed in the last <a href="http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/">post</a>. Since all the functionality is hidden within this class, coding our main program is very straightforward. Additionally, zooming in and out is performed with the help of our Image Swapping class we created in a <a href="http://spatialhorizons.com/2007/06/17/xna-and-gis-4-image-swapping/">previous post</a>. This adds a fading in/out effect but none of this code is even visible in the main program.</p>
<p>As shown above, a mouse click invokes a pan of the map (click <a href="http://spatialhorizons.com/2007/06/09/mapscript-demo-3/">here</a> for more information on zooming and panning with Mapscript). The mouse&#8217;s X/Y click coordinates will become the new center of the map. Within the MapserverXNA class, we added a shift effect when a pan occurs using the previously mentioned <a href="http://spatialhorizons.com/2007/06/17/xna-and-gis-4-image-swapping/">Image Swapping class</a>. Upon the mouse click, the existing image is shifted and then the new image then just replaces it. This sliding effect is an alternative to the fading effect used for zooming b/c it wouldn&#8217;t make sense to pan the map and then fade in the new image.</p>
<p>The last portion of code added is in the draw method. Here we draw our map image, mouse cursor, and some text.</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Draw<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">GraphicsDevice</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span>SpriteBlendMode<span style="color: #008000;">.</span><span style="color: #0000FF;">AlphaBlend</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawMap</span><span style="color: #008000;">&#40;</span>spriteBatch, gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>spriteBatch<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span>font, <span style="color: #666666;">&quot;Z = Zoom in&quot;</span>, <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span>, Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span>font, <span style="color: #666666;">&quot;X = Zoom out&quot;</span>, <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">16</span><span style="color: #008000;">&#41;</span>, Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span>font, <span style="color: #666666;">&quot;C = Full Extent&quot;</span>, <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">28</span><span style="color: #008000;">&#41;</span>, Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span>font, <span style="color: #666666;">&quot;Click to pan&quot;</span>, <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">40</span><span style="color: #008000;">&#41;</span>, Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">End</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</p>
<p><strong>Conclusion</strong></p>
<p>This post concludes probably the most complex project we have demonstrated so far. Some of the Mapserver-related code was not discussed because it has been covered in our previous projects (<a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">Mapserver &#8211; 10 Minute Tutorial</a>, <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">Mapscript Demo</a>). The entire source code for this project is available to download below. The program&#8217;s requirements (C#, XNA, Mapserver, and a valid Mapserver map file) have all been discussed in our other <a href="http://spatialhorizons.com/projects/">projects</a>. Questions and comments are always welcome at james [at] spatialhorizons.com.</p>
<p><a href="http://spatialhorizons.com/downloads/MapscriptXNA.zip"><img border="0" alt="Download<br />
Source Code" src="http://spatialhorizons.com/downloads/code_download.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript and XNA (2)</title>
		<link>http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/</link>
		<comments>http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/#comments</comments>
		<pubDate>Sun, 01 Jul 2007 13:38:00 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/</guid>
		<description><![CDATA[In order to use Mapserver in XNA, we need to create a class that primarily serves as an interface between XNA and Mapscript (Mapserver&#8217;s API). Our last post discussed in general how to accomplish this but now let&#8217;s add some code. MapserverXNA Class We named the class MapserverXNA and the properties and methods look like [...]]]></description>
			<content:encoded><![CDATA[<p>In order to use Mapserver in XNA, we need to create a class that primarily serves as an interface between XNA and Mapscript (Mapserver&#8217;s API).  Our last <a href="http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/">post</a> discussed in general how to accomplish this but now let&#8217;s add some code.</p>
<p><strong>MapserverXNA Class</strong></p>
<p>We named the class MapserverXNA and the properties and methods look like this:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/07/uml_mapserverxna.jpg" alt="UML MapserverXNA Class" /><br />
<span id="more-77"></span></p>
<p>The properties include information on the map&#8217;s size (height, width, and center), the location of the map file, and a boolean value (RefreshMap) that tracks whether the map needs to be updated.</p>
<p>The methods include the ability to update the map (i.e. generate a new image from Mapserver) and draw the map onscreen. There are also two zoom methods: ZoomMap allows for zooming in, out, and panning while ZoomMapFullExtent returns the map to the full extent.</p>
<p>We won&#8217;t discuss in detail the code inside of this class. It is included in the source code download below so you are welcome to take a look yourself.</p>
<p><strong>Using the MapserverXNA Class</strong></p>
<p>The next step is to setup a XNA program to use the newly designed class. We could start from scratch but instead this project will loosely build off the structure of <a href="http://spatialhorizons.com/2007/04/02/world-mapper-2/">World Mapper</a> project from a while back.  It does follow the standard form of a XNA game application as discussed in a <a href="http://spatialhorizons.com/2007/03/25/xna-and-gis-2/">previous post</a>. We also incorporated what we learned in our <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">Mapscript Demo</a> and our post on <a href="http://spatialhorizons.com/2007/05/06/xna-and-gis-3/">XNA font</a>.</p>
<p>With our XNA game program setup, we added several more fields (private variables) to the main game class:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">GraphicsDeviceManager graphics<span style="color: #008000;">;</span>
ContentManager content<span style="color: #008000;">;</span>
SpriteBatch spriteBatch<span style="color: #008000;">;</span>
shMouse mouse<span style="color: #008000;">;</span>
shMapserver mapserver<span style="color: #008000;">;</span>
SpriteFont font<span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>In the constructor, we then setup our mouse class and initialized the MapserverXNA class with a valid map file string and size in pixels.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> MapscriptXNA<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    graphics <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GraphicsDeviceManager<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentManager<span style="color: #008000;">&#40;</span>Services<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mouse <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> shMouse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    mapserver <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> shMapserver<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\ms4w\apps\gmap\htdocs\gmap75.map&quot;</span>,
        <span style="color: #FF0000;">600</span>, <span style="color: #FF0000;">600</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>In the initialize method, we resize the screen to match the map size:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Initialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">PreferredBackBufferWidth</span> <span style="color: #008000;">=</span> mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapWidth</span><span style="color: #008000;">;</span>
    graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">PreferredBackBufferHeight</span> <span style="color: #008000;">=</span> mapserver<span style="color: #008000;">.</span><span style="color: #0000FF;">MapHeight</span><span style="color: #008000;">;</span>
    graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplyChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>In the load graphics method, we load both the mouse cursor and font from using XNA&#8217;s Content Pipeline:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadGraphicsContent<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> loadAllContent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>loadAllContent<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        spriteBatch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SpriteBatch<span style="color: #008000;">&#40;</span>graphics<span style="color: #008000;">.</span><span style="color: #0000FF;">GraphicsDevice</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">Cursor</span> <span style="color: #008000;">=</span> content<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;Content\Textures\MouseCursor&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        font <span style="color: #008000;">=</span> content<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;Content\Font\MapFont&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>We will continue in our next post with the rest of the application&#8217;s code but the entire source code can be downloaded below. For new readers, we encourage you to glance over our <a href="http://spatialhorizons.com/projects/">past projects</a> or read the new <a href="http://spatialhorizons.com/faqs/">FAQs page</a>.</p>
<p><a href="http://spatialhorizons.com/downloads/MapscriptXNA.zip"><img src="http://spatialhorizons.com/downloads/code_download.jpg" border="0" alt="Download Source Code" /></a></p>
<p><a title="Mapscript and XNA (3)" href="http://spatialhorizons.com/2007/07/08/mapscript-and-xna-3/">Continue to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript and XNA (1)</title>
		<link>http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/</link>
		<comments>http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/#comments</comments>
		<pubDate>Sun, 24 Jun 2007 13:20:53 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/</guid>
		<description><![CDATA[We&#8217;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&#8217;ve learned in an effort to create more interesting products. This next series of posts combines aspects from several of our projects, [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;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&#8217;ve learned in an effort to create more interesting products. This next series of posts combines aspects from several of our projects, including <a href="http://spatialhorizons.com/2007/02/15/world-mapper-1/">World Mapper</a>, <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">Using Mapserver</a>, <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">Mapscript Demo</a>, and the <a href="http://spatialhorizons.com/2007/06/17/xna-and-gis-4-image-swapping/">Image Swapping example</a>. Basically, we now have the opportunity to incorporate Mapserver-generated images into an XNA program.</p>
<p><span id="more-71"></span><br />
<strong>Quick Review</strong></p>
<p><a href="http://mapserver.gis.umn.edu/">Mapserver</a> is a map-rendering program mainly used for web mapping applications.  It can also be used for desktop applications via it&#8217;s <a href="http://mapserver.gis.umn.edu/docs/reference/mapscript">Mapscript API</a>.</p>
<p>In our previous Mapserver and Mapscript posts, we used a sample dataset of Canada that can be downloaded <a href="http://www.maptools.org/dl/ms4w/gmap_ms4w_ms410.zip">here</a>. 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.</p>
<p><strong>Adding XNA</strong></p>
<p>We already <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">created</a> 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 <a href="http://en.wikipedia.org/wiki/Microsoft_XNA">XNA</a>, in addition to being a game development platform, also offers a great set of features for producing mapping applications with some graphical enhancements.</p>
<p><strong>Using Mapserver in XNA</strong></p>
<p>It is actually very easy to use Mapserver map images in XNA. Before we begin coding our project, let&#8217;s think about the steps needed to do so:</p>
<ol>
<li>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.</li>
<li>Using the Mapscript API, a new map image needs to be generated (see <a href="http://spatialhorizons.com/2007/06/02/mapscript-demo-2/">Mapscript Demo 2</a> 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.</li>
<li>The XNA program then loads the map image as a 2D texture and displays it on-screen (see <a href="http://spatialhorizons.com/2007/03/25/xna-and-gis-2/">XNA and GIS 2</a> for more info). Our <a href="http://spatialhorizons.com/2007/06/17/xna-and-gis-4-image-swapping/">image swapping example</a> shows one way to produce fade in/out transitions we could use when switching map images.</li>
</ol>
<p>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.</p>
<p>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.</p>
<p><a title="Mapscript and XNA (2)" href="http://spatialhorizons.com/2007/07/01/mapscript-and-xna-2/">Continue to part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/06/24/mapscript-and-xna-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript Demo (3)</title>
		<link>http://spatialhorizons.com/2007/06/09/mapscript-demo-3/</link>
		<comments>http://spatialhorizons.com/2007/06/09/mapscript-demo-3/#comments</comments>
		<pubDate>Sat, 09 Jun 2007 13:09:21 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/06/09/mapscript-demo-3/</guid>
		<description><![CDATA[The final portion of this project involves adding some map functionality to our program. As a quick review, we first setup Mapserver on our computer and then created a simple desktop application (part 1, part 2) that used the Mapscript API. Zooming The main object in our program is Mapscript&#8217;s map object (mapObj). This object [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/05/mapscriptdemo_running.thumbnail.jpg" alt="Mapscript Desktop Program"  style="float: right;" /><br />
The final portion of this project involves adding some map functionality to our program.  As a quick review, we first <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/"> setup Mapserver on our computer</a> and then created a simple desktop application (<a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">part 1</a>, <a href="http://spatialhorizons.com/2007/06/02/mapscript-demo-2/">part 2</a>) that used the Mapscript API.<br />
<span id="more-70"></span><br />
<strong>Zooming</strong></p>
<p>The main object in our program is Mapscript&#8217;s map object (<a href="http://mapserver.gis.umn.edu/docs/reference/mapscript/index_html#mapobj">mapObj</a>).  This object has a method called zoomPoint that we can use to zoom in or out.  The method looks like this:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">zoomPoint<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> zoomfactor, pointObj imgpoint, 
      <span style="color: #6666cc; font-weight: bold;">int</span> width, <span style="color: #6666cc; font-weight: bold;">int</span> height, rectObj extent, rectObj maxextent<span style="color: #008000;">&#41;</span></pre></div></div>

</p>
<p>In our program, we simplify things by only allowing the user to zoom in or out on the center of the map.  A single zoom function can be used for both zooming in and out: </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ZoomMap<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> zoomFactor<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">zoomPoint</span><span style="color: #008000;">&#40;</span>zoomFactor, 
          <span style="color: #008000;">new</span> pointObj<span style="color: #008000;">&#40;</span>picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>, picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>, 
          picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span>, m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    RefreshMap<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</p>
<p>The zoomFactor value depends on the action. Values greater than 1 zoom in, values less than 1 zoom out.  In our case, clicking on the Zoom In fires this code:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ZoomMap<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</p>
<p>while zooming out is the opposite:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ZoomMap<span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</p>
<p><strong>Panning</strong></p>
<p>Panning the map is very similar to zooming except that this is controlled by clicking on the map. In fact, we can use the same zoomPoint method as we discussed above.  The difference being when the zoomFactor is 1, the map remains at the same scale and only changes location.</p>
<p>Capturing a mouse click on the map (which is really our picture box control) is as simple this:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> picMap_MouseClick<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, MouseEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//--&gt; pan map based on mouse click coordinates</span>
    m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">zoomPoint</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, 
         <span style="color: #008000;">new</span> pointObj<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>, 
         picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span>, m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    RefreshMap<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</p>
<p><strong>Conclusion</strong></p>
<p>The source code is available to download below so feel free to test out the application as long as you have a valid map file and data available.  There is still a lot of room for improvement on this application, including more functionality and error checking, but we need to move on to bigger and better projects. The primary purpose of this project was to demonstrate how Mapserver (via Mapscript) could be used to generate maps for a desktop mapping application. Our next demonstration project involves improving the visual aspects of our program in a way that hasn&#8217;t been done before. Stay tuned.</p>
<p><a href="http://spatialhorizons.com/downloads/MapscriptDemo.zip"><img border="0" alt="Download Source Code" src="http://spatialhorizons.com/downloads/code_download.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/06/09/mapscript-demo-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript Demo (2)</title>
		<link>http://spatialhorizons.com/2007/06/02/mapscript-demo-2/</link>
		<comments>http://spatialhorizons.com/2007/06/02/mapscript-demo-2/#comments</comments>
		<pubDate>Sat, 02 Jun 2007 17:06:47 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/06/02/mapscript-demo-2/</guid>
		<description><![CDATA[Continuing with our exploring into Mapscript, our desktop mapping application needs some more setup. Our last post added a map file constant and now we will add two more variables to our form: &#160; const string MAP_FILE = @&#34;C:\ms4w\apps\gmap\htdocs\gmap75.map&#34;; mapObj m_map; rectObj m_fullExtent; &#160; Both are Mapscript objects. The first (m_map) is our map object [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing with our exploring into Mapscript, our desktop mapping application needs some more setup. Our last <a href="http://spatialhorizons.com/2007/05/26/mapscript-demo-1/">post</a> added a map file constant and now we will add two more variables to our form:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> MAP_FILE <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;C:\ms4w\apps\gmap\htdocs\gmap75.map&quot;</span><span style="color: #008000;">;</span>
mapObj m_map<span style="color: #008000;">;</span>
rectObj m_fullExtent<span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>Both are Mapscript objects. The first (m_map) is our map object that will be the primary class used in this project. The second will simply remember the extent of the map upon startup, which is usually also the full extent of the map.</p>
<p>In the form&#8217;s constructor method, we setup our map to match our picture box size and the define the full extent:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">m_map <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> mapObj<span style="color: #008000;">&#40;</span>MAP_FILE<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">width</span> <span style="color: #008000;">=</span> picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span>
m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">height</span> <span style="color: #008000;">=</span> picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span>
m_fullExtent <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> rectObj<span style="color: #008000;">&#40;</span>m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span><span style="color: #008000;">.</span><span style="color: #0000FF;">minx</span>, m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span><span style="color: #008000;">.</span><span style="color: #0000FF;">miny</span>,
                   m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span><span style="color: #008000;">.</span><span style="color: #0000FF;">maxx</span>, m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">extent</span><span style="color: #008000;">.</span><span style="color: #0000FF;">maxy</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p><span id="more-66"></span></p>
<p>Mapscript&#8217;s map object allows us to access Mapserver and generate map images.  Here is one way to generate an image and load it into our picture box control. This code is based off a great Mapserver tutorial found <a href="http://www.paolocorti.net/public/wordpress/index.php/2006/07/26/implementing-the-c-mapscript-code/">here</a>.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">imageObj image <span style="color: #008000;">=</span> m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">draw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> img <span style="color: #008000;">=</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">getBytes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>MemoryStream ms <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #008000;">&#40;</span>img<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  picMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Image</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Image</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FromStream</span><span style="color: #008000;">&#40;</span>ms<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/06/mapscript_image.jpg" alt="Mapscript Image" /></p>
<p>Similarly, to generate a legend image:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">imageObj legend <span style="color: #008000;">=</span> m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">drawLegend</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> img <span style="color: #008000;">=</span> legend<span style="color: #008000;">.</span><span style="color: #0000FF;">getBytes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>MemoryStream ms <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #008000;">&#40;</span>img<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    picLegend<span style="color: #008000;">.</span><span style="color: #0000FF;">Image</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Image</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FromStream</span><span style="color: #008000;">&#40;</span>ms<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/06/mapscript_legend.jpg" alt="Mapscript Legend" /></p>
<p>Our application also has a sidebar that displays a legend that lists each layer and a check box displaying whether the layer is on or off. Each time the map is updated, this layers list needs to be updated by looping through the map&#8217;s layers and finding their visibility. A TreeView control is used to display the layers.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">numlayers</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">--</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    layerObj layer <span style="color: #008000;">=</span> m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">getLayer</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    TreeNode node <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TreeNode<span style="color: #008000;">&#40;</span>m_map<span style="color: #008000;">.</span><span style="color: #0000FF;">getLayer</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>layer<span style="color: #008000;">.</span><span style="color: #0000FF;">status</span> <span style="color: #008000;">==</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>mapscript<span style="color: #008000;">.</span><span style="color: #0000FF;">MS_ON</span><span style="color: #008000;">&#41;</span>
        node<span style="color: #008000;">.</span><span style="color: #008000;">Checked</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
        node<span style="color: #008000;">.</span><span style="color: #008000;">Checked</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
    trvLegend<span style="color: #008000;">.</span><span style="color: #0000FF;">Nodes</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>node<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/06/mapscript_layerslist.jpg" alt="Map Layers List" /></p>
<p>The code samples above are each placed into separate functions appropriately named RefreshMap, RefreshMapLayers, and RefreshLegend.  They will be called when the program starts up and each time the user interacts with the map. The last piece of the puzzle, adding the zooming and panning functionality, will be covered in the next post but the entire source code can be downloaded below.</p>
<p><a href="http://spatialhorizons.com/downloads/MapscriptDemo.zip"><img src="http://spatialhorizons.com/downloads/code_download.jpg" border="0" alt="Download Source Code" /></a></p>
<p><a title="Mapscript Demo (3)" href="http://spatialhorizons.com/2007/06/09/mapscript-demo-3/">Continue to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/06/02/mapscript-demo-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapscript Demo (1)</title>
		<link>http://spatialhorizons.com/2007/05/26/mapscript-demo-1/</link>
		<comments>http://spatialhorizons.com/2007/05/26/mapscript-demo-1/#comments</comments>
		<pubDate>Sat, 26 May 2007 02:12:17 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/05/26/mapscript-demo-1/</guid>
		<description><![CDATA[In our last post, we setup MapServer using the MS4W package. That tutorial enabled us to have a working web mapping environment on a desktop machine without much hassle. If you haven&#8217;t read that post, I encourage you to read it before continuing. Today&#8217;s project will build off that setup by creating a small desktop [...]]]></description>
			<content:encoded><![CDATA[<p>In our last <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">post</a>, we setup <a href="http://mapserver.gis.umn.edu/" target="_blank">MapServer</a> using the <a href="http://www.maptools.org/ms4w/" target="_blank">MS4W</a> package. That tutorial enabled us to have a working web mapping environment on a desktop machine without much hassle. If you haven&#8217;t read that post, I encourage you to read it before continuing.</p>
<p>Today&#8217;s project will build off that setup by creating a small desktop application that uses MapServer&#8217;s ability to generate map images. Since MapServer is just a program (mapserv.exe) and some required DLLs, it is possible to create stand-alone applications that don&#8217;t have a web component.</p>
<p><strong>Introducing Mapscript</strong></p>
<p>In their own <a href="http://mapserver.gis.umn.edu/new_users/index_html#mapscript" target="_blank">words</a>&#8230;</p>
<p class="shquote">MapScript provides a scripting interface for MapServer for the construction of Web and stand-alone applications. MapScript is used independently of CGI MapServer, it is a loadable module that adds MapServer capability to your favorite scripting language. MapScript currently exists in Php, Perl, Python, Ruby, Tcl, Java, and C# flavors.</p>
<p><span id="more-63"></span></p>
<p>This means we can use Mapscript in <a href="http://msdn.microsoft.com/vstudio/express/visualcsharp/default.aspx" target="_blank">Visual C# Express</a> to access MapServer&#8217;s capabilities to draw and query maps. The maps generated are just images so we can easily integrate them into a standard Windows application. So let&#8217;s go ahead and create a desktop mapping application powered by MapServer.</p>
<p><strong>Create a MapServer Stand-alone Viewer</strong></p>
<p>Here are the steps to creating a MapServer application using Mapscript in C#. We will assume that a valid MapServer map file is already setup and ready to use. See our last post on how to download and setup a sample mapping application.</p>
<p><em>Step 1 &#8211; Create a Windows Application in Visual C# Express</em></p>
<p>Create a new Windows Application and add some basic controls to the form.  I first added some buttons for zooming in, out and to return to the full extent of the map.  A tab control was added on the side to contain both a list of the map layers and an image of the legend.  Most importantly, I placed a large picture box control on the form to serve as the map display area.</p>
<p><img id="image64" src="http://spatialhorizons.com/wp-content/uploads/2007/05/mapscriptdemo_form.jpg" alt="Mapscript Demo Form" /></p>
<p>At this point, I went ahead and compiled the application so the bin\debug folder would be generated within our application&#8217;s directory.  Step 2 explains why we need that folder right away.</p>
<p><em>Step 2 &#8211;  Adding the Mapserver DLLs</em></p>
<p>Stand-alone Mapserver applications need a number of DLLs to be present in the folder where the executable (*.EXE) will be generated. Luckily, the MS4W package ships with everything we will need. Navigate to the C:\ms4w\Apache\cgi-bin folder and note all the DLL files that are present there.  There are approximately 54 in total so just go ahead and copy them all and then paste them into the bin\debug folder of your application.  Some DLLs are unnecessary or only needed in certain situations but it&#8217;s easier right now just to copy them all.</p>
<p>Next, look in the C:\ms4w\Apache\cgi-bin\mapscript\csharp folder and copy the two DLLs there &#8211; mapscript.dll and mapscript_csharp.dll. Paste both of these into the same debug folder as above. Finally, back in the C# application, add a reference to the mapscript_csharp.dll only using the Tools&#8230;Add Reference&#8230; menu option.  All of the other DLLs just need to be present in the folder.</p>
<p>Now were are ready to code in the map functionality. Because we referenced the mapscript C# DLL, we have access to an API for interacting with Mapserver.</p>
<p style="font-style: italic;">Step 3 &#8211; Coding with Mapscript</p>
<p>A valid Mapserver application (i.e. GIS data sets and a map file) is still required but we can use the free sample application we setup in the last <a href="http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/">post</a>. Mapserver applications run on a configuration file referred to as a map file (*.map). So this is the first piece of information we need to add to our form.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> MAP_FILE <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;C:\ms4w\apps\gmap\htdocs\gmap75.map&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>This file path is the only variable that our application will need to know before running.  All other code we add is independent of any specific map file or dataset. So if you want to use your own data, feel free to change the value in the source code below.</p>
<p><strong>To Be Continued&#8230; </strong></p>
<p>There is still a lot of coding to discuss in the next couple of posts but for those of you can&#8217;t wait, the complete source code of the application is available to download below along with a screenshot of the program running.  The download includes the Mapserver DLLs so the program should compile and run right away assuming the MAP_FILE constant is pointed to a valid map file. Comments and questions are always welcome at james [at] spatialhorizons.com.</p>
<p><a href="http://spatialhorizons.com/downloads/MapscriptDemo.zip"><img src="http://spatialhorizons.com/downloads/code_download.jpg" border="0" alt="Download Source Code" /></a></p>
<p><img id="image65" src="http://spatialhorizons.com/wp-content/uploads/2007/05/mapscriptdemo_running.jpg" alt="Mapscript Demo Running" /></p>
<p><a title="Mapscript Demo (2)" href="http://spatialhorizons.com/2007/06/02/mapscript-demo-2/">Continue to part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/05/26/mapscript-demo-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MapServer (1) &#8211; 10 Minute Tutorial</title>
		<link>http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/</link>
		<comments>http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/#comments</comments>
		<pubDate>Sat, 19 May 2007 14:08:00 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Mapserver]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/</guid>
		<description><![CDATA[UPDATE: This post was originally posted in May 2007 but since has been updated to cover the more recent release of Mapserver version 5.0.0. Most of our recent posts have focused on topics relating more to the components of a mapping application then on the maps and data. This post begins the first of several [...]]]></description>
			<content:encoded><![CDATA[<p class="moreinfo"><strong>UPDATE:</strong> This post was originally posted in May 2007 but since has been updated to cover the more recent release of Mapserver version 5.0.0.</p>
<p>Most of our recent posts have focused on topics relating more to the components of a mapping application then on the maps and data. This post begins the first of several posts discussing free and open source mapping software applications. After introducing each software, we will explore various ways to create applications around them.</p>
<p><strong>Introduction to MapServer</strong></p>
<p>One of the most popular open source GIS applications is <a href="http://mapserver.gis.umn.edu/" target="_blank">MapServer</a>.  Initially developed by the University of Minnesota, MapServer has evolved into a fast and easy-to-use internet map server.</p>
<p class="shquote">&#8220;MapServer is an Open Source development environment for building spatially-enabled internet applications. MapServer is not a full-featured GIS system, nor does it aspire to be. Instead, MapServer excels at rendering spatial data (maps, images, and vector data) for the web.&#8221; &#8211; <a href="http://mapserver.gis.umn.edu/" target="_blank">http://mapserver.gis.umn.edu/</a></p>
<p><span id="more-58"></span><br />
<strong>Create Your Own Web Mapping Site in 10 Minutes</strong></p>
<p>We just need to setup MapServer to run on a local machine for now and the best way to do that is to use <a href="http://www.maptools.org/ms4w/index.phtml" target="_blank">MapServer for Windows (MS4W)</a>. This package is a all-in-one web mapping environment. It comes complete with Apache Web Server, PHP, MapServer, and other open source GIS utilities. Here&#8217;s how to set it up and add a &#8220;Google Maps&#8221;-like interface (using <a href="http://ka-map.maptools.org/" target="_blank">ka-Map</a>) in under 10 minutes . I won&#8217;t describe the details of how everything interacts because you don&#8217;t really have to know right now. It just works.</p>
<ol>
<li>Download the MS4W PHP5 Base Installer v2.2.6 (Sept 18,2007) from <a href="http://www.maptools.org/ms4w/index.phtml?page=downloads.html" target="_blank">here</a>.</li>
<li>Unzip the file to the root of your C:\ drive and this will create a folder C:\ms4w.</li>
<li>For some initial data, download the PHP/MapScript Sample Application <a href="http://maptools.org/ms4w/index.phtml?page=downloads.html" target="_blank">here</a>.  The zip file is named gmap_ms4w_ms410.zip.</li>
<li>Again, unzip this file at the root of your C:\ drive and it will automatically be placed into the folder C:\ms4w\apps\gmap.</li>
<li>Download the ka-Map Javascript API <a href="http://www.maptools.org/ms4w/index.phtml?page=downloads.html" target="_blank">from the same page</a>. This file is named ka-map-ms4w-1.0.zip.</li>
<li>Again, unzip this file to the root of your C:\ drive and it will automatically be placed into the folder C:\ms4w\apps\ka-map-1.0.</li>
<li>Navigate to C:\ms4w and double-click on the file named apache-install.bat. In one-click, you&#8217;ve setup your web server and MapServer is ready to use.</li>
<li>Open your web browser and goto http://localhost/.  You should see an Introduction to MapServer 4 Windows page.</li>
<li>Scroll to the bottom of the page and click the ka-Map Sample Interface link. This will load up the sample application we downloaded in step 3 using the ka-Map interface. The map should look like this:</li>
</ol>
<p><img id="image60" src="http://spatialhorizons.com/wp-content/uploads/2007/05/kamap1.jpg" alt="kamap1" width="440" height="307" /></p>
<p><img id="image61" src="http://spatialhorizons.com/wp-content/uploads/2007/05/kamap2.jpg" alt="kamap2" width="440" height="290" /><br />
Congratulations! You&#8217;ve just setup a mapping website. In short, ka-Map is basically a web page front-end for MapServer that is similar to Google Maps.  MapServer is used to generate map images in the form of map tiles and ka-Map then presents them to the user so they can pan, zoom, identify, and search the map. More details to follow in later posts.</p>
<p><a title="Using MapServer (2) – Generating Map Tiles" href="http://spatialhorizons.com/2007/11/14/using-mapserver-2-generating-map-tiles/">Continue to part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/05/19/mapserver-1-10-minute-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

