Using Python, I have an api with security incidents that I want to populate into a feature service layer. I've achieved this successfully using Add Features via the API endpoint (described in this documentation). However the original point feature class is stored in SDE which has a defined coordinate system of "WGS_1984_Web_Mercator_Auxiliary_Sphere".
Therefore my points are being added with the wrong projection (because I'm using just geographic coordinates).
Are there any programmatic solutions to change the geometry to a matching projection before adding the features?
My (slightly modified) working code is included. Basically I have two functions - the first one grabs the API data and then returns a list of reformatted features. Then, the next function takes that list as input and adds each feature to the feature service layer.
Any help is much appreciated.
Thanks.
<SPAN class="keyword token">import</SPAN> urllib
<SPAN class="keyword token">import</SPAN> requests
<SPAN class="keyword token">import</SPAN> json<SPAN class="punctuation token">,</SPAN> time
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">import</SPAN> unicodedata
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getFeaturesReformatPayload</SPAN><SPAN class="punctuation token">(</SPAN>api_url<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">''' Requests API, returns list with reformatted payload for adding features.
'''</SPAN>
<SPAN class="comment token"># get planet risk data</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Getting Planet Risk data...'</SPAN><SPAN class="punctuation token">)</SPAN>
response <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>api_url<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> response<SPAN class="punctuation token">.</SPAN>status_code <SPAN class="operator token">==</SPAN> <SPAN class="number token">200</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Success"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"There was an error"</SPAN><SPAN class="punctuation token">)</SPAN>
payload <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">.</SPAN>json<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># reformat the payload to be added to feature layer</SPAN>
payload_reformatted <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN> x <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>payload<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'result'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'resourceList'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'resource'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># convert datetime in time cols - 'updated', 'created'</SPAN>
created_time <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'created'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># shortened epoch to seconds</SPAN>
<SPAN class="comment token"># print('Length of created timestamp: {}'.format(len(str(created_time))))</SPAN>
updated_time <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'updated'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># print('Length of updated timestamp: {}'.format(len(str(updated_time))))</SPAN>
<SPAN class="comment token"># convert unicode to string</SPAN>
created_time_str <SPAN class="operator token">=</SPAN> unicodedata<SPAN class="punctuation token">.</SPAN>normalize<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'NFKD'</SPAN><SPAN class="punctuation token">,</SPAN> created_time<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'ascii'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ignore'</SPAN><SPAN class="punctuation token">)</SPAN>
updated_time_str <SPAN class="operator token">=</SPAN> unicodedata<SPAN class="punctuation token">.</SPAN>normalize<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'NFKD'</SPAN><SPAN class="punctuation token">,</SPAN> updated_time<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'ascii'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ignore'</SPAN><SPAN class="punctuation token">)</SPAN>
created_formatted <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>fromtimestamp<SPAN class="punctuation token">(</SPAN>float<SPAN class="punctuation token">(</SPAN>created_time_str<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
updated_formatted <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>fromtimestamp<SPAN class="punctuation token">(</SPAN>float<SPAN class="punctuation token">(</SPAN>updated_time_str<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># copy dict then update formatted time in the dict</SPAN>
copy_dict <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">.</SPAN>copy<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
copy_dict<SPAN class="punctuation token">.</SPAN>update<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">'created'</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>created_formatted<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># removing timestamp - just date</SPAN>
copy_dict<SPAN class="punctuation token">.</SPAN>update<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">'updated'</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>updated_formatted<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># print('Feature #: {} , was created on {}, and was last updated on {}'.format(i, created_formatted, updated_formatted))</SPAN>
<SPAN class="comment token"># create geometry - delete unwanted fields - append to reformatted</SPAN>
geometry_dict <SPAN class="operator token">=</SPAN> dict<SPAN class="punctuation token">(</SPAN>y <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'lat'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> x <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'lng'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
deleteFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'lat'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'lng'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'resourcetype'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'id'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> deleteFields<SPAN class="punctuation token">:</SPAN>
copy_dict<SPAN class="punctuation token">.</SPAN>pop<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN>
payload_out <SPAN class="operator token">=</SPAN> dict<SPAN class="punctuation token">(</SPAN>attributes <SPAN class="operator token">=</SPAN> copy_dict<SPAN class="punctuation token">,</SPAN> geometry <SPAN class="operator token">=</SPAN> geometry_dict<SPAN class="punctuation token">)</SPAN>
payload_reformatted<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>payload_out<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Total Number of Incidents: {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>payload_reformatted<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> payload_reformatted
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">addIncidentsToFSL</SPAN><SPAN class="punctuation token">(</SPAN>input_payload<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">''' Adds input payload to the feature service layer via proxy.
'''</SPAN>
<SPAN class="comment token"># proxy bypasses authentication </SPAN>
proxy_path <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://servername.example.net/DotNet/proxy.ashx?"</SPAN>
feature_layer_endpoint <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://servername.example.com/arcgis/rest/services/National/examplePublishedMXD/FeatureServer/3/addFeatures"</SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>proxy_path<SPAN class="punctuation token">,</SPAN>feature_layer_endpoint <SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># print(url)</SPAN>
<SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN> k <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>input_payload<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'\nAdding Incident to Feature Service Layer: {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
payload_body <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'json'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'features'</SPAN><SPAN class="punctuation token">:</SPAN> json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>input_payload<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># print(payload_body)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'...'</SPAN><SPAN class="punctuation token">)</SPAN>
r <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">,</SPAN> data <SPAN class="operator token">=</SPAN> payload_body<SPAN class="punctuation token">,</SPAN> verify <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># print response</SPAN>
<SPAN class="keyword token">if</SPAN> r<SPAN class="punctuation token">.</SPAN>status_code <SPAN class="operator token">==</SPAN> <SPAN class="number token">200</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Success'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Failed'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># wait 3 seconds</SPAN>
time<SPAN class="punctuation token">.</SPAN>sleep<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># define api, get features, reformat, add to feature service layer</SPAN>
example_api <SPAN class="operator token">=</SPAN> <SPAN class="string token">'string_with_apiURL'</SPAN>
reformatted_payload <SPAN class="operator token">=</SPAN> getFeaturesReformatPayload<SPAN class="punctuation token">(</SPAN>example_api<SPAN class="punctuation token">)</SPAN>
addIncidentsToFSL<SPAN class="punctuation token">(</SPAN>reformatted_payload<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'All Features Added from API to Feature Service Layer'</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></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><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>