ThinkGeo.com    |     Documentation    |     Premium Support

Buffer a Feature

I looked  "Buffer a Feature" sample  on map suite web samples and tryed to do it but it does not draw any polygon on map. 


 


private void BufferAFeature(string wellKnowntext)


{


  InMemoryFeatureLayer mshapeLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Layers["ShapeLayer"];

  

  Feature feature = new Feature(wellKnowntext);

  mshapeLayer.InternalFeatures.Add(feature.Id,feature);

  


   Map1.EditOverlay.Features.Add(feature.Id, feature); //  if I remove this line it does not draw the first polygon

                

  ((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Redraw();         

                        

   InMemoryFeatureLayer bufferLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Layers["bufferLayer"];


   AreaBaseShape baseShape = (AreaBaseShape)mshapeLayer.InternalFeatures[feature.Id].GetShape();

   MultipolygonShape bufferedShape = baseShape.Buffer(10, 8, BufferCapType.Round, GeographyUnit.Meter, DistanceUnit.Meter);

   Feature bufferFeature = new Feature(bufferedShape);


   bufferLayer.InternalFeatures.Clear();

   bufferLayer.InternalFeatures.Add("BufferFeature", bufferFeature);

           


   ((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Redraw();


   txtFeature.Text = String.Empty;

   txtFeature.Text = txtFeature.Text + bufferLayer.InternalFeatures["BufferFeature"].GetWellKnownText();


 


 



}


I send "POLYGON((3869260.00994757 4910833.45833652,3866818.43713419 4815612.11861444,4018195.95156416 4803404.25454751,4008429.66031061 4905950.31270975,3869260.00994757 4910833.45833652))" as parameter


and I get this MULTIPOLYGON(((3869260.36060897 4910843.45218646,4008430.01097201 4905960.30655969,4008432.04605659 4905960.02395149,4008433.98042842 4905959.33138937,4008435.73242921 4905958.25810941,4008437.22809936 4905956.84941943,4008438.4043001 4905955.1647864,4008439.21137887 4905953.27532602,4008439.61526534 4905951.26080068,4008439.61526534 4905951.26080068,4018205.90651889 4803405.20263844,4018205.90252736 4803403.26544112,4018205.52510112 4803401.36536249,4018204.78840404 4803399.57370774,4018203.7200825 4803397.95771316,4018202.36022794 4803396.578023,4018200.75987229 4803395.48641356,4018198.97907289 4803394.72385022,4018197.08465867 4803394.31895005,4018195.14772226 4803394.28690796,3866817.63329229 4815602.15097489,3866815.77014653 4815602.48081506,3866814.00248176 4815603.15569874,3866812.39358229 4815604.15146436,3866811.00104842 4815605.43246245,3866809.87473436 4815606.95283191,3866809.05496334 4815608.6581419,3866808.57108405 4815610.4873405,3866808.44041988 4815612.37494045,3869250.01323326 4910833.71466253,3869250.25792822 4910835.6715059,3869250.8818901 4910837.54227664,3869251.86085232 4910839.25421839,3869253.15674195 4910840.74075183,3869254.71916039 4910841.94406401,3869256.48734345 4910842.81735678,3869258.39252452 4910843.32666681,3869260.36060897 4910843.45218646)))

 


but it does not draw anything.


1. What I missing ?


2. I want to make it with LINE not POLYGON so the user will draw the line and I will find the MULTIPOLYGON of the line


 



 


 


 


 



Ayhan, 


Thanks for your posts!
 
First problem: it seems you probably set wrong MapUnit and CurrentExtent
 
Try my following codes to see whether it works

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#B3C6D4"));
    //Map1.CurrentExtent = new RectangleShape(-140, 60, 140, -60);
    Map1.MapUnit = GeographyUnit.Meter;

    //ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(MapPath("~/App_Data/Countries02.shp"));
    //worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(255, 243, 239, 228), GeoColor.FromArgb(255, 218, 193, 163), 1);
    //worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

    InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();

    shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
    shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

    LayerOverlay shapeOverlay = new LayerOverlay("ShapeOverlay");
    shapeOverlay.Layers.Add("ShapeLayer", shapeLayer);
    Map1.CustomOverlays.Add(shapeOverlay);

    InMemoryFeatureLayer bufferLayer = new InMemoryFeatureLayer();
    bufferLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Water1;
    bufferLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

    LayerOverlay bufferOverlay = new LayerOverlay("BufferOverlay");
    bufferOverlay.Layers.Add("bufferLayer", bufferLayer);
    Map1.CustomOverlays.Add(bufferOverlay);
 }
}

private void BufferAFeature(string wellKnowntext)
{
  InMemoryFeatureLayer mshapeLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Layers["ShapeLayer"];

  Feature feature = new Feature(wellKnowntext);
  mshapeLayer.InternalFeatures.Add(feature.Id, feature);

  //Map1.EditOverlay.Features.Add(feature.Id, feature); //  if I remove this line it does not draw the first polygon

  //((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Redraw();

  InMemoryFeatureLayer bufferLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Layers["bufferLayer"];

  AreaBaseShape baseShape = (AreaBaseShape)feature.GetShape();
  MultipolygonShape bufferedShape = baseShape.Buffer(10, 8, BufferCapType.Round, GeographyUnit.Meter, DistanceUnit.Meter);
  Feature bufferFeature = new Feature(bufferedShape);

  bufferLayer.Open();
  bufferLayer.InternalFeatures.Clear();
  bufferLayer.InternalFeatures.Add("BufferFeature", bufferFeature);
  Map1.CurrentExtent = bufferLayer.GetBoundingBox();
  bufferLayer.Close();

  ((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Redraw();

           
  //txtFeature.Text = String.Empty;
  //txtFeature.Text = txtFeature.Text + bufferLayer.InternalFeatures["BufferFeature"].GetWellKnownText();
}

protected void Button1_Click(object sender, EventArgs e)
{
  string polygon = "POLYGON((3869260.00994757 4910833.45833652,3866818.43713419 4815612.11861444,4018195.95156416 4803404.25454751,4008429.66031061 4905950.31270975,3869260.00994757 4910833.45833652))";
  BufferAFeature(polygon);
}

Second Problem: I am not exactly sure what your attempt.



So basically what you want to is buffer a Line-type feature or a Pont-type feature?
If so, I think it is almost the same only difference lies in passing feature should be Line or Point instead of Polygon.
 
Any more questions just let me know.
 
Thanks.
 
Yale


the first answer that I need like the buffer.jpg



 


the second like linebuffer.jpg


 




Ayhan, 


Thanks for your post!
 
Following codes can implement the screenshots functionalities, just take a try and play with the styles if you do not like them.
 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#B3C6D4"));
                Map1.MapUnit = GeographyUnit.Meter;
             
                InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer();
                shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(150, 60, 180, 60);
                shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.GeographicColors.DeepOcean;
                shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = new LineStyle(new GeoPen(GeoColor.FromArgb(150, 60, 180, 60), 3f));
                shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                LayerOverlay shapeOverlay = new LayerOverlay("ShapeOverlay");
                shapeOverlay.Layers.Add("ShapeLayer", shapeLayer);
                Map1.CustomOverlays.Add(shapeOverlay);

                InMemoryFeatureLayer bufferLayer = new InMemoryFeatureLayer();
                bufferLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, 200, 255, 200);
                bufferLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.LightGreen;
                bufferLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                LayerOverlay bufferOverlay = new LayerOverlay("BufferOverlay");
                bufferOverlay.Layers.Add("bufferLayer", bufferLayer);
                bufferOverlay.IsBaseOverlay = false;
                Map1.CustomOverlays.Add(bufferOverlay);
            }
        }

        private void BufferAFeature(string wellKnowntext)
        {
            Feature feature = new Feature(wellKnowntext);
            
            InMemoryFeatureLayer mshapeLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Layers["ShapeLayer"];
            mshapeLayer.InternalFeatures.Clear();
            mshapeLayer.InternalFeatures.Add(feature.Id, feature);


            InMemoryFeatureLayer bufferLayer = (InMemoryFeatureLayer)((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Layers["bufferLayer"];
            BaseShape baseShape = feature.GetShape();
            MultipolygonShape bufferedShape = baseShape.Buffer(100000, 8, BufferCapType.Round, GeographyUnit.Meter, DistanceUnit.Meter);
            Feature bufferFeature = new Feature(bufferedShape);

            bufferLayer.InternalFeatures.Clear();
            bufferLayer.InternalFeatures.Add("BufferFeature", bufferFeature);
            
            bufferLayer.Open();
            Map1.CurrentExtent = bufferLayer.GetBoundingBox();
            bufferLayer.Close();

            ((LayerOverlay)Map1.CustomOverlays["ShapeOverlay"]).Redraw();
            ((LayerOverlay)Map1.CustomOverlays["BufferOverlay"]).Redraw();
        }

        protected void btnBufferPolygon_Click(object sender, EventArgs e)
        {
            string polygon = "POLYGON((3869260.00994757 4910833.45833652,3866818.43713419 4815612.11861444,4018195.95156416 4803404.25454751,4008429.66031061 4905950.31270975,3869260.00994757 4910833.45833652))";
            BufferAFeature(polygon);
        }

        protected void btnBufferLine_Click(object sender, EventArgs e)
        {
            string line = "LINESTRING(0 0, 2000000 1500000, 4000000 1500000, 6000000 2500000)";
            BufferAFeature(line);
        }

 

Any more questions just let me know.
 
Thanks.
 
Yale


Thanks Yale

Welcome, Ayhan! 
  
 Any more questions just let me know. 
  
 Thanks. 
  
 Yale