how to dispaly shapefile from sqlserver 2005 using sharpmap

Topics: User Forum
Feb 18, 2008 at 7:47 PM
I have imported data from shapefile into ms sql server 2005 and now want to display this in web using sharpmap. whats the code.
Feb 29, 2008 at 8:18 PM
SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("XXXLAYERNAME");
myLayer.DataSource = new SharpMap.Data.Providers.MsSqlSpatial(@"Data Source=192.168.1.1;Initial Catalog=spatialdb;Persist Security Info=True;User ID=xxx;Password=xxx;", "XXXTABLENAME", "uniqueId");
myLayer.Style.Fill = new SolidBrush(Color.Red);
myLayer.Style.Line.Width = 1;
myLayer.Style.Line.Color = Color.Black;
myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end
myLayer.Style.Line.DashPattern = new float[] { 4.0f, 2.0f }; //Dashed linestyle
myLayer.Style.EnableOutline = true;
myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
myLayer.MinVisible = 1000;
myLayer.MaxVisible = 250000;
myMap.Layers.Add(myLayer);
Nov 24, 2008 at 3:47 AM
hi sheikh
This is neoo.Its great that u import data form shapefile into ms sql server 2005 but I failed todo this. Could u plz help me , how can I import data form shapefile.
Best Regard
neoo 
Developer
Nov 24, 2008 at 7:46 PM
hi neoo use the msscmd utility in the console project hth jd
Nov 27, 2008 at 4:32 AM
dear,
i am quite busy nowadays but soon i will send u complete detail.

--- On Mon, 11/24/08, neoo <notifications@codeplex.com> wrote:
From: neoo <notifications@codeplex.com>
Subject: Re: how to dispaly shapefile from sqlserver 2005 using sharpmap [MsSqlSpatial:22401]
To: rahim_pak@yahoo.com
Date: Monday, November 24, 2008, 9:48 AM

#yiv1158942265 {font-family:Verdana;font-size:0.75em;}#yiv1158942265 #ThreadNotificationFooter{border-top:1px solid #ccc;color:gray;}#yiv1158942265 #ThreadNotificationPostBody{Margin-Bottom:2em;} #yiv1158942265 {font-family:Verdana;font-size:0.75em;}#yiv1158942265 #ThreadNotificationFooter{color:gray;border-top:1px solid #ccc;}#yiv1158942265 #ThreadNotificationPostBody{Margin-Bottom:2em;}

From: neoo

hi sheikh
This is neoo.Its great that u import data form shapefile into ms sql server 2005 but I failed todo this. Could u plz help me , how can I import data form shapefile.
Best Regard
neoo

Nov 29, 2008 at 4:50 AM

Hi sheikh

Thank you for your quick answer and also thank you to johndiss, I sucessfully imported shape file to MS SQL 2005.But the proble is tried tunzey provied code but it does not generate any map.May be problem is in my code.

To show the whole may what should I do,here in my  code I call a collom of my table, is the right to show whole map(leble,area,polygon....).My code is here

HttpContext.Current.Trace.Write("Initializing map...");
//string nm = HttpContext.Current.Session["Name"].ToString();
//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map(size);

SharpMap.Styles.VectorStyle max = new SharpMap.Styles.VectorStyle();
SharpMap.Styles.VectorStyle min = new SharpMap.Styles.VectorStyle();
max.Outline.Width = 1f;
min.Outline.Width = .5f;
max.Outline.Color = Color.Red;
min.Outline.Color = Color.Yellow;

 SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("XXXLAYERNAME");
// myLayer.DataSource = new SharpMap.Data.Providers.MsSqlSpatial(@"Data Source=192.168.1.1;Initial Catalog=spatialdb;Persist Security Info=True;User ID=xxx;Password=xxx;", "XXXTABLENAME", "uniqueId");
myLayer.DataSource = new SharpMap.Data.Providers.MsSqlSpatial(@"Data Source=LAST007-12D0938\SQLEXPRESS;Initial Catalog=test123;Integrated Security=True;", "t1", "PLOT_NUM");
// Data Source=LAST007-12D0938\SQLEXPRESS;Initial Catalog=test123;Integrated Security=True;Pooling=False
myLayer.Style.Fill = new SolidBrush(Color.Red);
myLayer.Style.Line.Width = 1;
myLayer.Style.Line.Color = Color.Black;
myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end
myLayer.Style.Line.DashPattern = new float[] { 4.0f, 2.0f }; //Dashed linestyle
myLayer.Style.EnableOutline = true;
myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
myLayer.MinVisible = 1000;
myLayer.MaxVisible = 250000;
map.Layers.Add(myLayer);

  map.Zoom = 360;
