This lesson is being piloted (Beta version)

Using ArcPy to partition grid into smaller boxes

Overview

Teaching: 5 min min
Exercises: 0 min
Questions
  • Using ArcPy to divide the shapefile into smaller boxes

Objectives

Introduction

Step 1: Create the boundary

image

Step 2: Split the big polygon into smaller 49mi2 grid:

image

image

import geopandas as gpd
dir = "/home/tuev/Projects/Makris/GIS/"
shape = gpd.read_file(dir+"DFW77.shp")
for i in shape.PageName:
    shapeout = shape[shape.PageName==i]
    shapeout.to_file(dir+i+".shp")

Step 4: Apply the same Grid Index Feature to split 300 grids to 0.16mi2 grid

# Import modules
import arcpy, os
from arcpy import env
import numpy as np
from zipfile import ZipFile

# Set environment settings to folders:
dir="c:/SMU/PROJECTS/Makris_cellphone/GIS/Miami/49mi2/" # <== This needs to be changed and make sure "/" is used instead of "\"
os.chdir(dir)

os.mkdir("../output0404")
arcpy.env.workspace = dir
output_folder = "../output0404/"

# Create the list of name of unique shapefile:
List1 = os.listdir(dir)
List2 = list()
for i in List1:
    pathname,extension = os.path.splitext(dir+i)
    filename = pathname.split('/')
    List2.append(filename[-1])

FinalList = np.unique(List2)

# Create the output folder output0404 and use ArcPy to generate the files

for i in FinalList:
    print(i)
    #Set local variables
    outFeatureClass = output_folder+i
    inFeatures = i
    
    polygonWidth = "0.4 miles"
    polygonHeight = "0.4 miles"
    
    # Execute GridIndexFeatures:
    arcpy.GridIndexFeatures_cartography(outFeatureClass,inFeatures,"","","",
                                        polygonWidth,polygonHeight)

The following grids are created:

image

Step 5: Zip the 1500 files into 300 zip files as input request for Vista Nears using the same python notebook from Step 4

os.chdir(dir+output_folder)
for i in FinalList:
    with ZipFile(i+'_Miami.zip','w') as zipObj:
        zipObj.write(i+'.shp')
        zipObj.write(i+'.shx')
        zipObj.write(i+'.dbf')
        zipObj.write(i+'.sbn')
        zipObj.write(i+'.sbx')
        zipObj.write(i+'.prj')

It is ready to use these zipped shapefile image

Key Points

  • ArcPy, ArcGIS Pro