0

I'm trying to achieve a python script to take a TileGrid (fishnet style) of polygons. First I want to add 5 new fields to the shapefile. TileID (Text), YMin (double), YMax (double), XMin (double), and XMax (double). Then the final goal would be to spit a txt file with those fields in the end. Ideally this text file would not include "" and : between the fields. Although, I'm not sure that will be possible.

Now I was able to achieve this (with some success) in ModelBuilder. However, I'd like to put it into an ArcPy code. I had ModelBuilder send out its version of the Python code (attached). However, it is very messy. Would anyone please help me clean this up some? I am very new to Py So I am really insterested in how this should be done.

# Import arcpy module
import arcpy


# Local variables:
Shapefile = ""
Output_Feature_Class__7_ = ""
Output_Feature_Class = ""
Output_Feature_Class__3_ = ""
Output_Feature_Class__4_ = ""
Output_Feature_Class__5_ = ""
Output_Feature_Class__6_ = ""
Output_Feature_Class__2_ = ""
Output_Feature_Class__8_ = ""
Output_Feature_Class__9_ = ""
v2016_lake_cook_county_qquads_shp__2_ = ""

# Process: Add Field
arcpy.AddField_management(Shapefile, "TileID", "TEXT", "", "", "", "",            "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (2)
arcpy.AddField_management(Output_Feature_Class__3_, "MinX", "DOUBLE", "",      "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (3)
arcpy.AddField_management(Output_Feature_Class__4_, "MaxY", "DOUBLE", "",     "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (4)
arcpy.AddField_management(Output_Feature_Class__5_, "MaxX", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (5)
arcpy.AddField_management(Output_Feature_Class__6_, "MinY", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Output_Feature_Class__7_, "MinX", "!shape.extent.XMin!", "PYTHON_9.3", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Output_Feature_Class, "MaxY", "!shape.extent.YMax!", "PYTHON_9.3", "")

# Process: Calculate Field (3)
arcpy.CalculateField_management(Output_Feature_Class__2_, "MaxX", "!shape.extent.XMax!", "PYTHON_9.3", "")

# Process: Calculate Field (4)
arcpy.CalculateField_management(Output_Feature_Class__8_, "MinY",    "!shape.extent.YMin!", "PYTHON_9.3", "")

# Process: Calculate Field (5)
arcpy.CalculateField_management(Output_Feature_Class__9_, "TileID", "  [TILE_NAME]", "VB", "")

# Process: Add Attribute Index
arcpy.AddIndex_management(v2016_lake_cook_county_qquads_shp__2_,   "TileID;MinX;MaxY;MaxX;MinY", "OutputTest.txt", "UNIQUE", "NON_ASCENDING")

2 Answers 2

1

When cleaning up ModelBuilder -> Arcpy code I first reduce the variables (as these are often just referring to the same thing), and remove the (often unnecessary) comments and leave some more useful ones of my own. I also remove any extra whitespace to make it more readable - deleting extra spaces or extra lines.

import arcpy

shapefile = r"c:\temp\myshapefile.shp"

# Add new fields
arcpy.AddField_management(shapefile, "TileID", "TEXT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shapefile, "MinX", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shapefile, "MaxY", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shapefile, "MaxX", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(shapefile, "MinY", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Calculate new fields
arcpy.CalculateField_management(shapefile, "MinX", "!shape.extent.XMin!", "PYTHON_9.3", "")
arcpy.CalculateField_management(shapefile, "MaxY", "!shape.extent.YMax!", "PYTHON_9.3", "")
arcpy.CalculateField_management(shapefile, "MaxX", "!shape.extent.XMax!", "PYTHON_9.3", "")
arcpy.CalculateField_management(shapefile, "MinY", "!shape.extent.YMin!", "PYTHON_9.3", "")
arcpy.CalculateField_management(shapefile, "TileID", "[TILE_NAME]", "VB", "")

# Add Attribute Index
arcpy.AddIndex_management(shapefile, "TileID;MinX;MaxY;MaxX;MinY", "OutputTest.txt", "UNIQUE", "NON_ASCENDING")

This would benefit from a few loops, but as-is this is functional.

0

I agree that ModelBuider exports very messy Python code, which is why, while developing ArcPy code, I far prefer to run each tool from its tool dialog and then go to the Geoprocessing | Results window to Copy As Python Snippet instead.

This code from yours should be close:

# Import arcpy module
import arcpy

# Local variables:
Shapefile = r"C:\temp\test.shp"

# Process: Add Field
arcpy.AddField_management(Shapefile, "TileID", "TEXT", "", "", "", "",            "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (2)
arcpy.AddField_management(Shapefile, "MinX", "DOUBLE", "",      "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (3)
arcpy.AddField_management(Shapefile, "MaxY", "DOUBLE", "",     "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (4)
arcpy.AddField_management(Shapefile, "MaxX", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Add Field (5)
arcpy.AddField_management(Shapefile, "MinY", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Shapefile, "MinX", "!shape.extent.XMin!", "PYTHON_9.3", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Shapefile, "MaxY", "!shape.extent.YMax!", "PYTHON_9.3", "")

# Process: Calculate Field (3)
arcpy.CalculateField_management(Shapefile, "MaxX", "!shape.extent.XMax!", "PYTHON_9.3", "")

# Process: Calculate Field (4)
arcpy.CalculateField_management(Shapefile, "MinY",    "!shape.extent.YMin!", "PYTHON_9.3", "")

# Process: Calculate Field (5)
arcpy.CalculateField_management(Shapefile, "TileID", "  [TILE_NAME]", "VB", "")

# Process: Add Attribute Index
arcpy.AddIndex_management(Shapefile,   "TileID;MinX;MaxY;MaxX;MinY", "OutputTest.txt", "UNIQUE", "NON_ASCENDING")

It could be easily improved with some for loops but if all this answer reinforces is that ModelBuilder exports messy Python code then it will have achieved its aim.

2
  • Thank you both, I appreciate the help. Since I'm new to Python. Would an appropriate for statement come after setting the shapefile variable?
    – norban
    Commented Feb 23, 2017 at 16:09
  • Yes - look for places where you have multiple lines which are almost identical.
    – PolyGeo
    Commented Feb 23, 2017 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.