/// <summary> /// Removes portions of the input line shapefile that fall within the polygons of the erase polygon shapefile. /// </summary> /// <param name="inputSF">The line shapefile to erase.</param> /// <param name="eraseSF">The polygon shapefile that will be used to erase portions of the line shapefile.</param> /// <param name="resultSF">The result shapefile.</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool EraseLineSFWithPolySF(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shapefile eraseSF, ref MapWinGIS.Shapefile resultSF) { MapWinUtility.Logger.Dbg("EraseLineSFWithPolySF(inputSF: " + Macro.ParamName(inputSF) + "\n" + " eraseSF: " + Macro.ParamName(eraseSF) + "\n" + " resultSF: " + Macro.ParamName(resultSF) + ")"); if (inputSF == null || eraseSF == null || resultSF == null) { gErrorMsg = "One of the input parameters is null."; Error.SetErrorMsg(gErrorMsg); Debug.WriteLine(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } bool status = true; MapWinGIS.Shapefile tempInput = new MapWinGIS.ShapefileClass(); string tempFile = System.IO.Path.GetTempPath() + "tempLineResult.shp"; //CDM 8/4/2006 tempInput.CreateNew(tempFile, inputSF.ShapefileType); Globals.PrepareResultSF(ref tempFile, ref tempInput, inputSF.ShapefileType); tempInput.StartEditingShapes(true, null); int shpIndex = 0; int numInputs = inputSF.NumShapes; for (int i = 0; i <= numInputs - 1; i++) { tempInput.EditInsertShape(inputSF.get_Shape(i), ref shpIndex); shpIndex++; } int numErase = eraseSF.NumShapes; for (int i = 0; i <= numErase - 1; i++) { Debug.WriteLine("Num shapes in tempInput = " + tempInput.NumShapes); MapWinGIS.Shape eraseShp = new MapWinGIS.ShapeClass(); eraseShp = eraseSF.get_Shape(i); resultSF.EditClear(); status = EraseLineSFWithPoly(ref tempInput, ref eraseShp, ref resultSF, false); if (i != numErase - 1) { int numResults = resultSF.NumShapes; if (numResults > 0) { tempInput.EditClear(); shpIndex = 0; for (int j = 0; j <= numResults - 1; j++) { tempInput.EditInsertShape(resultSF.get_Shape(j), ref shpIndex); shpIndex++; } } } } MapWinUtility.Logger.Dbg("Finished EraseLineSFWithPolySF"); return(status); }
/// <summary> /// Converts a list of 3d-points to a point shapefile with z-value field. /// This function creates a new shapefile. The shapefile has two fields: /// a 'MWShapeId' field and a field which contains the z-value. /// </summary> /// <param name="ShpFileName">Name of the resulting point shapefile</param> /// <param name="ZFieldName">Name of the z-field in the shapefile</param> public void ToShapefile(string ShpFileName, string ZFieldName) { MapWinGIS.Shapefile newSF = new MapWinGIS.Shapefile(); try { Hashtable FieldIndices = new Hashtable(); MapWinGIS.ShpfileType sftype; sftype = MapWinGIS.ShpfileType.SHP_POINT; int fldIdx = 0; // if shapefile exists - open it and clear all shapes if (System.IO.File.Exists(ShpFileName)) { newSF.Open(ShpFileName, null); newSF.StartEditingShapes(true, null); newSF.EditClear(); } else //else, create a new shapefile { if (!newSF.CreateNew(ShpFileName, sftype)) { throw new InvalidOperationException ("Error creating shapefile " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } newSF.StartEditingShapes(true, null); } //check existing fields: for (int i = 0; i < newSF.NumFields; ++i) { MapWinGIS.Field fl = newSF.get_Field(i); if (fl.Name == "MWShapeID") { FieldIndices.Add("MWShapeID", i); } if (fl.Name == ZFieldName) { FieldIndices.Add(ZFieldName, i); } } //Add the fields: if (!FieldIndices.ContainsKey("MWShapeID")) { //First an ID field MapWinGIS.Field idFld = new MapWinGIS.Field(); idFld.Name = "MWShapeID"; idFld.Type = MapWinGIS.FieldType.INTEGER_FIELD; fldIdx = newSF.NumFields; if (newSF.EditInsertField(idFld, ref fldIdx, null) == false) { throw new InvalidOperationException("error inserting field " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } FieldIndices.Add("MWShapeID", fldIdx); } if (!FieldIndices.ContainsKey(ZFieldName)) { //Second add a Z-field MapWinGIS.Field zFld = new MapWinGIS.Field(); zFld.Name = "Z"; zFld.Type = MapWinGIS.FieldType.DOUBLE_FIELD; fldIdx = newSF.NumFields; if (newSF.EditInsertField(zFld, ref fldIdx, null) == false) { throw new InvalidOperationException("error inserting field " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } FieldIndices.Add("Z", fldIdx); } foreach (ICoordinate pt in _points) { //first, add a point shape (geometry) MapWinGIS.Shape newShp = new MapWinGIS.Shape(); newShp.Create(MapWinGIS.ShpfileType.SHP_POINT); MapWinGIS.Point newPt = new MapWinGIS.Point(); newPt.x = pt.X; newPt.y = pt.Y; int ptIdx = 0; newShp.InsertPoint(newPt, ref ptIdx); int shpIdx = newSF.NumShapes; newSF.EditInsertShape(newShp, ref shpIdx); //second add the z-value newSF.EditCellValue(fldIdx, shpIdx, pt.Z); } } finally { //finally stop editing and close the shapefile newSF.StopEditingShapes(true, true, null); if (newSF.Close() == false) { throw new InvalidOperationException("error closing shapefile " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } } }