Select to view content in your preferred language

Select features from layers and send to output tables...

1129
1
12-04-2012 01:34 PM
JonathanHodel
Occasional Contributor
I'm sure there is an easier way to do this so any help would be appreciated:

I have a shapefile with all valves (hydrant valves and main valves) included in it.  I want to select all of the main valves in the currentview extent and output them to a table called "Main Valves" and select the hydrant valves in the current extent and output them to a table called "Hydrant Valves".

  for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.name == "Main Valves"
      extentPolygon = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft,df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
      arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", extentPolygon, "", "NEW_SELECTION")
      arcpy.CopyRows_management("Main valves", tbl1)
      mainvalves_outputrows = arcpy.SearchCursor(tbl1,"","","Valve_ID; vDiameter; LOCATION_DATA","Valve_ID")
    elif lyr.name == "Hydrant Valves"
      extentPolygon = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft,df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
      arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", extentPolygon, "", "NEW_SELECTION")
      arcpy.CopyRows_management("Hydrant valves", tbl2)
      hydrantvalves_outputrows = arcpy.SearchCursor(tbl2,"","","Valve_ID; vDiameter; LOCATION_DATA","Valve_ID")
    else:
        
  Clear all table text values
  tab1Col1Txt.text = " "; tab1Col2Txt.text = " "; tab1Col3Txt.text = " "
  tab1Col1Value = ""
  tab1Col2Value = ""
  tab1Col3Value = ""
  tab2Col1Txt.text = " "; tab2Col2Txt.text = " "; tab2Col3Txt.text = " "
  tab2Col1Value = ""
  tab2Col2Value = ""
  tab2Col3Value = ""
  
  for row in mainvalves_outputrows: 
    tab1Col1Value += str(row.Valve_ID) + "\n"
    tab1Col2Value += str(row.vDiameter) + "\n"
    tab1Col3Value += str(row.LOCATION_DATA) + "\n"

  tab1Col1Txt.text = tab1Col1Value
  tab1Col2Txt.text = tab1Col2Value
  tab1Col3Txt.text = tab1Col3Value

  for row in hydrantvalves_outputrows: 
    tab2Col1Value += str(row.Valve_ID) + "\n"
    tab2Col2Value += str(row.vDiameter) + "\n"
    tab2Col3Value += str(row.LOCATION_DATA) + "\n"

  tab2Col1Txt.text = tab2Col1Value
  tab2Col2Txt.text = tab2Col2Value
  tab2Col3Txt.text = tab2Col3Value
Tags (2)
0 Kudos
1 Reply
markdenil
Frequent Contributor
Clearly, you are expecting to do more than just extract the data to tables. You accomplish that with CopyRows.

It looks like you are also wanting a text output: a file or report of some kind.
Do you really want the table? or just the report? or both?
I am guessing that the report will appear on the map, and that is what the ~text.text elements are.

Why do you define the cursors inside the table extraction loop?

You know what your target tables will be named (or the variables that will hold the names),
and you seem to be doing the same thing to each output table

Why use a list of the extracted tables, and loop through them, reusing the cursor
(and doing something with the cursor output before looping)

for tbl in [tbl1, tbl2]:
    cur  = arcpy.SearchCursor(tbl,"","","Valve_ID; ... ect...


Further, you could write your three output coulmn value vars as elements in a list,
and give the lists names in nested lists in a modification of the table list as shown above.


t1OUT = []
t2OUT = []
for tbl in [[tbl1, t1OUT], [tbl2, t2OUT]]:

    cur  = arcpy.SearchCursor(tbl[0],"","","Valve_ID; ... ect...  # <- note that tbl is now a list
        for row in cur:
        ##  other stuff
        Col1Value += str(row.Valve_ID) + "\n"
        # ... and so on...
        tbl[1].append([Col1Value, Col2Value, Col3Value])
    del cur
     

Then go on to write your text element text from the t1OUT and t2OUT tables,
concatenating each list content into one line string.


This is obviously quite rough, but may be simpler
0 Kudos