private static void buildMergeDownstreamUpStream(IFeatureSet newshed, int IDFieldNum, int LinksFieldNum, int DSFieldNum, int USFieldNum1, int USFieldNum2) { for (int i = 0; i <= newshed.NumRows() - 1; i++) { string currDSField = newshed.get_CellValue(DSFieldNum, i).ToString(); if (currDSField != "-1") { for (int j = 0; j <= newshed.NumRows() - 1; j++) { string links = newshed.get_CellValue(LinksFieldNum, j).ToString(); string[] split = links.Split(','); for (int k = 0; k <= split.Length - 1; k++) { if (split[k].Trim() == currDSField) { newshed.EditCellValue(DSFieldNum, i, newshed.get_CellValue(IDFieldNum, j)); string upstream1 = newshed.get_CellValue(USFieldNum1, j).ToString(); if (upstream1 == "-1") { newshed.EditCellValue(USFieldNum1, j, newshed.get_CellValue(IDFieldNum, i)); } else { newshed.EditCellValue(USFieldNum2, j, newshed.get_CellValue(IDFieldNum, i)); } break; } } } } } }
//private static int GetWshedFromStreamLink(int streamLink, IFeatureSet streamShape, IFeatureSet shedShape) //{ // int streamindx; // const int LinkIDField = 0; // const int WaterShedIDField = 13; // for (streamindx = 0; streamindx <= streamShape.NumRows() - 1; streamindx++) // { // if (int.Parse(streamShape.get_CellValue(LinkIDField, streamindx).ToString()) == streamLink) // { // return int.Parse(streamShape.get_CellValue(WaterShedIDField, streamindx).ToString()); // } // } // return -1; //} // private static bool ShapefileToArrays(string shpfileName, double[] X, double[] Y, int PntCount) // { // Shapefile shpfile = new Shapefile(); // int numShapes; // int NumPoints; // System.Collections.ArrayList xPnts = new System.Collections.ArrayList(); // System.Collections.ArrayList yPnts = new System.Collections.ArrayList(); // MapWinGIS.ShpfileType sft; // bool result = false; // if (shpfile.Open(shpfileName, null) == false) // { // return false; // } // sft = shpfile.ShapefileType; // if (sft != MapWinGIS.ShpfileType.SHP_MULTIPOINT & sft != MapWinGIS.ShpfileType.SHP_POINT & sft != MapWinGIS.ShpfileType.SHP_POINTZ & sft != MapWinGIS.ShpfileType.SHP_POINTZ & sft != MapWinGIS.ShpfileType.SHP_POINTM) // { // MessageBox.Show("Error: Invalid shapefile type selected. Must be a point shapefile", "Argument Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.DialogResult.OK); // return false; // } // numShapes = shpfile.NumRows(); // IFeature shp; // MapWinGIS.Point pnt; // for (int curShp = 0; curShp <= numShapes - 1; curShp++) // { // shp = shpfile.get_Shape(curShp); // NumPoints = shp.numPoints; // for (int curPnt = 0; curPnt <= NumPoints - 1; curPnt++) // { // pnt = shp.get_Point(curPnt); // xPnts.Add(pnt.x); // yPnts.Add(pnt.y); // } // } // NumPoints = xPnts.Count; // PntCount = NumPoints; // if (NumPoints <= 0) // { // return false; // } // //This is what it used to do. I have NO clue why. ARA // //X = new double[NumPoints-1]; // //Y = new double[NumPoints-1]; // //for (int curPnt = 1; curPnt < NumPoints; curPnt++) // //{ // // X[curPnt - 1] = System.Convert.ToDouble(xPnts[curPnt]); // // Y[curPnt - 1] = System.Convert.ToDouble(yPnts[curPnt]); // //} // X = new double[NumPoints]; // Y = new double[NumPoints]; // for (int curPnt = 0; curPnt < NumPoints; curPnt++) // { // X[curPnt] = System.Convert.ToDouble(xPnts[curPnt]); // Y[curPnt] = System.Convert.ToDouble(yPnts[curPnt]); // } // result = true; // return result; // } // private static bool ShapefileToArraysWithIDs(string shpfileName, double[] X, double[] Y, int PntCount, int[] idNodes) // { // Shapefile shpfile = new Shapefile(); // int numShapes; // int NumPoints; // int numFields; // System.Collections.ArrayList xPnts = new System.Collections.ArrayList(); // System.Collections.ArrayList yPnts = new System.Collections.ArrayList(); // System.Collections.ArrayList idNodesLocal = new System.Collections.ArrayList(); // MapWinGIS.ShpfileType sft; // if (shpfile.Open(shpfileName, null) == false) // { // return false; // } // sft = shpfile.ShapefileType; // if (sft != MapWinGIS.ShpfileType.SHP_MULTIPOINT & sft != MapWinGIS.ShpfileType.SHP_POINT & sft != MapWinGIS.ShpfileType.SHP_POINTZ & sft != MapWinGIS.ShpfileType.SHP_POINTZ & sft != MapWinGIS.ShpfileType.SHP_POINTM) // { // MessageBox.Show("Error: Invalid shapefile type selected. Must be a point shapefile", "Argument Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.DialogResult.OK); // return false; // } // numShapes = shpfile.NumRows(); // IFeature shp; // MapWinGIS.Point pnt; // MapWinGIS.Field Field; // string FieldName; // int theFieldIndex; // numFields = shpfile.NumFields; // theFieldIndex = -1; // for (int theField = 0; theField <= numFields - 1; theField++) // { // Field = shpfile.get_Field(theField); // FieldName = Field.Name; // if (FieldName.ToLower() == "id" | FieldName.ToLower() == "mwshapeid") // { // theFieldIndex = theField; // break; // } // } // if (theFieldIndex < 0) // { // MapWinGIS.Field f; // f = new MapWinGIS.Field(); // f.Type = MapWinGIS.FieldType.INTEGER_FIELD; // f.Name = "MWShapeID"; // shpfile.StartEditingTable(null); // int tmpNumFields = shpfile.NumFields; // shpfile.EditInsertField(f, tmpNumFields, null); // int IDIndex; // IDIndex = shpfile.NumFields - 1; // for (int i = 0; i <= shpfile.NumRows() - 1; i++) // { // shpfile.EditCellValue(IDIndex, i, i + 1); // } // shpfile.StopEditingTable(true, null); // numFields = shpfile.NumFields; // theFieldIndex = -1; // for (int theField = 0; theField <= numFields - 1; theField++) // { // Field = shpfile.get_Field(theField); // FieldName = Field.Name; // if (FieldName.ToLower() == "id" || FieldName.ToLower() == "mwshapeid") // { // theFieldIndex = theField; // break; // } // } // } // for (int curShp = 0; curShp <= numShapes - 1; curShp++) // { // NumPoints = shpfile.get_Shape(curShp).numPoints; // shp = shpfile.get_Shape(curShp); // for (int curPnt = 0; curPnt <= NumPoints - 1; curPnt++) // { // pnt = shp.get_Point(curPnt); // xPnts.Add(pnt.x); // yPnts.Add(pnt.y); // } // idNodesLocal.Add(shpfile.get_CellValue(theFieldIndex, curShp)); // } // NumPoints = xPnts.Count; // PntCount = NumPoints; // if (NumPoints <= 0) // { // return false; // } // X = new double[NumPoints]; // Y = new double[NumPoints]; // idNodes = new int[NumPoints]; // for (int curPnt = 0; curPnt < NumPoints; curPnt++) // { // X[curPnt] = System.Convert.ToDouble(xPnts[curPnt]); // Y[curPnt] = System.Convert.ToDouble(yPnts[curPnt]); // idNodes[curPnt] = System.Convert.ToInt32(idNodesLocal[curPnt]); // } // return true; // } // /// <summary>Copies the projection of the grid to the other file</summary> // /// <param name="gridFromPath">The path of the grid </param> // /// <param name="fileToPath">The path to the grid or shapefile</param> // private static void CopyProjectionFromGrid(string gridFromPath, string fileToPath) // { // var gridFrom = new Grid(); // gridFrom.Open(gridFromPath, GridDataType.UnknownDataType, true, GridFileType.UseExtension, null); // var projection = gridFrom.Projection.ToString(); // gridFrom.Close(); // if (fileToPath.Contains(".shp")) // { // var shpTo = new Shapefile(); // shpTo.Open(fileToPath, null); // shpTo.Projection = projection; // shpTo.Close(); // } // else // { // var gridTo = new Grid(); // gridTo.Open(fileToPath, GridDataType.UnknownDataType, true, GridFileType.UseExtension, null); // gridTo.AssignNewProjection(projection); // gridTo.Close(); // } // } // private static IFeature getShapeFromExtents(MapWinGIS.Extents ext) // { // int numPoints; // IFeature shp = new IFeature(); // MapWinGIS.Point pt; // shp.Create(MapWinGIS.ShpfileType.SHP_POLYGON); // pt = new MapWinGIS.Point(); // pt.x = ext.xMin; // pt.y = ext.yMax; // numPoints = shp.numPoints; // shp.InsertPoint(pt, numPoints); // pt = new MapWinGIS.Point(); // pt.x = ext.xMax; // pt.y = ext.yMax; // numPoints = shp.numPoints; // shp.InsertPoint(pt, numPoints); // pt = new MapWinGIS.Point(); // pt.x = ext.xMax; // pt.y = ext.yMin; // numPoints = shp.numPoints; // shp.InsertPoint(pt, numPoints); // pt = new MapWinGIS.Point(); // pt.x = ext.xMin; // pt.y = ext.yMin; // numPoints = shp.numPoints; // shp.InsertPoint(pt, numPoints); // pt = new MapWinGIS.Point(); // pt.x = ext.xMin; // pt.y = ext.yMax; // // CWG 24/4/11 add final point to close shape // pt = new MapWinGIS.Point(); // pt.x = ext.xMin; // pt.y = ext.yMax; // numPoints = shp.numPoints; // shp.InsertPoint(pt, numPoints); // return shp; // } // private static MapWinGIS.Extents getGridExtents(Raster g) // { // MapWinGIS.Extents ext = new MapWinGIS.Extents(); // RasterHeader head; // double centerX; // double centerY; // double yHalf; // double xHalf; // double xMin; // double xMax; // double yMin; // double yMax; // // This is fine for speed, but remember this is a reference-copied header // head = g.Header; // xHalf = head.dX / 2; // yHalf = head.dY / 2; // g.CellToProj(0, 0, out centerX, out centerY); // xMin = centerX - xHalf; // yMax = centerY + yHalf; // g.CellToProj(head.NumberCols - 1, head.NumberRows - 1, out centerX, out centerY); // xMax = centerX + xHalf; // yMin = centerY - yHalf; // ext.SetBounds(xMin, yMin, 0, xMax, yMax, 0); // return ext; // } // #endregion // #region Binary trees for modelling drainage /// <summary> /// Build drainage tree recursively upstream from outlet or reservoir, /// stopping at inlet, outlet, or reservoir (ignoring point sources) /// </summary> /// <param name="shed"></param> /// <param name="outlets"></param> /// <param name="haveOutletsShapefile">if this is false any dsnode not -1 stops the tree </param> /// <param name="isRoot">Flag to avoid stopping on root node, which is an outlet or reservoir</param> /// <param name="sindx"></param> /// <param name="dsNodeFieldNum"></param> /// <param name="us1FieldNum"></param> /// <param name="us2FieldNum"></param> /// <returns></returns> private static BinTree getDrainageFromSubbasin(IFeatureSet shed, IFeatureSet outlets, bool haveOutletsShapefile, bool isRoot, int sindx, int dsNodeFieldNum, int us1FieldNum, int us2FieldNum) { int nodeID; int leftID; int rightID; int Idx; BinTree left = null; BinTree right = null; nodeID = int.Parse(shed.get_CellValue(dsNodeFieldNum, sindx).ToString()); if ((!isRoot) && (nodeID != -1)) { if (!haveOutletsShapefile) { return null; } if (getDSNodeType(outlets, nodeID) != DSNode.PointSource) { return null; } } leftID = int.Parse(shed.get_CellValue(us1FieldNum, sindx).ToString()); rightID = int.Parse(shed.get_CellValue(us2FieldNum, sindx).ToString()); if (leftID != -1) { Idx = GetBasinIndexByID(shed, leftID); if (Idx != -1) { left = getDrainageFromSubbasin(shed, outlets, haveOutletsShapefile, false, Idx, dsNodeFieldNum, us1FieldNum, us2FieldNum); } } if (rightID != -1) { Idx = GetBasinIndexByID(shed, rightID); if (Idx != -1) { right = getDrainageFromSubbasin(shed, outlets, haveOutletsShapefile, false, Idx, dsNodeFieldNum, us1FieldNum, us2FieldNum); } } return new BinTree(sindx, left, right); }
private static int GetWshedFromStreamLink(int streamLink, IFeatureSet streamShape, IFeatureSet shedShape) { int streamindx; const int LinkIDField = 0; const int WaterShedIDField = 13; for (streamindx = 0; streamindx <= streamShape.NumRows() - 1; streamindx++) { if (int.Parse(streamShape.get_CellValue(LinkIDField, streamindx).ToString()) == streamLink) { return int.Parse(streamShape.get_CellValue(WaterShedIDField, streamindx).ToString()); } } return -1; }