MapWinGIS:SampleCode-C Sharp Net:GridToPoints

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

         /// <summary>
         /// Creates a shapefile of Points from a Grid
         /// </summary>
         private void RasterToPoint()
         {
             int Layer;
             string tag;
             if (tvLayers.SelectedNode == null)
             {
                 MessageBox.Show("Please select the raster layer in the tree view");
                 return;
             }
             tag = tvLayers.SelectedNode.Tag.ToString();
             if (tag.Substring(0, 1) != "L") return;
             Layer = int.Parse(tag.Substring(1, tag.Length - 1));
             // We should have a valid layer, check if it is in our hashtable
             if (!LayerGrids.ContainsKey(Layer))
             {
                 MessageBox.Show("Please select the raster layer in the tree view");
                 return;
             }
             MapWinGIS.Grid grd = LayerGrids[Layer] as MapWinGIS.Grid;
             MapWinGIS.Shape mwShape;
             MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
             string filename = tvLayers.SelectedNode.Text;
             filename = System.IO.Path.ChangeExtension(filename, "");
             filename = filename.Substring(0, filename.Length - 1);
             int I = 1;
             while(System.IO.File.Exists(filename + I.ToString() + ".shp"))
             {
                 I++;
             }
             filename = filename + I.ToString() + ".shp";
             sf.CreateNew(filename, MapWinGIS.ShpfileType.SHP_POINT);
             MapWinGIS.Field mwField = new MapWinGIS.Field();
             mwField.Name = "Elevation";
             mwField.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
             mwField.Precision = 8;
             int fld = 1;
             sf.EditInsertField(mwField, ref fld, this);
             double dx = grd.Header.dX;
             double dy = grd.Header.dY;
             double Xll = grd.Header.XllCenter;
             double Yll = grd.Header.YllCenter;
             int pt = 0;
             int shp = 0;
             int oldProg = 0;
             int NumRows = grd.Header.NumberRows;
             int NumCols = grd.Header.NumberCols;
             sf.StartEditingShapes(true, this);
             float NoData;
             if ((double)grd.Header.NodataValue < (double)float.NegativeInfinity)
             {
                 NoData = float.NegativeInfinity;
             }
             else if ((double)grd.Header.NodataValue > (double)float.MaxValue)
             {
                 NoData = float.MaxValue;
             }
             else
             {
                 NoData = float.Parse(grd.Header.NodataValue.ToString());
             }
             for (int row = 0; row < NumRows; row++)
             {
                 float[] elevations = new float[NumCols];
                 grd.GetRow(row, ref elevations[0]);
                 for (int col = 0; col < NumCols; col++)
                 {
                     if (elevations[col] == NoData) continue;
                     mwShape = new MapWinGIS.Shape();
                     mwShape.Create(MapWinGIS.ShpfileType.SHP_POINT);
                     MapWinGIS.Point mwPoint = new MapWinGIS.Point();
                     mwPoint.x = Xll + dx * col;
                     mwPoint.y = Yll + dy * (NumRows - row);
                     mwShape.InsertPoint(mwPoint, ref pt);
                     if (sf.EditInsertShape(mwShape, ref shp) == false)
                     {
                         toolStripStatusLabel1.Text = "Error on point " + shp + ":\n" + sf.get_ErrorMsg(sf.LastErrorCode);
                         // write the error but don't halt
                         continue;
                     }
                     sf.EditCellValue(fld, shp, elevations[col]); 
                 }
                 int prog = (int)((row * 100) / NumRows);
                 if (prog > oldProg)
                 {
                     toolStripProgressBar1.Value = prog;
                     toolStripStatusLabel1.Text = "Grid to Point..." + row.ToString() + "/" + NumRows.ToString();
                     Application.DoEvents();
                 }
             }
             sf.StopEditingShapes(true, true, this);
             //sf.SaveAs(filename, this);
             toolStripProgressBar1.Value = 0;
             toolStripStatusLabel1.Text = "Ready.";
         }

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-C_Sharp_Net:GridToPoints"

This page has been accessed 3,172 times. This page was last modified on 21 March 2007, at 21:52.