Select to view content in your preferred language

How do I measure the line length of several elements?

2929
7
Jump to solution
07-22-2021 12:28 AM
DavidMrázek
Frequent Contributor

Hello, I try to programmatically measure the length of several lines that are not in the same layer at the same time. After that I want to write the individual distances in the form (there is no problem with that ...). Unfortunately, I don't know how to write a method that would measure individual lines for me.

 

What I have tried:

 

namespace ProAppModule1
{


internal class Delka : Button
{

protected override void OnClick()
{
Form1 formular = new Form1();
double delka = new double();
var lyrs = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
MapView.Active.SelectLayers(lyrs.ToList());
var openTableBtnCmd = FrameworkApplication.GetPlugInWrapper("esri_editing_table_openTablePaneButton") as ICommand;
if (openTableBtnCmd != null)
{
openTableBtnCmd.Execute(null);
}


formular.richTextBox1.Text = delka.ToString();
formular.Show();
}
}
}

 

Sorry for the wrong explanation, I'll try better.

I described the individual things in the picture, and now my idea, I would like the individual lines to be measured after pressing the button and then write their lengths in the form (I already have it ready, this is not a problem).

 

Perhaps it is understandable and understandable.


I will be happy for any advice or help.

Thank you.

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I attached a sample that is using a dockpane to show the selected [line] rows and the respective length.  Note that the length was computed using a area specific projection (in my case Hawaii), so you might want to change this to fit your needs.  When you click on a line in the grid the corresponding feature will flash on the map.  

Wolf_0-1627321560965.png

 

 

View solution in original post

7 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I don't quite understand what your workflow is and why you are opening the attribute table.  Can you show a screenshot of what you're trying to show?  Are you computing something based on an attribute column's value?

0 Kudos
DavidMrázek
Frequent Contributor

I tried to adjust it so maybe it's better.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I think I see your desired workflow now, i will try to find some matching samples over the weekend.

0 Kudos
DavidMrázek
Frequent Contributor

Hello Wolf,

I hate to bother you, but haven't found any examples or anything about my project?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I attached a sample that is using a dockpane to show the selected [line] rows and the respective length.  Note that the length was computed using a area specific projection (in my case Hawaii), so you might want to change this to fit your needs.  When you click on a line in the grid the corresponding feature will flash on the map.  

Wolf_0-1627321560965.png

 

 

DavidMrázek
Frequent Contributor

Thank you so much, if you had a problem I could still call?

0 Kudos
DavidMrázek
Frequent Contributor

Hello,

I tried to transform the code into its own form, but I have a problem with lambda:

namespace ShowLineLength
{
internal class ShowSelectedLineLengths : Button
{
ProAppModule2.Form1 formular = new ProAppModule2.Form1();

protected override async void OnClick()
{
try
{
var result = await QueuedTask.Run<(ProAppModule2.Form1 formular ,double Total)>(() =>
{
double dTotalLen = 0.0;
// retrieve the currently selected features in the map view
var selectedFeatureSets = MapView.Active.Map.GetSelection();
var mapa = SpatialReferenceBuilder.CreateSpatialReference(3759);
// use this projection to get the most accurate length
if (MapView.Active != null)
{
// Get the active map view.
var mapView = MapView.Active;
// Get the list of selected layers.
IReadOnlyList<Layer> selectedLayerList = MapView.Active.GetSelectedLayers();
if (selectedLayerList.Count > 0)


// for each of the map members that is a line feature we put a row
// into our dataTable
foreach (var selectedFeatureSet in selectedFeatureSets)
{
var featLayer = selectedFeatureSet.Key as BasicFeatureLayer;
if (featLayer == null) continue;
if (!(featLayer.ShapeType == esriGeometryType.esriGeometryPolyline)) continue;
// use the selection to get all records
using (var rowCursor = featLayer.Search(new QueryFilter() { ObjectIDs = selectedFeatureSet.Value }))
{
while (rowCursor.MoveNext())
{
using (var feature = rowCursor.Current as Feature)
{
var theLine = feature.GetShape();
if (theLine != null)
{
var projectedLine = GeometryEngine.Instance.Project(theLine, mapa) as Polyline;

}
}
}
}
}


}
});
formular.richTextBox1.Text = result.ToString();
formular.Show();

}
catch (Exception ex)
{
MessageBox.Show($@"Exception in ShowSelectedLineLengths: {ex}");
}
}


}
}

 

 

0 Kudos