Select to view content in your preferred language

HOW TO ITERATE THIS: Isolate single point in pointfile and merge with other shapefile

694
1
08-09-2010 10:42 AM
PageWeil
Emerging Contributor
I am very new at geoprocessing and Python in general. 

I have two pointfiles; one is my raw data and one is a grid of points every 8th of a degree.

I need to iterate through all values in the grid pointfile, merge the currently selected point with my raw data, export the merged points as a new shapefile and then conduct some analysis on the merged file.

Ideally, I would use the iterators built into ArcGIS 10 but that is not available to me.  My first stab was to make some code that used a SQL expression to perform a select on the currently iterated FID.   I would then merge that selection with my dataset and output and process individual files from there, but there is something wrong with my code.

Is there any way to use series' or lists to iterate through all the values within a single shapefile in ArcGIS 9?

I have the code below but I am getting a script syntax error. 

Please help!

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

try:
    
    # Load required toolboxes...
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

    # Script arguments...
    v8thd_gridCopy_shp = sys.argv[1]
    v8thd_gridCopy_shp = "P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy.shp" # provide a default value if unspecified

    # Local variables...
    v8thd_gridCopy_Select_shp = "P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp"
    NN_test__n__shp = "P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_test_%n%.shp"
    NN_Rain_Can_shp = "P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp"

    # Iteration through all grid values
    counter = 0
    therowcount = gp.GetCount_management(v8thd_gridCopy_shp)

    while counter < therowcount:
        # Process: Select...
        gp.Select_analysis(v8thd_gridCopy_shp, v8thd_gridCopy_Select_shp, "\"FID\" = " + counter )

        # Process: Merge...
        gp.Merge_management("'P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp';'P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp'", NN_test__n__shp, "KRIEG 'KRIEG' true true false 4 Short 0 4 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,KRIEG,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,KRIEG,-1,-1;OBJECTID 'OBJECTID' true true false 9 Long 0 9 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,OBJECTID,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,OBJECTID,-1,-1;StnID 'StnID' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,StnID,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,StnID,-1,-1;Station_Na 'Station_Na' true true false 254 Text 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Station_Na,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Station_Na,-1,-1;FY 'FY' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,FY,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,FY,-1,-1;LY 'LY' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,LY,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,LY,-1,-1;Elev 'Elev' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Elev,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Elev,-1,-1;X_Coord 'X_Coord' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,X_Coord,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,X_Coord,-1,-1;Y_Coord 'Y_Coord' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Y_Coord,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Y_Coord,-1,-1;Jan 'Jan' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Jan,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Jan,-1,-1;Feb 'Feb' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Feb,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Feb,-1,-1;Mar 'Mar' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Mar,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Mar,-1,-1;Apr 'Apr' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Apr,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Apr,-1,-1;May 'May' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,May,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,May,-1,-1;Jun 'Jun' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Jun,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Jun,-1,-1;Jul 'Jul' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Jul,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Jul,-1,-1;Aug 'Aug' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Aug,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Aug,-1,-1;Sep 'Sep' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Sep,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Sep,-1,-1;Oct 'Oct' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Oct,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Oct,-1,-1;Nov 'Nov' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Nov,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Nov,-1,-1;Dec_ 'Dec_' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Dec_,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Dec_,-1,-1;PCP05 'PCP05' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,PCP05,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,PCP05,-1,-1;Winter05 'Winter05' true true false 13 Float 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Winter05,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Winter05,-1,-1;Summer05 'Summer05' true true false 13 Float 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Summer05,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Summer05,-1,-1;Annual 'Annual' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Annual,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Annual,-1,-1;Summer 'Summer' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Summer,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Summer,-1,-1;Winter 'Winter' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Winter,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Winter,-1,-1;ST 'ST' true true false 254 Text 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,ST,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,ST,-1,-1;CD 'CD' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,CD,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,CD,-1,-1;Lat 'Lat' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Lat,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Lat,-1,-1;Long_ 'Long_' true true false 19 Double 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,Long_,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,Long_,-1,-1;WinterPct 'WinterPct' true true false 13 Float 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,WinterPct,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,WinterPct,-1,-1;SummerPct 'SummerPct' true true false 13 Float 0 0 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\NN_Rain_Can.shp,SummerPct,-1,-1,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,SummerPct,-1,-1;POINTID 'POINTID' true true false 6 Long 0 6 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,POINTID,-1,-1;GRID_CODE 'GRID_CODE' true true false 10 Double 0 10 ,First,#,P:\\Project Name\\Data\\GIS\\Analysis\\VRC Model\\8thd_gridCopy_Select.shp,GRID_CODE,-1,-1")
        print counter

        # Additional Analysis can go here within the if loop
0 Kudos
1 Reply
GerryGabrisch
Frequent Contributor
Here you are concatenating an integer to a string.  This will throw an error.

gp.Select_analysis(v8thd_gridCopy_shp, v8thd_gridCopy_Select_shp, "\"FID\" = " + counter )

Try using

gp.Select_analysis(v8thd_gridCopy_shp, v8thd_gridCopy_Select_shp, "\"FID\" = " + str(counter) )
0 Kudos