<SPAN class="comment token">#DC Road Data</SPAN>
<SPAN class="comment token">##"https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154/query?outFields=*&where=1%3D1"</SPAN>
<SPAN class="comment token">#gathering a list of field names into a comma separated list is key to copying over the feature service layer into a local geodatabase so that it can be utilized in the Clip analysis</SPAN>
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayer
<SPAN class="comment token">#identify the boundary layer you will use to query the data</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"O:\Professional Services\GIS Information\GIS\Boundary\NAMA_Boundary.gdb"</SPAN> <SPAN class="comment token">#enter gdb where your park boundary is located</SPAN>
boundary <SPAN class="operator token">=</SPAN> <SPAN class="string token">"NAMA_Boundary_ply"</SPAN>
<SPAN class="comment token"># Describe the projection of the feature service so that your extents of the boundary layer will be in the appropriate spatial reference</SPAN>
desc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154"</SPAN><SPAN class="punctuation token">)</SPAN>
boundaryproj1 <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"O:\Professional Services\GIS Information\GIS\Boundary\NAMA_Boundary.gdb\boundary_proj_1"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Project_management<SPAN class="punctuation token">(</SPAN>boundary<SPAN class="punctuation token">,</SPAN>boundaryproj1<SPAN class="punctuation token">,</SPAN> desc<SPAN class="punctuation token">.</SPAN>spatialReference<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#query parameters for all rows</SPAN>
base<SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154"</SPAN>
where <SPAN class="operator token">=</SPAN> <SPAN class="string token">'0=0'</SPAN>
token <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN>
outPath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb"</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="string token">"*"</SPAN>
<SPAN class="comment token">##find extent of your parks boundaries and assign it to variable "boundaryext"</SPAN>
descenv <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>boundaryproj1<SPAN class="punctuation token">)</SPAN>
descenvext <SPAN class="operator token">=</SPAN> descenv<SPAN class="punctuation token">.</SPAN>extent
boundaryext <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{},{},{},{}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>descenvext<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN> descenvext<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">,</SPAN> descenvext<SPAN class="punctuation token">.</SPAN>XMax<SPAN class="punctuation token">,</SPAN> descenvext<SPAN class="punctuation token">.</SPAN>YMax<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#################################start of code to try to get >2000 records</SPAN>
<SPAN class="comment token"># Get record extract limit</SPAN>
<SPAN class="keyword token">from</SPAN> urllib<SPAN class="punctuation token">.</SPAN>request <SPAN class="keyword token">import</SPAN> urlopen
base<SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154"</SPAN>
urlstring <SPAN class="operator token">=</SPAN> base <SPAN class="operator token">+</SPAN> <SPAN class="string token">"?f=json"</SPAN>
j <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>request<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>urlstring<SPAN class="punctuation token">)</SPAN>
js <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>j<SPAN class="punctuation token">)</SPAN>
maxrc <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>js<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"maxRecordCount"</SPAN><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">"Record extract limit: %s"</SPAN> <SPAN class="operator token">%</SPAN> maxrc<SPAN class="punctuation token">)</SPAN>
outdata <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb\DC_Roads_all"</SPAN>
<SPAN class="comment token"># Get object ids of features</SPAN>
where <SPAN class="operator token">=</SPAN> <SPAN class="string token">"1=1"</SPAN>
urlstring <SPAN class="operator token">=</SPAN> base <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/query?where={}&returnIdsOnly=true&f=json"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>where<SPAN class="punctuation token">)</SPAN>
j <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>request<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>urlstring<SPAN class="punctuation token">)</SPAN>
js <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>j<SPAN class="punctuation token">)</SPAN>
idfield <SPAN class="operator token">=</SPAN> js<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"objectIdFieldName"</SPAN><SPAN class="punctuation token">]</SPAN>
idlist <SPAN class="operator token">=</SPAN> js<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"objectIds"</SPAN><SPAN class="punctuation token">]</SPAN>
idlist<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
numrec <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>idlist<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Number of target records: %s"</SPAN> <SPAN class="operator token">%</SPAN> numrec<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Gather features</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Gathering records..."</SPAN><SPAN class="punctuation token">)</SPAN>
fs <SPAN class="operator token">=</SPAN> dict<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> numrec<SPAN class="punctuation token">,</SPAN> maxrc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
torec <SPAN class="operator token">=</SPAN> i <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>maxrc <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> torec <SPAN class="operator token">></SPAN> numrec<SPAN class="punctuation token">:</SPAN>
torec <SPAN class="operator token">=</SPAN> numrec <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN>
fromid <SPAN class="operator token">=</SPAN> idlist<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN>
toid <SPAN class="operator token">=</SPAN> idlist<SPAN class="punctuation token">[</SPAN>torec<SPAN class="punctuation token">]</SPAN>
where <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{} >= {} and {} <= {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>idfield<SPAN class="punctuation token">,</SPAN> fromid<SPAN class="punctuation token">,</SPAN> idfield<SPAN class="punctuation token">,</SPAN> toid<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">" {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>where<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
query <SPAN class="operator token">=</SPAN> <SPAN class="string token">"/query?where={}&outFields={}&returnGeometry=true&f=json&token={}&geometry={}&geometryType=esriGeometryEnvelope&returnExceededLimitFeatures=false"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>where<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">,</SPAN> boundaryext<SPAN class="punctuation token">)</SPAN>
urlstring <SPAN class="operator token">=</SPAN> base <SPAN class="operator token">+</SPAN> query
fs<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FeatureSet<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fs<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>urlstring<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Save features</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Saving features..."</SPAN><SPAN class="punctuation token">)</SPAN>
fslist <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> key<SPAN class="punctuation token">,</SPAN>value <SPAN class="keyword token">in</SPAN> fs<SPAN class="punctuation token">.</SPAN>items<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
fslist<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>value<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Merge_management<SPAN class="punctuation token">(</SPAN>fslist<SPAN class="punctuation token">,</SPAN> outdata<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Done!"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#################################end</SPAN>
<SPAN class="comment token">#create local copy of traffic layer and clip by the boundary layer</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN>r<SPAN class="string token">"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb"</SPAN>
where <SPAN class="operator token">=</SPAN> <SPAN class="string token">'1=1'</SPAN>
boundaryext <SPAN class="operator token">=</SPAN> <SPAN class="string token">"393932.62135367095,131367.2651843205,400957.7405239567,139578.9519640468"</SPAN>
query <SPAN class="operator token">=</SPAN> <SPAN class="string token">"/query?where={}&outFields={}&returnGeometry=true&f=json&token={}&geometry={}&geometryType=esriGeometryEnvelope&returnExceededLimitFeatures=false"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>where<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">,</SPAN> boundaryext<SPAN class="punctuation token">)</SPAN>
fsURL <SPAN class="operator token">=</SPAN> base <SPAN class="operator token">+</SPAN> query
fs <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FeatureSet<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fs<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>fsURL<SPAN class="punctuation token">)</SPAN>
outputName <SPAN class="operator token">=</SPAN> <SPAN class="string token">'DCRoads_extent'</SPAN>
outputPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>outPath<SPAN class="punctuation token">,</SPAN> outputName<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>fs<SPAN class="punctuation token">,</SPAN> outputPath<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>