示例#1
0
        /// <summary>
        /// Create shapefile named shapeName  Any existing shapefile of the same name is deleted.
        /// </summary>
        /// <param name="shapeName">Path of output shapefile</param>
        /// <param name="cb">Callback for reporting errors</param>
        /// <returns>null if any error, else shapefile</returns>
        public MapWinGIS.Shapefile makeShapefile(string shapeName, MapWinGIS.ICallback cb)
        {
            MapWinGeoProc.DataManagement.DeleteShapefile(ref shapeName);
            MapWinGIS.Shapefile shpfile = new MapWinGIS.Shapefile();
            if (!shpfile.CreateNew(shapeName, ShpfileType.SHP_POLYGON))
            {
                if (cb != null)
                {
                    cb.Error("ManhattanShapes.makeShapefile", clsUtils.ManhattanShapes.nocreateshapefile + shapeName);
                }
                return(null);
            }
            if (!shpfile.StartEditingShapes(true, null))
            {
                if (cb != null)
                {
                    cb.Error("ManhattanShapes.makeShapefile", clsUtils.ManhattanShapes.noeditshapefile + shapeName);
                }
                return(null);
            }
            int shapeindex = 0;

            foreach (int k in ShapesTable.Keys)
            {
                if (!InsertShape(shpfile, k, ref shapeindex))
                {
                    if (cb != null)
                    {
                        cb.Error("ManhattanShapes.makeShapefile", clsUtils.ManhattanShapes.noaddshape + shapeName);
                    }
                    shpfile.StopEditingShapes(true, true, null);
                    shpfile.Close();
                    return(null);
                }
            }
            if (!shpfile.StopEditingShapes(true, true, null))
            {
                if (cb != null)
                {
                    cb.Error("ManhattanShapes.makeShapefile", clsUtils.ManhattanShapes.noeditshapefile + shapeName);
                }
                return(null);
            }
            if (!shpfile.Close())
            {
                if (cb != null)
                {
                    cb.Error("ManhattanShapes.makeShapefile", clsUtils.ManhattanShapes.nocloseshapefile + shapeName);
                }
                return(null);
            }
            return(shpfile);
        }
示例#2
0
        /// <summary>
        /// Converts grid to a shapefile, removing any existing shapefile.
        /// Assumed to be an integer grid.  Adds attribute headed id with the grid values, and
        /// attribute headed "Area" with the area for each polygon.
        /// </summary>
        /// <param name="gridName">Path of input grid</param>
        /// <param name="shapeName">Path of output shapefile</param>
        /// <param name="id">String to use for name of grid values attribute</param>
        /// <param name="cb">Callback for reporting errors</param>
        /// <returns>null if any errors, else shapefile</returns>
        public static MapWinGIS.Shapefile GridToShapeManhattan(string gridName, string shapeName,
                                                               string id, MapWinGIS.ICallback cb)
        {
            if (!System.IO.File.Exists(gridName))
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nogrid + gridName);
                }
                return(null);
            }
            MapWinGIS.Grid grid = new MapWinGIS.GridClass();
            if (!grid.Open(gridName, GridDataType.UnknownDataType, true, GridFileType.UseExtension, null))
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noopengrid + gridName);
                }
                return(null);
            }
            MapWinGIS.GridHeader header = grid.Header;
            int numRows = header.NumberRows;
            int numCols = header.NumberCols;

            MapWinGeoProc.DataManagement.DeleteShapefile(ref shapeName);
            string oldProj = System.IO.Path.ChangeExtension(gridName, ".prj");
            string newProj = System.IO.Path.ChangeExtension(shapeName, ".prj");

            if (System.IO.File.Exists(oldProj) && !oldProj.Equals(newProj))
            {
                System.IO.File.Copy(oldProj, newProj);
            }
            MapWinGIS.Shapefile shapeFile = new MapWinGIS.Shapefile();
            bool success = shapeFile.CreateNew(shapeName, ShpfileType.SHP_POLYGON);

            if (!success)
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nocreateshapefile + shapeName);
                }
                return(null);
            }
            success = shapeFile.StartEditingShapes(true, null);
            if (!success)
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditshapefile + shapeName);
                }
                return(null);
            }
            ManhattanShapes shapes = new ManhattanShapes(header);

            for (int i = 0; i < numRows; i++)
            {
                shapes.addGridRow(grid, i, numCols);
            }
            if (!shapes.lastError.Equals(""))
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", shapes.lastError);
                }
                return(null);
            }
            grid.Close();
            shapes.FinishShapes();
            MapWinGIS.Field idField = new MapWinGIS.Field();
            idField.Name = id;
            idField.Type = FieldType.INTEGER_FIELD;
            int idIndex = 0;

            success = shapeFile.EditInsertField(idField, ref idIndex, null);
            if (!success)
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddfield + shapeName);
                }
                shapeFile.Close();
                return(null);
            }
            MapWinGIS.Field areaField = new MapWinGIS.Field();
            areaField.Name = "Area";
            areaField.Type = FieldType.DOUBLE_FIELD;
            int areaIndex = 1;

            success = shapeFile.EditInsertField(areaField, ref areaIndex, null);
            if (!success)
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddfield + shapeName);
                }
                shapeFile.Close();
                return(null);
            }
            int shapeIndex = 0;

            foreach (int k in shapes.ShapesTable.Keys)
            {
                success = shapes.InsertShape(shapeFile, k, ref shapeIndex);
                if (!success)
                {
                    if (cb != null)
                    {
                        cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddshape + shapeName);
                    }
                    shapeFile.Close();
                    return(null);
                }
                success = shapeFile.EditCellValue(idIndex, shapeIndex, k);
                if (!success)
                {
                    if (cb != null)
                    {
                        cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditcell + shapeName);
                    }
                    shapeFile.Close();
                    return(null);
                }
                double area = shapes.Area(k);
                success = shapeFile.EditCellValue(areaIndex, shapeIndex, area);
                if (!success)
                {
                    if (cb != null)
                    {
                        cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditcell + shapeName);
                    }
                    shapeFile.Close();
                    return(null);
                }
            }
            success = shapeFile.StopEditingShapes(true, true, null);
            if (!success)
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditshapefile + shapeName);
                }
                shapeFile.Close();
                return(null);
            }
            if (!shapeFile.Close())
            {
                if (cb != null)
                {
                    cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nocloseshapefile + shapeName);
                }
                return(null);
            }
            return(shapeFile);
        }