I have a multidimensional mosaic layer which contains 3 raster datasets. I am writing a tool that reads one of the raster datasets into a numpy array by applying a definition query on the Variable associated with the raster. However, I can't seem to find a way to successfully accomplish this.
Regardless of the definition query I apply, the output ALWAYS uses the same Variable.
Example: I have Variables SnowA, SnowB, SnowC
If I set the definition query to Variable = SnowA, the geoprocessing results will reflect those of SnowC
If I set the definition query to Variable = SnowB, the geoprocessing results will reflect those of SnowC
If I set the definition query to Variable = SnowC, the geoprocessing results will reflect those of SnowC
Any help would be greatly appreciated!
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> re
now <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%Y%m%d_%H%M"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Define input Variables</SPAN>
ptFLS<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Feature layers </SPAN>
mosLyr<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Mosaic layer</SPAN>
minThresh<SPAN class="operator token">=</SPAN>int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Integer representing inches</SPAN>
outLoc<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Out Folder</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># Other variables</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><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
symbologyLyr <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"\\\\xyz\\xyz\\xyz\\xyz\\xyz\\xyz\\HighlightedPoints_toolDependency.lyrx"</SPAN>
saveName <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>mosLyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>nameString<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
groupName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>mosLyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>nameString<SPAN class="operator token">+</SPAN><SPAN class="string token">"\Image"</SPAN>
mosWhere <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> aprx<SPAN class="punctuation token">.</SPAN>activeMap<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LONGNAME"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">and</SPAN> lyr<SPAN class="punctuation token">.</SPAN>longName <SPAN class="operator token">==</SPAN> groupName<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>
procTemp <SPAN class="operator token">=</SPAN> re<SPAN class="punctuation token">.</SPAN>findall<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"'([^']*)'"</SPAN><SPAN class="punctuation token">,</SPAN> mosWhere<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Get raster information</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" > Reading raster properties..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeMosaicLayer_management <SPAN class="punctuation token">(</SPAN>in_mosaic_dataset<SPAN class="operator token">=</SPAN>mosLyr<SPAN class="punctuation token">,</SPAN> out_mosaic_layer<SPAN class="operator token">=</SPAN><SPAN class="string token">"mos_Lyr"</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>mosWhere<SPAN class="punctuation token">,</SPAN> processing_template<SPAN class="operator token">=</SPAN>procTemp<SPAN class="punctuation token">)</SPAN>
rast <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"mos_Lyr"</SPAN><SPAN class="punctuation token">)</SPAN>
desc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>rast<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get upper left corner of the raster and spatial reference</SPAN>
ulx <SPAN class="operator token">=</SPAN> desc<SPAN class="punctuation token">.</SPAN>Extent<SPAN class="punctuation token">.</SPAN>XMin
uly <SPAN class="operator token">=</SPAN> desc<SPAN class="punctuation token">.</SPAN>Extent<SPAN class="punctuation token">.</SPAN>YMax
sr <SPAN class="operator token">=</SPAN> desc<SPAN class="punctuation token">.</SPAN>spatialReference
<SPAN class="comment token"># Convert raster to numpy array</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" > Creating raster array..."</SPAN><SPAN class="punctuation token">)</SPAN>
mosLyr_npArr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray <SPAN class="punctuation token">(</SPAN>in_raster<SPAN class="operator token">=</SPAN>rast<SPAN class="punctuation token">)</SPAN>
xMax<SPAN class="punctuation token">,</SPAN> yMax <SPAN class="operator token">=</SPAN> mosLyr_npArr<SPAN class="punctuation token">.</SPAN>shape
<SPAN class="comment token"># Calculate row and col numpy array position based on how far the point is from the upper left corner and pass result into numpy array to extract</SPAN>
<SPAN class="keyword token">for</SPAN> ptFL <SPAN class="keyword token">in</SPAN> ptFLS<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" > Reading {} data..."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ptFL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>FeatureClassToFeatureClass_conversion<SPAN class="punctuation token">(</SPAN>in_features<SPAN class="operator token">=</SPAN>ptFL<SPAN class="punctuation token">,</SPAN> out_path<SPAN class="operator token">=</SPAN><SPAN class="string token">"memory"</SPAN><SPAN class="punctuation token">,</SPAN> out_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"ptFC"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>r<SPAN class="string token">"memory\\ptFC"</SPAN><SPAN class="punctuation token">,</SPAN> field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"inUnits"</SPAN><SPAN class="punctuation token">,</SPAN> field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"FLOAT"</SPAN><SPAN class="punctuation token">,</SPAN> field_scale<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" >> Determining impact at client locations..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"memory\\ptFC"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"SHAPE@"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"inUnits"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cur<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cur<SPAN class="punctuation token">:</SPAN>
pnt <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>projectAs<SPAN class="punctuation token">(</SPAN>sr<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Assumes every point falls to the right and below the uperleft corner ... some points fall beyond footprint of raster i.e. Hawaii, Carribean, eronious misplaced points</SPAN>
deltaX <SPAN class="operator token">=</SPAN> pnt<SPAN class="punctuation token">.</SPAN>centroid<SPAN class="punctuation token">.</SPAN>X <SPAN class="operator token">-</SPAN> ulx
deltaY <SPAN class="operator token">=</SPAN> uly <SPAN class="operator token">-</SPAN> pnt<SPAN class="punctuation token">.</SPAN>centroid<SPAN class="punctuation token">.</SPAN>Y
arow <SPAN class="operator token">=</SPAN> round<SPAN class="punctuation token">(</SPAN>deltaY<SPAN class="operator token">/</SPAN>rast<SPAN class="punctuation token">.</SPAN>meanCellHeight<SPAN class="punctuation token">)</SPAN>
acol <SPAN class="operator token">=</SPAN> round<SPAN class="punctuation token">(</SPAN>deltaX<SPAN class="operator token">/</SPAN>rast<SPAN class="punctuation token">.</SPAN>meanCellWidth<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> arow <SPAN class="operator token"><</SPAN> xMax <SPAN class="operator token">and</SPAN> acol <SPAN class="operator token"><</SPAN> yMax<SPAN class="punctuation token">:</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> mosLyr_npArr<SPAN class="punctuation token">[</SPAN>arow<SPAN class="punctuation token">,</SPAN>acol<SPAN class="punctuation token">]</SPAN>
cur<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" >> Exporting impacted locations to output folder..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeTableView_management <SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>r<SPAN class="string token">"memory\\ptFC"</SPAN><SPAN class="punctuation token">,</SPAN> out_view<SPAN class="operator token">=</SPAN>r<SPAN class="string token">"memory\\impactLocTBL"</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN><SPAN class="string token">"{} > {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"inUnits"</SPAN><SPAN class="punctuation token">,</SPAN> minThresh<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>TableToExcel_conversion <SPAN class="punctuation token">(</SPAN>Input_Table<SPAN class="operator token">=</SPAN>r<SPAN class="string token">"memory\\impactLocTBL"</SPAN><SPAN class="punctuation token">,</SPAN> Output_Excel_File<SPAN class="operator token">=</SPAN>outLoc<SPAN class="operator token">+</SPAN><SPAN class="string token">"\SnowStormReport_{}_{}_{}.xls"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>saveName<SPAN class="punctuation token">,</SPAN>ptFL<SPAN class="punctuation token">,</SPAN>now<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>in_features<SPAN class="operator token">=</SPAN>r<SPAN class="string token">"memory\\ptFC"</SPAN><SPAN class="punctuation token">,</SPAN> out_layer<SPAN class="operator token">=</SPAN><SPAN class="string token">"Impacted{}_Layer"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ptFL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN><SPAN class="string token">"{} > {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"inUnits"</SPAN><SPAN class="punctuation token">,</SPAN> minThresh<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#arcpy.ApplySymbologyFromLayer_management (in_layer="Impacted{}_Layer".format(ptFL), in_symbology_layer=symbologyLyr)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SaveToLayerFile_management<SPAN class="punctuation token">(</SPAN>in_layer<SPAN class="operator token">=</SPAN><SPAN class="string token">"Impacted{}_Layer"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ptFL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> out_layer<SPAN class="operator token">=</SPAN>outLoc<SPAN class="operator token">+</SPAN><SPAN class="string token">"\SnowStormReport_{}_{}_{}.lyrx"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>saveName<SPAN class="punctuation token">,</SPAN>ptFL<SPAN class="punctuation token">,</SPAN>now<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> version<SPAN class="operator token">=</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Impacted{}_Layer"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ptFL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"C:\Users\mde\Desktop\Projects\scratchSpace.gdb\rasterCalResult_{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>now<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN>in_data<SPAN class="operator token">=</SPAN><SPAN class="string token">"memory"</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>