I'm converting Python standalone scripts from Desktop (10.6.1) to Pro (2.3.2). I can add a raster to an open Pro project file interactively by right clicking on the raster in the Catalog and choosing "Add to Current Map". I would like to do this in a script and have tried two ways (one commented out below):
inRas <SPAN class="operator token">=</SPAN> <SPAN class="string token">'<projectDir>/Risks/riskv0'</SPAN>
outAPRX <SPAN class="operator token">=</SPAN> <SPAN class="string token">'<projectDir>/Reports/riskv0/riskv0.aprx'</SPAN>
templateFile <SPAN class="operator token">=</SPAN> <SPAN class="string token">'<projectDir>/MapTemplates/risksTemplate/risksTemplate.aprx'</SPAN>
aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN>templateFile<SPAN class="punctuation token">)</SPAN>
map <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
map<SPAN class="punctuation token">.</SPAN>addDataFromPath<SPAN class="punctuation token">(</SPAN>inRas<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#refLyr = map.listLayers()[1]</SPAN>
<SPAN class="comment token">#map.insertLayer(refLyr, inRas, "AFTER")</SPAN>
aprx<SPAN class="punctuation token">.</SPAN>saveACopy<SPAN class="punctuation token">(</SPAN>outAPRX<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> aprx<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>
When I use addDataFromPath, I receive a RuntimeError for line 6 with no further information.
The listLayers/insertLayer approach results in a ValueErr at line 8 for the inRas file.
I would be grateful for any advice on adding a raster to a map in Pro.