using GeoAPI.Geometries; using HeuristicLab.BioBoost.Persistence; using HeuristicLab.BioBoost.ProblemDescription; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Persistence.Default.Xml; using SharpMap.Data; using SharpMap.Data.Providers; using SharpMap.Forms; using SharpMap.Layers; using SharpMap.Rendering.Thematics; using SharpMap.Styles; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Windows.Forms; using ColorBlend = SharpMap.Rendering.Thematics.ColorBlend; namespace HeuristicLab.BioBoost.Views { [Content(typeof(BioBoostProblemData), IsDefaultView = true)] public partial class BioBoostProblemDataView : NamedItemView { public new BioBoostProblemData Content { get { return (BioBoostProblemData)base.Content; } set { base.Content = value; } } private VectorLayer regions; private FilterDialog filterDialog; public BioBoostProblemDataView() { InitializeComponent(); filterDialog = new FilterDialog(); } private GeometryFeatureProvider gfp2; private void PrepareMap(object sender, DoWorkEventArgs e) { var gfp = ShapeFileLoader.LoadShapeFile("C:\\Users\\P40031\\SkyDrive\\NUTS3\\NUTS3_regions_2010_WGS84.shp"); // TODO! var ms = new MemoryStream(); XmlGenerator.Serialize(gfp, ms); gfp2 = XmlParser.Deserialize(new MemoryStream(ms.ToArray())); } private void ShowMap(object sender, RunWorkerCompletedEventArgs e) { var outline = new Pen(Color.FromArgb(128, Color.White), 0.5f); var small = new VectorStyle { Fill = Brushes.Blue, EnableOutline = true, Outline = outline }; var large = new VectorStyle { Fill = Brushes.Red, EnableOutline = true, Outline = outline }; regions = new VectorLayer("NUTS3 Regions") { DataSource = gfp2, Theme = new GradientTheme("Shape_Area", 0, 5, small, large) { FillColorBlend = ColorBlend.Rainbow5 }, }; // add labels var l = new LabelLayer("NUTS3 Labels") { DataSource = gfp2, LabelColumn = "NUTS_ID", PriorityColumn = "Shape_Area", }; // add geometries var geometries = new Collection(); GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices(); var f = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(4326); regions.DataSource.Open(); var rows = regions.DataSource.GetObjectIDsInView(regions.Envelope); foreach (var row in rows) { var source = regions.DataSource.GetFeature(row).Geometry.Centroid; var ds = new FeatureDataSet(); regions.DataSource.ExecuteIntersectionQuery(source.Buffer(0.1), ds); var neighbors = ds.Tables[0].Rows; if (neighbors.Count > 0) { var ls = f.CreateLineString(new[] { source.Coordinate, ((FeatureDataRow) neighbors[rng.Next(neighbors.Count)]). Geometry.Centroid. Coordinate, }); geometries.Add(ls); } } regions.DataSource.Close(); var v = new VectorLayer("Vectors") { DataSource = new GeometryProvider(geometries), }; v.Style.Line.SetLineCap(LineCap.NoAnchor, LineCap.ArrowAnchor, DashCap.Flat); // add layers mapBox.Map.Layers.Add(regions); mapBox.Map.Layers.Add(l); mapBox.Map.Layers.Add(v); // configure map mapBox.Map.ZoomToExtents(); mapBox.Refresh(); mapBox.ActiveTool = MapBox.Tools.Pan; tabControl.SelectedTab = mapPage; } private readonly Dictionary styles = new Dictionary(); private readonly Random rng = new Random(); private VectorStyle GetRandomStyle(FeatureDataRow row) { var name = row["NUTS_ID"].ToString(); VectorStyle style; if (!styles.TryGetValue(name, out style)) { style = new VectorStyle { Fill = new SolidBrush(Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256))) }; styles[name] = style; } return style; } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.GeometryChanged -= GeometryChanged; } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.GeometryChanged += GeometryChanged; } #region Event Handlers (Content) private void GeometryChanged(object sender, EventArgs eventArgs) { if (InvokeRequired) { Invoke(new EventHandler(GeometryChanged), new object[] { sender, eventArgs }); } else { UpdateMap(); } } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { parameterCollectionView.Content = null; } else { parameterCollectionView.Content = Content.Parameters; } UpdateMap(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); parameterCollectionView.Enabled = Content != null; loadShapeFileButton.Enabled = Content != null; importFeedstockButton.Enabled = Content != null; importDistancesButton.Enabled = Content != null; } private void UpdateMap() { mapBox.Map.Layers.Clear(); mapBox.ShowProgressUpdate = false; if (Content == null || Content.Geometry == null) return; mapBox.ShowProgressUpdate = true; var outline = new Pen(Color.FromArgb(128, Color.White), 0.5f); var small = new VectorStyle { Fill = Brushes.Blue, EnableOutline = true, Outline = outline }; var large = new VectorStyle { Fill = Brushes.Red, EnableOutline = true, Outline = outline }; mapBox.Map.Layers.Add(new VectorLayer("Regions", Content.Geometry) { //Theme = new GradientTheme("Shape_Area", 0, 5, small, large) { //FillColorBlend = ColorBlend.Rainbow5, //}, }); mapBox.Map.ZoomToExtents(); mapBox.Refresh(); mapBox.ActiveTool = MapBox.Tools.Pan; } #region Event Handlers (child controls) private void mapBox_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { var c = mapBox.Map.ImageToWorld(e.Location); var f = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(4326); var ds = new FeatureDataSet(); regions.ExecuteIntersectionQuery(f.CreatePoint(c), ds); if (ds.Tables[0].Rows.Count > 0) { var text = ds.Tables[0].Rows[0]["NUTS_ID"].ToString(); toolTip.SetToolTip(mapBox, text); } } } private void importFeedstockButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "Comma Separted Value|*.csv|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadFeedstockPotentials(openFileDialog.FileName); } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void loadShapeFileButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "ESRI Shape Files|*.shp|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadShapeFile(openFileDialog.FileName); tabControl.SelectedTab = mapPage; } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void importDistancesButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "Comma Serparted Value|*.csv|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadDistanceMatrix(openFileDialog.FileName); } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void importNeighborsButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "Comma Separated Value|*.csv|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadNeighbors(openFileDialog.FileName); } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void checkButton_Click(object sender, EventArgs e) { var warnings = Content.CheckGeoData(); if (!string.IsNullOrEmpty(warnings)) MessageBox.Show(warnings, "Warnings", MessageBoxButtons.OK); } private void importConversionsButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "YAML Files|*.yaml|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadDescriptors(openFileDialog.FileName); } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void splitRegionFactorsImportButton_Click(object sender, EventArgs e) { openFileDialog.Filter = "CSV Files|*.csv|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { Enabled = false; Content.LoadSplitRegionDistanceFactors(openFileDialog.FileName); } catch (Exception x) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, x); } finally { Enabled = true; } } } private void filterButton_Click(object sender, EventArgs e) { if (filterDialog.ShowDialog() == DialogResult.OK) { if (filterDialog.Keep) { Content.KeepRegions(filterDialog.Regex); } else { Content.RemoveRegions(filterDialog.Regex); } } } #endregion } }