SharpMap.NET – Post 1

I’ve been fiddling around with SharpMap and PostGIS so that I could display some GIS Data in a web application. I searched around and found very meager resources out w.r.t SharpMap. So I decided to post my progress as I programmed my way around the component.

For my first post, I just displayed some GIS Data for the state of Louisiana.

ASPX Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Geo.Master" AutoEventWireup="true" CodeBehind="map.aspx.cs" Inherits="" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <div style="border: 1px solid #000000; padding: 10px;">
            <asp:Image ID="Image1" runat="server" />

C# Code:

        protected void Page_Load(object sender, EventArgs e)
            //Get the map object
            SharpMap.Map mp = new SharpMap.Map();
            mp.MinimumZoom = 100;
            mp.BackColor = System.Drawing.Color.Transparent;

            //create the vector layer
            SharpMap.Layers.VectorLayer vl = new SharpMap.Layers.VectorLayer("LA_HOUSE");
            vl.DataSource = new SharpMap.Data.Providers.PostGIS(ConfigurationSettings.AppSettings["GISConnString"], "la_uscon", "17796");
            vl.Style.Outline = System.Drawing.Pens.Red;
            vl.Style.EnableOutline = true;
            vl.Style.Line = new System.Drawing.Pen(System.Drawing.Color.Aquamarine);
            vl.Style.Fill = new SolidBrush(Color.Aquamarine);

            //create the label acetate layer
            SharpMap.Layers.LabelLayer layASLabel = new SharpMap.Layers.LabelLayer("LA_HOUSE_DISTS");
            layASLabel.DataSource = vl.DataSource;
            layASLabel.LabelColumn = "cd111fp";
            layASLabel.Style.Font = new Font("Arial", 6, FontStyle.Bold);
            layASLabel.Style = new SharpMap.Styles.LabelStyle();
            layASLabel.Style.ForeColor = Color.Black;
            layASLabel.Style.Offset = new PointF(10, 0);
            layASLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            layASLabel.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            layASLabel.Style.CollisionDetection = true;
            layASLabel.Style.CollisionBuffer = new SizeF(5, 5);
            layASLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;

            System.Drawing.Image img = mp.GetMap();
            int ht = img.Height;
            int wd = img.Width;
            double ratio =  Convert.ToDouble( wd / ht);
            int new_ht = 400;
            int new_wd = -1;
            new_wd = Convert.ToInt32(new_ht * ratio);
            Image1.Height = new_ht;
            Image1.Width = new_wd;
            string imgID = SharpMap.Web.Caching.InsertIntoCache(5,img);

            Image1.ImageUrl ="mymap.aspx?ID=" + HttpUtility.UrlEncode(imgID);

It results in this map.

Now now…I know it ain’t purdy, but its a map. We’ll pretty it up later and add functionality as well.

NOTE: Dont forget to add the Http Handler in your web.config that will handle the requests to mymap.aspx!
<add verb=”*” path=”mymap.aspx” type=”SharpMap.Web.HttpHandler,SharpMap”/>