Hi Python experts, I am writing my first python script and would need some advice on how to read and manipulate a list.
On my network drive, I have several folders named after Project and they belong to one or more Years.
I have a FGDB feature class in which I have 2 desired columns FLYING, FILMNAME representing Year, Project respectively.
For e.g.,
| Year | Project |
|---|
| 1960 | A |
| 1972 | B |
| 1980 | C |
| 1972 | D |
| 1980 | E |
| 1980 | F |
| 1960 | G |
| 1972 | H |
| 1972 | I |
| 1960 | J |
I would like to read all the rows and create one mosaic data set for each Year and add the Project into each Year's mosaic .
e.g, 1960 will have A;G;J 1972 will have B;D;H
To do this, I would like to get two lists
1. a list of unique Years - to create empty mosaics
2. a list of Projects per Year - to append airphoto folders to each mosaic
Below is the code that does the first list. Any pointers on how to progress further would be much helpful. Thanks in advance.
<SPAN class="comment token">#import modules</SPAN>
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime
<SPAN class="comment token">#assign variables</SPAN>
in_fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Working\airphoto.gdb\airphoto_index'</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'FLYING'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'FILMNAME'</SPAN><SPAN class="punctuation token">]</SPAN>
gdb <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:\Working\images.gdb'</SPAN>
sourceDir <SPAN class="operator token">=</SPAN> <SPAN class="string token">"O:\\airphoto\\75dpi\\"</SPAN>
in_mosaic <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:\Working\images.gdb\test'</SPAN>
prjfile <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3857</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#create lists</SPAN>
Years <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
Films <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
YearFilmString <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
FilmList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#read feature class columns and iterate through rows</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN>fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#extract year from datetime field</SPAN>
yearString <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
Years<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>yearString<SPAN class="punctuation token">)</SPAN>
filmString <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
Films<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>filmString<SPAN class="punctuation token">)</SPAN>
uniqYears <SPAN class="operator token">=</SPAN> sorted<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>Years<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
uniqYearsList <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>uniqYears<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#for each year, create one mosaic</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> uniqYearsList<SPAN class="punctuation token">:</SPAN>
x <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\Working\images.gdb\T"</SPAN><SPAN class="operator token">+</SPAN>i
<SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Exists<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"mosaic already exists, skipping..."</SPAN>
<SPAN class="keyword token">continue</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CreateMosaicDataset_management<SPAN class="punctuation token">(</SPAN>gdb<SPAN class="punctuation token">,</SPAN>i<SPAN class="punctuation token">,</SPAN>prjfile<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><SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> i<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"mosaic created"</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>