-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
276 lines (244 loc) · 12.8 KB
/
Program.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Studio A&T s.r.l.">
// Copyright (c) Studio A&T s.r.l. All rights reserved.
// </copyright>
// <author>Nicogis</author>
//-----------------------------------------------------------------------
namespace Studioat.ArcGIS.Voronoi
{
using System;
using System.Collections.Generic;
using System.Text;
using CommandLine;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
/// <summary>
/// class main
/// </summary>
public class Program
{
/// <summary>
/// arcobjects License Initializer
/// </summary>
private static LicenseInitializer arcobjectsLicenseInitializer = new LicenseInitializer();
/// <summary>
/// name field of identifier polygon
/// </summary>
private static string nameFieldIdOutput = "Id";
/// <summary>
/// main method
/// </summary>
/// <param name="args">array of args</param>
[STAThread]
private static void Main(string[] args)
{
try
{
// ESRI License Initializer generated code.
if (!arcobjectsLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { }))
{
Console.WriteLine(arcobjectsLicenseInitializer.LicenseMessage());
Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown.");
arcobjectsLicenseInitializer.ShutdownApplication();
return;
}
var options = new Options();
Parser parser = new Parser();
if (args.Length == 0 || args[0] == "-h" || args[0].Trim().ToLowerInvariant() == "help")
{
Console.WriteLine(options.GetUsage());
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
return;
}
if (parser.ParseArguments(args, options))
{
if (!Helper.ExistsFileGdb(options.PathAndFGDB))
{
Console.WriteLine("Filegeodatabase '{0}' not exists!", options.PathAndFGDB);
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
return;
}
IWorkspace workspace = Helper.FileGdbWorkspaceFromPath(options.PathAndFGDB);
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
string inputFeatureClassName = options.FeatureClassNameInput ?? "Points";
IWorkspace2 workspace2 = workspace as IWorkspace2;
if (!workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, inputFeatureClassName))
{
Console.WriteLine("FeatureClass '{0}' not exists in filegeodatabase '{1}'!", inputFeatureClassName, options.PathAndFGDB);
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
return;
}
IFeatureClass featureClassPoints = featureWorkspace.OpenFeatureClass(inputFeatureClassName);
if (featureClassPoints.ShapeType != esriGeometryType.esriGeometryPoint)
{
Console.WriteLine("FeatureClass '{0}' is not type point!", inputFeatureClassName);
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
return;
}
string outputFeatureClassName = options.FeatureClassNameOutput ?? "Polygons";
if (workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, outputFeatureClassName))
{
Console.Write("\nThe featureClass '{0}' exists. You want to overwrite it? Y/N ", outputFeatureClassName);
while (true)
{
ConsoleKeyInfo answer = Console.ReadKey(true);
if (answer.Key == ConsoleKey.N)
{
return;
}
else if (answer.Key == ConsoleKey.Y)
{
Console.Write("\n");
break;
}
else
{
Console.Write("\nPlease select a valid option (Y/N)!");
}
}
IDataset outputDataset = (IDataset)featureWorkspace.OpenFeatureClass(outputFeatureClassName);
if (outputDataset.CanDelete())
{
outputDataset.Delete();
}
else
{
Console.WriteLine("FeatureClass '{0}' couldn't be deleted", outputFeatureClassName);
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
return;
}
}
IFeatureClass featureClassPolygons = Program.CreateFeatureClassOutput(workspace, Helper.GetSpatialReference(featureClassPoints), outputFeatureClassName);
List<IPoint> locations = new List<IPoint>();
using (ComReleaser comReleaser = new ComReleaser())
{
IFeatureCursor cursor = featureClassPoints.Search(null, true);
comReleaser.ManageLifetime(cursor);
IFeature feature = null;
while ((feature = cursor.NextFeature()) != null)
{
IPoint p = feature.ShapeCopy as IPoint;
locations.Add(p);
}
}
IList<IGeometry> thiessenPolygons = Triangulation.GeometryVoronoi(locations);
int idxId = featureClassPolygons.FindField(Program.nameFieldIdOutput);
int i = 0;
StringBuilder errorGeneratePolygon = new StringBuilder();
foreach (IGeometry pg in thiessenPolygons)
{
i++;
try
{
IFeature feature = featureClassPolygons.CreateFeature();
feature.Shape = pg as IPolygon;
feature.set_Value(idxId, i);
feature.Store();
}
catch (Exception ex)
{
errorGeneratePolygon.AppendLine(string.Format("Polygon id: {0} - Error: Message[{1}]\r\nSource[{2}]\r\nTrace[{3}]", i, ex.Message, ex.Source, ex.StackTrace));
errorGeneratePolygon.AppendLine();
}
}
string errorResult = errorGeneratePolygon.ToString();
if (!string.IsNullOrEmpty(errorResult))
{
Console.WriteLine(errorResult);
Console.WriteLine("Create polygons voronoi with errors!");
}
else
{
Console.WriteLine("Create polygons voronoi with success!");
}
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
else
{
Console.WriteLine(options.GetUsageError());
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: Message[{0}]\r\nSource[{1}]\r\nTrace[{2}]", ex.Message, ex.Source, ex.StackTrace);
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
finally
{
try
{
// ESRI License Initializer generated code.
// Do not make any call to ArcObjects after ShutDownApplication()
arcobjectsLicenseInitializer.ShutdownApplication();
}
catch
{
}
}
}
/// <summary>
/// create feature class of output
/// </summary>
/// <param name="workspace">object workspace</param>
/// <param name="spatialReference">spatial reference of feature class of output</param>
/// <param name="nameFeatureClass">name of feature class</param>
/// <returns>object feature class</returns>
private static IFeatureClass CreateFeatureClassOutput(IWorkspace workspace, ISpatialReference spatialReference, string nameFeatureClass)
{
IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
// Create the fields collection.
IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IField oidField = new FieldClass();
IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
oidFieldEdit.Name_2 = "OBJECTID";
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.AddField(oidField);
// Create the Shape field.
IField shapeField = new Field();
IFieldEdit shapeFieldEdit = (IFieldEdit)shapeField;
// Set up the geometry definition for the Shape field.
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
// By setting the grid size to 0, you're allowing ArcGIS to determine the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size will be 1000. If in a file or ArcSDE geodatabase, the grid size
// will be based on the initial loading or inserting of features.
geometryDefEdit.HasM_2 = false;
geometryDefEdit.HasZ_2 = false;
geometryDefEdit.SpatialReference_2 = spatialReference;
// Set standard field properties.
shapeFieldEdit.Name_2 = featureClassDescription.ShapeFieldName;
shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
shapeFieldEdit.GeometryDef_2 = geometryDef;
shapeFieldEdit.IsNullable_2 = true;
shapeFieldEdit.Required_2 = true;
fieldsEdit.AddField(shapeField);
IField idField = new FieldClass();
IFieldEdit idIsolaFieldEdit = (IFieldEdit)idField;
idIsolaFieldEdit.Name_2 = Program.nameFieldIdOutput;
idIsolaFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
fieldsEdit.AddField(idField);
// Use IFieldChecker to create a validated fields collection.
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = workspace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
return featureWorkspace.CreateFeatureClass(nameFeatureClass, fields, objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, featureClassDescription.ShapeFieldName, string.Empty);
}
}
}