I am trying to randomly move a feature in an AGOL layer. The layer has a geometry property that is in web mercator and lat/long (WGS84) fields in the attribute table. Anyone know how I can update the location of the feature AND keep the lat/long fields in sync?
This is what I have tried:
latitude <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lat"</SPAN><SPAN class="punctuation token">)</SPAN>
longitude <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"long"</SPAN><SPAN class="punctuation token">)</SPAN>
geometry <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"geometry"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The latitude is {latitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The longitude is {longitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The geometry is {geometry}"</SPAN><SPAN class="punctuation token">)</SPAN>
feature<SPAN class="punctuation token">.</SPAN>set_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lat"</SPAN><SPAN class="punctuation token">,</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lat"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
feature<SPAN class="punctuation token">.</SPAN>set_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"long"</SPAN><SPAN class="punctuation token">,</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"long"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
latitude <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lat"</SPAN><SPAN class="punctuation token">)</SPAN>
longitude <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"long"</SPAN><SPAN class="punctuation token">)</SPAN>
geometry <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"geometry"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The latitude is {latitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The longitude is {longitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The geometry is {geometry}"</SPAN><SPAN class="punctuation token">)</SPAN>
geometry_dict <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>get_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"geometry"</SPAN><SPAN class="punctuation token">)</SPAN>
geometry_dict<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'x'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">)</SPAN>
geometry_dict<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'y'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">)</SPAN>
feature<SPAN class="punctuation token">.</SPAN>set_value<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"geometry"</SPAN><SPAN class="punctuation token">,</SPAN> geometry_dict<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The latitude is {latitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The longitude is {longitude}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"The geometry is {geometry}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="string token">'''
The code above results in the following output:
The latitude is 43.8849
The longitude is -100.3152
The geometry is {'x': -11167036.982825391, 'y': 5447647.436134641}
The latitude is 44.38470494318065
The longitude is -99.40659382285271
The geometry is {'x': -11167036.982825391, 'y': 5447647.436134641}
The latitude is 44.38470494318065
The longitude is -99.40659382285271
The geometry is {'x': -11167031.21161315, 'y': 5447647.819034746}
'''</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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>As you can see, changing the geometry does NOT change the Lat/Long properties. Do I have to do a conversion and update these fields separately? What would be the most efficient way to do this?