map.Center = new SharpMap.Geometries.Point(0, 0);
//map.ZoomToExtents();
HttpContext.Current.Trace.Write("Map initialized");
return map;
----------------------------------

can u say where I am wrong ?


Developer
Nov 29, 2008 at 2:35 PM
hi neoo, are you sure that point 0,0 exists in your map? try setting myLayer.MinVisible = 0 and MaxVisible to 10000000 and call map.ZoomToExtents hth jd
Dec 2, 2008 at 10:55 AM
hi johndiss
Thank you  for your quick response.I am going to do your answer, but I have some confution.
After importing shape file to MS sql 2005, it generate a  table and this table has many coloum like oid,.....and many data.I am not understanding which coloum should I select or use to display whole map ?
like here COLOUM ?
myLayer.DataSource = new SharpMap.Data.Providers.MsSqlSpatial(@"Data Source=LAST007-12D0938\SQLEXPRESS;Initial Catalog=test123;Integrated Security=True;", "t1", "COLOUM");
Developer
Dec 2, 2008 at 9:52 PM
Edited Dec 3, 2008 at 9:37 AM
You should put the name of the geometry column. you can find it out by looking for the varbinary column in the tables' schema or in the st.geometry_columns table hth jd
Dec 5, 2008 at 12:08 PM
Edited Dec 7, 2008 at 6:54 AM

hi johndiss
here I add all my code.plz answer me where I am wrong.
if I add  map.ZoomToExtents(); in my code, then this error shows.

[NullReferenceException: Object reference not set to an instance of an object.]
   SharpMap.Data.Providers.MsSqlSpatial.GetExtents() in D:\Projects\SharpLatest\SharpMap-20719\SharpMap\Data\Providers\MsSqlSpatial.cs:615
   SharpMap.Layers.VectorLayer.get_Envelope() in D:\Projects\SharpLatest\SharpMap-20719\SharpMap\Layers\VectorLayer.cs:299
   SharpMap.Map.GetExtents() in D:\Projects\SharpLatest\SharpMap-20719\SharpMap\Map\Map.cs:417
   SharpMap.Map.ZoomToExtents() in D:\Projects\SharpLatest\SharpMap-20719\SharpMap\Map\Map.cs:238
   MapHelper.InitializeMap(Size size) in e:\Home work\In UPS sharpmap\DemoWebSite\App_Code\MapHelper.cs:158
   Simple.Page_Load(Object sender, EventArgs e) in e:\Home work\In UPS sharpmap\DemoWebSite\Simple.aspx.cs:25
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
   System.Web.UI.Control.OnLoad(EventArgs e) +88
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3036



My total code is here

///------------this MapHelper.cs file is  under App_Code folder

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;

public class MapHelper
{

 public static SharpMap.Map InitializeMap(System.Drawing.Size size)
    {
            HttpContext.Current.Trace.Write("Initializing map...");
            //string nm = HttpContext.Current.Session["Name"].ToString();
            //Initialize a new map of size 'imagesize'
            SharpMap.Map map = new SharpMap.Map(size);
            SharpMap.Styles.VectorStyle max = new SharpMap.Styles.VectorStyle();
            SharpMap.Styles.VectorStyle min = new SharpMap.Styles.VectorStyle();
            max.Outline.Width = 1f;
            min.Outline.Width = .5f;
            max.Outline.Color = Color.Red;
            min.Outline.Color = Color.Yellow;


            SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("XXXLAYERNAME");
            myLayer.DataSource = new SharpMap.Data.Providers.MsSqlSpatial(@"Data Source=SAYED\SQLEXPRESS;Initial Catalog=gis_db001;Integrated Security=True;", "tbl_001", "the_geom");
            myLayer.Style.Fill = new SolidBrush(Color.Red);
            myLayer.Style.Line.Width = 1;
            myLayer.Style.Line.Color = Color.Black;
            myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end
            myLayer.Style.Line.DashPattern = new float[] { 4.0f, 2.0f }; //Dashed linestyle
            myLayer.Style.EnableOutline = true;
            myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines

        myLayer.MinVisible = 0;
       // mayLayer.MaxVisible = 10000000;
        myLayer.MaxVisible = 250000;
        map.Layers.Add(myLayer);
      map.ZoomToExtents();
            map.BackColor = Color.LightBlue;    
            HttpContext.Current.Trace.Write("Map initialized");
            return map;
    }
}
//-------------end


