I have a single feature selected and need to get the attribute value (string) from "PS_CODE" field to a variable.
//--The answer was row.GetOriginalValue--
private async void SsPltbtn_Click(object sender, EventArgs e) { GVar.PltVal = null; //global variable to carry attribute field value.
if ((MapView.Active.Map.SelectionCount == 1) && (Pltsrch_txtbx.Text != null) && (Pltsrch_txtbx.Text != "")) { await ActMap(); //active map checker
Map map = MapView.Active.Map;
var QS_lyr = map.FindLayers("Quarter Section").FirstOrDefault() as FeatureLayer;
if (QS_lyr == null) { return; }
await QueuedTask.Run(() => { var QS_TD = QS_lyr.GetTable().GetDefinition(); //table definition of featurelayer
var Find_Fld = QS_TD.FindField("PS_CODE"); //find the search field index
var QS_lyr_sel = QS_lyr.GetSelection(); //This is NOT a table, but a Selection -- it works.
using (RowCursor rowCursor = QS_lyr_sel.Search()) //http://pro.arcgis.com/en/pro-app/sdk/api-reference/index.html#topic7003.html { while (rowCursor.MoveNext()) { using (Row row = rowCursor.Current) { string OrigVal = row.GetOriginalValue(Find_Fld) as string; GVar.PltVal = OrigVal; } } } }); try { var filepath = @C:\GISdata\Plat_Sheets\PDF\Sewer\ + GVar.PltVal + ".pdf"; System.Diagnostics.Process.Start(filepath); } catch (Exception) { MessageBox.Show(" The filepath is incorrect or file is missing.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); return; } } else { return; } }
Hi Mike, Sorry I missed the question but it sounds like Brad has you up and running.
Featurelayer.GetSelection() returns a selection class which is more or less a collection of oids for that layer.
From there you can get the list of oids and feed those into the inspector if you wanted to modify them all at once or iterate through the list with a for each loop and load them individually.
<SPAN class="comment token">//get selected features from featlayer and update them all at once</SPAN> Selection selSet <SPAN class="operator token">=</SPAN> featLayer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetSelection</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> IReadOnlyList<SPAN class="operator token"><</SPAN><SPAN class="keyword token">long</SPAN><SPAN class="operator token">></SPAN> selSetOids <SPAN class="operator token">=</SPAN> selSet<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetObjectIDs</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> op <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">EditOperation</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> op<SPAN class="punctuation token">.</SPAN>Name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Update parcel"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> insp <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Inspector</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> insp<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Load</SPAN><SPAN class="punctuation token">(</SPAN>featLayer<SPAN class="punctuation token">,</SPAN> selSetOids<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> insp<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Pin"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="number token">42</SPAN><SPAN class="punctuation token">;</SPAN> op<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Modify</SPAN><SPAN class="punctuation token">(</SPAN>insp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> op<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Execute</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
You could have also come in through the maps selection and filter the selection by layer. The map's selection is a simple dictionary so you can extract the oids by a layer key and feed those oids into the inspector as above.
<SPAN class="comment token">//get the oids for featlayer from the map selection</SPAN> Dictionary<SPAN class="operator token"><</SPAN>MapMember<SPAN class="punctuation token">,</SPAN>List<SPAN class="operator token"><</SPAN><SPAN class="keyword token">long</SPAN><SPAN class="operator token">></SPAN><SPAN class="operator token">></SPAN> mapSel <SPAN class="operator token">=</SPAN> MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetSelection</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> List<SPAN class="operator token"><</SPAN><SPAN class="keyword token">long</SPAN><SPAN class="operator token">></SPAN> featLayerOids <SPAN class="operator token">=</SPAN> mapSel<SPAN class="punctuation token">[</SPAN>featLayer<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Seeing more code examples cant hurt.
Thanks, Brad.
Care to share your Inspector-based solution that gets the attribute value of a selection from a specific featurelayer instead of Active.Map.GetSelection()?
I had the same question. I saw the RowCursor class, but thought there would be a simpler way the get an attributte for a selected feature of a specified layer. I tried both approaches but went with the Inspector. It I don't like to use RowCursors unless i'm manipulating tables.
Thanks for the snippets!
Thanks, Sean. I'll try this method when I have some time.
(Ln 7): ".First()" is not available to Featurelayer.GetSelection();. I need to work from selections on a specific feature layer, not MapView.Active.Map.GetSelection();.
Alternatively you can use an inspector.
QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// get the currently selected features in the map</SPAN> <SPAN class="keyword token">var</SPAN> selectedFeatures <SPAN class="operator token">=</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Mapping<SPAN class="punctuation token">.</SPAN>MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetSelection</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// get the first layer and its corresponding selected feature OIDs</SPAN> <SPAN class="keyword token">var</SPAN> firstSelectionSet <SPAN class="operator token">=</SPAN> selectedFeatures<SPAN class="punctuation token">.</SPAN><SPAN class="token function">First</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// create an instance of the inspector class</SPAN> <SPAN class="keyword token">var</SPAN> inspector <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Editing<SPAN class="punctuation token">.</SPAN>Attributes<SPAN class="punctuation token">.</SPAN>Inspector</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// load the selected features into the inspector using a list of object IDs</SPAN> inspector<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Load</SPAN><SPAN class="punctuation token">(</SPAN>firstSelectionSet<SPAN class="punctuation token">.</SPAN>Key<SPAN class="punctuation token">,</SPAN> firstSelectionSet<SPAN class="punctuation token">.</SPAN>Value<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//get the value of</SPAN> <SPAN class="keyword token">var</SPAN> pscode <SPAN class="operator token">=</SPAN> inspector<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"ps_code"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.