df = arcpy.mapping.ListDataFrames(mxd)[0] newExtent = df.extent newExtent.XMin, newExtent.YMin = -180.0, -90.0 newExtent.XMax, newExtent.YMax = 180.0, 90.0 df.extent = newExtent
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] extent = df.extent array = arcpy.Array() array.add(extent.lowerLeft) array.add(extent.lowerRight) array.add(extent.upperRight) array.add(extent.upperLeft) array.add(extent.lowerLeft) polygon = arcpy.Polygon(array) arcpy.CopyFeatures_management(polygon, r"C:\GIS\Default.gdb\polytest")
import arcpy, os, sys mxdin = acrpy.GetParameter(0) mxd = arcpy.mapping.MapDocument(mxdin) df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] extent = df.extent array = arcpy.Array() array.add(extent.lowerLeft) array.add(extent.lowerRight) array.add(extent.upperRight) array.add(extent.upperLeft) array.add(extent.lowerLeft) try: polygon = arcpy.Polygon(array) arcpy.CopyFeatures_management(polygon, r"C:\GIS\Default.gdb\polytest" except: print "Failed to create polygon"
# Dataframe To Feature Class tool. # Pulls the extent of the active dataframe in an mxd, and fills out fields based on input values # from the user. # Author: LCpl Patrick Fischer # Riff.Ibanezius@gmail.com # 3D Topo Plt, 3D Intel BN, Camp Hansen Okinawa, Japan import arcpy, os, sys # Deletes feature class from previous time the script was ran. arcpy.Delete_management(r"Z:\Training\Python\test_scripts\Extent_map.gdb\polygon") # Collect Parameters from User input mxdin = arcpy.GetParameterAsText(0) mxd = arcpy.mapping.MapDocument(mxdin) df = arcpy.mapping.ListDataFrames(mxd, "La*") [0] # select main dataframe not location diagram ext = df.extent array = arcpy.Array() array.add(ext.lowerLeft) array.add(ext.lowerRight) array.add(ext.upperRight) array.add(ext.upperLeft) array.add(ext.lowerLeft) polygon = arcpy.Polygon(array, df.spatialReference) arcpy.AddMessage("Extent has successfully been converted to a polygon") #Checks spatial reference from the dataframe to see if it needs to be transformed into another projection. if df.spatialReference == "WGS 1984": arcpy.Project_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon", "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj") else: arcpy.CopyFeatures_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon") print 'Already projected into WGS 1984' #change to message box arcpy.AddMessage("Successfuly projected into WGS 1984") polypro = r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon" #add fields to new feature class arcpy.AddField_management(polypro, "PRODUCT_ID", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PRODUCT_DATE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "SCALE", "FLOAT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "EDITION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "CLASSIFICATION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "TYPE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PRODUCER", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "COUNTRY_CODE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "NOTES", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PDF_PATH", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED") #Set parameters for user input PROID = arcpy.GetParameterAsText(1) PRODA = arcpy.GetParameterAsText(2) ED = arcpy.GetParameterAsText(3) CLA = arcpy.GetParameterAsText(4) TYP = arcpy.GetParameterAsText(5) PROD = arcpy.GetParameterAsText(6) CC = arcpy.GetParameterAsText(7) NOTES = arcpy.GetParameterAsText(8) PDF = arcpy.GetParameterAsText(9) arcpy.AddMessage("Calculating fields based on input values") #Calculate Fields based on user input for el in arcpy.mapping.ListLayoutElements(mxd, "DATAFRAME_ELEMENT"): SCALE = el.scale arcpy.CalculateField_management(polypro, "PRODUCT_ID", PROID, "", "") arcpy.CalculateField_management(polypro, "PRODUCT_DATE", PRODA, "", "") arcpy.CalculateField_management(polypro, "SCALE", SCALE, "", "") arcpy.CalculateField_management(polypro, "EDITION", ED, "", "") arcpy.CalculateField_management(polypro, "CLASSIFICATION", CLA, "", "") arcpy.CalculateField_management(polypro, "TYPE", TYP, "", "") arcpy.CalculateField_management(polypro, "PRODUCER", PROD, "", "") arcpy.CalculateField_management(polypro, "COUNTRY_CODE", CC, "", "") arcpy.CalculateField_management(polypro, "NOTES", NOTES, "", "") arcpy.CalculateField_management(polypro, "PDF_PATH", PDF, "", "") arcpy.AddMessage("Succesfully calculated fields, appending to Topo Products feature class.") Feature = arcpy.GetParameterAsText(10) arcpy.Append_management(polypro, Feature ", "NO_TEST", "", "")
rows = arcpy.UpdateCursor(layer) for row in rows: row.values = 1 rows.updateRow(row)
Ok I now have a working script that does everything I wanted to do. Some things i've noticed while running this is hat calculate fields is particularly slow, is there a way to speed it up or another function I can use instead? I took a few things from corea's script, thank you btw it was a great help. I have thought about using the rows part but i'm unsure how to use that exactly. This whole thing has been a wonderful learning process for me and i'm happy I was able to get to where i'm at now, thank you everyone 🙂
Here is my script in its current form.# Dataframe To Feature Class tool. # Pulls the extent of the active dataframe in an mxd, and fills out fields based on input values # from the user. # Author: LCpl Patrick Fischer # Riff.Ibanezius@gmail.com # 3D Topo Plt, 3D Intel BN, Camp Hansen Okinawa, Japan import arcpy, os, sys # Deletes feature class from previous time the script was ran. arcpy.Delete_management(r"Z:\Training\Python\test_scripts\Extent_map.gdb\polygon") # Collect Parameters from User input mxdin = arcpy.GetParameterAsText(0) mxd = arcpy.mapping.MapDocument(mxdin) df = arcpy.mapping.ListDataFrames(mxd, "La*") [0] # select main dataframe not location diagram ext = df.extent array = arcpy.Array() array.add(ext.lowerLeft) array.add(ext.lowerRight) array.add(ext.upperRight) array.add(ext.upperLeft) array.add(ext.lowerLeft) polygon = arcpy.Polygon(array, df.spatialReference) arcpy.AddMessage("Extent has successfully been converted to a polygon") #Checks spatial reference from the dataframe to see if it needs to be transformed into another projection. if df.spatialReference == "WGS 1984": arcpy.Project_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon", "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj") else: arcpy.CopyFeatures_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon") print 'Already projected into WGS 1984' #change to message box arcpy.AddMessage("Successfuly projected into WGS 1984") polypro = r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon" #add fields to new feature class arcpy.AddField_management(polypro, "PRODUCT_ID", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PRODUCT_DATE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "SCALE", "FLOAT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "EDITION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "CLASSIFICATION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "TYPE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PRODUCER", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "COUNTRY_CODE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "NOTES", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(polypro, "PDF_PATH", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED") #Set parameters for user input PROID = arcpy.GetParameterAsText(1) PRODA = arcpy.GetParameterAsText(2) ED = arcpy.GetParameterAsText(3) CLA = arcpy.GetParameterAsText(4) TYP = arcpy.GetParameterAsText(5) PROD = arcpy.GetParameterAsText(6) CC = arcpy.GetParameterAsText(7) NOTES = arcpy.GetParameterAsText(8) PDF = arcpy.GetParameterAsText(9) arcpy.AddMessage("Calculating fields based on input values") #Calculate Fields based on user input for el in arcpy.mapping.ListLayoutElements(mxd, "DATAFRAME_ELEMENT"): SCALE = el.scale arcpy.CalculateField_management(polypro, "PRODUCT_ID", PROID, "", "") arcpy.CalculateField_management(polypro, "PRODUCT_DATE", PRODA, "", "") arcpy.CalculateField_management(polypro, "SCALE", SCALE, "", "") arcpy.CalculateField_management(polypro, "EDITION", ED, "", "") arcpy.CalculateField_management(polypro, "CLASSIFICATION", CLA, "", "") arcpy.CalculateField_management(polypro, "TYPE", TYP, "", "") arcpy.CalculateField_management(polypro, "PRODUCER", PROD, "", "") arcpy.CalculateField_management(polypro, "COUNTRY_CODE", CC, "", "") arcpy.CalculateField_management(polypro, "NOTES", NOTES, "", "") arcpy.CalculateField_management(polypro, "PDF_PATH", PDF, "", "") arcpy.AddMessage("Succesfully calculated fields, appending to Topo Products feature class.") Feature = arcpy.GetParameterAsText(10) arcpy.Append_management(polypro, Feature ", "NO_TEST", "", "")
Also sometimes i'll get an error about list is out of range, on one mxd it was due to what I think was to many data frames (around 50), but it happened again on another with just 2 data frames. I've ran this successfully with mxds with up to 5 data frames with no issues. Is this an anomaly or is something seriously wrong?
I agree with Matthew that you should use a cursor instead of Calculating Fields. An Insert Cursor should work nicely for you.
Also, the "list out of range" error may be due to no Dataframes in the MXD that start with "La" as you specify when you are creating the "df" variable. If ArcPy can't find any dataframes in the MXD that start with "La" then it creates an empty list. Calling the first index "[0]" on an empty list returns the out of range error.
Still want to know how to put the list of dataframes into a list you can select from in arcCatalog.