Python - cursor through a layer and export to shapefile

460
3
09-02-2010 01:39 PM
FrankOgiamien
New Contributor II
I'm having trouble with the last bit of my script.  What I want to do is select a number of features in a SDE layer, then export each feature individually into its own shapefile.
I am able to open a searchcursor and go through it record by record however the Select_analysis command just creates an empty shapefile for each record.
Any ideas what I'm doing wrong?  Thanks,
Frank Ogiamien

print "Opening cursor in the local block layer"
cur = gp.SearchCursor("Block_Out_Testing")
row = cur.next()
while row:
        try:
            rec_id = str(row.OBJECTID)
            lic = str(row.FOREST_BC_V_BLOCKS_LICENCE_ID)
            cp = str(row.FOREST_BC_V_BLOCKS_PERMIT_ID)
            blk = str(row.FOREST_BC_V_BLOCKS_BLOCK_ID)
            blk4 = blk.replace("!", "")
            blk3 = blk4.replace("-", "_")
            blk2 = blk3.replace("/", "_")
            outshp = OUTPUT_DIR + "\\" + lic + "\\" + cp + "\\" + blk2 + ".shp"
            if not gp.exists(OUTPUT_DIR + "\\" + lic):
                os.mkdir(OUTPUT_DIR + "\\" + lic)
            if not gp.exists(OUTPUT_DIR + "\\" + lic + "\\" + cp):
                os.mkdir(OUTPUT_DIR + "\\" + lic + "\\" + cp)
            print "Exporting: " + rec_id + "   " + lic + "  " + cp +  "  " + blk
            gp.Select_analysis("C:\\Projects\\Block_Export\\Block_Shape_Export.mdb\\Block_Out_Testing", outshp, "\"FOREST_BC1\" = " + "'" + blk + "'")
            row = cur.next()

        except:
            print "an error occurred"
            print gp.GetMessages(0)
            row = cur.next()
0 Kudos
3 Replies
Luke_Pinner
MVP Regular Contributor
Please repost your script/edit your post and wrap [noparse]
[/noparse] tags around your code so your indentation is preserved and we can easily see what your script is doing.
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos
DanPatterson_Retired
MVP Emeritus
Have you tried
http://arcscripts.esri.com/details.asp?dbid=14127
you may have to export to a local file, not tested on that network stuff
0 Kudos
FrankOgiamien
New Contributor II
Thank you both for the comments.  I noticed the definition query had a different column name in it.  Not sure if it had something to do with recently upgrading from 9.2 to 9.3.1
Anyways changing the Select command's query to
"[FOREST_BC_V_BLOCKS_BLOCK_ID] = " + "'" + blk + "'"
solved my problem.

Thanks,

Frank Ogiamien
0 Kudos