|
POST
|
I've written a Python function that is meant to update a File Geodatabase Table (Landuse) based on a Feature Class (Watershed). I'm using a Update Cursor to update each field value where it finds a match within a Python Dictionary of HydroID and in return I use the "Shape_Area" as part of my calculations to update each field value. There are eight landuse fields that I want to update using the same formula for each HydroID match: Commercial_Forestry Cultivated Indigenous Mines Natural_Vegetation_Forest Urban Waterbodies Wetlands So instead of specifying each field through its index position manually, I step into a for loop and loop through each field updating the field value based on the formula. The problem that I'm having is that the Update Cursor is not updating the rows, although I'm calling updateRow() afterwards. Is there anyway I can get around the following without having to specify each field through its index position manually? # calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
watershed_fields = ["HydroID", "Shape_Area"]
valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)} # @UndefinedVariable
landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
print(landuse_fields)
with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur: # @UndefinedVariable
for row in upcur:
keyvalue = row[0]
if keyvalue in valuedict:
print(keyvalue)
for landuse in row[1:]:
landuse = (landuse*(30*30))/(valuedict[keyvalue][0])*100
upcur.updateRow(row)
print(landuse)
landuse_percentage(watershed, output_pivot) Python Code: for loop over each field 12366 (HydroID) 0.962788133178 - (Landuse percentage based on Shape Area) 62.0189203295 31.8703202065 0.123498273168 13.8522293032 1.24419729078 1.02530804735 2.19975286437 Python Console: Correct Values printed but table not updated. # calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
watershed_fields = ["HydroID", "Shape_Area"]
valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)} # @UndefinedVariable
landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur: # @UndefinedVariable
for row in upcur:
keyvalue = row[0]
if keyvalue in valuedict:
row[1] = (row[1]*(30*30))/(valuedict[keyvalue][0])*100
row[2] = (row[2]*(30*30))/(valuedict[keyvalue][0])*100
row[3] = (row[3]*(30*30))/(valuedict[keyvalue][0])*100
row[4] = (row[4]*(30*30))/(valuedict[keyvalue][0])*100
row[5] = (row[5]*(30*30))/(valuedict[keyvalue][0])*100
row[6] = (row[6]*(30*30))/(valuedict[keyvalue][0])*100
row[7] = (row[7]*(30*30))/(valuedict[keyvalue][0])*100
row[8] = (row[8]*(30*30))/(valuedict[keyvalue][0])*100
upcur.updateRow(row)
landuse_percentage(watershed, output_pivot) Python Code: specifying index position of each field manually. Landuse: Updated Table using second Python Function So any help how to loop through each each instead of having to manually specify the index position will be appreciated.
... View more
07-13-2016
03:08 AM
|
0
|
6
|
3872
|
|
POST
|
Thanks Dan Truly appreciate your ongoing help and advice.
... View more
07-12-2016
12:45 PM
|
0
|
0
|
1842
|
|
POST
|
Hi Dan I solved it by using numpy.core.records.fromarrays: '''
Created on 06 Jul 2016
Zonal Histogram Pivot:
Pivot Results Table
@author: PeterW
'''
# import site-packages and modules
import numpy as np
import arcpy
# set arguments
hist_table = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_stats_160706"
output_pivot = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_pivot_v2_160712"
# set environment settings
arcpy.env.overwriteOutput = True
# pivot zonal histogram results table
def hist_pivot(hist_table, output_pivot):
sum_list = []
fields = [f.name for f in arcpy.ListFields(hist_table)[1:]]
fields_pivot = ["HydroID" if x == "LABEL" else int(x.replace("Hydro_", "")) for x in fields]
sum_list.append(fields_pivot)
with arcpy.da.SearchCursor(hist_table, fields) as scur:# @UndefinedVariable
for row in scur:
sum_list.append(list(row))
arr = np.asarray(sum_list, dtype="object")
arr_pivot = arr.T
arr_names = str(", ".join(arr_pivot[0]))
dtypes_list = ["float64" for number in xrange(len(arr_pivot[0])-1)]
dtypes_list.insert(0, "int64")
formats = str(", ".join(dtypes_list))
rec_arr = np.core.records.fromarrays(arr_pivot[1:].transpose(), formats=formats, names = arr_names)
print(rec_arr)
arcpy.da.NumPyArrayToTable(rec_arr, output_pivot)# @UndefinedVariable
hist_pivot(hist_table, output_pivot) Python Code: Python Pivot Results Table
... View more
07-12-2016
10:43 AM
|
1
|
3
|
1842
|
|
POST
|
HI Dan Apologies that I never got back to you sooner, as I've been off sick. Why can't you Pivot\Transpose a Structured Array? I have to forgo using TableToNumPyArray and use a SearchCursor to build a Python list from my Zonal Histogram table results. I've achieved the pivoted results that I was looking for, but now need assistance getting the following back into a Structured Array so that I can use NumPyArrayToTable: '''
Created on 06 Jul 2016
Zonal Histogram:
Pivot Results Table
@author: PeterW
'''
# import site-packages and modules
import numpy as np
import arcpy
# set arguments
hist_table = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_stats_160706"
output_pivot = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_pivot_160712"
# set environment settings
arcpy.env.overwriteOutput = True
# pivot zonal histogram results table
def hist_pivot(hist_table, output_pivot):
sum_list = []
fields = [f.name for f in arcpy.ListFields(hist_table)[1:]]
fields_pivot = ["HydroID" if x == "LABEL" else int(x.replace("Hydro_", "")) for x in fields]
sum_list.append(fields_pivot)
with arcpy.da.SearchCursor(hist_table, fields) as scur:# @UndefinedVariable
for row in scur:
sum_list.append(list(row))
arr = np.asarray(sum_list, dtype="object")
arr_pivot = arr.T
print(arr_pivot)
# arcpy.da.NumPyArrayToTable(arr_pivot, output_pivot)# @UndefinedVariable
hist_pivot(hist_table, output_pivot) Python Code Python Console: Print Statement of array transposed.
... View more
07-12-2016
04:27 AM
|
0
|
5
|
1842
|
|
POST
|
I'm busy summarizing the landuse found within each of my watersheds using Zonal Histogram. I'd like to pivot the Hydro (i.e. Hydro_1236 etc.) columns to rows and pivot the Label Column values to column headings. I've attached the Zonal Histogram Results Table as well as a mock-up of the table structure that I'm looking for. Watersheds + Land-use Raster Zonal Histogram Results Table Landuse Raster: Classification Landuse Raster: Labels Pivot Results Table: Note that the HydroID field values were based on the following fields: Hydro_1236 etc. I removed "Hydro_" before adding them to the HydroID field. How can I achieve the following using either Python Pivot Table or Pandas, thanks in advance for any help on the following.
... View more
07-06-2016
08:19 AM
|
1
|
7
|
4165
|
|
POST
|
Hi Steve Thanks for getting back to me on the following. There's 26821 columns and 26161 rows which equates to 701 664 181 cells. 5min, but the cell size is 3160 and the accuracy of the results are lost as the results of averaging over the study area. None ArcMap 10.3.1 All my cores are being used: X6 2.8Ghz How is one meant to maintain the accuracy of the output GA Layer without having to reduce the cellsize when exporting the result to a raster? Is there a way of determining the cellsize of the existing GA Layer to maintain the accuarcy of the raster being produced when converting the GA Layer? Thanks for helping with the following.
... View more
06-29-2016
07:37 AM
|
0
|
2
|
2342
|
|
POST
|
I've used Geostatistical Analyst Cokriging (Rainfall + Elevation) to create an MAP Surface (Mean Annual Precipitation). The GA layer that is produced as an output of the analysis can't be used in any of the other ArcGIS tools for further processing. You have to convert the GA Layer to Grid using the following tool: GA Layer to Grid. The problem that I'm having is that when I try to convert my GA Layer to Grid with a cell size of 30m based on the resolution of the DEM, it runs forever without completing. I left is running for 48 hrs without any sign that it was about to complete running. There seems to be no way of telling what the current cell size is of the GA Layer to understand why converting the GA Layer to Grid is not working. I don't wish to reduce the cell size as this will reduce the accuracy of the predicted surface which I have spent considerable time getting the input variables and models correctly setup. Any help as to why GA Layer to Grid is unable to export the following Grid (30m Cellsize) will be appreciated. It would help going forward if the GA layer could be used as input to other ArcGIS Tools to prevent the need of having to covert the results to a Grid unnecessarily. Geostatistical Layer: Cokriging Result I've attached my geostatistical model xml file.
... View more
06-29-2016
06:18 AM
|
0
|
6
|
4883
|
|
DOC
|
I needed to create a ArcMap Document with Multiple Data Driven Data Frames. I realised that Data Driven Pages wouldn't allow me to accomplish the following as well as I needed it to handle any number of Data Frames without having to rewrite my Python Function. I have accomplished the following by creating a list of unique features based on my index layer that one would use if using Data Driven Pages. I then loop through each Data Frame, three layers within each Data Frame that needs to be updated based on the index layer. I then apply a definition query to each of the layers to create the view that I'm looking for for each index. I then obtain the extent of each index and set the Data Frames extent to the indexes extent as well as adjusted the Data Frames scale to the nearest 5000, which can be adjusted as needed. I've attached print screens of the ArcMap Documents with: X2 Data Frames ; X3 Data Frames ; X4 Data Frames as well as the code. '''
Created on 02 Jun 2016
Export mxds to jpegs:
Multiple Data Driven
Frames
@author: PeterW
'''
# import site-packages and modules
import math
import arcpy
# set arguments
input_mxd = r"E:\Projects\2016\G112358\SAA\Models\Settlements\mxd\Python_Setup_Mutliple_DataFrames\DDP_Settlements02_Python.mxd"
output_folder = r"E:\Projects\2016\G112358\SAA\Models\Settlements\mxd\Python_Setup_Mutliple_DataFrames\jpegs02"
# set environment settings
arcpy.env.overwriteOutput = True
# export mxd with multiple data driven dataframes
def multiple_dataframes_jpeg(input_mxd, output_folder):
mxd = arcpy.mapping.MapDocument(input_mxd)
dfs = arcpy.mapping.ListDataFrames(mxd)
index_lyr = arcpy.mapping.ListLayers(mxd, "", dfs[0])[0]
with arcpy.da.SearchCursor(index_lyr, ["NAME"]) as scur:
for row in scur:
settlements = sorted({row[0][:-2] for row in scur})
for settlement in settlements:
for df in dfs:
df_position = int(df.name[-2:])
lyrs = arcpy.mapping.ListLayers(mxd, "", df)[:3]
name = "{0}0{1}".format(settlement, df_position)
def_query1 = "{0} = {1} AND {2} = '{3}'".format("DATAFRAME", df_position, "NAME", name)
lyrs[0].definitionQuery = def_query1
def_query2 = "{0} = '{1}'".format("NAME", name)
lyrs[1].definitionQuery = def_query2
def_query3 = "{0} = '{1}'".format("NAME", name)
lyrs[2].definitionQuery = def_query3
with arcpy.da.SearchCursor(lyrs[0], ["SHAPE@"]) as scur:
for row in scur:
df.extent = row[0].extent
df.scale = int(math.ceil(df.scale/5000)*5000)
arcpy.RefreshActiveView()
output_jpeg = "{0}\\{1}.jpg".format(output_folder, settlement.replace(" ", ""))
print output_jpeg
arcpy.mapping.ExportToJPEG(mxd, output_jpeg, df_export_width=1200, df_export_height=1200, resolution=300)
multiple_dataframes_jpeg(input_mxd, output_folder) Dynamic Data Frames X2 Dynamic Data Frames X3 Dynamic Data Frames X4
... View more
06-04-2016
01:07 PM
|
3
|
1
|
5128
|
|
POST
|
Hi Luke Thanks for your help, I was able to install the correct Python packages based on your advice.
... View more
06-04-2016
12:44 PM
|
0
|
0
|
3801
|
|
POST
|
Hi Luke Thanks for the following, truly appreciated.
... View more
06-03-2016
01:18 AM
|
0
|
0
|
3801
|
|
POST
|
I'm busy setting up a virtualenv for my ArcGIS 10.3.1 Python (2.7.8 64bit) Development. I've created a new virtualenv using virtualenvwrapper-win and it's installed a clean installation of Python based on the current default version installed by ArcGIS. I trying to install the ArcGIS required versions of the following packages: Numpy (1.7.1) matplotlib (1.3.0) xlrd (0.9.2) xwlt (0.7.5) Pyparsing (1.5.7) When I try to install Numpy (1.7.1) and matplotlib (1.3.0) via command line using pip install, it fails as it can't find a Python Wheel for the following and only finds a zipped file that it tries to compile and fails. I don't have any experience in how to achieve the following. I tried downloading an executional installation for the following but unfortunately it doesn't give you the option to select an alternative installation directory but the default Python installation under my C Drive. Any help to solve the following will truly be appreciated. ArcGIS 10.3.1 Python Requirements Virtualenvwrapper-win: pip install Numpy (1.7.1) Virtualenvwrapper-win: pip install Numpy (1.7.1) error message unable to compile Numpy 1.7.1 Executional Installation (Unable to change Python Directory or Installation Directory)
... View more
06-02-2016
01:36 AM
|
0
|
3
|
12004
|
|
POST
|
Hi Curtis Price , Dan Patterson I copied the following two Python pth files: ArcHydroTools.pth DTBGGP64.pth to the following directory: E:\Python\Venv\ArcPy64bit\Lib\site-packages When I try to install the the specific versions of python packages required for ArcGIS: Numpy (1.7.1) Matplotlib (1.3.0) xlrd (0.9.2) xlwt (0.7.5) Pyparsing (1.5.7) based on the python requirements for ArcGIS 10.3.1 I'm unable to install Numpy (1.7.1) within my virtualenv via command line: Python: pip install numpy==1.7.1 Python: Pip install numpy==1.7.1 error message. If I install numpy without specifying a version it works perfectly, no error messages. I tried downloading numpy-1.7.1.win32-py2.7.exe but unfortunately you are unable to change the directory into which it will install. Any help to resolve the following will truly be appreciated.
... View more
06-01-2016
08:10 AM
|
0
|
2
|
2165
|
|
POST
|
Most of my code is still in Python 2.7 as I'm still using ArcGIS 10.3 I'll have to convert a substantial amount of code once I start using Python 3.4
... View more
05-27-2016
01:21 PM
|
0
|
5
|
2686
|
| Title | Kudos | Posted |
|---|---|---|
| 3 | 01-16-2012 02:34 AM | |
| 1 | 05-07-2016 03:04 AM | |
| 1 | 04-10-2016 01:09 AM | |
| 1 | 03-13-2017 12:27 PM | |
| 1 | 02-17-2016 02:34 PM |
| Online Status |
Offline
|
| Date Last Visited |
03-04-2021
12:50 PM
|