Solved! Go to Solution.
Dave-
Thank you, but how should that look here??:for n in list: arcpy.TableSelect_analysis(table, r"in_memory\table_sel", "TWACs_NUMBER = " + str(n)) out_graph_name = n out_graph_pdf = r"C:\MEWCo GIS System\Electric Graphs\Electric Meters\F-1 Feeder" + "\\" + str(n)[:-2] + ".pdf" input_template = r"C:\MEWCo GIS System\Electric Graphs\GIS Graph Temps\ELECTRIC METER DEMAND - KWH USAGE.grf" input_data = r"in_memory\table_sel"
## extract unique records from a gdb table and make mulitple tables for ## graphing import arcpy, os arcpy.env.overwriteOutput = True table = r'H:\Python\test.gdb\billing' fields = ['BILLING_PERIOD','METER_NUMBER','KWH','KW_DEMAND'] # empty list to store unique meter numbers meterNumbers = [] # use a search cursor to extract the unique numbers with arcpy.da.SearchCursor(table,fields) as cursor: for row in cursor: if not row[1] in meterNumbers: meterNumbers.append(row[1]) # for each unique cursor make a table view using the billing number for # the selection for meter in meterNumbers: ## omit the quotes if you table stores the meter number as a number not a string expression = arcpy.AddFieldDelimiters(table,'METER_NUMBER') + " = " + "'" + meter + "'" arcpy.MakeTableView_management(table,"CurrentMeter",expression) # copy the rows to a permanent table in the same location outTable = os.path.join(os.path.dirname(table),("meter_" + meter)) arcpy.CopyRows_management("CurrentMeter",outTable) ## at this point can use the graphing options to generate the graphs and save ## unfortunately don't have the answer yet for this
import arcpy from arcpy import env env.overwriteOutput = 1 env.workspace = r"C:\temp\python\test.gdb" table = "Meters" arcpy.TableSelect_analysis(table, r"in_memory\table_sel", "METER_NUMBER = " + arcpy.GetParameterAsText(0)) out_graph_name = arcpy.GetParameterAsText(0) out_graph_pdf = r"C:\Temp\python\Userdata" + "\\" + arcpy.GetParameterAsText(0) + ".pdf" input_template = r"C:\Temp\Python\UserData\KW Demand.grf" #grf file previously saved input_data = r"in_memory\table_sel" # Create the graph graph = arcpy.Graph() # Add a vertical bar series to the graph graph.addSeriesBarVertical(input_data, "KW_DEMAND") # Specify the title of the left axis graph.graphAxis[0].title = "KW Demand" # Specify the title of the bottom axis graph.graphAxis[2].title = "Meter Number" # Specify the title of the Graph graph.graphPropsGeneral.title = "KW Demand by Meter" # Output a graph, which is created in-memory arcpy.MakeGraph_management(input_template, graph, out_graph_name) # Save the graph as an image arcpy.SaveGraph_management(out_graph_name, out_graph_pdf, "MAINTAIN_ASPECT_RATIO", 600, 375)
Hi,
Have part of an answer for you, the code below will allow you to extract out the information based on the unique billing numbers within your table. It doesn't yet create and export the graphs but more on that in a minute. Here is the code for extracting the unique billing tables.## extract unique records from a gdb table and make mulitple tables for ## graphing import arcpy, os arcpy.env.overwriteOutput = True table = r'H:\Python\test.gdb\billing' fields = ['BILLING_PERIOD','METER_NUMBER','KWH','KW_DEMAND'] # empty list to store unique meter numbers meterNumbers = [] # use a search cursor to extract the unique numbers with arcpy.da.SearchCursor(table,fields) as cursor: for row in cursor: if not row[1] in meterNumbers: meterNumbers.append(row[1]) # for each unique cursor make a table view using the billing number for # the selection for meter in meterNumbers: ## omit the quotes if you table stores the meter number as a number not a string expression = arcpy.AddFieldDelimiters(table,'METER_NUMBER') + " = " + "'" + meter + "'" arcpy.MakeTableView_management(table,"CurrentMeter",expression) # copy the rows to a permanent table in the same location outTable = os.path.join(os.path.dirname(table),("meter_" + meter)) arcpy.CopyRows_management("CurrentMeter",outTable) ## at this point can use the graphing options to generate the graphs and save ## unfortunately don't have the answer yet for this
the copy rows at the end is not strictly need as you can do the graph creation in memory for each billing table.
If you want to create the graph purely using arcpy and ArcGIS you have a couple of options. There are the make and save graph tools which you can use based on a template graph created in arcmap, all that is required then is to modify the data source for each graph to your current table.
Alternatively use the arcpy graph class to carry out all these steps, I believe you will need to use this class to modify the data sources in the previous example. THe save graph will then allow you to export to pdf.
There is also matplotlib shipped with arcpy and that may be a better way to create the graphs. Sorry haven't used it so can't give you any steering on utilising it.
Hope this helps and gets you started. If i come up with anything I'll post again.
Cheers
Dave
Here is an example on how to accomplish this. First, you will want to create a sample graph in ArcMap. Here is a screen shot of the parameters I used:
[ATTACH=CONFIG]27045[/ATTACH]
After the graph is created, right-click on it and save it out as a .grf file. Now you can use the below code to create a graph of the "KW Demand by Meter" by specifying a meter number:import arcpy from arcpy import env env.overwriteOutput = 1 env.workspace = r"C:\temp\python\test.gdb" table = "Meters" arcpy.TableSelect_analysis(table, r"in_memory\table_sel", "METER_NUMBER = " + arcpy.GetParameterAsText(0)) out_graph_name = arcpy.GetParameterAsText(0) out_graph_pdf = r"C:\Temp\python\Userdata" + "\\" + arcpy.GetParameterAsText(0) + ".pdf" input_template = r"C:\Temp\Python\UserData\KW Demand.grf" #grf file previously saved input_data = r"in_memory\table_sel" # Create the graph graph = arcpy.Graph() # Add a vertical bar series to the graph graph.addSeriesBarVertical(input_data, "KW_DEMAND") # Specify the title of the left axis graph.graphAxis[0].title = "KW Demand" # Specify the title of the bottom axis graph.graphAxis[2].title = "Meter Number" # Specify the title of the Graph graph.graphPropsGeneral.title = "KW Demand by Meter" # Output a graph, which is created in-memory arcpy.MakeGraph_management(input_template, graph, out_graph_name) # Save the graph as an image arcpy.SaveGraph_management(out_graph_name, out_graph_pdf, "MAINTAIN_ASPECT_RATIO", 600, 375)
## extract unique records from a gdb table and make mulitple tables for ## graphing import arcpy, os arcpy.env.overwriteOutput = True table = "C:\MEWCo GIS System\Electric System\MEWCo_Electric_Model-LOCAL.gdb\billing" fields = "BILLING_PERIOD";"METER_NUMBER";"KWH";"KW_DEMAND" # empty list to store unique meter numbers meterNumbers = [] # use a search cursor to extract the unique numbers with arcpy.SearchCursor(table,fields) as cursor: for row in cursor: if not row[1] in meterNumbers: meterNumbers.append(row[1])
fields = "BILLING_PERIOD";"METER_NUMBER";"KWH";"KW_DEMAND"
fields = ["BILLING_PERIOD","METER_NUMBER","KWH","KW_DEMAND"]
Your fields need to be a list.
http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000
Instead of:fields = "BILLING_PERIOD";"METER_NUMBER";"KWH";"KW_DEMAND"
try:fields = ["BILLING_PERIOD","METER_NUMBER","KWH","KW_DEMAND"]
Mind me asking what's the purpose of creating a list of the meter numbers?
That is what the original script that I posted does. What you will need to do is create a sample graph and update the script with the correct paths to your data. Then, create a toolbox > right-click on the toolbox > Add Script > browse to the script. You will also need to add a parameter to the script of type Long.
A user can then click on the script and it will open a dialog for them to specify a Meter Number. Once they do and execute the tool, the meter number is queried and the graph is saved to a PDF.