//---this is my simple.cs file which is using as start Page

using System;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Simple : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
    {
        //Set up the map. We use the method in the App_Code folder for initializing the map

        myMap = MapHelper.InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value,(int)imgMap.Height.Value));

        if (Page.IsPostBack)
        {
            //Page is post back. Restore center and zoom-values from viewstate
            myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];
            myMap.Zoom = (double)ViewState["mapZoom"];
        }
        else
        {

            GenerateMap();
        }
        
        
    }
}

}

//---end

Please help me .Map is not displaying.
Developer
Dec 8, 2008 at 10:47 PM
Hi neoo, can you post the html markup as well tia jd
Dec 11, 2008 at 7:46 AM
Edited Dec 11, 2008 at 8:00 AM
Thank u JohnDiss for ur reply.
Here I am using sharpmap example.I can generate map using sharpmap from shape file.But I am field to generate map from MS SQL2005 as u know.Do u have any example on MsSqlSpatial like sharpmap WebDemo ?
The example in which u use Ms Sql file to generate map and also some extra work.If u have plz give me.

I am badly in need of this kind of example to solve my problem

Best Regards
neeo 
Developer
Dec 11, 2008 at 12:08 PM
Edited Dec 11, 2008 at 12:11 PM
Hi neoo, are you sure the geometry column in your database is called "the_geom"?


I was able to run the simple.aspx demo using the following map helper: 

 



public
static SharpMap.Map InitializeMapMsSqlSpatial(System.Drawing.Size size)

 

{

 

 

HttpContext.Current.Trace.Write("Initializing map...");

 

 

 

string connectionString = ConfigurationManager.ConnectionStrings["mssqlspatial"].ConnectionString;

 

 

 

//Initialize a new map of size 'imagesize'

 

 

SharpMap.

Map map = new SharpMap.Map(size);

 

 

 

//Set up the countries layer

 

 

SharpMap.Layers.

VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");

 

layCountries.DataSource =

 

 

new SharpMap.Data.Providers.MsSqlSpatial(connectionString, "Country", "Wkb_Geometry", "oid");

 

 

 

//Set fill-style to green

 

 

layCountries.Style.Fill =

new SolidBrush(Color.Green);

 

 

 

//Set the polygons to have a black outline

 

 

layCountries.Style.Outline = System.Drawing.

Pens.Black;

 

layCountries.Style.EnableOutline =

 

true;

 

layCountries.SRID = 4326;

 

 

//Set up a river layer

 

 

SharpMap.Layers.

VectorLayer layRivers = new SharpMap.Layers.VectorLayer("Rivers");

  

 

 

layRivers.DataSource =

new SharpMap.Data.Providers.MsSqlSpatial(connectionString, "Rivers", "Wkb_Geometry", "oid");

 

 

 

//Define a blue 1px wide pen

 

 

layRivers.Style.Line =

new Pen(Color.Blue, 1);

 

layRivers.SRID = 4326;

 

 

//Set up a river layer

 

 

SharpMap.Layers.

VectorLayer layCities = new SharpMap.Layers.VectorLayer("Cities");

 

 

layCities.DataSource =

new SharpMap.Data.Providers.MsSqlSpatial(connectionString, "NamedPlaces", "Wkb_Geometry", "oid");

 

 

 

//Define a blue 1px wide pen

 

 

 

//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));

 

 

layCities.Style.SymbolScale = 0.8f;

layCities.MaxVisible = 40;

layCities.SRID = 4326;

 

//Set up a country label layer

 

 

SharpMap.Layers.

LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Country labels");

 

layLabel.DataSource = layCountries.DataSource;

layLabel.Enabled =

 

true;

 

layLabel.LabelColumn =

 

"Name";

 

layLabel.Style =

 

new SharpMap.Styles.LabelStyle();

 

layLabel.Style.ForeColor =

 

Color.White;

 

layLabel.Style.Font =

 

new Font(FontFamily.GenericSerif, 12);

 

layLabel.Style.BackColor =

 

new System.Drawing.SolidBrush(Color.FromArgb(128, 255, 0, 0));

 

layLabel.MaxVisible = 90;

layLabel.MinVisible = 30;

layLabel.Style.HorizontalAlignment = SharpMap.Styles.

 

LabelStyle.HorizontalAlignmentEnum.Center;

 

layLabel.SRID = 4326;

layLabel.MultipartGeometryBehaviour = SharpMap.Layers.

 

LabelLayer.MultipartGeometryBehaviourEnum.Largest;

 

 

 

//Set up a city label layer

 

 

SharpMap.Layers.

LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City labels");

 

layCityLabel.DataSource = layCities.DataSource;

layCityLabel.Enabled =

 

true;

 

layCityLabel.LabelColumn =

 

"Name";

 

layCityLabel.Style =

 

new SharpMap.Styles.LabelStyle();

 

layCityLabel.Style.ForeColor =

 

Color.Black;

 

layCityLabel.Style.Font =

 

new Font(FontFamily.GenericSerif, 11);

 

layCityLabel.MaxVisible = layLabel.MinVisible;

layCityLabel.Style.HorizontalAlignment = SharpMap.Styles.

 

LabelStyle.HorizontalAlignmentEnum.Left;

 

layCityLabel.Style.VerticalAlignment = SharpMap.Styles.

 

LabelStyle.VerticalAlignmentEnum.Bottom;

 

layCityLabel.Style.Offset =

 

new PointF(3, 3);

 

layCityLabel.Style.Halo =

 

new Pen(Color.Yellow, 2);

 

layCityLabel.TextRenderingHint = System.Drawing.Text.

 

TextRenderingHint.AntiAlias;

 

layCityLabel.SmoothingMode =

 

SmoothingMode.AntiAlias;

 

layCityLabel.SRID = 4326;

layCityLabel.LabelFilter = SharpMap.Rendering.

 

LabelCollisionDetection.ThoroughCollisionDetection;

 

layCityLabel.Style.CollisionDetection =

 

true;

 

 

 

//Add the layers to the map object.

 

 

 

//The order we add them in are the order they are drawn, so we add the rivers last to put them on top

 

 

map.Layers.Add(layCountries);

map.Layers.Add(layRivers);

map.Layers.Add(layCities);

map.Layers.Add(layLabel);

map.Layers.Add(layCityLabel);

 

map.BackColor = Color.LightBlue;

 

map.ZoomToExtents();

 

 

HttpContext.Current.Trace.Write("Map initialized");

 

 

 

return map;

 

}

 

