-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathWktGeometryCreator.cs
More file actions
121 lines (100 loc) · 3.72 KB
/
WktGeometryCreator.cs
File metadata and controls
121 lines (100 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using NetTopologySuite.IO;
namespace SharpMap.Forms
{
/// <summary>
/// A geometry editor for WKT Text
/// </summary>
public partial class WktGeometryCreator : Form
{
private static readonly Dictionary<string, string> _wktTokens = new Dictionary<string, string>();
static WktGeometryCreator()
{
_wktTokens.Add("POINT", "POINT(10 10)");
_wktTokens.Add("LINESTRING", "LINESTRING(5 5, 7 16, 3 8)");
_wktTokens.Add("POLYGON", "POLYGON((10 10, 10 20, 20 20, 20 10, 10 10), (12 12, 18 12, 18 18, 12 18, 12 12))");
_wktTokens.Add("MULTIPOINT", "MULTIPOINT((10 10), (15 15), (13 9))");
_wktTokens.Add("MULTILINESTRING", "MULTILINESTRING((5 5, 7 16, 3 8), (15 15, 13 9))");
_wktTokens.Add("MULTIPOLYGON", "MULTIPOLYGON(((10 10, 10 20, 20 20, 20 10, 10 10), (12 12, 18 12, 18 18, 12 18, 12 12)), ((21 21, 21 31, 31 31, 31 21, 21 21)))");
_wktTokens.Add("GEOMETRYCOLLECTION", "GEOMETRYCOLLECTION(MULTIPOINT((10 10), (15 15), (13 9)), LINESTRING(5 5, 7 16, 3 8))");
}
/// <summary>
/// Creates an instance of this class
/// </summary>
public WktGeometryCreator()
{
InitializeComponent();
ShowInTaskbar = false;
foreach (var kvp in _wktTokens)
cboWktKeywords.Items.Add(kvp);
cboWktKeywords.DisplayMember = "Key";
cboWktKeywords.ValueMember = "Value";
}
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Hide();
}
private GeoAPI.Geometries.IGeometry _geometry;
/// <summary>
/// Gets or sets a value indicating the current geometry
/// </summary>
public GeoAPI.Geometries.IGeometry Geometry
{
get
{
return _geometry;
}
set
{
if (ReferenceEquals(_geometry, value))
return;
_geometry = value;
OnGeometrySet();
}
}
private readonly WKTWriter _wktWriter =
new WKTWriter(2) {Formatted = true, MaxCoordinatesPerLine = 3, Tab = 2};
private WKTReader _wktReader = new WKTReader();
private void OnGeometrySet()
{
if (_geometry == null)
{
txtWkt.Text = string.Empty;
return;
}
txtWkt.Text = _wktWriter.Write(_geometry);
_wktReader = new WKTReader(_geometry.Factory);
}
/// <summary>
/// Gets a value indicating the spatial reference id of the geometry created
/// </summary>
public int SRID
{
get => _wktReader?.Factory.SRID ?? 0;
}
private void txtWkt_TextChanged(object sender, EventArgs e)
{
string txt = txtWkt.Text;
if (string.IsNullOrEmpty(txt))
return;
try
{
var geometry = _wktReader.Read(txt);
if (geometry.EqualsExact(_geometry))
return;
Geometry = geometry;
txtWkt.ForeColor = SystemColors.WindowText;
lblError.Text = @"No Errors";
}
catch (Exception ex)
{
txtWkt.ForeColor = Color.Red;
lblError.Text = ex.Message;
}
}
}
}