3. FeatureClassToNumPyArray
import arcpy
import numpy
input = "C:/Data/ABSData.gdb/VIC_CCDs"
arr = arcpy.da.FeatureClassToNumPyArray(input,('CCD', 'pop_m', 'pop_f'))
# Sum the total population for males & females #
print(arr['pop_m'].sum())
print(arr['pop_f'].sum())
# Sum the female population for a particular CCD #
print(arr[arr['CCD'] == "2010101"]['pop_f'].sum())
4. FeatureClassToNumPyArray
import arcpy
import numpy
input = "C:/Data/ABSData.gdb/VIC_CCDs"
field1 = "INCOME"
field2 = "EDUCATION"
arr = arcpy.da.FeatureClassToNumPyArray(input, (field1, field2))
# Print correlation coefficients for the two fields
print(numpy.corrcoef((arr[field1],arr[field2])))
5. Improved Cursors
# Turn the contents of a GDB table into a Python list of lists
# Simple syntax & little code
with arcpy.da.SearchCursor(zonalstats_results_table,
["MINORITY","MAJORITY","MEAN"]) as tblcur:
results = [r for r in tblcur]
6. Improved Cursors
import arcpy
arcpy.env.workspace = "c:/data/output.gdb"
fc = "c:/data/base.gdb/roads"
fields = ("ROAD_TYPE", "BUFFER_DISTANCE")
with arcpy.da.UpdateCursor(fc, fields) as cursor:
# Update the field used in Buffer so the distance is based on road
# type. Road type is either 1, 2, 3 or 4. Distance is in meters.
for row in cursor:
row[1] = row[0] * 100
cursor.updateRow(row)
7. arcpy.mapping: update/apply symbology
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:ProjectProject.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Census")[0]
lyr = arcpy.mapping.ListLayers(mxd, "StatePopulation", df)[0]
lyrFile = arcpy.mapping.Layer(r"C:ProjectLYRsPopulation.lyr")
arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)
if lyr.symbologyType == "GRADUATED_COLORS":
lyr.symbology.valueField = "POP2000"
lyr.symbology.classBreakValues = [250000, 999999, 4999999, 9999999, 35000000]
lyr.symbology.classBreakLabels = ["250,000 to 999,999", "1,000,000 to 4,999,999",
"5,000,000 to 9,999,999",
"10,000,000 to 35,000,000"]
arcpy.mapping.ExportToPDF(mxd, r"C:ProjectOutputStatePopulation.pdf")
del mxd, lyrFile
** da module - to numpy array- what's numpy?- can convert values from a feature class to a numpy array- this allows lots of things straight-up like very fast column-based summary statistics (can take the mean of a column) or manipulation of all fields/rows at once (ever needed to multiply all values in all fields by 10?)- numpy arrays are the native datatype of many scipy & related libraries. allows for good extension of functionality- even better, easy(er) transfer to-from R
** da module - fast cursors- cursors have been vastly improved; 30x faster for SearchCursor, >10x faster for insert- support "with" keyword for setup/teardown; prevents locks- have some funky shape properties accessors (centroid, x, y etc)- different syntax - requires a rewrite, not find-replace (fields req'd)
** improved mapping module- can specify basic layer symbology e.g. graduated colours, symbols, unique vals, raster classifications- can apply legend styles- automatically create server connections (deployable)
** Difference between add-ins & script tools- addins involve a toolbar- user interaction e.g. click on feature, drag envelope, type in address- anything that requires dynamic interaction is better as an addin** How to make/distribute them- have a "template wizard" which sets up the folder structure & basic classes- fills in the config.xml which could be edited directly- ultimately just a .zip file with a different extension** List of functions/events you can use- Can capture following events: - dbl-click - circle, rectangle, line - info about mouse click: - which button - map OR screen coordinates on mouse-down or mouse-up - whether a modifier (shift, ctrl, alt) was present - keypress captures (incl. modifiers)- All these can be fed into your tool logic. Could generate custom editing, extraction, interactive geoprocessing, etc.
** What they are- .pyt file- Just a Python script with specific classes- ArcGIS understands them as a Toolbox- Incorporates everything: parameter info, descriptions, validation, program logic** Advantages over "normal" toolboxes- Easier to deploy- Easier to maintain - can actually have >1 person editing at once, with proper version control!- Could theoretically generate them automatically... with different tools according to some parameters. We used to have to make >1 toolbox with subsets of tools, this would have helped- No worrying about relative paths/python files going missing/models losing their sub-models