PolyGon Sample

Topics: Data Access, SharpMap v0.9 / v1.x
Apr 6, 2009 at 8:58 PM

Hi Everyone,
I'd like to introduce sample code for ShapeFile. ^^

//Map data : http://www.vdstech.com/map_data.htm
 public static SharpMap.Map InitializeMap_usaState(System.Drawing.Size size)
 {
  //Initialize a new map of size 'imagesize'
  SharpMap.Map map = new SharpMap.Map(size);
  map.BackColor = Color.White;

  //Layer
  SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("Countries");
  SharpMap.Data.Providers.ShapeFile sf = new SharpMap.Data.Providers.ShapeFile(@"C:\...\Map\data\usstates\usa_st.shp");

  string str = getColumnNames(sf);
  layer.DataSource = sf ;

  layer.Style.Fill = Brushes.LightGreen;
  layer.Style.EnableOutline = true;
  layer.Style.Outline = Pens.DarkGreen;
  
  //Set up a city label layer
  SharpMap.Layers.LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City labels");
  layCityLabel.DataSource = layer.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.Style.CollisionDetection = true;

  layCityLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;

  /*
  MinVisible : If you set this property to the extents of the layer, you won't see the labels.
  Setting it to zero will show them at smaller zoom levels.
  */
  //layCityLabel.MaxVisible = layer.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.LabelStringDelegate = getString;

  map.Layers.Add(layer);
  map.Layers.Add(layCityLabel);

  return map;
 }

 //http://sharpmap.codeplex.com/Thread/View.aspx?ThreadId=10754
 public static String getColumnNames(SharpMap.Data.Providers.ShapeFile shapefile)
 {
  String columns = "";
  SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
  SharpMap.Geometries.BoundingBox bbox = new SharpMap.Geometries.BoundingBox(0, 0, 1, 1);

  shapefile.Open();
  shapefile.ExecuteIntersectionQuery(bbox, ds);
  shapefile.Close();

  for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
  {
   columns += (ds.Tables[0].Columns[i].ColumnName) + ",";
  }

  return columns;
 }

 private static string getString(SharpMap.Data.FeatureDataRow fdr)
 {
  return fdr[4].ToString();
 }

Apr 6, 2009 at 9:16 PM

  //http://sharpmap.codeplex.com/Wiki/View.aspx?title=CustomTheme&referringTitle=Styling%20based%20on%20feature%20data
  private SharpMap.Styles.VectorStyle GetCountryStyle(SharpMap.Data.FeatureDataRow row)
  {
   SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();

   string strTemp = row[4].ToString();

   if (strTemp.ToUpper() == "CA")
   {
    style.Fill = Brushes.Green;

    //style.Symbol = this.GetChart(70,30);
    //style.SymbolScale = 0.8f;
    //int iHeight = style.Symbol.Height;

    return style;
   }

   return null;
  }
   
  private void button1_Click(object sender, EventArgs e)
  {
   map = InitializeMap_usaState(mapImage1.Size);
   
   SharpMap.Rendering.Thematics.CustomTheme iTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetCountryStyle);
   SharpMap.Styles.VectorStyle defaultstyle = new SharpMap.Styles.VectorStyle();
   defaultstyle.Fill = Brushes.Gray;

   iTheme.DefaultStyle = defaultstyle;
   (map.GetLayerByName("Countries") as SharpMap.Layers.VectorLayer).Theme = iTheme;
  }