Get start / end coordinates of selected line feature

191
2
Jump to solution
05-10-2021 04:33 PM
TorBørset
New Contributor II

Hi,

I haven't a lot of C# experience, so my question might be on the dim side. However I will ask anyway. 

What would be the easiest way to go about checking if the first element of selected features in the map is a Polyline, and if yes; get the first and last coordinate in the coordinate array? 

 

I have tried both Map.GetSelection and MapView.GetFeatures, however I am struggling to understand the best way to access the specific features and specifically its coord-array. 

 

All help would be dearly appreciated. 

0 Kudos
1 Solution

Accepted Solutions
SreenivasaRaoPigili
Occasional Contributor

Hi,

1. Select 'Map Tool' project template to create the add-in.

2. Changed the cursor as normal cursor 

in the constructor(),

public CaptureCoordinates()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Point; //.Point;
//UseSnapping = true;
SketchOutputMode = SketchOutputMode.Screen;
var systemCursor = System.Windows.Input.Cursors.Arrow; 
Cursor = systemCursor;
}

3. This will give us "onsketchcompleteasync" functionality. This will return the selected geometry as Geometry object.

    within this method, use

protected async override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
long targetID=0;
string strTargetLayerName = null;
await QueuedTask.Run(() =>
{
var sourceSelection = ActiveMapView.SelectFeatures(geometry, SelectionCombinationMethod.New);

foreach (KeyValuePair<BasicFeatureLayer, List<long>> eachSel in sourceSelection)
{
targetID = eachSel.Value[0];
strTargetLayerName = eachSel.Key.Name;
}

var inspTargetInspector = new Inspector();

var selectedTargetFeatures = ArcGIS.Desktop.Mapping.MapView.Active.Map.GetSelection();
inspTargetInspector.Load(selectedTargetFeatures.Keys.First(), targetID);
if (inspTargetInspector.Shape.GeometryType == GeometryType.Polyline)
{
var objpolyline = inspTargetInspector.Shape as Polyline;

//Below logic will change based on the logic. For now, we are just printing X, Y coordinates of each point in the polyline.
for (int i = 0; i < objpolyline.PointCount; i++)
{
MessageBox.Show("Points are : " + objpolyline.Points[i].X.ToString() + " " +
objpolyline.Points[i].Y.ToString());
}
}
});
return true;
}

 

After running application --> click on the add-in --> select the feature which we want to get coordinates.

 

I hope, This will help you to get started as a draft. I am sure we can tweak this code further. 

Thanks.

Sreeni.

View solution in original post

0 Kudos
2 Replies
SreenivasaRaoPigili
Occasional Contributor

Hi,

1. Select 'Map Tool' project template to create the add-in.

2. Changed the cursor as normal cursor 

in the constructor(),

public CaptureCoordinates()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Point; //.Point;
//UseSnapping = true;
SketchOutputMode = SketchOutputMode.Screen;
var systemCursor = System.Windows.Input.Cursors.Arrow; 
Cursor = systemCursor;
}

3. This will give us "onsketchcompleteasync" functionality. This will return the selected geometry as Geometry object.

    within this method, use

protected async override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
long targetID=0;
string strTargetLayerName = null;
await QueuedTask.Run(() =>
{
var sourceSelection = ActiveMapView.SelectFeatures(geometry, SelectionCombinationMethod.New);

foreach (KeyValuePair<BasicFeatureLayer, List<long>> eachSel in sourceSelection)
{
targetID = eachSel.Value[0];
strTargetLayerName = eachSel.Key.Name;
}

var inspTargetInspector = new Inspector();

var selectedTargetFeatures = ArcGIS.Desktop.Mapping.MapView.Active.Map.GetSelection();
inspTargetInspector.Load(selectedTargetFeatures.Keys.First(), targetID);
if (inspTargetInspector.Shape.GeometryType == GeometryType.Polyline)
{
var objpolyline = inspTargetInspector.Shape as Polyline;

//Below logic will change based on the logic. For now, we are just printing X, Y coordinates of each point in the polyline.
for (int i = 0; i < objpolyline.PointCount; i++)
{
MessageBox.Show("Points are : " + objpolyline.Points[i].X.ToString() + " " +
objpolyline.Points[i].Y.ToString());
}
}
});
return true;
}

 

After running application --> click on the add-in --> select the feature which we want to get coordinates.

 

I hope, This will help you to get started as a draft. I am sure we can tweak this code further. 

Thanks.

Sreeni.

View solution in original post

0 Kudos
TorBørset
New Contributor II

This was exactly what I was looking for.

I added some logic to check if a line feature was already selected, and if so just use the selected line feature into the code you posted. 

Thanks a lot!

0 Kudos