Select to view content in your preferred language

Help with python code

1440
2
11-10-2011 12:35 PM
by Anonymous User
Not applicable
Original User: luke.kaim

I am very new to python and the syntax.

I am trying to write code so that It takes a shapefile with any amounts of rows and break that file into individual shapefiles with 1,000,000 records each. Then with the outputs I need to do a number of steps. I am much better at understanding the big picture then I am at coding it.

import arcpy, sys, os, re,glob
from arcpy import env

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\Users\\Luke Kaim\\Documents\\university_of_maine\\research4\\test2"
rasterpoints_shp = "rasterpoints.shp"



fieldName= "FID"
recordsCounted = 0
number=0
featureClass = "rasterpoints_shp"
i=0
files=""
nextFile = str(outfile[i+1])+".shp"

rows = arcpy.SearchCursor(featureClass)

for row in rows:
    number += row.getValue(fieldName)
if recordsCounted ==1000000:
     arcpy.CreateFeature(nextFile,"")
recordsCounted =0
i=i+1

recordsCounted += 1
row = rows.next()

for files in glob.glob("outfile" "+" "*"):
    
# Process: Select
arcpy.Select_analysis(rasterpoints_shp, point0_shp, "\"FID\" >=0 AND \"FID\" <100000")

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(point0_shp, Output_Layer, "", "", "Shape Shape VISIBLE NONE;FID FID VISIBLE NONE;POINTID POINTID VISIBLE NONE;GRID_CODE GRID_CODE VISIBLE NONE")


# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Output_Layer, "COMPLETELY_WITHIN", APPROACH_POLYGON_Project_shp, "", "NEW_SELECTION")

# Process: Select Layer By Location (2)
arcpy.SelectLayerByLocation_management(point0_Layer, "COMPLETELY_WITHIN", "", "", "SWITCH_SELECTION")

# Process: Feature To Point
arcpy.FeatureToPoint_management(point0__5_, FinalPoint0_shp, "CENTROID")

# Process: Add XY Coordinates
arcpy.AddXY_management(ProjectFinalPoint0_shp)

fc = "outfile"

prj = r"C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj"
spatRef = arcpy.SpatialReference(prj)
env.geographicTransformations = "NAD_1983_To_WGS_1984_1"

rows = arcpy.UpdateCursor(fc, "", spatRef)
for row in rows:
    geom = row.shape
    row.POINT_X = geom.centroid.X
    row.POINT_Y = geom.centroid.Y
    rows.updateRow(row)

del row, rows

# Process: Merge (2)
arcpy.Merge_management("'ProjectFinalPoint0.shp'", File1_shp, "POINTID \"POINTID\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINTID,-1,-1;GRID_CODE \"GRID_CODE\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,GRID_CODE,-1,-1;ORIG_FID \"ORIG_FID\" true true false 0 Long 0 0 ,First,#,ProjectFinalPoint0.shp,ORIG_FID,-1,-1;POINT_X \"POINT_X\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_X,-1,-1;POINT_Y \"POINT_Y\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_Y,-1,-1;POINT_Z \"POINT_Z\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_Z,-1,-1;POINT_M \"POINT_M\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_M,-1,-1")


print"Finished"
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: luke.kaim

I made some changes to the code.

import arcpy, sys, os, re,glob
from arcpy import env

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\Users\\Luke Kaim\\Documents\\university_of_maine\\research4\\test2"
rasterpoints_shp = "rasterpoints.shp"



fieldName= "FID"
recordsCounted = 0
number=0
featureClass = "rasterpoints_shp"
i=0
nextFile = str(outfile[i+1])+".shp"

rows = arcpy.SearchCursor(featureClass)

for row in rows:
    number += row.getValue(fieldName)
if recordsCounted ==1000000:
     arcpy.CreateFeature(nextFile,"")
recordsCounted =0
i=i+1

recordsCounted += 1
row = rows.next()

for files in glob.glob("outfile" "+" "*"):
    


    # Process: Make Feature Layer
    arcpy.MakeFeatureLayer_management(files, Output_Layer, "", "", "Shape Shape VISIBLE NONE;FID FID VISIBLE NONE;POINTID POINTID VISIBLE NONE;GRID_CODE GRID_CODE VISIBLE NONE")


    # Process: Select Layer By Location
    arcpy.SelectLayerByLocation_management(files, "COMPLETELY_WITHIN", APPROACH_POLYGON_Project_shp, "", "NEW_SELECTION")

    # Process: Select Layer By Location (2)
    arcpy.SelectLayerByLocation_management(files, "COMPLETELY_WITHIN", "", "", "SWITCH_SELECTION")

    # Process: Feature To Point
    arcpy.FeatureToPoint_management(files, FinalPoint0_shp, "CENTROID")

    # Process: Add XY Coordinates
    arcpy.AddXY_management(files)

    fc = "files"

    prj = r"C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj"
    spatRef = arcpy.SpatialReference(prj)
    env.geographicTransformations = "NAD_1983_To_WGS_1984_1"

    rows = arcpy.UpdateCursor(fc, "", spatRef)
    for row in rows:
        geom = row.shape
        row.POINT_X = geom.centroid.X
        row.POINT_Y = geom.centroid.Y
        rows.updateRow(row)

        del row, rows

        # Process: Merge (2)
        arcpy.Merge_management("'files'", File1_shp, "POINTID \"POINTID\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINTID,-1,-1;GRID_CODE \"GRID_CODE\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,GRID_CODE,-1,-1;ORIG_FID \"ORIG_FID\" true true false 0 Long 0 0 ,First,#,ProjectFinalPoint0.shp,ORIG_FID,-1,-1;POINT_X \"POINT_X\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_X,-1,-1;POINT_Y \"POINT_Y\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_Y,-1,-1;POINT_Z \"POINT_Z\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_Z,-1,-1;POINT_M \"POINT_M\" true true false 0 Double 0 0 ,First,#,ProjectFinalPoint0.shp,POINT_M,-1,-1")


print"Finished"
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Luke,

You could do something similar to the code below.  You will just need to update it for a shapefile rather than a feature class:

import arcpy, os
from arcpy import env
env.overwriteOutput = True
env.workspace = r"C:\temp\python\test.gdb"

fc = "Cities"

count = str(arcpy.GetCount_management(fc))

x = 0
y = 1000000
z = 1

while x < int(count):
    where = "OBJECTID > " + str(x) + "AND OBJECTID <= " + str(y)
    arcpy.MakeFeatureLayer_management(fc, "Cities_FL", where)
    arcpy.CopyFeatures_management("Cities_FL", "Cities_" + str(z))
    x += 1000000
    y += 1000000
    z += 1
0 Kudos