Hi Everyone,
The goal of the code is to test how similar two polygons are. I do this by comparing bounding boxes as well as shape area. The code is working and giving correct printed values, but the intersection operation only creates two polygons. I expected it to create a polygon wherever two polygons overlap. That is not the result I am getting. I am using nested for loops so I do not see where my issue is stemming from. I tried using append , but that seems like that is not working either. I think my logic is close to being correct. Does anyone know why I would be only seeing two polygons in the newly created intersection shapefile? Do I need another for loop?
Thank you in advance for looking at my code.
Luke Kaim (SIE)
Lucas.Kaim@maine.edu
(914)263-7866
“Don't dwell on what went wrong. Instead, focus on what to do next. Spend your energies on moving forward toward finding the answer" (Denis Waitley).
The goal of the code is to test how similar two polygons are. I do this by comparing bounding boxes as well as shape area. The code is working and giving correct printed values, but the intersection operation only creates two polygons. I expected it to create a polygon wherever two polygons overlap. That is not the result I am getting. I am using nested for loops so I do not see where my issue is stemming from. I tried using append , but that seems like that is not working either. I think my logic is close to being correct. Does anyone know why I would be only seeing two polygons in the newly created intersection shapefile? Do I need another for loop?
Code:
## Luke Kaim
## This code should test if two polygons are similar.
#definition main
def main():
#try block
try:
# import modules
import arcpy, sys, traceback, os, string, locale,arcgisscripting
# overwrite environment parameters
arcpy.env.overwriteOutput = True
#set work space.
from arcpy import env
env.workspace =r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar"
# Input shapefiles.
VGIFile = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\vgi2.shp"
GNIS_FC = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\gnis2.shp"
polyint= []
#Definition to test if there are overlapping bounding box between two polygons
def boundingbox():
#output variable names.
outputVGIboundingbox="VGIboundingbox.shp"
outputGNISboundingbox="GNISboundingbox.shp"
# This code tests if the file exists and if it does delete it. Then create the bounding box around every polygon.
if arcpy.Exists(outputGNISboundingbox):
arcpy.Delete_management(outputGNISboundingbox)
GNISboundingbox= arcpy.FeatureEnvelopeToPolygon_management(GNIS_FC,outputGNISboundingbox,"MULTIPART")
# This code tests if the file exists and if it does delete it. Then create the bounding box around every polygon.
if arcpy.Exists(outputVGIboundingbox):
arcpy.Delete_management(outputVGIboundingbox)
VGIboundingbox= arcpy.FeatureEnvelopeToPolygon_management(VGIFile,outputVGIboundingbox,"MULTIPART")
# Create search cursor for the VGI shapefile
VGIboundingbox_Rows=arcpy.SearchCursor(outputVGIboundingbox)
# Describe is being used to describe the shapefield and make it accessible.
VGIboundingbox_FileShapeName = arcpy.Describe(outputVGIboundingbox).ShapeFieldName
# for loop to iterate over the rows in the vgi bounding box.
for VGIboundingRow in VGIboundingbox_Rows:
# Get value is being used to get the shape field value for ever row.
VGIboundingGEOM= VGIboundingRow.getValue(VGIboundingbox_FileShapeName)
# Create search cursor for the GNIS bounding box.
GNISboundingbox_Rows = arcpy.SearchCursor(outputGNISboundingbox)
# Describe is being used to describe the shapefield and make it accessible.
GNISboundingbox_FCShapeName = arcpy.Describe(outputGNISboundingbox).ShapeFieldName
#For loop to iterate over the gnis bounding box rows
for GNISboundingRow in GNISboundingbox_Rows:
#Get value is being used to get the shape field value for ever row.
GNISboundingGEOM=GNISboundingRow.getValue(GNISboundingbox_FCShapeName)
# This if statement test if the gnis bounding box overlaps vgi bounding box
if GNISboundingGEOM.overlaps(VGIboundingGEOM):
# If it does then get the VGI bounding box area.
VGIboundingAREA=VGIboundingGEOM.area
# If it does then get the VGI bounding box length.
VGIboundingLength=VGIboundingGEOM.length
# If it does then get the GNIS bounding box area.
GNISboundingAREA=GNISboundingGEOM.area
# If it does then get the GNIS bounding box length.
GNISboundingLength=GNISboundingGEOM.length
#Print statement to test if the two polygons overlap.
print 'true'
# This is where the intersection operation takes place. If the two polygons overlap or contains or equal then do the intersection.
test2=arcpy.Intersect_analysis([GNISboundingGEOM,VGIboundingGEOM],"boundingint.shp")
polyint.add(test2)
# Create search cursor for the intersection shapefile.
intersectionboundingboxRow = arcpy.SearchCursor("boundingint.shp")
# Describe is being used to get access to the field id.
intersectionboundingboxDecs= arcpy.Describe("boundingint.shp").OIDFieldName
# For loop to iterate over the rows in the intersection file.
for intersection in intersectionboundingboxRow:
# Get acess to the shape field.
intersectionboundingboxAREA=intersection.Shape.area
# Get acess to the shapefield.
intersectionboundingboxLENGTH=intersection.Shape.length
# Overlap 1 is equal to the overlap bounding box area / by the vgi bounding box.
overlap1=intersectionboundingboxAREA/VGIboundingAREA
# Overlap 2 is equal to the overlap bounding box area / by the GNIS bounding box.
overlap2=intersectionboundingboxAREA/GNISboundingAREA
# Overlap 3 is equal to the overlap bounding box area / by the VGI bounding box.
overlap3=intersectionboundingboxLENGTH/VGIboundingLength
# Overlap 4 is equal to the overlap boundinf box area / by the GNIS bounding box.
overlap4=intersectionboundingboxLENGTH/GNISboundingLength
# get value is being used to get the field id in each row.
fieldID2=intersection.getValue(intersectionboundingboxDecs)
#print statement
for row in test2:
print "overlap bounding box / vgi bounding box: " + str(overlap1)+ " " +str(fieldID2)
print "overlap bounding box / GNIS bounding box: " + str(overlap2)+ " " +str(fieldID2)
print "overlap bounding box perimeter/ vgi perimeter" + str(overlap3)+ " " + str(fieldID2)
print "overlap bounding box perimeter/ GNIS perimeter"+ str(overlap4)+ " " + str (fieldID2)
# Else print false. This will tell if the two polygons do not overlap.
else: print 'false'
# del search cursor.
del VGIboundingbox_Rows,GNISboundingbox_Rows
boundingbox()
Thank you in advance for looking at my code.
Luke Kaim (SIE)
Lucas.Kaim@maine.edu
(914)263-7866
“Don't dwell on what went wrong. Instead, focus on what to do next. Spend your energies on moving forward toward finding the answer" (Denis Waitley).