Dec 13, 2008 at 3:37 AM
hi JohnDiss
Thank u for ur example code.
"the_geom"  is my geometry column.Here is my databse table's design view.
-------------start-----------
oid    int    Unchecked
the_geom    varbinary(MAX)    Checked
AREA    float    Checked
PERIMETER    float    Checked
PLOT_    bigint    Checked
PLOT_ID    bigint    Checked
USER_ID    bigint    Checked
LOCATION    varchar(4000)    Checked
BLOCK_NAME    varchar(4000)    Checked
SECTION    varchar(4000)    Checked
PLOT_NO    varchar(4000)    Checked
TYPE    varchar(4000)    Checked
ROAD_NO    smallint    Checked
ROADNAME    varchar(4000)    Checked
LAND_USE    bigint    Checked
PLT_CTRY    varchar(4000)    Checked
PAY_ST    varchar(4000)    Checked
SALE_STS    varchar(4000)    Checked
PLOT_ORT    varchar(4000)    Checked
FREEZE    varchar(4000)    Checked
ALOT_STS    varchar(4000)    Checked
PL_DIS    varchar(4000)    Checked
PRESENTSIT    varchar(4000)    Checked
ALLOT_ID    varchar(4000)    Checked
LBLHEIGHT    float    Checked
PLOT_NUM    varchar(4000)    Checked
the_geom_Envelope_MinX    float    Checked
the_geom_Envelope_MinY    float    Checked
the_geom_Envelope_MaxX    float    Checked
the_geom_Envelope_MaxY    float    Checked
        Unchecked

-----------end----------------

I am trying to solve my problem using ur code.
Do u have any complete example package like what we can download form sharpmap download section "webdemo".
if u add database file and other necessary files with ur example package,that will be great.

Again thank you

 
Developer
Dec 13, 2008 at 12:13 PM
Hi neoo, I have added a demo web page to the sharpmap source tree. Also in the tree is a db backup which needs to be restored to an instance of  Sql Server 2008 Express or greater as I don't have 2005 or Express (so I can't mount the db from App_Data)  installed on this machine. hth jd
Jan 3, 2009 at 7:13 AM
Edited Jan 3, 2009 at 7:19 AM
hi johndiss
sorry for let reply.Thank u for the demo.I am working on it
Nov 28, 2010 at 7:47 AM

HI,everyone.I am a GIS student come from Shanghai,China.I  have import my shapefile into SQL2005.Now ,i want to display it  using sharpmap.Is there anyone can help me?Thanks a lot!