Using ArcPy to partition grid into smaller boxes
Overview
Teaching: 5 min min
Exercises: 0 minQuestions
Using ArcPy to divide the shapefile into smaller boxes
Objectives
Introduction
- Due to the restriction from NEAR portal that we cannot download the grid boxes with size bigger than 5 million sqft and the total area of all gridboxes cannot exceed 1.4 billion sqft
- Therefore, we need to divide the area of interest to smaller gridboxes.
- For example, the area of DFW can be divided to 300 small grids with size of 49 mi2 each, each of the 49 mi2 will again be divided into smaller 0.16 mi2 grid:
Step 1: Create the boundary
- Go to geojson.io
- Assuming that we have the boundary in term of lon/lat, we will need to create the shapefile for that.
- You can create the shapefile in ArcGIS or go to geojson.io and draw the polygon and modify the value:
- You can save the polygon as shapefile and open it in ArcGIS Pro
Step 2: Split the big polygon into smaller 49mi2 grid:
- Open the ArcGIS Pro and load in the polygon
- Click on Analyses\Tools to load the ArcGIS Toolbox
- Search for Grid Index Feature Tool and set the polygon width/height to 7 mi:
- The 300 grids are generated:
-
Save the file into shapefile: DFW77.shp
Step 3: Save 300 49mi2 grids to shapefiles
-
We can use python geopandas to save DFW77 to 300 shapefiles:
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")
- Total 1500 files are created for 300 shapefiles (each shapefile consists of 5 other files)
Step 4: Apply the same Grid Index Feature to split 300 grids to 0.16mi2 grid
- We cannot use Grid Index Features for 300 grids, therefore, we will use ArcPy which is ArcGIS library in python
- ArcPy is only available in ArcGIS Pro but not in HPC or Macs yet.
- To use ArcPy, click on Analyses\Python\Python Notebook
- The new notebook appears and we can type in the following code:
# 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:
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
Key Points
ArcPy, ArcGIS Pro