/// <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); }
/// <summary> /// 'Copyright (C) 2008 Enrico Antonio Chiaradia, UNIMI ///'This program is free software; you can redistribute it and/or ///'modify it under the terms of the GNU General Public License ///'version 2, 1991 as published by the Free Software Foundation. ///'This program is distributed in the hope that it will be useful, ///'but WITHOUT ANY WARRANTY; without even the implied warranty of ///'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ///'GNU General Public License for more details. ///'A copy of the full GNU General Public License is available at: ///'http://www.gnu.org/copyleft/gpl.html ///'or from: ///'The Free Software Foundation, Inc., 59 Temple Place - Suite 330, ///'Boston, MA 02111-1307, USA. ///'If you wish to use or incorporate this program (or parts of it) into ///'other software that does not meet the GNU General Public License ///'conditions contact the author to request permission. ///'If you have any question or suggestion, please contact the author. ///'Enrico A. Chiaradia ///'University of Milan ///'Ist. di Idraulica Agraria ///'via Celoria 2 ///'20133, Milan, Italy ///'email: [email protected] /// </summary> /// <param name="sfPath">the name of the shapefile</param> /// <param name="resultGridPath">the name of the new grid</param> /// <param name="gridFileType">file type of the new grid</param> /// <param name="gridDataType">data format of the new grid</param> /// <param name="sfValueField">the name of the field that contains data</param> /// <param name="resultGridHeader">contains information about dimension of the new grid</param> /// <param name="callback">(optional) reports progress and error messages</param> /// <returns></returns> public bool ShapefileToGrid2(string SfNm, string GrdName, MapWinGIS.GridFileType GrdFileType, MapWinGIS.GridDataType GrdDataType, string Fldname, MapWinGIS.GridHeader GrdHd, MapWinGIS.ICallback cback) { int i; bool flg; string projStr; //open the shapefile MapWinGIS.Shapefile MySf = new MapWinGIS.Shapefile(); flg = MySf.Open(SfNm, cback); if (flg == false) { reportError("ERROR in opening shapefile: " + SfNm, cback); MySf.Close(); return false; } //get the handle for the field MapWinGIS.Field field; int FldId = -1; int LayFldNum = MySf.NumFields; i = 0; for (i = 0; i < LayFldNum; ++i) { field = MySf.get_Field(i); if (field.Name.ToLower() == Fldname.ToLower()) { FldId = i; break; } } if (FldId < 0) { reportError("The shapefile " + SfNm + " doesn't have a field " + Fldname, cback); MySf.Close(); return false; } //copy shapefile projection projStr = MySf.Projection; if (!MapWinUtility.Strings.IsEmpty(projStr)) { GrdHd.Projection = projStr; } //create a new grid and a new gridheader MapWinGIS.Grid NewGrd = new MapWinGIS.Grid(); flg = NewGrd.CreateNew(GrdName, GrdHd, GrdDataType, GrdHd.NodataValue, false, GrdFileType, cback); if (flg == false) { reportError("ERROR in grid initialization: " + GrdName, cback); NewGrd.Close(); MySf.Close(); } //verify the type of shapefile and call rasterization function MapWinGIS.ShpfileType SfType = new MapWinGIS.ShpfileType(); SfType = MySf.ShapefileType; switch (SfType) { case ShpfileType.SHP_POLYGON: case ShpfileType.SHP_POLYGONM: case ShpfileType.SHP_POLYGONZ: flg = Poly2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_POLYLINE: case ShpfileType.SHP_POLYLINEM: case ShpfileType.SHP_POLYLINEZ: flg = Line2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_POINT: case ShpfileType.SHP_POINTM: case ShpfileType.SHP_POINTZ: flg = Point2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_MULTIPOINT: case ShpfileType.SHP_MULTIPOINTM: case ShpfileType.SHP_MULTIPOINTZ: flg = Multipoint2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; default: reportError("The shapefile type " + SfType.ToString() + "is not supported.", cback); NewGrd.Close(); MySf.Close(); flg = false; break; } //save and close the grid, close shapefile NewGrd.Save(GrdName, GrdFileType, cback); NewGrd.Close(); MySf.Close(); return flg; }
/// <summary> /// 'Copyright (C) 2008 Enrico Antonio Chiaradia, UNIMI ///'This program is free software; you can redistribute it and/or ///'modify it under the terms of the GNU General Public License ///'version 2, 1991 as published by the Free Software Foundation. ///'This program is distributed in the hope that it will be useful, ///'but WITHOUT ANY WARRANTY; without even the implied warranty of ///'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ///'GNU General Public License for more details. ///'A copy of the full GNU General Public License is available at: ///'http://www.gnu.org/copyleft/gpl.html ///'or from: ///'The Free Software Foundation, Inc., 59 Temple Place - Suite 330, ///'Boston, MA 02111-1307, USA. ///'If you wish to use or incorporate this program (or parts of it) into ///'other software that does not meet the GNU General Public License ///'conditions contact the author to request permission. ///'If you have any question or suggestion, please contact the author. ///'Enrico A. Chiaradia ///'University of Milan ///'Ist. di Idraulica Agraria ///'via Celoria 2 ///'20133, Milan, Italy ///'email: [email protected] /// </summary> /// <param name="sfPath">the name of the shapefile</param> /// <param name="resultGridPath">the name of the new grid</param> /// <param name="gridFileType">file type of the new grid</param> /// <param name="gridDataType">data format of the new grid</param> /// <param name="sfValueField">the name of the field that contains data</param> /// <param name="resultGridHeader">contains information about dimension of the new grid</param> /// <param name="callback">(optional) reports progress and error messages</param> /// <returns></returns> public bool ShapefileToGrid2(string SfNm, string GrdName, MapWinGIS.GridFileType GrdFileType, MapWinGIS.GridDataType GrdDataType, string Fldname, MapWinGIS.GridHeader GrdHd, MapWinGIS.ICallback cback) { int i; bool flg; string projStr; //open the shapefile MapWinGIS.Shapefile MySf = new MapWinGIS.Shapefile(); flg = MySf.Open(SfNm, cback); if (flg == false) { reportError("ERROR in opening shapefile: " + SfNm, cback); MySf.Close(); return(false); } //get the handle for the field MapWinGIS.Field field; int FldId = -1; int LayFldNum = MySf.NumFields; i = 0; for (i = 0; i < LayFldNum; ++i) { field = MySf.get_Field(i); if (field.Name.ToLower() == Fldname.ToLower()) { FldId = i; break; } } if (FldId < 0) { reportError("The shapefile " + SfNm + " doesn't have a field " + Fldname, cback); MySf.Close(); return(false); } //copy shapefile projection projStr = MySf.Projection; if (!MapWinUtility.Strings.IsEmpty(projStr)) { GrdHd.Projection = projStr; } //create a new grid and a new gridheader MapWinGIS.Grid NewGrd = new MapWinGIS.Grid(); flg = NewGrd.CreateNew(GrdName, GrdHd, GrdDataType, GrdHd.NodataValue, false, GrdFileType, cback); if (flg == false) { reportError("ERROR in grid initialization: " + GrdName, cback); NewGrd.Close(); MySf.Close(); } //verify the type of shapefile and call rasterization function MapWinGIS.ShpfileType SfType = new MapWinGIS.ShpfileType(); SfType = MySf.ShapefileType; switch (SfType) { case ShpfileType.SHP_POLYGON: case ShpfileType.SHP_POLYGONM: case ShpfileType.SHP_POLYGONZ: flg = Poly2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_POLYLINE: case ShpfileType.SHP_POLYLINEM: case ShpfileType.SHP_POLYLINEZ: flg = Line2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_POINT: case ShpfileType.SHP_POINTM: case ShpfileType.SHP_POINTZ: flg = Point2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; case ShpfileType.SHP_MULTIPOINT: case ShpfileType.SHP_MULTIPOINTM: case ShpfileType.SHP_MULTIPOINTZ: flg = Multipoint2Grid(MySf, FldId, NewGrd, GrdHd, GrdDataType, cback); break; default: reportError("The shapefile type " + SfType.ToString() + "is not supported.", cback); NewGrd.Close(); MySf.Close(); flg = false; break; } //save and close the grid, close shapefile NewGrd.Save(GrdName, GrdFileType, cback); NewGrd.Close(); MySf.Close(); return(flg); }
/// <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); }