# Import arcpy module import arcpy import os arcpy.env.overwriteOutput = True strWorkspace = "C:\Users\hamalainens\Data\GDB\FarmstoRegionsFinal.gdb" strWorking_Folder = os.path.join(strWorkspace, "Outputs") arcpy.env.workspace = strWorking_Folder arcpy.CheckOutExtension("GeoStats") # Input shapefiles strIDUs = os.path.join(strWorkspace, "IDU_Update") strCram = os.path.join(strWorkspace, "CRAM9_IntersectCADS") strCramLots = os.path.join(strWorkspace, "CRAM_9_cadastre_attr_UTM_lots") strCADsClean = os.path.join(strWorkspace, "SelectedCADS") strRdmPointCADs = os.path.join(strWorkspace, "Random_Points") # Fields strCram_ID_Field = "CAD_ID" #Define SearchCursor cursor_Cram = arcpy.SearchCursor(strCram) #Start for loop, looping through each row for rowCram in cursor_Cram: #Define the variable for the current row CAD_ID strCram_ID = str(rowCram.getValue(strCram_ID_Field)) #Variable for CAD_ID temporary file strCram_Current = "Cram_" + str(strCram_ID[0:6]) print strCram_ID[0:6] #Make a layer for the current CAD lyrCurrent_Cram = arcpy.MakeFeatureLayer_management(strCram,strCram_Current, "\"" + strCram_ID_Field + "\" = " + strCram_ID + "") #Variable for buffer zone strBuffer_Output = "Buff_" + str(strCram_ID[0:6]) print "Searching 500 metre zone for suitable IDU areas....." #Create buffer zone, looking in a 500 meter radius strBuffer = arcpy.Buffer_analysis(lyrCurrent_Cram, strBuffer_Output, "500 Meters", "OUTSIDE_ONLY", "ROUND", "NONE", "") #Variable for current IDU strCurrent_IDUs = "IDU_" + strCram_ID[0:6] #Make current IDU layer lyrCurrent_IDU = arcpy.MakeFeatureLayer_management(strIDUs, strCurrent_IDUs) #Select IDUs that fall within the buffer zone strIDUs_Select_Location = arcpy.SelectLayerByLocation_management(lyrCurrent_IDU, "INTERSECT", strBuffer, "", "NEW_SELECTION") #Select attribute based on farm type strIDUs_Select_Attribute = arcpy.SelectLayerByAttribute_management(strIDUs_Select_Location, "SUBSET_SELECTION", "\"LC_CLASS\" = 'Shrubland' OR \"LC_CLASS\" = 'Hay/Pasture'") #Variable for dissolve strDissolve_Output = "Dissolve_" + strCram_ID[0:6] #Dissolve based on CAD_ID strDissolve = arcpy.Dissolve_management(strIDUs_Select_Attribute, strDissolve_Output, "CAD_ID", "", "MULTI_PART", "DISSOLVE_LINES") #Variable for erase strErase_Output = "Erase_" + strCram_ID[0:6] #Erase the CAD boundary of the current row CAD_ID strErase = arcpy.Erase_analysis(strDissolve, lyrCurrent_Cram, strErase_Output) #Create variable for Random Point strRdmPoint_MergeCAD = "RandomPoint_" + strCram_ID[0:6] #Create a master random point layer strRdmPoint_Merge = arcpy.MakeFeatureLayer_management(strRdmPointCADs, strRdmPoint_MergeCAD) print strRdmPoint_Merge CountResult = int(arcpy.GetCount_management(strErase).getOutput(0)) print "Found " + str(CountResult) + " suitable IDU areas" if CountResult < 2: print "Cannot find enough suitable IDU areas searching 1,000 metre zone....." #Variable for buffer zone strBuffer_Output = "Buff_" + str(strCram_ID[0:6]) #Create buffer zone, looking in a 1000 meter radius strBuffer = arcpy.Buffer_analysis(lyrCurrent_Cram, strBuffer_Output, "1000 Meters", "OUTSIDE_ONLY", "ROUND", "NONE", "") #Variable for current IDU strCurrent_IDUs = "IDU_" + strCram_ID[0:6] #Make current IDU layer lyrCurrent_IDU = arcpy.MakeFeatureLayer_management(strIDUs, strCurrent_IDUs) #Select IDUs that fall within the buffer zone strIDUs_Select_Location = arcpy.SelectLayerByLocation_management(lyrCurrent_IDU, "INTERSECT", strBuffer, "", "NEW_SELECTION") #Select attribute based on farm type strIDUs_Select_Attribute = arcpy.SelectLayerByAttribute_management(strIDUs_Select_Location, "SUBSET_SELECTION", "\"LC_CLASS\" = 'Shrubland' OR \"LC_CLASS\" = 'Hay/Pasture'") #Variable for dissolve strDissolve_Output = "Dissolve_" + strCram_ID[0:6] #Dissolve based on CAD_ID strDissolve = arcpy.Dissolve_management(strIDUs_Select_Attribute, strDissolve_Output, "CAD_ID", "", "MULTI_PART", "DISSOLVE_LINES") #Variable for erase strErase_Output = "Erase_" + strCram_ID[0:6] #Erase the CAD boundary of the current row CAD_ID strErase = arcpy.Erase_analysis(strDissolve, lyrCurrent_Cram, strErase_Output) #Create variable for Random Point strRdmPoint_MergeCAD = "RandomPoint_" + strCram_ID[0:6] #Create a master random point layer strRdmPoint_Merge = arcpy.MakeFeatureLayer_management(strRdmPointCADs, strRdmPoint_MergeCAD) CountResult = int(arcpy.GetCount_management(strErase).getOutput(0)) print "Found " + str(CountResult) + " suitable IDU areas" if CountResult < 2: print "Cannot find enough suitable IDU areas searching 1,500 metre zone...." #Variable for buffer zone strBuffer_Output = "Buff_" + str(strCram_ID[0:6]) #Create buffer zone, looking in a 1500 meter radius strBuffer = arcpy.Buffer_analysis(lyrCurrent_Cram, strBuffer_Output, "1500 Meters", "OUTSIDE_ONLY", "ROUND", "NONE", "") #Variable for current IDU strCurrent_IDUs = "IDU_" + strCram_ID[0:6] #Make current IDU layer lyrCurrent_IDU = arcpy.MakeFeatureLayer_management(strIDUs, strCurrent_IDUs) #Select IDUs that fall within the buffer zone strIDUs_Select_Location = arcpy.SelectLayerByLocation_management(lyrCurrent_IDU, "INTERSECT", strBuffer, "", "NEW_SELECTION") #Select attribute based on farm type strIDUs_Select_Attribute = arcpy.SelectLayerByAttribute_management(strIDUs_Select_Location, "SUBSET_SELECTION", "\"LC_CLASS\" = 'Shrubland' OR \"LC_CLASS\" = 'Hay/Pasture'") #Variable for dissolve strDissolve_Output = "Dissolve_" + strCram_ID[0:6] #Dissolve based on CAD_ID strDissolve = arcpy.Dissolve_management(strIDUs_Select_Attribute, strDissolve_Output, "CAD_ID", "", "MULTI_PART", "DISSOLVE_LINES") #Variable for erase strErase_Output = "Erase_" + strCram_ID[0:6] #Erase the CAD boundary of the current row CAD_ID strErase = arcpy.Erase_analysis(strDissolve, lyrCurrent_Cram, strErase_Output) #Create variable for Random Point strRdmPoint_MergeCAD = "RandomPoint_" + strCram_ID[0:6] #Create a master random point layer strRdmPoint_Merge = arcpy.MakeFeatureLayer_management(strRdmPointCADs, strRdmPoint_MergeCAD) CountResult = int(arcpy.GetCount_management(strErase).getOutput(0)) print "Found " + str(CountResult) + " suitable IDU areas" if CountResult < 2: print "Cannot find enough suitable IDU areas searching 2,000 metre zone....." #Variable for buffer zone strBuffer_Output = "Buff_" + str(strCram_ID[0:6]) #Create buffer zone, looking in a 2000 meter radius strBuffer = arcpy.Buffer_analysis(lyrCurrent_Cram, strBuffer_Output, "2000 Meters", "OUTSIDE_ONLY", "ROUND", "NONE", "") #Variable for current IDU strCurrent_IDUs = "IDU_" + strCram_ID[0:6] #Make current IDU layer lyrCurrent_IDU = arcpy.MakeFeatureLayer_management(strIDUs, strCurrent_IDUs) #Select IDUs that fall within the buffer zone strIDUs_Select_Location = arcpy.SelectLayerByLocation_management(lyrCurrent_IDU, "INTERSECT", strBuffer, "", "NEW_SELECTION") #Select attribute based on farm type strIDUs_Select_Attribute = arcpy.SelectLayerByAttribute_management(strIDUs_Select_Location, "SUBSET_SELECTION", "\"LC_CLASS\" = 'Shrubland' OR \"LC_CLASS\" = 'Hay/Pasture'") #Variable for dissolve strDissolve_Output = "Dissolve_" + strCram_ID[0:6] #Dissolve based on CAD_ID strDissolve = arcpy.Dissolve_management(strIDUs_Select_Attribute, strDissolve_Output, "CAD_ID", "", "MULTI_PART", "DISSOLVE_LINES") #Variable for erase strErase_Output = "Erase_" + strCram_ID[0:6] #Erase the CAD boundary of the current row CAD_ID strErase = arcpy.Erase_analysis(strDissolve, lyrCurrent_Cram, strErase_Output) #Create variable for Random Point strRdmPoint_MergeCAD = "RandomPoint_" + strCram_ID[0:6] #Create a master random point layer strRdmPoint_Merge = arcpy.MakeFeatureLayer_management(strRdmPointCADs, strRdmPoint_MergeCAD) CountResult = int(arcpy.GetCount_management(strErase).getOutput(0)) print "Found " + str(CountResult) + " suitable IDU areas" if CountResult < 2: print "Cannot find enough suitable IDU areas, creating points that are available...." #Create variable random points strRdmPoint_Output = "Random_" + strCram_ID[0:6] #Create the random points, putting one in each of the subset of CADs you selected strRdmPoint = arcpy.CreateRandomPoints_management(strWorking_Folder, strRdmPoint_Output, strErase, "", 1) cursorErase = arcpy.SearchCursor(strErase) for rowsErase in cursorErase: whereClause1 = "CAD_ID = " + str(rowsErase.getValue("CAD_ID")) print whereClause1 arcpy.SelectLayerByAttribute_management(lyrCurrent_IDU, "NEW_SELECTION", whereClause1) arcpy.DeleteFeatures_management(lyrCurrent_IDU) #Add fields to the random point layer arcpy.AddField_management(strRdmPoint, "SLC_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "CLI_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "FIELD_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "Iteration", "TEXT", "", "", "254") #Populate these fields with the data associated with the HQ point cursor = arcpy.UpdateCursor(strRdmPoint) for RdmRow in cursor: RdmRow.setValue("SLC_ID", rowCram.getValue("SLC_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("CLI_ID", rowCram.getValue("CLI_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("FIELD_ID", rowCram.getValue("FIELD_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("Iteration", rowCram.getValue("Iteration")) cursor.updateRow(RdmRow) #Append the Random point layers made this iteration to the master Random point layer arcpy.Append_management(strRdmPoint, strRdmPoint_Merge) #----- Delete "current point" and "CAD" layers ----- if arcpy.Exists(strRdmPoint_Output): arcpy.Delete_management(strRdmPoint_Output) if arcpy.Exists(strDissolve): arcpy.Delete_management(strDissolve) #----- Delete "Iteration" layers ----- if arcpy.Exists(lyrCurrent_IDU): arcpy.Delete_management(lyrCurrent_IDU) if arcpy.Exists(strBuffer_Output): arcpy.Delete_management(strBuffer_Output) if arcpy.Exists(strDissolve_Output): arcpy.Delete_management(strDissolve_Output) if arcpy.Exists(strRdmPoint_MergeCAD): arcpy.Delete_management(strRdmPoint_MergeCAD) if arcpy.Exists(strErase_Output): arcpy.Delete_management(strErase_Output) if arcpy.Exists(strDissolve): arcpy.Delete_management(strDissolve) continue #Create a variable for subset layer strSubset = strWorking_Folder + "Subset_" + strCram_ID[0:6] #Subest the amount of different CADs you want selected strSubsetCADS = arcpy.SubsetFeatures_ga(strErase, strSubset, "", 2, "ABSOLUTE_VALUE") #Create variable random points strRdmPoint_Output = "Random_" + strCram_ID[0:6] #Create the random points, putting one in each of the subset of CADs you selected strRdmPoint = arcpy.CreateRandomPoints_management(strWorking_Folder, strRdmPoint_Output, strSubsetCADS, "", 1) rowSubset = arcpy.SearchCursor(strSubsetCADS) for rows in rowSubset: whereClause1 = "CAD_ID = " + str(rows.getValue("CAD_ID")) print whereClause1 arcpy.SelectLayerByAttribute_management(lyrCurrent_IDU, "NEW_SELECTION", whereClause1) arcpy.DeleteFeatures_management(lyrCurrent_IDU) #Add fields to the random point layer arcpy.AddField_management(strRdmPoint, "SLC_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "CLI_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "FIELD_ID", "TEXT", "", "", "254") arcpy.AddField_management(strRdmPoint, "Iteration", "TEXT", "", "", "254") #Populate these fields with the data associated with the HQ point cursor = arcpy.UpdateCursor(strRdmPoint) for RdmRow in cursor: RdmRow.setValue("SLC_ID", rowCram.getValue("SLC_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("CLI_ID", rowCram.getValue("CLI_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("FIELD_ID", rowCram.getValue("FIELD_ID")) cursor.updateRow(RdmRow) RdmRow.setValue("Iteration", rowCram.getValue("Iteration")) cursor.updateRow(RdmRow) #Append the Random point layers made this iteration to the master Random point layer arcpy.Append_management(strRdmPoint, strRdmPoint_Merge) #----- Delete "current point" and "CAD" layers ----- if arcpy.Exists(strRdmPoint_Output): arcpy.Delete_management(strRdmPoint_Output) if arcpy.Exists(strDissolve): arcpy.Delete_management(strDissolve) if arcpy.Exists(strSubsetCADS): arcpy.Delete_management(strSubsetCADS) #----- Delete "Iteration" layers ----- if arcpy.Exists(lyrCurrent_IDU): arcpy.Delete_management(lyrCurrent_IDU) if arcpy.Exists(strBuffer_Output): arcpy.Delete_management(strBuffer_Output) if arcpy.Exists(strDissolve_Output): arcpy.Delete_management(strDissolve_Output) if arcpy.Exists(strRdmPoint_MergeCAD): arcpy.Delete_management(strRdmPoint_MergeCAD) if arcpy.Exists(strErase_Output): arcpy.Delete_management(strErase_Output) if arcpy.Exists(strDissolve): arcpy.Delete_management(strDissolve) print("\nCompleted...")