Common Custom renderers
Common_CustomRenderers_CSharp\App_Code\GenerateGraphicsHelper.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

namespace ESRI.ADF.Samples.Renderers
{
  /// <summary>
  /// This class is used for generating point, line and polygon layers with a set of random features
  /// </summary>
  public class GenerateGraphicsHelper
  {
        // Random number generator
    private static System.Random randomizer = new System.Random((int)System.DateTime.Now.Ticks);

    /// <summary>
    /// Creates the specified number of random polygons within the given extent
    /// </summary>
    /// <param name="layerName">Name of layer</param>
    /// <param name="adfEnvelope">Extent to create features within</param>
    /// <param name="count">Number of features to create</param>
    /// <returns>FeatureGraphicsLayer</returns>
    public static ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer CreatePolygonFeatures(
            string layerName, ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope, int count)
    {
      if (adfEnvelope == null) return null; // Cannot create features within a null extent
    
            // Create a feature graphics layer and give it columns for height, width, and color
      ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = 
                new ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer(ESRI.ArcGIS.ADF.Web.FeatureType.Polygon);
      featureGraphicsLayer.TableName = layerName;
      featureGraphicsLayer.Columns.Add("Height", typeof(System.Double));
      featureGraphicsLayer.Columns.Add("Width", typeof(int));
      featureGraphicsLayer.Columns.Add("Color", typeof(System.Drawing.Color));

            // Calculate the maximum width/height of a polygon.  Specify that it not exceed 1/10th of the
            // width of the passed-in extent
      double maxSize = adfEnvelope.Width / 10;

            // Create the number of polygons specified by the passed in argument.  Add each to the feature graphics
            // layer.  The polygons created will be rectangles.
      for (int i = 0; i < count; i++)
      {
                // Calculate an origin for the rectangle within the passed-in extent
        double xmin = adfEnvelope.XMin + randomizer.NextDouble() * adfEnvelope.Width;
        double ymin = adfEnvelope.YMin + randomizer.NextDouble() * adfEnvelope.Height;

                // Calculate rotation factors
        double rotation = randomizer.NextDouble() * System.Math.PI;
        double cosRot = System.Math.Cos(rotation);
        double sinRot = System.Math.Sin(rotation);

                // Calculate a width and height less than the maximum size
        double width = randomizer.NextDouble() * maxSize;
        double height = randomizer.NextDouble() * maxSize;

                // Create the rectangle
        ESRI.ArcGIS.ADF.Web.Geometry.Ring adfRing = 
                    new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
        adfRing.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(xmin, ymin));
                adfRing.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(xmin + cosRot * width, ymin + sinRot * width));
                adfRing.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(xmin + cosRot * width + sinRot * height, 
                    ymin + sinRot * width - cosRot * height));
                adfRing.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(xmin + sinRot * height, ymin - cosRot * height));
                adfRing.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(xmin, ymin));
                ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
        adfPolygon.Rings.Add(adfRing);

                // Add the rectangle to the graphics layer
        System.Data.DataRow row = featureGraphicsLayer.Add(adfPolygon);

                // Populate the color, height, and width fields of the graphic feature
        row["Color"] = getRandomColor(); // Random color
        row["Height"] = randomizer.NextDouble(); // Height between 0 and 1
        row["Width"] = System.Convert.ToInt32(randomizer.NextDouble() * 5 + 1); // Width between 1 and 6
      }
      return featureGraphicsLayer;
    }

    /// <summary>
    /// Creates the specified number of random polylines within the given extent
    /// </summary>
    /// <param name="layerName">Name of layer</param>
    /// <param name="adfEnvelope">Extent to create features within</param>
    /// <param name="count">Number of features to create</param>
    /// <returns>FeatureGraphicsLayer</returns>
    public static ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer 
            CreatePolylineFeatures(string layerName, ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope, int count)
    {
      if (adfEnvelope == null) return null; // Cannot create features within a null extent

            // Create a feature graphics layer and give it columns for height, width, and color
            ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = 
                new ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer(ESRI.ArcGIS.ADF.Web.FeatureType.Line);
      featureGraphicsLayer.TableName = layerName;
      featureGraphicsLayer.Columns.Add("Height", typeof(System.Double));
      featureGraphicsLayer.Columns.Add("Width", typeof(int));
      featureGraphicsLayer.Columns.Add("Color", typeof(System.Drawing.Color));

      double maxSize = adfEnvelope.Width / 10; // Maximum width/height of a polygon

            // Create the specified number of polylines, adding each to the graphics layer
      for (int i = 0; i < count; i++)
      {
                // Calculate the origin of the line as a random point within the specified extent
        double x = adfEnvelope.XMin + randomizer.NextDouble() * adfEnvelope.Width;
        double y = adfEnvelope.YMin + randomizer.NextDouble() * adfEnvelope.Height;

                // Create a polyline with 3 - 10 randomly placed vertices
        ESRI.ArcGIS.ADF.Web.Geometry.Path adfPath = new ESRI.ArcGIS.ADF.Web.Geometry.Path();
        adfPath.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(x, y));
                // In the loop control statement, we add three to the random number to ensure the
                // polyline has at least three points
        for (int j = 0; j < randomizer.Next(7) + 3; j++)
        {
          x += randomizer.NextDouble() * maxSize / 5;
          y += randomizer.NextDouble() * maxSize / 5 - maxSize / 10;
          adfPath.Points.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(x, y));
        }
        ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline = new ESRI.ArcGIS.ADF.Web.Geometry.Polyline();
        adfPolyline.Paths.Add(adfPath);

                // Add the polyline to the graphics layer
        System.Data.DataRow row = featureGraphicsLayer.Add(adfPolyline);

                // Populate the color, height, and width fields of the graphic feature
                row["Color"] = getRandomColor(); // Random color
        row["Height"] = randomizer.NextDouble(); // Height between 0 and 1
        row["Width"] = System.Convert.ToInt32(randomizer.NextDouble() * 5 + 1); // Width between 1 and 6
      }
      return featureGraphicsLayer;
    }

    /// <summary>
    /// Creates a number of random points within the given extent
    /// </summary>
    /// <param name="layerName">Name of layer</param>
    /// <param name="env">Extent to create features within</param>
    /// <param name="count">Number of features to create</param>
    /// <returns>FeatureGraphicsLayer</returns>
    public static ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer CreatePointFeatures(
            string layerName, ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope, int count)
    {
      if (adfEnvelope == null) return null; // Cannot create features within a null extent

            // Create a graphics layer for the points
            ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = 
                new ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer(ESRI.ArcGIS.ADF.Web.FeatureType.Point);
      featureGraphicsLayer.TableName = layerName;
      featureGraphicsLayer.Columns.Add("ImagePath", typeof(string));
      featureGraphicsLayer.Columns.Add("RandomName", typeof(string));

            // Create the specified number of points and add them to the graphics layer
      for (int i = 0; i < count; i++)
      {
                // Create a point with a random geometry within the specified extent and add it to the
                // graphics layer
        double x = adfEnvelope.XMin + randomizer.NextDouble() * adfEnvelope.Width;
        double y = adfEnvelope.YMin + randomizer.NextDouble() * adfEnvelope.Height;
        System.Data.DataRow row = featureGraphicsLayer.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(x,y));

                // Populate the ImagePath field with a path that points to 1.gif, 2.gif, or 3.gif.  
        row["ImagePath"] = System.String.Format("~/images/{0}.gif", randomizer.Next(3) + 1);
                // Populate the name field with an arbitrary name
        row["RandomName"] = RandomNames[randomizer.Next(RandomNames.Length)]; //Random name
      }
      return featureGraphicsLayer;
    }

        // Array of names to use in assigning a random name
    private static string[] RandomNames = { "Antilocapra americana", 
                        "Euarctos americanus",
                        "Cervus canadensis",
                        "Felis concolor",
                        "Pelecanus erythrorhynchos" };

    /// <summary>
    /// Creates a random color
    /// </summary>
    /// <returns>System.Drawing.Color</returns>
    private static System.Drawing.Color getRandomColor()
    {
      byte[] rgb = new byte[3];
      randomizer.NextBytes(rgb);
      return System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]);
    }
  }
}