Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/MapViews/LazyVectorLayerGenerator.cs @ 13072

Last change on this file since 13072 was 13072, checked in by gkronber, 7 years ago

#2499: added code from HeuristicLab.BioBoost.Views (from private repository) nothing much has been changed

File size: 4.2 KB
Line 
1using System.Data;
2using GeoAPI.Geometries;
3using NetTopologySuite.Geometries;
4using SharpMap.Data;
5using SharpMap.Data.Providers;
6using SharpMap.Layers;
7using System;
8using System.Collections.Generic;
9using System.Collections.ObjectModel;
10using System.Drawing;
11using System.Drawing.Drawing2D;
12using System.Linq;
13
14namespace HeuristicLab.BioBoost.Views.MapViews {
15  public class LazyVectorLayerGenerator : ILazyLayerGenerator {
16
17    private GeometryFeatureProvider geom;
18    private IEnumerable<string> locationNames;
19    private IEnumerable<int> transportTargets;
20    private IEnumerable<double> transportAmounts;
21    private ILayer layer;
22    public String LayerName { get; set; }
23    public Color VectorColor { get; set; }
24    public int LineOffset { get; set; }
25
26    public LazyVectorLayerGenerator(GeometryFeatureProvider geom,
27      IEnumerable<string> locationNames,
28      IEnumerable<int> transportTargets,
29      IEnumerable<double> transportAmounts,
30      Color vectorColor,
31      String layerName) {
32      this.geom = geom;
33      if (locationNames == null) throw new ArgumentNullException("locationNames");
34      if (transportTargets == null) throw new ArgumentNullException("transportTargets");
35      this.locationNames = locationNames;
36      this.transportTargets = transportTargets;
37      this.transportAmounts = transportAmounts;
38      this.VectorColor = vectorColor;
39      this.LayerName = layerName;
40    }
41
42    public ILayer Layer {
43      get {
44        if (layer == null) BuildLayer();
45        return layer;
46      }
47    }
48
49    private void BuildLayer() {
50      string[] locationNames = this.locationNames.ToArray();
51      int[] transportTargets = this.transportTargets.ToArray();
52      double[] transportAmounts = this.transportAmounts == null ? null : this.transportAmounts.ToArray();
53      this.locationNames = null;
54      this.transportTargets = null;
55      var nameIndex = locationNames.Select((loc, idx) => new { loc, idx }).ToDictionary(p => p.loc, p => p.idx);
56      var centroids = new Coordinate[locationNames.Length];
57      foreach (FeatureDataRow row in geom.Features) {
58        int idx;
59        if (nameIndex.TryGetValue((string)row["NUTS_ID"], out idx)) {
60          var centroid = row.Geometry.Centroid;
61          try {
62            if (!centroid.Within(row.Geometry)) centroid = row.Geometry.InteriorPoint;
63          } catch (Exception x) { }
64          centroids[idx] = centroid.Coordinate;
65        }
66      }
67      var dataTable = new FeatureDataTable();
68      dataTable.Columns.Add("NUTS_ID_SRC");
69      dataTable.Columns.Add("NUTS_ID_DEST");
70
71      // var transports = new Collection<IGeometry>();
72      // GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
73      // var f = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(4326);
74      for (int i = 0; i < locationNames.Length; i++) {
75        if (transportAmounts != null && transportAmounts[i] <= 0) continue;
76        IGeometry g = null;
77        var j = transportTargets[i];
78        var source = centroids[i];
79        if (source == null) continue;
80        if (i == j)
81          g = new NetTopologySuite.Geometries.Point(source);
82        if (j == -1) continue;
83        var target = j < centroids.Length && j >= 0 ? centroids[j] : null;
84        if (target != null)
85          g = new LineString(new[] { source, target });
86
87        var row = dataTable.NewRow();
88        row.Geometry = g;
89        row["NUTS_ID_SRC"] = locationNames[i];
90        row["NUTS_ID_DEST"] = locationNames[j];
91        dataTable.AddRow(row);
92      }
93
94      var geometryProv = new GeometryFeatureProvider(dataTable);
95      var vectorLayer = new VectorLayer(LayerName, geometryProv);
96      vectorLayer.SmoothingMode = SmoothingMode.HighQuality;
97      vectorLayer.Style.Line.SetLineCap(LineCap.NoAnchor, LineCap.ArrowAnchor, DashCap.Flat);
98      vectorLayer.Style.Line.CustomEndCap = new AdjustableArrowCap(3, 5);
99      vectorLayer.Style.Line.Color = VectorColor;
100      vectorLayer.Style.Line.Width = 1;
101      vectorLayer.Style.LineOffset = LineOffset;
102      vectorLayer.Style.PointColor = new SolidBrush(VectorColor);
103      vectorLayer.Style.PointSize = 2;
104      this.layer = vectorLayer;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.