-
Notifications
You must be signed in to change notification settings - Fork 1
/
clsAutoInterpolate.cs
908 lines (660 loc) · 36.8 KB
/
clsAutoInterpolate.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Catalog;
using System.Windows.Forms;
namespace BathymetryTools10
{
class clsAutoInterpolate
{
IStepProgressor pStepProgressor;
public void Calculate(string sShorelineLayer, string sDepthPointsLayer, double dblInterval, string strShoreDepthField, string strDepthPointDepthField)
{
try
{
IMxDocument pmxdoc = ArcMap.Document as IMxDocument;
IMap pmap = pmxdoc.FocusMap;
IFeatureLayer pDepthSoundings = FindLayer(pmap, sDepthPointsLayer) as IFeatureLayer;
IFeatureLayer pShorelineLayer = FindLayer(pmap, sShorelineLayer) as IFeatureLayer;
//IFeatureLayer pOutpoints = FindLayer(pmap, sOutpoints) as IFeatureLayer;
IFeatureLayer pOutpoints = new FeatureLayerClass();
pOutpoints.FeatureClass = MakePointFC();
pOutpoints.Name = "Output Points";
ArcMap.Document.ActiveView.FocusMap.AddLayer(pOutpoints);
IFeatureLayer pConnectingLines = FindLayer(pmap, "ConnectingLines") as IFeatureLayer;
IFeatureLayer pShorePoints = FindLayer(pmap, "ShorePoints") as IFeatureLayer;
////Add fields if necessary
//AddField(pOutpoints, "Distance");
//AddField(pOutpoints, "Elevation");
//Set up the Outpoints cursor
IFeatureCursor pFCurOutPoints = pOutpoints.Search(null, false);
pFCurOutPoints = pOutpoints.FeatureClass.Insert(true);
IFeatureBuffer pFOutPointsBuffer = pOutpoints.FeatureClass.CreateFeatureBuffer();
//Set up the LineLayer Cursor (now using selected lines)
IFeatureSelection pShoreLineSelection = pShorelineLayer as IFeatureSelection;
if (pShoreLineSelection.SelectionSet.Count == 0)
{
MessageBox.Show("You must have at least one shoreline feature selected");
return;
}
ICursor pShorelineCursor;
IFeatureCursor pSelShoreFeatCur;
pShoreLineSelection.SelectionSet.Search(null, false, out pShorelineCursor);
pSelShoreFeatCur = pShorelineCursor as IFeatureCursor;
IFeature pShoreLineFeature = pSelShoreFeatCur.NextFeature();
//IFeatureCursor pFCur = pShorelineLayer.Search(null, false);
//IFeature pShoreLineFeature = pFCur.NextFeature();
//Set up the pConnectingLines cursor
IFeatureCursor pFCurOutLines = pConnectingLines.Search(null, false);
pFCurOutLines = pConnectingLines.FeatureClass.Insert(true);
IFeatureBuffer pFBuffer = pConnectingLines.FeatureClass.CreateFeatureBuffer();
//Set up the pShorePoints cursor
IFeatureCursor pShorePointsCursor = pShorePoints.Search(null, false);
IFeature pShorePointFeature = pShorePointsCursor.NextFeature();
IFeature pNewLineFeature = null;
IPointCollection pNewLinePointColl = null;
double dblDistance = 0;
double dblTotalDistanceCalculated = 0;
int iNumIntervals = 0;
double dblElevationDiff = 0;
double dblElevationInterval = 0;
double dblElevation = 0;
double dlbStartElevation = 0;
double dblEndElevation = 0;
int iLineProgressCount = 0;
IProgressDialog2 pProgressDialog = ShowProgressIndicator("Calculating points for shoreline vertex: ", 0, 1);
pProgressDialog.ShowDialog();
IEnvelope pCombinedEnvelope = CombineExtents(pDepthSoundings.FeatureClass, pShorelineLayer.FeatureClass);
while (pShoreLineFeature != null)
{
//IPointCollection4 pPointColl = pShoreLineFeature.Shape as IPointCollection4;
IPoint ppoint = new PointClass();
//for (int i = 0; i <= pPointColl.PointCount - 1; i++)
while (pShorePointFeature != null)
{
ppoint = pShorePointFeature.ShapeCopy as IPoint;
System.Diagnostics.Debug.WriteLine("ppoint: " + ppoint.X.ToString());
System.Diagnostics.Debug.WriteLine("shorepoint: " + pShorePointFeature.OID.ToString());
////try walking line at set intervals instead of just at vertices
//IPoint pLineVertex = new PointClass();
//pLineVertex = pPointColl.get_Point(i);
IFeatureCursor pDepthCursor = pDepthSoundings.Search(null, false);
IFeatureIndex2 pFtrInd = new FeatureIndexClass();
pFtrInd.FeatureClass = pDepthSoundings.FeatureClass;
pFtrInd.FeatureCursor = pDepthCursor;
pFtrInd.Index(null, pCombinedEnvelope);
IIndexQuery2 pIndQry = pFtrInd as IIndexQuery2;
int FtdID = 0;
double dDist2Ftr = 0;
pIndQry.NearestFeature(ppoint, out FtdID, out dDist2Ftr);
IFeature pCloseFeature = pDepthSoundings.FeatureClass.GetFeature(FtdID);
IPoint pEndPoint = new PointClass();
pEndPoint = pCloseFeature.Shape as IPoint;
//Make the line here
pNewLineFeature = pConnectingLines.FeatureClass.CreateFeature();
pNewLinePointColl = new PolylineClass();
object missing = Type.Missing;
pNewLinePointColl.AddPoint(pEndPoint, ref missing, ref missing);
pNewLinePointColl.AddPoint(ppoint, ref missing, ref missing);
pNewLineFeature.Shape = pNewLinePointColl as PolylineClass;
//Check to see if new line crosses land, if not, process it.
//bool bLineIntersectsShore = FeatureIntersects(pShorelineLayer, pConnectingLines, pNewLineFeature);
bool bLineIntersectsShore = false;
if (bLineIntersectsShore == false)
{
pNewLineFeature.Store();
ICurve pCurve = pNewLineFeature.Shape as ICurve;
pCurve.Project(pmap.SpatialReference);
//Get the Starting Elevation from the closest depth point
dlbStartElevation = GetStartElevation(pDepthSoundings, pConnectingLines, pNewLineFeature, strDepthPointDepthField);
//The Elevation for the first run IS the start elevation
dblElevation = dlbStartElevation;
//////Get the ending elevation from the shoreline
dblEndElevation = GetEndElevation(pShorelineLayer, pConnectingLines, pNewLineFeature, strShoreDepthField);
//number of line segments based on the user's interval
iNumIntervals = Convert.ToInt32(pCurve.Length / dblInterval);
dblElevationDiff = Math.Abs(dblEndElevation - dlbStartElevation);
//The calculated elevation interval to step up each time
dblElevationInterval = dblElevationDiff / iNumIntervals;
ppoint = new PointClass();
while (dblTotalDistanceCalculated <= pCurve.Length)
{
pFOutPointsBuffer.set_Value(pFOutPointsBuffer.Fields.FindField("LineOID"), pNewLineFeature.OID);
pFOutPointsBuffer.set_Value(pFOutPointsBuffer.Fields.FindField("Distance"), dblDistance);
pFOutPointsBuffer.set_Value(pFOutPointsBuffer.Fields.FindField("Elevation"), dblElevation);
//this code set the point on the line at a distance
pCurve.QueryPoint(0, dblDistance, false, ppoint);
pFOutPointsBuffer.set_Value(pFOutPointsBuffer.Fields.FindField("X"), ppoint.X);
pFOutPointsBuffer.set_Value(pFOutPointsBuffer.Fields.FindField("Y"), ppoint.Y);
//reCalc the new distance and elevation values for the next iteration
dblDistance = dblDistance + dblInterval;
//add or subtract elevation depending on whether dblElevationDiff is positve or negative
if (dblElevationDiff > 0)
{
dblElevation = dblElevation - dblElevationInterval;
}
else
{
dblElevation = dblElevation + dblElevationInterval;
}
dblTotalDistanceCalculated = dblTotalDistanceCalculated + dblInterval;
//Insert the feature into the featureclass
pFOutPointsBuffer.Shape = ppoint;
pFCurOutPoints.InsertFeature(pFOutPointsBuffer);
}
//Reset the distance values back to 0 for the next feature
dblDistance = 0;
dblTotalDistanceCalculated = 0;
pFCurOutPoints.Flush();
pStepProgressor.Step();
pProgressDialog.Description = "Calculating points for shoreline vertex: " + pShorePointFeature.OID.ToString();
iLineProgressCount++;
}
pShorePointFeature = pShorePointsCursor.NextFeature();
}
pShoreLineFeature = pSelShoreFeatCur.NextFeature();
}
//cleanup
pFCurOutLines.Flush();
pSelShoreFeatCur.Flush();
pProgressDialog.HideDialog();
pmxdoc.ActiveView.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}
private IFeatureClass MakePointFC()
{
string strFCName;
try
{
IGxCatalogDefaultDatabase Defaultgdb = ArcMap.Application as IGxCatalogDefaultDatabase;
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspace pWorkspace = workspaceFactory.OpenFromFile(Defaultgdb.DefaultDatabaseName.PathName, 0);
IFeatureWorkspace workspace = pWorkspace as IFeatureWorkspace;
UID CLSID = new UID();
CLSID.Value = "esriGeodatabase.Feature";
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
pFieldsEdit.FieldCount_2 = 6;
IGeoDataset geoDataset = ArcMap.Document.ActiveView.FocusMap.get_Layer(0) as IGeoDataset;
IGeometryDef pGeomDef = new GeometryDef();
IGeometryDefEdit pGeomDefEdit = pGeomDef as IGeometryDefEdit;
pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGeomDefEdit.SpatialReference_2 = geoDataset.SpatialReference;
IField pField;
IFieldEdit pFieldEdit;
//pField = new FieldClass();
//pFieldEdit = pField as IFieldEdit;
//pFieldEdit.AliasName_2 = "ObjectID";
//pFieldEdit.Name_2 = "ObjectID";
//pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
//pFieldsEdit.set_Field(0, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "SHAPE";
pFieldEdit.Name_2 = "SHAPE";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeomDef;
pFieldsEdit.set_Field(0, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "LineOID";
pFieldEdit.Name_2 = "LineOID";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldsEdit.set_Field(1, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "Distance";
pFieldEdit.Name_2 = "Distance";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.set_Field(2, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "Elevation";
pFieldEdit.Name_2 = "Elevation";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.set_Field(3, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "X";
pFieldEdit.Name_2 = "X";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.set_Field(4, pFieldEdit);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.AliasName_2 = "Y";
pFieldEdit.Name_2 = "Y";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.set_Field(5, pFieldEdit);
strFCName = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName());
char[] chars = strFCName.ToCharArray();
if (Char.IsDigit(chars[0]))
{
strFCName = strFCName.Remove(0, 1);
}
KillExistingFeatureclass(strFCName);
IFeatureClass pFeatureClass = workspace.CreateFeatureClass(strFCName, pFieldsEdit, CLSID, null, esriFeatureType.esriFTSimple, "SHAPE", "");
return pFeatureClass;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
return null;
}
}
private void KillExistingFeatureclass(string strFilename)
{
try
{
IGxCatalogDefaultDatabase Defaultgdb = ArcMap.Application as IGxCatalogDefaultDatabase;
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspace pWorkspace = workspaceFactory.OpenFromFile(Defaultgdb.DefaultDatabaseName.PathName, 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(strFilename);
IDataset pDataset = pFeatureLayer.FeatureClass as IDataset;
if (pDataset.CanDelete())
{
pDataset.Delete();
}
}
catch { }
}
private double GetStartElevation(IFeatureLayer pDepthSoundings, IFeatureLayer pLineLayer, IFeature pLineFeature, string strDepthField)
{
try
{
IEnvelope pCombinedEnvelope = CombineExtents(pDepthSoundings.FeatureClass, pLineLayer.FeatureClass);
IPointCollection4 pPointColl = pLineFeature.Shape as IPointCollection4;
IPoint ppoint = new PointClass();
//pPointColl.QueryPoint(0, ppoint);
ppoint = pPointColl.get_Point(0);
IFeatureCursor pDepthCursor = pDepthSoundings.Search(null, false);
IFeatureIndex2 pFtrInd = new FeatureIndexClass();
pFtrInd.FeatureClass = pDepthSoundings.FeatureClass;
pFtrInd.FeatureCursor = pDepthCursor;
pFtrInd.Index(null, pCombinedEnvelope);
IIndexQuery2 pIndQry = pFtrInd as IIndexQuery2;
int FtdID = 0;
double dDist2Ftr = 0;
pIndQry.NearestFeature(ppoint, out FtdID, out dDist2Ftr);
IFeature pCloseFeature = pDepthSoundings.FeatureClass.GetFeature(FtdID);
return Convert.ToDouble(pCloseFeature.get_Value(pCloseFeature.Fields.FindField(strDepthField)));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
return 0;
}
}
private double GetEndElevation(IFeatureLayer pDepthSoundings, IFeatureLayer pLineLayer, IFeature pLineFeature, string strDepthField)
{
try
{
IEnvelope pCombinedEnvelope = CombineExtents(pDepthSoundings.FeatureClass, pLineLayer.FeatureClass);
IPointCollection4 pPointColl = pLineFeature.Shape as IPointCollection4;
IPoint ppoint = new PointClass();
//Get the last point on the line
ppoint = pPointColl.get_Point(-1);
IFeatureCursor pDepthCursor = pDepthSoundings.Search(null, false);
IFeatureIndex2 pFtrInd = new FeatureIndexClass();
pFtrInd.FeatureClass = pDepthSoundings.FeatureClass;
pFtrInd.FeatureCursor = pDepthCursor;
pFtrInd.Index(null, pCombinedEnvelope);
IIndexQuery2 pIndQry = pFtrInd as IIndexQuery2;
int FtdID = 0;
double dDist2Ftr = 0;
pIndQry.NearestFeature(ppoint, out FtdID, out dDist2Ftr);
IFeature pCloseFeature = pDepthSoundings.FeatureClass.GetFeature(FtdID);
return Convert.ToDouble(pCloseFeature.get_Value(pCloseFeature.Fields.FindField(strDepthField)));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
return 0;
}
}
public void PlotShorePoints(string sShorelineLayer, double dblInterval)
{
IMxDocument pmxdoc = ArcMap.Document as IMxDocument;
IMap pmap = pmxdoc.FocusMap;
IFeatureLayer pShorelineLayer = FindLayer(pmap, sShorelineLayer) as IFeatureLayer;
IFeatureLayer pShorePoints = FindLayer(pmap, "ShorePoints") as IFeatureLayer;
//Set up the Outpoints cursor
IFeatureCursor pFCurOutPoints = pShorePoints.Search(null, false);
pFCurOutPoints = pShorePoints.FeatureClass.Insert(true);
////Set up the LineLayer Cursor
//IFeatureCursor pFCur = pShorelineLayer.Search(null, false);
//IFeature pLineFeature = pFCur.NextFeature();
IFeatureSelection pShoreLineSelection = pShorelineLayer as IFeatureSelection;
if (pShoreLineSelection.SelectionSet.Count == 0)
{
System.Windows.Forms.MessageBox.Show("You must first select at least one feature in the shoreline layer to proceed.");
pmap.DeleteLayer(pShorelineLayer);
pmap.DeleteLayer(pShorePoints);
return;
}
ICursor pShorelineCursor;
IFeatureCursor pSelShoreFeatCur;
pShoreLineSelection.SelectionSet.Search(null, false, out pShorelineCursor);
pSelShoreFeatCur = pShorelineCursor as IFeatureCursor;
IFeature pShoreLineFeature = pSelShoreFeatCur.NextFeature();
IFeatureBuffer pFBuffer = pShorePoints.FeatureClass.CreateFeatureBuffer();
ICurve pCurve;
IPoint ppoint;
double dblTotalDistanceCalculated = 0;
double dblDistance = 0;
while (pShoreLineFeature != null)
{
pCurve = pShoreLineFeature.Shape as ICurve;
pCurve.Project(pmap.SpatialReference);
ppoint = new PointClass();
while (dblTotalDistanceCalculated <= pCurve.Length)
{
//this code set the point on the line at a distance
pCurve.QueryPoint(0, dblDistance, false, ppoint);
dblDistance = dblDistance + dblInterval;
dblTotalDistanceCalculated = dblTotalDistanceCalculated + dblInterval;
//Insert the feature into the featureclass
pFBuffer.Shape = ppoint;
pFCurOutPoints.InsertFeature(pFBuffer);
}
dblDistance = 0;
dblTotalDistanceCalculated = 0;
pShoreLineFeature = pSelShoreFeatCur.NextFeature();
}
pFCurOutPoints.Flush();
pSelShoreFeatCur.Flush();
pmxdoc.ActiveView.Refresh();
}
public void MakeScratchLines()
{
try
{
IMxDocument pmxdoc = ArcMap.Document as IMxDocument;
IMap pmap = pmxdoc.FocusMap;
ILayer pConnectingLines = FindLayer(pmap, "ConnectingLines");
if (pConnectingLines != null)
{
pmap.DeleteLayer(pConnectingLines);
}
IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactoryClass();
IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2;
IWorkspace scratchWorkspace = workspaceFactory2.CreateNewScratchWorkspace();
IFeatureWorkspace pFeatWorkspace = scratchWorkspace as IFeatureWorkspace;
// This function creates a new feature class in a supplied feature dataset by building all of the
// fields from scratch. IFeatureClassDescription (or IObjectClassDescription if the table is
// created at the workspace level) can be used to get the required fields and are used to
// get the InstanceClassID and ExtensionClassID.
// Create new fields collection with the number of fields you plan to add. Must add at least two fields
// for a feature class: Object ID and Shape field.
IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
fieldsEdit.FieldCount_2 = 3;
// Create Object ID field.
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;
fieldEdit.Name_2 = "OBJECTID";
fieldEdit.AliasName_2 = "OBJECT ID";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.set_Field(0, fieldUserDefined);
// Create Shape field.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit)fieldUserDefined;
// Set up geometry definition for the Shape field.
// You do not have to set the spatial reference, as it is inherited from the feature dataset.
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline;
geometryDefEdit.SpatialReference_2 = pmap.SpatialReference;
// By setting the grid size to 0, you are allowing ArcGIS to determine the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size is 1,000. If in a file or ArcSDE geodatabase, the grid size
// is based on the initial loading or inserting of features.
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.set_GridSize(0, 0);
geometryDefEdit.HasM_2 = false;
geometryDefEdit.HasZ_2 = false;
// Set standard field properties.
fieldEdit.Name_2 = "SHAPE";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEdit.GeometryDef_2 = geometryDef;
fieldEdit.IsNullable_2 = true;
fieldEdit.Required_2 = true;
fieldsEdit.set_Field(1, fieldUserDefined);
// Create a field of type double to hold some information for the features.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit)fieldUserDefined;
fieldEdit.Name_2 = "ID";
fieldEdit.AliasName_2 = "ID";
fieldEdit.Editable_2 = true;
fieldEdit.IsNullable_2 = false;
fieldEdit.Precision_2 = 2;
fieldEdit.Scale_2 = 5;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldsEdit.set_Field(2, fieldUserDefined);
// Create a feature class description object to use for specifying the CLSID and EXTCLSID.
IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
IFeatureClass pLineFClass = pFeatWorkspace.CreateFeatureClass("ConnectingLines", fields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "SHAPE", "");
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pLineFClass;
pFeatureLayer.Name = "ConnectingLines";
pmxdoc.AddLayer(pFeatureLayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}
public void MakeScratchPoints()
{
try
{
IMxDocument pmxdoc = ArcMap.Document as IMxDocument;
IMap pmap = pmxdoc.FocusMap;
ILayer pShorePoints = FindLayer(pmap, "ShorePoints");
if (pShorePoints != null)
{
pmap.DeleteLayer(pShorePoints);
}
IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactoryClass();
IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2;
IWorkspace scratchWorkspace = workspaceFactory2.CreateNewScratchWorkspace();
IFeatureWorkspace pFeatWorkspace = scratchWorkspace as IFeatureWorkspace;
// This function creates a new feature class in a supplied feature dataset by building all of the
// fields from scratch. IFeatureClassDescription (or IObjectClassDescription if the table is
// created at the workspace level) can be used to get the required fields and are used to
// get the InstanceClassID and ExtensionClassID.
// Create new fields collection with the number of fields you plan to add. Must add at least two fields
// for a feature class: Object ID and Shape field.
IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
fieldsEdit.FieldCount_2 = 3;
// Create Object ID field.
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;
fieldEdit.Name_2 = "OBJECTID";
fieldEdit.AliasName_2 = "OBJECT ID";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.set_Field(0, fieldUserDefined);
// Create Shape field.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit)fieldUserDefined;
// Set up geometry definition for the Shape field.
// You do not have to set the spatial reference, as it is inherited from the feature dataset.
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = pmap.SpatialReference;
// By setting the grid size to 0, you are allowing ArcGIS to determine the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size is 1,000. If in a file or ArcSDE geodatabase, the grid size
// is based on the initial loading or inserting of features.
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.set_GridSize(0, 0);
geometryDefEdit.HasM_2 = false;
geometryDefEdit.HasZ_2 = false;
// Set standard field properties.
fieldEdit.Name_2 = "SHAPE";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEdit.GeometryDef_2 = geometryDef;
fieldEdit.IsNullable_2 = true;
fieldEdit.Required_2 = true;
fieldsEdit.set_Field(1, fieldUserDefined);
// Create a field of type double to hold some information for the features.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit)fieldUserDefined;
fieldEdit.Name_2 = "ID";
fieldEdit.AliasName_2 = "ID";
fieldEdit.Editable_2 = true;
fieldEdit.IsNullable_2 = false;
fieldEdit.Precision_2 = 2;
fieldEdit.Scale_2 = 5;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldsEdit.set_Field(2, fieldUserDefined);
// Create a feature class description object to use for specifying the CLSID and EXTCLSID.
IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
IFeatureClass pLineFClass = pFeatWorkspace.CreateFeatureClass("ShorePoints", fields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "SHAPE", "");
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pLineFClass;
pFeatureLayer.Name = "ShorePoints";
pmxdoc.AddLayer(pFeatureLayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}
private IFeature WalkLine(IFeatureLayer pDepthSoundings, IFeatureLayer pLineLayer, IFeature pLineFeature)
{
try
{
IEnvelope pCombinedEnvelope = CombineExtents(pDepthSoundings.FeatureClass, pLineLayer.FeatureClass);
IPointCollection4 pPointColl = pLineFeature.Shape as IPointCollection4;
IPoint ppoint = new PointClass();
IFeatureCursor pDepthCursor = pDepthSoundings.Search(null, false);
IFeatureIndex2 pFtrInd = new FeatureIndexClass();
pFtrInd.FeatureClass = pDepthSoundings.FeatureClass;
pFtrInd.FeatureCursor = pDepthCursor;
pFtrInd.Index(null, pCombinedEnvelope);
IIndexQuery2 pIndQry = pFtrInd as IIndexQuery2;
int FtdID = 0;
double dDist2Ftr = 0;
pIndQry.NearestFeature(ppoint, out FtdID, out dDist2Ftr);
IFeature pCloseFeature = pDepthSoundings.FeatureClass.GetFeature(FtdID);
return pCloseFeature;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
return null;
}
}
private IEnvelope CombineExtents(IFeatureClass pfclass1, IFeatureClass pfeatclass2)
{
try
{
IEnvelope pEnv = new EnvelopeClass();
IGeoDataset pGeoDS = pfclass1 as IGeoDataset;
pGeoDS.Extent.QueryEnvelope(pEnv);
pGeoDS = pfeatclass2 as IGeoDataset;
pEnv.Union(pGeoDS.Extent);
return pEnv;
}
catch
{
System.Windows.Forms.MessageBox.Show("Be sure that both layers have matching Spatial References.");
IEnvelope pEnv = new EnvelopeClass();
pEnv.SetEmpty();
return pEnv;
}
}
private bool FeatureIntersects(IFeatureLayer pShorelineLayer, IFeatureLayer pLineHolderLayer, IFeature pFeature)
{
bool bHasIntersection = false;
// Create the spatial filter and set its spatial constraints.
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = pFeature.ShapeCopy;
spatialFilter.GeometryField = pLineHolderLayer.FeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses ;
//Apply the filter t0 the feature layer
IFeatureSelection featureSelection = pShorelineLayer as IFeatureSelection;
featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
if (featureSelection.SelectionSet.Count > 0)
{
bHasIntersection = true;
}
featureSelection.Clear();
return bHasIntersection;
}
//private void AddField(IFeatureLayer pFeaturelayer, string strFieldName)
//{
// IMxDocument pmxdoc = ArcMap.Document as IMxDocument;
// IMap pmap = pmxdoc.FocusMap;
// IFeatureClass pFeatureClass = pFeaturelayer.FeatureClass;
// IFieldEdit pNewField = new FieldClass();
// pNewField.Name_2 = strFieldName;
// if (pFeatureClass.FindField(strFieldName) != -1)
// {
// //MessageBox.Show("Field: " + strFieldName + " already exists.");
// return;
// }
// pNewField.Type_2 = esriFieldType.esriFieldTypeDouble;
// pNewField.Length_2 = 50;
// pNewField.Editable_2 = true;
// pNewField.IsNullable_2 = true;
// pNewField.DefaultValue_2 = null;
// pFeatureClass.AddField(pNewField);
// pNewField = null;
// GC.Collect();
//}
private IProgressDialog2 ShowProgressIndicator(string strTitle, int iMax, int iStepValue)
{
IProgressDialogFactory pProgressDlgFact;
IProgressDialog2 pProgressDialog;
ITrackCancel pTrackCancel;
//'Show a progress dialog while we cycle through the features
pTrackCancel = new CancelTrackerClass();
pProgressDlgFact = new ProgressDialogFactoryClass();
pProgressDialog = (IProgressDialog2)pProgressDlgFact.Create(pTrackCancel, 0);
pProgressDialog.CancelEnabled = false;
pProgressDialog.Title = strTitle;
pProgressDialog.Animation = esriProgressAnimationTypes.esriProgressGlobe;
//'Set the properties of the Step Progressor
pStepProgressor = (IStepProgressor)pProgressDialog;
pStepProgressor.MinRange = 0;
pStepProgressor.MaxRange = iMax;
pStepProgressor.StepValue = iStepValue;
return pProgressDialog;
}
public ILayer FindLayer(IMap pmap, string layer)
{
for (int i = 0; i <= pmap.LayerCount - 1; i++)
{
if (pmap.get_Layer(i).Name == layer)
return pmap.get_Layer(i);
}
return null;
}
}
}