// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using Deltares.Dam.Data;
using Deltares.Geographic;
using Deltares.Standard.Forms.DExpress;
using Deltares.Standard.Forms.Maps;
using Deltares.Standard.Maps;
using Deltares.Standard.Logging;
using DotSpatial.Data;
using DotSpatial.Projections;
using DotSpatial.Symbology;
using DotSpatial.Topology;
namespace Deltares.Dam.Forms
{
public class DamMapEditor
{
private DamProjectData project = null;
private MapEditor mapEditor = null;
private LocationJobSymbol symbol = null;
public DamMapEditor(MapEditor mapEditor, LocationJobSymbol symbol)
{
this.mapEditor = mapEditor;
this.symbol = symbol;
// uncomment this call to enable the button to open the OpenStreetMap background layer
//mapEditor.EnableOpenStreetMapSupport();
mapEditor.RegisterLayer(typeof(LocationJob), new MapLayerDefinition(FeatureType.Point, "Locations", this.GetLocationScheme()));
mapEditor.RegisterLayer(typeof(BackgroundRepositoryFeature), new MapLayerDefinition(FeatureType.Polygon, "Background", Color.LightGray, false));
}
public DamProjectData Project
{
get { return project; }
set
{
if (project != value)
{
project = value;
this.mapEditor.Clear();
if (project.DataSourceEsriProjection != null)
{
// esri projection definition comes from the DataSourceEsriProjection attribute defined in the .defx file
var projectionInfo = ProjectionInfo.FromEsriString(project.DataSourceEsriProjection);
if (Math.Abs(projectionInfo.Unit.Meters - 1.0d) > 0.001) LogManager.Add(new LogMessage(LogMessageType.Warning, project, "The imported geographic data is using a Non-Metric coordinate system, this may lead to incorrect calculations in DAM !"));
MapProjectionManager.Instance.SetActiveMapProjectionAsDotSpatial(projectionInfo);
this.mapEditor.MapControl.Projection = projectionInfo;
this.mapEditor.StatusBarProjection = MapProjection.Custom;
}
this.mapEditor.AddLayer((IList)project.WaterBoard.BackgroundRepositoryFeatures);
this.mapEditor.AddLayer(project.LocationJobs);
this.mapEditor.ZoomData();
this.mapEditor.MapControl.Refresh();
this.mapEditor.ShowWebLayer = true;
// Following line commented out, because it is the default setting.
// Kept here as placeholder when we want to change the default
//this.mapEditor.WebLayerType = MapEditor.WebLayerTypes.OpenStreetMapnik;
this.mapEditor.ShowSelectWebLayer = true;
}
}
}
private IFeatureScheme GetLocationScheme()
{
PointScheme scheme = new PointScheme();
PointCategory notCalculatedCategory = new PointCategory(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.NoRun), false), 16);
notCalculatedCategory.FilterExpression = string.Format("[Result] = {0}", (int) JobResult.NoRun);
notCalculatedCategory.LegendText = "Not calculated";
LocalizationSupport.Register(this.GetType(), notCalculatedCategory);
notCalculatedCategory.SelectionSymbolizer = new PointSymbolizer(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.NoRun), true), 16);
scheme.AddCategory(notCalculatedCategory);
PointCategory failedCategory = new PointCategory(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Failed), false), 16);
failedCategory.FilterExpression = string.Format("[Result] = {0}", (int)JobResult.Failed);
failedCategory.LegendText = "Failed";
LocalizationSupport.Register(this.GetType(), failedCategory);
failedCategory.SelectionSymbolizer = new PointSymbolizer(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Failed), true), 16);
scheme.AddCategory(failedCategory);
PointCategory goodCategory = new PointCategory(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Good), false), 16);
goodCategory.FilterExpression = string.Format("[Result] = {0}", (int)JobResult.Good);
goodCategory.LegendText = "Good";
LocalizationSupport.Register(this.GetType(), goodCategory);
goodCategory.SelectionSymbolizer = new PointSymbolizer(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Good), true), 16);
scheme.AddCategory(goodCategory);
PointCategory badCategory = new PointCategory(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Bad), false), 16);
badCategory.FilterExpression = string.Format("[Result] = {0}", (int)JobResult.Bad);
badCategory.LegendText = "Bad";
LocalizationSupport.Register(this.GetType(), badCategory);
badCategory.SelectionSymbolizer = new PointSymbolizer(symbol.GetImage(JobResultInterpreter.GetJobResultColor(JobResult.Bad), true), 16);
scheme.AddCategory(badCategory);
var percentages = new double[3];
percentages[0] = 33;
percentages[1] = 34;
percentages[2] = 33;
var values = new double[3];
values[0] = 0.5;
values[1] = 1.0;
values[2] = 3.0;
PointCategory mixedCategory = new PointCategory(symbol.GetPieImage(percentages, values, false), 16);
mixedCategory.FilterExpression = string.Format("[Result] = {0}", (int)JobResult.Mixed);
mixedCategory.LegendText = "Mixed";
LocalizationSupport.Register(this.GetType(), mixedCategory);
mixedCategory.SelectionSymbolizer = new PointSymbolizer(symbol.GetPieImage(percentages,values, true), 16);
scheme.AddCategory(mixedCategory);
return scheme;
}
///
///
///
///
///
///
///
private static string DetermineScenarioColumName(string resultType, string selectionType, ScenarioType scenarioType)
{
return String.Format("S{2:D2}{0}{1}", resultType, selectionType, (int)scenarioType);
}
///
///
///
public void ExportScenarios()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Shape files (*.shp)|*.shp";
dialog.Title = "Save scenario results";
if (dialog.ShowDialog() == DialogResult.OK)
{
List jobs = this.project.LocationJobs;
FeatureSet featureSet = new FeatureSet();
featureSet.Features.Clear();
if (project.DataSourceEsriProjection != null)
{
featureSet.Projection = ProjectionInfo.FromEsriString(project.DataSourceEsriProjection);
}
else
{
featureSet.Projection = KnownCoordinateSystems.Projected.NationalGrids.DutchRD;
}
foreach (IGeographicPoint geographic in jobs)
{
Feature feature = new Feature(new DotSpatial.Topology.Coordinate(geographic.X, geographic.Y));
featureSet.AddFeature(feature);
}
featureSet.AddFid();
featureSet.DataTable.Columns.Add("LocationId", typeof(string));
featureSet.DataTable.Columns.Add("DetrFactor", typeof(double));
featureSet.DataTable.Columns.Add("PRPIHPNAME", typeof(string));
featureSet.DataTable.Columns.Add("PRPIHPVAL", typeof(double));
featureSet.DataTable.Columns.Add("PRSTHPNAME", typeof(string));
featureSet.DataTable.Columns.Add("PRSTHPVAL", typeof(double));
foreach (ScenarioType scenarioType in Enum.GetValues(typeof(ScenarioType)))
{
featureSet.DataTable.Columns.Add(DetermineScenarioColumName("SF", "PH", scenarioType), typeof(double));
featureSet.DataTable.Columns.Add(DetermineScenarioColumName("SF", "LF", scenarioType), typeof(double));
featureSet.DataTable.Columns.Add(DetermineScenarioColumName("SPN", "LF", scenarioType), typeof(string));
featureSet.DataTable.Columns.Add(DetermineScenarioColumName("SPV", "LF", scenarioType), typeof(double));
}
for (int i = 0; i < featureSet.Features.Count; i++)
{
LocationJob locationJob = jobs[i];
featureSet.Features[i].DataRow["LocationId"] = locationJob.Location.Name;
featureSet.Features[i].DataRow["DetrFactor"] = locationJob.Location.DetrimentFactor;
var pipingProfile = locationJob.Location.GetMostProbableProfile(FailureMechanismSystemType.Piping);
if (pipingProfile != null)
{
featureSet.Features[i].DataRow["PRPIHPNAME"] = pipingProfile.Name;
featureSet.Features[i].DataRow["PRPIHPVAL"] = locationJob.Location.Segment.GetSoilProfileProbability(pipingProfile,
FailureMechanismSystemType.Piping);
}
var stabilityProfile = locationJob.Location.GetMostProbableProfile(FailureMechanismSystemType.StabilityInside);
if (stabilityProfile != null)
{
featureSet.Features[i].DataRow["PRSTHPNAME"] = stabilityProfile.Name;
featureSet.Features[i].DataRow["PRSTHPVAL"] = locationJob.Location.Segment.GetSoilProfileProbability(stabilityProfile,
FailureMechanismSystemType.StabilityInside);
}
foreach (ScenarioType scenarioType in Enum.GetValues(typeof(ScenarioType)))
{
RWScenarioProfileResult result = locationJob.GetRWScenarioResultOfProfileWithHighestProbablilityOfOccurrence(scenarioType);
featureSet.Features[i].DataRow[DetermineScenarioColumName("SF", "PH", scenarioType)] = result != null ? result.SafetyFactor : DamGlobalConstants.NoRunValue;
result = locationJob.GetRWScenarioResultWithLowestSafetyFactor(scenarioType);
featureSet.Features[i].DataRow[DetermineScenarioColumName("SF", "LF", scenarioType)] = result != null ? result.SafetyFactor : DamGlobalConstants.NoRunValue;
featureSet.Features[i].DataRow[DetermineScenarioColumName("SPN", "LF", scenarioType)] = result != null ? result.SoilProfileName : "";
featureSet.Features[i].DataRow[DetermineScenarioColumName("SPV", "LF", scenarioType)] = result != null ? result.SoilProfileProbability : 0.0;
}
}
featureSet.SaveAs(dialog.FileName, true);
}
}
}
}