[13074] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[13072] | 22 | using GeoAPI.Geometries;
|
---|
| 23 | using NetTopologySuite.Geometries;
|
---|
| 24 | using SharpMap.Data;
|
---|
| 25 | using SharpMap.Data.Providers;
|
---|
| 26 | using SharpMap.Layers;
|
---|
| 27 | using System;
|
---|
| 28 | using System.Collections.Generic;
|
---|
| 29 | using System.Drawing;
|
---|
| 30 | using System.Drawing.Drawing2D;
|
---|
| 31 | using System.Linq;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.BioBoost.Views.MapViews {
|
---|
| 34 | public class LazyVectorLayerGenerator : ILazyLayerGenerator {
|
---|
| 35 |
|
---|
| 36 | private GeometryFeatureProvider geom;
|
---|
| 37 | private IEnumerable<string> locationNames;
|
---|
| 38 | private IEnumerable<int> transportTargets;
|
---|
| 39 | private IEnumerable<double> transportAmounts;
|
---|
| 40 | private ILayer layer;
|
---|
| 41 | public String LayerName { get; set; }
|
---|
| 42 | public Color VectorColor { get; set; }
|
---|
| 43 | public int LineOffset { get; set; }
|
---|
| 44 |
|
---|
| 45 | public LazyVectorLayerGenerator(GeometryFeatureProvider geom,
|
---|
| 46 | IEnumerable<string> locationNames,
|
---|
| 47 | IEnumerable<int> transportTargets,
|
---|
| 48 | IEnumerable<double> transportAmounts,
|
---|
| 49 | Color vectorColor,
|
---|
| 50 | String layerName) {
|
---|
| 51 | this.geom = geom;
|
---|
| 52 | if (locationNames == null) throw new ArgumentNullException("locationNames");
|
---|
| 53 | if (transportTargets == null) throw new ArgumentNullException("transportTargets");
|
---|
| 54 | this.locationNames = locationNames;
|
---|
| 55 | this.transportTargets = transportTargets;
|
---|
| 56 | this.transportAmounts = transportAmounts;
|
---|
| 57 | this.VectorColor = vectorColor;
|
---|
| 58 | this.LayerName = layerName;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public ILayer Layer {
|
---|
| 62 | get {
|
---|
| 63 | if (layer == null) BuildLayer();
|
---|
| 64 | return layer;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private void BuildLayer() {
|
---|
| 69 | string[] locationNames = this.locationNames.ToArray();
|
---|
| 70 | int[] transportTargets = this.transportTargets.ToArray();
|
---|
| 71 | double[] transportAmounts = this.transportAmounts == null ? null : this.transportAmounts.ToArray();
|
---|
| 72 | this.locationNames = null;
|
---|
| 73 | this.transportTargets = null;
|
---|
| 74 | var nameIndex = locationNames.Select((loc, idx) => new { loc, idx }).ToDictionary(p => p.loc, p => p.idx);
|
---|
| 75 | var centroids = new Coordinate[locationNames.Length];
|
---|
| 76 | foreach (FeatureDataRow row in geom.Features) {
|
---|
| 77 | int idx;
|
---|
| 78 | if (nameIndex.TryGetValue((string)row["NUTS_ID"], out idx)) {
|
---|
| 79 | var centroid = row.Geometry.Centroid;
|
---|
| 80 | try {
|
---|
| 81 | if (!centroid.Within(row.Geometry)) centroid = row.Geometry.InteriorPoint;
|
---|
| 82 | } catch (Exception x) { }
|
---|
| 83 | centroids[idx] = centroid.Coordinate;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | var dataTable = new FeatureDataTable();
|
---|
| 87 | dataTable.Columns.Add("NUTS_ID_SRC");
|
---|
| 88 | dataTable.Columns.Add("NUTS_ID_DEST");
|
---|
| 89 |
|
---|
| 90 | // var transports = new Collection<IGeometry>();
|
---|
| 91 | // GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
|
---|
| 92 | // var f = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(4326);
|
---|
| 93 | for (int i = 0; i < locationNames.Length; i++) {
|
---|
| 94 | if (transportAmounts != null && transportAmounts[i] <= 0) continue;
|
---|
| 95 | IGeometry g = null;
|
---|
| 96 | var j = transportTargets[i];
|
---|
| 97 | var source = centroids[i];
|
---|
| 98 | if (source == null) continue;
|
---|
| 99 | if (i == j)
|
---|
| 100 | g = new NetTopologySuite.Geometries.Point(source);
|
---|
| 101 | if (j == -1) continue;
|
---|
| 102 | var target = j < centroids.Length && j >= 0 ? centroids[j] : null;
|
---|
| 103 | if (target != null)
|
---|
| 104 | g = new LineString(new[] { source, target });
|
---|
| 105 |
|
---|
| 106 | var row = dataTable.NewRow();
|
---|
| 107 | row.Geometry = g;
|
---|
| 108 | row["NUTS_ID_SRC"] = locationNames[i];
|
---|
| 109 | row["NUTS_ID_DEST"] = locationNames[j];
|
---|
| 110 | dataTable.AddRow(row);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | var geometryProv = new GeometryFeatureProvider(dataTable);
|
---|
| 114 | var vectorLayer = new VectorLayer(LayerName, geometryProv);
|
---|
| 115 | vectorLayer.SmoothingMode = SmoothingMode.HighQuality;
|
---|
| 116 | vectorLayer.Style.Line.SetLineCap(LineCap.NoAnchor, LineCap.ArrowAnchor, DashCap.Flat);
|
---|
| 117 | vectorLayer.Style.Line.CustomEndCap = new AdjustableArrowCap(3, 5);
|
---|
| 118 | vectorLayer.Style.Line.Color = VectorColor;
|
---|
| 119 | vectorLayer.Style.Line.Width = 1;
|
---|
| 120 | vectorLayer.Style.LineOffset = LineOffset;
|
---|
| 121 | vectorLayer.Style.PointColor = new SolidBrush(VectorColor);
|
---|
| 122 | vectorLayer.Style.PointSize = 2;
|
---|
| 123 | this.layer = vectorLayer;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|