Python at 10.1


Published on

Python presentation by Thomas Mackey at the Melbourne Esri Australia Developer Meetup

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • ** 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
  • Python at 10.1

    1. 1. Python in 10.1Thom Mackey
    2. 2. Contents• arcpy Enhancements• Add-ins• Python Toolboxes
    3. 3. FeatureClassToNumPyArrayimport arcpyimport numpyinput = "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. 4. FeatureClassToNumPyArrayimport arcpyimport numpyinput = "C:/Data/ABSData.gdb/VIC_CCDs"field1 = "INCOME"field2 = "EDUCATION"arr = arcpy.da.FeatureClassToNumPyArray(input, (field1, field2))# Print correlation coefficients for the two fieldsprint(numpy.corrcoef((arr[field1],arr[field2])))
    5. 5. Improved Cursors# Turn the contents of a GDB table into a Python list of lists# Simple syntax & little codewith arcpy.da.SearchCursor(zonalstats_results_table, ["MINORITY","MAJORITY","MEAN"]) as tblcur: results = [r for r in tblcur]
    6. 6. Improved Cursorsimport arcpyarcpy.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. 7. arcpy.mapping: update/apply symbologyimport arcpymxd = 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
    8. 8. Add-Ins
    9. 9. Python Toolboxes