MapWindow Script Directory
Script: UMN Mapserver Map Generator
/*
* Created by Senen Ebio (senenebio at yahoo dot com)
* See also: http://gis.naga.gov.ph
* Date: 10/20/2006
* Time: 7:15 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
namespace MapServerMapScriptGenerator
{
///
/// Description of MyClass.
///
using System;
using System.IO;
using System.Drawing;
using MapWindow.Interfaces;
using MapWinGIS;
using System.Windows.Forms;
using Microsoft.VisualBasic;
// Each script should (but doesn't have to) have a unique name. Change MyExample here to something meaningful. ScriptMain should remain as "Main" however.
class MyExample
{
private IMapWin m_mapwin;
public string m_mapfile;
//public bool m_overwrite;
//name section
public string m_mapname;
public string m_mapwidth;
public string m_mapheight;
public string m_mapunits;
public string m_mapimagetype;
//web section
public string m_mapminscale;
public string m_mapmaxscale;
public string m_mapheader;
public string m_maptemplate;
public string m_mapfooter;
public static void ScriptMain(IMapWin m_MapWin)
{
MessageBox.Show("This is a simple script will generate Mapserver map file.");
if (m_MapWin.Layers.NumLayers <=0) {
MessageBox.Show("Number of Layers: " m_MapWin.Layers.NumLayers);
return;
}
MyExample obj = new MyExample ();
obj.m_mapwin = m_MapWin;
obj.m_mapfile = "name.map";
obj.m_mapname = "mapname";
//obj.m_overwrite = true;
obj.m_mapwidth = "800";
obj.m_mapheight = "600";
obj.m_mapunits = "dd";
obj.m_mapimagetype = "png";
obj.m_mapminscale = "10";
obj.m_mapmaxscale = "10000";
obj.m_mapheader = ""; //"header.html"
obj.m_maptemplate= ""; //"template.html"
obj.m_mapfooter = ""; //"footer.html"
obj.BuildMapFile ();
MessageBox.Show("Done, generated ----> " obj.m_mapfile);
}
public void BuildMapFile ()
{
try
{
StreamWriter textfile = File.CreateText(this.m_mapfile);
HeaderComments (textfile);
//if (!this.m_overwrite)
{
BuildNameSection (textfile);
BuildWebSection (textfile);
BuildProjectionSection (textfile);
BuildQueryMapSection (textfile);
BuildLegendSection (textfile);
BuildScaleBarSection (textfile);
BuildReferenceSection (textfile);
}
BuildLayersSection (textfile);
FooterComments (textfile);
textfile.Close ();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString (), "BuildMapFile" );
}
}
private void HeaderComments (StreamWriter textfile)
{
textfile.WriteLine ("# Map file generated by MapWindow Plugin : " "ScriptName");
textfile.WriteLine ("# Version " "ScriptName.Version");
textfile.WriteLine ("# Edit this file to customize for your interface.");
textfile.WriteLine ("# Not all sections are complete. See comments for details.");
textfile.WriteLine ("# ");
}
private void BuildNameSection (StreamWriter textfile)
{
textfile.WriteLine ("NAME " this.m_mapname);
textfile.WriteLine ("\tSTATUS ON");
textfile.WriteLine ("");
textfile.WriteLine ("\tSIZE " this.m_mapwidth " " this.m_mapheight);
textfile.WriteLine ("\tUNITS " this.m_mapunits);
textfile.WriteLine ("\tIMAGETYPE " this.m_mapimagetype);
this.m_mapwin.View.ZoomToMaxExtents ();
MapWinGIS.IExtents fullext = this.m_mapwin.View.Extents;
this.m_mapwin.View.ZoomToPrev ();
textfile.WriteLine ("\tEXTENT " fullext.xMin " " fullext.yMin " "
fullext.yMax " " fullext.yMax);
textfile.WriteLine ("");
textfile.WriteLine ("\t#IMAGECOLOR 255 255 255");
textfile.WriteLine ("\t#SHAPEPATH ../data");
textfile.WriteLine ("\t#SYMBOLSET ../etc/symbols.sym");
textfile.WriteLine ("\t#FONTSET ../etc/fonts.txt");
textfile.WriteLine ("");
}
private void BuildWebSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of web interface definition");
textfile.WriteLine ("WEB");
textfile.WriteLine ("\tMINSCALE " this.m_mapminscale);
textfile.WriteLine ("\tMAXCALE " this.m_mapmaxscale);
textfile.WriteLine ("");
textfile.WriteLine ("\t# On Windows systems, /tmp and /tmp/ms_tmp/ should be created at");
textfile.WriteLine ("\t# the root of the drive where the .MAP file resides.");
textfile.WriteLine ("\t#IMAGEPATH '/ms4w/tmp/ms_tmp/'");
textfile.WriteLine ("\t#IMAGEURL '/ms_tmp/'");
textfile.WriteLine ("");
textfile.WriteLine ("\t#HEADER " this.m_mapheader );
textfile.WriteLine ("\t#TEMPLATE " this.m_maptemplate);
textfile.WriteLine ("\t#FOOTER " this.m_mapfooter);
textfile.WriteLine ("END");
textfile.WriteLine ("");
}
private void BuildProjectionSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of projection interface definition");
textfile.WriteLine ("#PROJECTION");
textfile.WriteLine ("#\tinit=epsg:4326");
textfile.WriteLine ("#END");
textfile.WriteLine ("");
}
private void BuildQueryMapSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of query map interface definition");
textfile.WriteLine ("#QUERYMAP");
textfile.WriteLine ("\t#STYLE HILITE");
textfile.WriteLine ("\t#COLOR 255 128 0");
textfile.WriteLine ("#END");
textfile.WriteLine ("");
}
private void BuildLegendSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of legend interface definition");
textfile.WriteLine ("#LEGEND");
textfile.WriteLine ("\t#KEYSIZE 18 12");
textfile.WriteLine ("\t#LABEL");
textfile.WriteLine ("\t\t#TYPE BITMAP");
textfile.WriteLine ("\t\t#SIZE MEDIUM");
textfile.WriteLine ("\t\t#COLOR 0 0 89");
textfile.WriteLine ("\t#END");
textfile.WriteLine ("\t#STATUS ON");
textfile.WriteLine ("#END");
textfile.WriteLine ("");
}
private void BuildScaleBarSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of scalebar interface definition");
textfile.WriteLine ("#SCALEBAR");
textfile.WriteLine ("\t#IMAGECOLOR 255 255 255");
textfile.WriteLine ("\t#LABEL");
textfile.WriteLine ("\t\t#COLOR 0 0 0");
textfile.WriteLine ("\t\t#SIZE SMALL");
textfile.WriteLine ("\t#END");
textfile.WriteLine ("\t#SIZE 200 5");
textfile.WriteLine ("\t#COLOR 255 255 255");
textfile.WriteLine ("\t#BACKGROUNDCOLOR 0 0 0");
textfile.WriteLine ("\t#OUTLINECOLOR 0 0 0");
textfile.WriteLine ("\t#UNITS KILOMETERS");
textfile.WriteLine ("\t#INTERVALS 2");
textfile.WriteLine ("\t#STATUS ON");
textfile.WriteLine ("#END");
textfile.WriteLine ("");
}
private void BuildReferenceSection (StreamWriter textfile)
{
textfile.WriteLine ("# Start of refefence map interface definition");
textfile.WriteLine ("#REFERENCE");
textfile.WriteLine ("\t#IMAGE images/keymap.png");
textfile.WriteLine ("\t#EXTENT minX minY maxX maxY");
textfile.WriteLine ("\t#STATUS ON");
textfile.WriteLine ("\t#COLOR -1 -1 -1");
textfile.WriteLine ("\t#OUTLINECOLOR 255 0 0");
textfile.WriteLine ("\t#SIZE 150 100");
textfile.WriteLine ("#END");
textfile.WriteLine ("");
}
private void BuildLayersSection (StreamWriter textfile)
{
for (int i = 0; i < this.m_mapwin.Layers.NumLayers ; i)
{
//int handle = this.m_mapwin.Layers[i].Handle;
Layer layer = this.m_mapwin.Layers[i];
MapWindow.Interfaces.eLayerType layertype = layer.LayerType;
MessageBox.Show("Layer type: " layertype);
if (layertype == MapWindow.Interfaces.eLayerType.LineShapefile ||
layertype == MapWindow.Interfaces.eLayerType.PointShapefile ||
layertype == MapWindow.Interfaces.eLayerType.PolygonShapefile)
{
//Get shapefile
MapWinGIS.Shapefile sf = (MapWinGIS.Shapefile) layer.GetObject ();
//save color scheme
MapWinGIS.ShapefileColorScheme cs = (MapWinGIS.ShapefileColorScheme) layer.ColoringScheme;
textfile.WriteLine ("LAYER");
textfile.WriteLine ("\tNAME " layer.Name);
switch (layertype)
{
case MapWindow.Interfaces.eLayerType.PointShapefile:
textfile.WriteLine ("\tTYPE POINT");
break;
case MapWindow.Interfaces.eLayerType.LineShapefile:
textfile.WriteLine ("\tTYPE LINE");
break;
case MapWindow.Interfaces.eLayerType.PolygonShapefile:
textfile.WriteLine ("\tTYPE POLYGON");
break;
}
if (layer.Visible)
{
textfile.WriteLine ("\tSTATUS ON");
}
else
{
textfile.WriteLine ("\tSTATUS OFF");
}
textfile.WriteLine ("\tDATA '" layer.FileName.Replace ("\\", "/") "'");
textfile.WriteLine ("\tCLASS");
textfile.WriteLine ("\t\tNAME '" layer.Name "'") ;
Color col = layer.Color;
Color outline = layer.OutlineColor;
textfile.WriteLine ("\t\tCOLOR " col.R.ToString ()
" " col.G.ToString () " " col.B.ToString () );
textfile.WriteLine ("\t\tOUTLINECOLOR " outline.R.ToString ()
" " outline.G.ToString () " " outline.B.ToString () );
textfile.WriteLine ("\tEND");
if ( cs != null ) {
textfile.WriteLine ("\t#CLASSITEM " sf.get_Field (cs.FieldIndex ).Name);
for (int brk = 0; brk < cs.NumBreaks () ; brk) {
MapWinGIS.ShapefileColorBreak cbrk = cs.get_ColorBreak (brk);
System.Drawing.Color scolor = System.Drawing.ColorTranslator.FromWin32 ( (int)cbrk.StartColor);;
//System.Drawing.Color ecolor = System.Drawing.ColorTranslator.FromWin32 ( (int)cbrk.EndColor);;
textfile.WriteLine ("\t#CLASS");
string class_name = layer.Name "_" cbrk.Caption;
class_name.Replace (" ", "_");
textfile.WriteLine ("\t\t#NAME '" class_name.ToUpper () "'") ;
textfile.WriteLine ("\t\t#EXPRESSION '" cbrk.Caption "'");
textfile.WriteLine ("\t\t#STYLE");
textfile.WriteLine ("\t\t\t#COLOR " scolor.R.ToString ()
" " scolor.G.ToString () " " scolor.B.ToString () );
textfile.WriteLine ("\t\t\t#OUTLINECOLOR " outline.R.ToString ()
" " outline.G.ToString () " " outline.B.ToString () );
textfile.WriteLine ("\t\t#END");
textfile.WriteLine ("\t#END");
}
}
textfile.WriteLine ("\t#PROJECTION");
textfile.WriteLine ("\t\t#'init=epsg:4326'");
textfile.WriteLine ("\t#END");
//how to get transparency?
if (layer.LayerType == MapWindow.Interfaces.eLayerType.PolygonShapefile)
{
textfile.WriteLine ("\t#TRANSPARENCY 50");
}
textfile.WriteLine ("END");
textfile.WriteLine ("");
}
}
}
private void FooterComments (StreamWriter textfile)
{
textfile.WriteLine ("");
textfile.WriteLine ("END # All map files must come to an end just as all other things must come to...");
}
}
}Back









