protected override void Run(Prediction prediction) { List <PostGIS.Point> predictionPoints = new List <PostGIS.Point>(); Area predictionArea = prediction.PredictionArea; double areaMinX = predictionArea.BoundingBox.MinX; double areaMaxX = predictionArea.BoundingBox.MaxX; double areaMinY = predictionArea.BoundingBox.MinY; double areaMaxY = predictionArea.BoundingBox.MaxY; for (double x = areaMinX + prediction.PredictionPointSpacing / 2d; x <= areaMaxX; x += prediction.PredictionPointSpacing) // place points in the middle of the square boxes that cover the region - we get display errors from pixel rounding if the points are exactly on the boundaries { for (double y = areaMinY + prediction.PredictionPointSpacing / 2d; y <= areaMaxY; y += prediction.PredictionPointSpacing) { predictionPoints.Add(new PostGIS.Point(x, y, predictionArea.Shapefile.SRID)); } } List <PostGIS.Point> incidentPoints = new List <PostGIS.Point>(Incident.Get(TrainingStart, TrainingEnd, predictionArea, IncidentTypes.ToArray()).Select(i => i.Location)); predictionPoints.AddRange(incidentPoints); Console.Out.WriteLine("Filtering prediction points to prediction area"); predictionPoints = predictionArea.Intersects(predictionPoints, prediction.PredictionPointSpacing / 2f).Select(i => predictionPoints[i]).ToList(); NpgsqlConnection connection = DB.Connection.OpenConnection; try { Console.Out.WriteLine("Inserting points into prediction"); Point.CreateTable(prediction, predictionArea.Shapefile.SRID); List <int> predictionPointIds = Point.Insert(connection, predictionPoints.Select(p => new Tuple <PostGIS.Point, string, DateTime>(p, PointPrediction.NullLabel, DateTime.MinValue)), prediction, predictionArea, false); Console.Out.WriteLine("Running overall KDE for " + IncidentTypes.Count + " incident type(s)"); List <float> density = GetDensityEstimate(incidentPoints, _trainingSampleSize, false, 0, 0, predictionPoints, _normalize); Dictionary <int, float> pointIdOverallDensity = new Dictionary <int, float>(predictionPointIds.Count); int pointNum = 0; foreach (int predictionPointId in predictionPointIds) { pointIdOverallDensity.Add(predictionPointId, density[pointNum++]); } Dictionary <int, Dictionary <string, double> > pointIdIncidentDensity = new Dictionary <int, Dictionary <string, double> >(pointIdOverallDensity.Count); if (IncidentTypes.Count == 1) { string incident = IncidentTypes.First(); foreach (int pointId in pointIdOverallDensity.Keys) { Dictionary <string, double> incidentDensity = new Dictionary <string, double>(); incidentDensity.Add(incident, pointIdOverallDensity[pointId]); pointIdIncidentDensity.Add(pointId, incidentDensity); } } else { foreach (string incidentType in IncidentTypes) { Console.Out.WriteLine("Running KDE for incident \"" + incidentType + "\""); incidentPoints = new List <PostGIS.Point>(Incident.Get(TrainingStart, TrainingEnd, predictionArea, incidentType).Select(i => i.Location)); density = GetDensityEstimate(incidentPoints, _trainingSampleSize, false, 0, 0, predictionPoints, _normalize); if (density.Count > 0) { pointNum = 0; foreach (int predictionPointId in predictionPointIds) { pointIdIncidentDensity.EnsureContainsKey(predictionPointId, typeof(Dictionary <string, double>)); pointIdIncidentDensity[predictionPointId].Add(incidentType, density[pointNum++]); } } } } PointPrediction.CreateTable(prediction); PointPrediction.Insert(GetPointPredictionValues(pointIdOverallDensity, pointIdIncidentDensity), prediction, false); Smooth(prediction); } finally { DB.Connection.Return(connection); } }
private void SetIncidentsToolTip() { if (_setIncidentsToolTip) { int numIncidents = 0; if (TrainingArea != null && IncidentTypes.Count > 0) { numIncidents = Incident.Count(trainingStart.Value, trainingEnd.Value, TrainingArea, IncidentTypes.ToArray()); } toolTip.SetToolTip(incidentTypes, numIncidents + " total incidents selected"); } }