private void menu_trend_Click(object sender, EventArgs e) { var series = SelectSeries(_selectedMenuItem); if (series != null) { int npt = series.Points.Count; double rs, slope, yint; double[] xx = new double[npt]; double[] yy = new double[npt]; for (int i = 0; i < npt; i++) { xx[i] = i + 1; yy[i] = series.Points[i].YValues[0]; } MyStatisticsMath.LinearRegression(xx, yy, 0, npt, out rs, out yint, out slope); string trend_name = "Trend of" + series.Name; lvStatistics.Items[4].SubItems[3].Text = string.Format("y = {0}x + {1}", slope.ToString("0.00"), yint.ToString("0.00")); double[] yy_trend = new double[2]; if (series.XValueType == ChartValueType.Date || series.XValueType == ChartValueType.DateTime) { DateTime[] date_trend = new DateTime[2]; double[] xx_trend = new double[2]; xx_trend[0] = 1; xx_trend[1] = npt; date_trend[0] = DateTime.FromOADate(series.Points[0].XValue); date_trend[1] = DateTime.FromOADate(series.Points[npt - 1].XValue); yy_trend[0] = slope * xx_trend[0] + yint; yy_trend[1] = slope * xx_trend[1] + yint; Plot <double>(date_trend, yy_trend, trend_name, SeriesChartType.Line); } else { double[] xx_trend = new double[2]; xx_trend[0] = 1; xx_trend[1] = npt; yy_trend[0] = slope * xx_trend[0] + yint; yy_trend[1] = slope * xx_trend[1] + yint; Plot <double>(xx_trend, yy_trend, trend_name, SeriesChartType.Line); } } }
public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler) { int var_indexA = 0; var matA = Get3DMat(InputDataCube, ref var_indexA); double prg = 0; int count = 1; if (matA != null) { int nstep = matA.Size[1]; int ncell = matA.Size[2]; var mat_out = new DataCube <float>(1, 1, ncell); mat_out.Name = OutputDataCube; mat_out.Variables = new string[] { "Slope" }; for (int c = 0; c < ncell; c++) { var vec = matA.GetVector(var_indexA, ":", c.ToString()); var dou_vec = MatrixOperation.ToDouble(vec); var steps = new double[nstep]; double rs, slope, yint; for (int t = 1; t < nstep; t++) { steps[t] = t + 1; } MyStatisticsMath.LinearRegression(steps, dou_vec, 0, nstep, out rs, out yint, out slope); mat_out[0, 0, c] = (float)slope; prg = (c + 1) * 100.0 / ncell; if (prg > count) { cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1)); count++; } } Workspace.Add(mat_out); return(true); } else { return(false); } }
private void PrePro(Dictionary<int, ReachFeatureCollection> fealist, out string msg) { double rs = 0, slope = 0, yint = 0; var dt = _out_sfr_layer.DataTable; var prj = MyAppManager.Instance.CompositionContainer.GetExportedValue<IProjectService>(); msg = ""; for (int i = 0; i < _out_sfr_layer.Features.Count; i++) { try { var dr = dt.Rows[i]; var geo = _out_sfr_layer.GetFeature(i).Geometry; if (geo.Length <= _dem_layer.CellHeight) { continue; } var npt = geo.Coordinates.Count(); int segid = int.Parse(dr[SegmentField].ToString()) + 1; double[] dis = new double[npt]; double[] ac_dis = new double[npt]; double[] elvs = new double[npt]; double elev_av = 0; var pt0 = geo.Coordinates[0]; var cell = _dem_layer.ProjToCell(pt0.X, pt0.Y); double ad = 0; dis[0] = 0; elvs[0] = _dem_layer.Value[cell.Row, cell.Column]; for (int j = 0; j < npt; j++) { cell = _ad_layer.ProjToCell(geo.Coordinates[j].X, geo.Coordinates[j].Y); ad += _ad_layer.Value[cell.Row, cell.Column]; } ad = ad / npt; for (int j = 1; j < npt; j++) { cell = _dem_layer.ProjToCell(geo.Coordinates[j].X, geo.Coordinates[j].Y); elvs[j] = _dem_layer.Value[cell.Row, cell.Column]; dis[j] = SpatialDistance.DistanceBetween(geo.Coordinates[j - 1], geo.Coordinates[j]); } for (int j = 0; j < npt; j++) { ac_dis[j] = dis.Take(j + 1).Sum(); } MyStatisticsMath.LinearRegression(ac_dis, elvs, 0, elvs.Length, out rs, out yint, out slope); if (slope < 0) { slope = -slope; } else if (slope == 0) { slope = _minum_slope; } for (int j = 0; j < npt; j++) { elvs[j] = yint + slope * ac_dis[j]; } elev_av = elvs.Average(); if (slope < _minum_slope) slope = _minum_slope; if (slope > _maximum_slope) slope = _maximum_slope; var rch = new ReachFeature() { Row = dr, Elevation = elev_av, Slope = slope }; if (fealist[segid].Reaches.ContainsKey(ad)) { ad += i * 0.001; } fealist[segid].Reaches.Add(ad, rch); fealist[segid].OutSegmentID = int.Parse(dr["DSLINKNO"].ToString()); dr["Length"] = geo.Length; } catch (Exception ex) { msg += ex.Message + "\n"; } } }