/// <summary>
    /// This code creates a point shapefile
    /// </summary>
    private void btnCreatePoints_Click(object sender, EventArgs e)
    {
        MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();

        // MWShapeId field will be added to attribute table
        bool result = sf.CreateNewWithShapeID("", MapWinGIS.ShpfileType.SHP_POINT);

        // bounding box for the new shapefile
        double xMin = 0.0;
        double yMin = 0.0;
        double xMax = 1000.0;
        double yMax = 1000.0;

        // the location of points will be random
        Random rnd = new Random(DateTime.Now.Millisecond);

        // creating points and inserting them in the shape
        for (int i = 0; i < 1000; i++)
        {
            MapWinGIS.Point pnt = new MapWinGIS.Point();
            pnt.x = xMin + (xMax - xMin) * rnd.NextDouble();
            pnt.y = yMin + (yMax - yMin) * rnd.NextDouble();

            MapWinGIS.Shape shp = new MapWinGIS.Shape();
            shp.Create(MapWinGIS.ShpfileType.SHP_POINT);

            int index = 0;
            shp.InsertPoint(pnt, ref index);
            sf.EditInsertShape(shp, ref i);
        }

        // adds shapefile to the map
        axMap1.AddLayer(sf, true);

        // saving the shapefile to the disk
        if (!sf.SaveAs(@"c:\points.shp", null))         // null is for callback, we don't use it
        {
            // most likely the file with the same name already exists
            MessageBox.Show(sf.get_ErrorMsg(sf.LastErrorCode));
        }
    }

    /// <summary>
    /// This code creates a polygon shapefile
    /// </summary>
    private void btnCreatePolygons_Click(object sender, EventArgs e)
    {
        MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
        bool result = sf.CreateNewWithShapeID("", MapWinGIS.ShpfileType.SHP_POLYGON);

        if (!result)
        {
            MessageBox.Show(sf.get_ErrorMsg(sf.LastErrorCode));
        }
        else
        {
            double xMin = 0.0;
            double yMin = 0.0;
            double xMax = 1000.0;
            double yMax = 1000.0;
            Random rnd = new Random(DateTime.Now.Millisecond);

            // In a loop we are creating 100 different points using the box established above.
            for (int i = 0; i < 10; i++)
            {
                double xCenter = xMin + (xMax - xMin) * rnd.NextDouble();
                double yCenter = yMin + (yMax - yMin) * rnd.NextDouble();

                // random radius from 10 to 100
                double radius = 10 + rnd.NextDouble() * 90;
                
                // Filled regions are defined by ArcGIS as clockwise
                MapWinGIS.Shape shp = new MapWinGIS.Shape();
                shp.Create(MapWinGIS.ShpfileType.SHP_POLYGON);

                for (int j = 0; j < 36; j++)
                {
                    MapWinGIS.Point pnt = new MapWinGIS.Point();
                    pnt.x = xCenter + radius * Math.Cos(j * Math.PI / 18);
                    pnt.y = yCenter - radius * Math.Sin(j * Math.PI / 18);
                    shp.InsertPoint(pnt, ref j);
                }
                sf.EditInsertShape(shp, ref i);
            }
            
            axMap1.AddLayer(sf, true);
            axMap1.ZoomToLayer(0);

            if (!sf.SaveAs(@"c:\polygons.shp", null))
            {
                MessageBox.Show(sf.get_ErrorMsg(sf.LastErrorCode));
            }
        }
    }

 

Last edited Aug 13, 2011 at 4:24 PM by sleschinski, version 2

Comments

No comments yet.