From 1fd28641b3f0ff8fc7c6f34801b6078a7514a324 Mon Sep 17 00:00:00 2001 From: Daniel Butt <47188958+Daniel-Butt@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:18:01 -0400 Subject: [PATCH] Prototype 1 --- MESH_MAP/FeatureClassBuilder.cs | 229 +++++++++++++ MESH_MAP/LengthReqsBox.xaml | 20 +- MESH_MAP/MESH_MAP.csproj | 17 + MESH_MAP/MESH_MAP.sln | 8 +- MESH_MAP/Main.xaml | 51 +-- MESH_MAP/Main.xaml.cs | 39 ++- MESH_MAP/MeshAnalysis.cs | 495 ++++++++++++++++++++++++++++ MESH_MAP/Mesh_Analysis.cs | 17 - MESH_MAP/RasterSelectionBox.xaml.cs | 14 + MESH_MAP/Swath.cs | 75 +++++ MESH_MAP/TextBoxStreamWriter.cs | 170 ++++++++++ MESH_MAP/Utils.cs | 75 +++++ python/graphTest.png | Bin 0 -> 382 bytes python/main.py | 9 +- 14 files changed, 1160 insertions(+), 59 deletions(-) create mode 100644 MESH_MAP/FeatureClassBuilder.cs create mode 100644 MESH_MAP/MeshAnalysis.cs delete mode 100644 MESH_MAP/Mesh_Analysis.cs create mode 100644 MESH_MAP/Swath.cs create mode 100644 MESH_MAP/TextBoxStreamWriter.cs create mode 100644 python/graphTest.png diff --git a/MESH_MAP/FeatureClassBuilder.cs b/MESH_MAP/FeatureClassBuilder.cs new file mode 100644 index 0000000..cf8d692 --- /dev/null +++ b/MESH_MAP/FeatureClassBuilder.cs @@ -0,0 +1,229 @@ +using ArcGIS.Desktop.Core.Geoprocessing; +using ArcGIS.Desktop.Core; +using ArcGIS.Desktop.Framework.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ArcGIS.Core.Geometry; +using ArcGIS.Core.Data; +using ArcGIS.Desktop.Mapping; +using System.Threading; +using System.Windows; +using ArcGIS.Desktop.Editing; +using ArcGIS.Core.Data.UtilityNetwork.Trace; +using ArcGIS.Core.Internal.Geometry; +using ArcGIS.Core.Data.Raster; + +namespace MESH_MAP +{ + internal class FeatureClassBuilder + { + public static async Task CreateFcWithAttributes(string fcName, FeatureClassType fcType, List attributes, int spatialReference=3857) + { + // Create feature class T1 + await CreateFeatureClass(fcName, fcType, spatialReference); + // double check to see if the layer was added to the map + var fcLayer = MapView.Active.Map.GetLayersAsFlattenedList().Where((l) => l.Name == fcName).FirstOrDefault() as FeatureLayer; + if (fcLayer == null) + { + MessageBox.Show($@"Unable to find {fcName} in the active map"); + return null; + } + + var dataSource = await GetDataSource(fcLayer); + + foreach (var attribute in attributes) + { + await ExecuteAddFieldToolAsync(fcLayer, new KeyValuePair(attribute.Name, attribute.Description), attribute.Type.ToString(), 50); + } + + return fcLayer; + } + + public enum AttributeType + { + TEXT, + DOUBLE, + LONG + } + + public struct Attribute + { + public string Name { get; set; } + public string Description { get; set; } + public AttributeType Type { get; set; } + + public Attribute(string name, string description, AttributeType type) + { + Name = name; + Description = description; + Type = type; + } + } + + public enum FeatureClassType + { + POINT, + MULTIPOINT, + POLYLINE, + POLYGON + } + + public static async Task CreatePolygon (OpenCvSharp.Point[] pts, RasterLayer rasterLayer) + { + Polygon p = null; + List mapPts = []; + + await QueuedTask.Run(() => + { + Raster raster = rasterLayer.GetRaster(); + + foreach (var pt in pts) + { + var coordPt = raster.PixelToMap(pt.X, pt.Y); + mapPts.Add(MapPointBuilderEx.CreateMapPoint(coordPt.Item1, coordPt.Item2)); + } + + p = PolygonBuilder.CreatePolygon(mapPts.ToArray()); + }); + + return p; + } + + public static async Task AddFeatures(string featureclassName, List> features) + { + var layer = MapView.Active.Map.GetLayersAsFlattenedList().Where((l) => l.Name == featureclassName).FirstOrDefault(); + + await QueuedTask.Run(() => + { + var editOp = new EditOperation + { + Name = "edit operation" + }; + + foreach (var feature in features) + { + editOp.Create(layer, feature); + } + var result = editOp.Execute(); + + if (result != true || editOp.IsSucceeded != true) + { + MessageBox.Show("Error: Could not edit feature layer " + featureclassName); + } + }); + + await Project.Current.SaveEditsAsync(); + } + + + public static async Task CreateFeatureClass(string featureclassName, FeatureClassType featureclassType, int spatialReference=3857) + { + List arguments = new List + { + // store the results in the default geodatabase + CoreModule.CurrentProject.DefaultGeodatabasePath, + // name of the feature class + featureclassName, + // type of geometry + featureclassType.ToString(), + // no template + "", + // no z values + "DISABLED", + // no m values + "DISABLED" + }; + await QueuedTask.Run(() => + { + // spatial reference + arguments.Add(SpatialReferenceBuilder.CreateSpatialReference(spatialReference)); + }); + IGPResult result = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(arguments.ToArray())); + } + + public static async Task GetDataSource(BasicFeatureLayer theLayer) + { + try + { + return await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => + { + var inTable = theLayer.Name; + var table = theLayer.GetTable(); + var dataStore = table.GetDatastore(); + var workspaceNameDef = dataStore.GetConnectionString(); + var workspaceName = workspaceNameDef.Split('=')[1]; + var fullSpec = System.IO.Path.Combine(workspaceName, inTable); + return fullSpec; + }); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + return string.Empty; + } + } + + public static async Task ExecuteAddFieldToolAsync( + BasicFeatureLayer theLayer, + KeyValuePair field, + string fieldType, int? fieldLength = null, + bool isNullable = true) + { + return await QueuedTask.Run(() => + { + try + { + var inTable = theLayer.Name; + var table = theLayer.GetTable(); + var dataStore = table.GetDatastore(); + var workspaceNameDef = dataStore.GetConnectionString(); + var workspaceName = workspaceNameDef.Split('=')[1]; + + var fullSpec = System.IO.Path.Combine(workspaceName, inTable); + System.Diagnostics.Debug.WriteLine($@"Add {field.Key} from {fullSpec}"); + + var parameters = Geoprocessing.MakeValueArray(fullSpec, field.Key, fieldType.ToUpper(), null, null, fieldLength, field.Value, isNullable ? "NULABLE" : "NON_NULLABLE"); + var cts = new CancellationTokenSource(); + var results = Geoprocessing.ExecuteToolAsync("management.AddField", parameters, null, cts.Token, + (eventName, o) => + { + System.Diagnostics.Debug.WriteLine($@"GP event: {eventName}"); + }); + var isFailure = results.Result.IsFailed || results.Result.IsCanceled; + return !isFailure ? "Failed" : "Ok"; + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + return ex.ToString(); + } + }); + } + + public static Task FeatureClassExistsAsync(string fcName) + { + return QueuedTask.Run(() => + { + try + { + using (Geodatabase projectGDB = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(Project.Current.DefaultGeodatabasePath)))) + { + using (FeatureClass fc = projectGDB.OpenDataset(fcName)) + { + return fc != null; + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($@"FeatureClassExists Error: {ex.ToString()}"); + return false; + } + }); + } + + } +} diff --git a/MESH_MAP/LengthReqsBox.xaml b/MESH_MAP/LengthReqsBox.xaml index 686a71c..bc73978 100644 --- a/MESH_MAP/LengthReqsBox.xaml +++ b/MESH_MAP/LengthReqsBox.xaml @@ -5,17 +5,17 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MESH_MAP" mc:Ignorable="d" - d:DesignHeight="150" d:DesignWidth="520"> + d:DesignHeight="150" d:DesignWidth="535"> - - -