def execute(self, params, messages): extent = params[0].valueAsText grid_shape = params[1].valueAsText size = params[2].valueAsText marxan_db = params[3].valueAsText arcpy.env.workspace = "in_memory" pulayer = arcpy.GenerateTessellation_management( "pulayer", extent, grid_shape, size) grid_lyr = arcpy.MakeFeatureLayer_management(pulayer, "grid_lyr") arcpy.SelectLayerByLocation_management(grid_lyr, "INTERSECT", extent, "", "NEW_SELECTION", "INVERT") arcpy.DeleteFeatures_management(grid_lyr) arcpy.AddField_management(grid_lyr, "id", "LONG", 0) i = 1 with arcpy.da.UpdateCursor(pulayer, "id") as cursor: for row in cursor: row[0] = i cursor.updateRow(row) i += 1 arcpy.CopyFeatures_management( pulayer, os.path.join(marxan_db, "pulayer", "pulayer.shp")) return
def REALM_Core(dirR,raduis): address=dirR arcpy.env.overwriteOutput = True arcpy.env.parallelProcessingFactor="100%" arcpy.env.workspace=os.path.join(address) os.chdir(arcpy.env.workspace) ################################ fc = arcpy.ListFeatureClasses("*_Dissolved*")[0] arcpy.MakeFeatureLayer_management(fc,"dissolved_Layer") name=fc[0:2].upper() OIDField = arcpy.Describe(fc).OIDFieldName # get OID/FID field name auxfold="REALMOutput" ### if not os.path.exists(auxfold): os.mkdir(auxfold) pocursor = arcpy.SearchCursor ("dissolved_Layer") try: for porow in pocursor: print name+"->"+"FID"+str (porow.getValue(OIDField)) ##################### Subsetting sql = '"' + OIDField + '" = ' + str (porow.getValue(OIDField)) #SQL to select one feature arcpy.SelectLayerByAttribute_management ("dissolved_Layer", "", sql) #Select polygon feature by OID memoryFeature=auxfold+"\\"+name + "_FID_" + str (porow.getValue(OIDField))+".shp" arcpy.CopyFeatures_management("dissolved_Layer", memoryFeature) arcpy.SelectLayerByAttribute_management ("dissolved_Layer", "CLEAR_SELECTION") arcpy.MakeFeatureLayer_management(memoryFeature,"selected_Poly") ############## Tessleation description = arcpy.Describe(memoryFeature) extent = description.extent memoryFeature_Tes=auxfold+"\\"+name + "_Tessel_" + str (porow.getValue(OIDField))+".shp" arcpy.GenerateTessellation_management(memoryFeature_Tes, extent, "HEXAGON", raduis+" SquareMiles") ############### Clip memoryFeature_Tes_clip = auxfold + "\\" + "Tessel_cliped_"+ str (porow.getValue(OIDField))+".shp" arcpy.Clip_analysis(memoryFeature_Tes, memoryFeature, memoryFeature_Tes_clip) ############### Spatial Join Tes_spatialJoin=auxfold+"\\"+name + "_Tessel_Joined_" + str (porow.getValue(OIDField))+".shp" arcpy.SpatialJoin_analysis(memoryFeature_Tes_clip, "selected_Poly", Tes_spatialJoin) del porow arcpy.Delete_management(memoryFeature) arcpy.Delete_management(memoryFeature_Tes) arcpy.Delete_management(memoryFeature_Tes_clip) del pocursor #--- Now it's time to put togther small piceses arcpy.env.workspace=os.path.join(address,auxfold) arcpy.Merge_management(arcpy.ListFeatureClasses("*"),name+"_merged.shp") #--- deleting small pieces for objFeatureClass in arcpy.ListFeatureClasses("*_Tessel_Joined*"): arcpy.Delete_management(objFeatureClass) #-- Calculate centroid arcpy.AddField_management(name+"_merged.shp", "xCentroid", "DOUBLE", 18, 11) arcpy.AddField_management(name+"_merged.shp", "yCentroid", "DOUBLE", 18, 11) arcpy.CalculateField_management(name+"_merged.shp", "xCentroid", "!SHAPE.CENTROID.X!", "PYTHON_9.3") arcpy.CalculateField_management(name+"_merged.shp", "yCentroid", "!SHAPE.CENTROID.Y!", "PYTHON_9.3") print "Done ......" except Exception as e: print "Error processing", fc print "Error", e
def runGenerateHexbins(counties, hexSize, hexUnit, projectGDBPath): # print("\nGenerating tessellation hexagons...") # Set environment settings arcpy.env.workspace = r"C:\Users\rkpalmerjr\Documents\School\WISC\Fall_2019\GEOG_777_Capstone_in_GIS_Development\Project_1\TESTING\Test_1\Scratch" dataFolder = r"C:\Users\rkpalmerjr\Documents\School\WISC\Fall_2019\GEOG_777_Capstone_in_GIS_Development\Project_1\GEOG777_Project_1_Nitrate_Cancer\Data" # Set local variables for Generate Tessellation outputName = "Hexagons_" + str(hexSize) + "_sq_" + hexUnit output = os.path.join(projectGDBPath, outputName) extentFeature = os.path.join(dataFolder, counties) # https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/describe.htm description = arcpy.Describe(extentFeature) extent = description.extent area = str(hexSize) + " Square" + hexUnit spatial_ref = extent.spatialReference # Execute Generate Tessellation arcpy.GenerateTessellation_management(output, extent, "Hexagon", area, spatial_ref) hexBins = output # Delete the hexagons that don't intersect with the counties # http://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/select-layer-by-location.htm # Make layers for hexbins and counties hexBins_lyr = arcpy.MakeFeatureLayer_management(hexBins, "hexBins_lyr") counties_lyr = arcpy.MakeFeatureLayer_management(extentFeature, "counties_lyr") # Select hexagons that DO NOT intersect with the counties layer arcpy.SelectLayerByLocation_management(hexBins_lyr, "intersect", counties_lyr, "", "", "INVERT") # Delete the selection # http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/delete-features.htm arcpy.DeleteFeatures_management(hexBins_lyr) # Add a Unique ID integer field to use later in OLS regression arcpy.AddField_management(hexBins_lyr, "UID", "SHORT", "", "", "", "UID") # Calculate the UID field from the FID field arcpy.CalculateField_management(hexBins_lyr, "UID", "!OBJECTID!", "PYTHON") # Delete local variables del dataFolder, outputName, output, extentFeature, description, extent, area, spatial_ref, \ hexBins_lyr, counties_lyr # print("\nTessellation hexagons generated.") # Return hexBins to global scope return hexBins
def LetsTessellate(wksp, extent, output_projection, shape, sizes, units): arcpy.env.workspace = wksp arcpy.env.overwriteOutput = True SizeLST = [] Output_File_Name = wksp Output = Output_File_Name PrintLST = [] GT_LST = [] # PREP # preparing Units string for adding to Unit_Sizes_LST Unit_Glue = " " + Units + "," # Joining prepped units to list string = Unit_Glue.join(Unit_Sizes_LST) # Creating new updated list from string SizeLST = string.split(",") # Remove last item of list and replace with updated item string = SizeLST.pop() string = string + " " + Units SizeLST.append(string) count = 0 unitSize = [] # Body ## loop through unit sizes for size in range(len(SizeLST)): unitSize = SizeLST[size] GT = "GT_" + Unit_Sizes_LST[count] + "mile" GT = Output + "/" + GT GT_LST.append(GT) PrintLST.append("Generated " + unitSize[:-1] + " " + Shape.lower() + " tessellations:") # Run Generate Tessellation arcpy.GenerateTessellation_management( GT, extent, # extent or FC to get extent from Shape, # tessellation shape (HEXAGON, TRIANGLE, SQUARE) unitSize, output_projection) count += 1 DICT = {'a': PrintLST, 'b': GT_LST} return DICT
def get_hexbins_full_extent(block_groups, output_hexbin_feature_class): """ Get hexbins covering the full rectangular extent of the area of interest. :param block_groups: Block groups covering the area of interest. :param output_hexbin_feature_class: Output path to the feature class to store the hexbins. :return: Path to the output hexbin feature class. """ # get a describe object of the block groups describe = arcpy.Describe(block_groups) # use the generate tesselation tool to build the hexbins return arcpy.GenerateTessellation_management( Output_Feature_Class=output_hexbin_feature_class, Extent=describe.extent, # get the extent from the describe object Shape_Type='HEXAGON', Size=get_hex_area_from_feature_extents(block_groups), # using the logic above to get the hexagon area Spatial_Reference=describe.spatialReference # use the same spatial reference as the input block groups )[0]
def runGenerateHexbins(counties, hexSize, hexUnit, projectGDBPath): # arcpy.AddMessage("\nGenerating tessellation hexagons...") # Set local variables for Generate Tessellation outputName = "Hexagons_" + hexSize + "_Sq_" + hexUnit if ("." in outputName): outputName = outputName.replace(".", "point") output = os.path.join(projectGDBPath, outputName) # https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/describe.htm description = arcpy.Describe(counties) extent = description.extent area = hexSize + " Square" + hexUnit spatial_ref = extent.spatialReference # Execute Generate Tessellation arcpy.GenerateTessellation_management(output, extent, "Hexagon", area, spatial_ref) # Get Generate Tesselation Messages arcpy.AddMessage("\n" + arcpy.GetMessages()) # hexBins = output # Delete the hexagons that don't intersect with the counties # http://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/select-layer-by-location.htm # Make layers for hexbins and counties hexBinsLyr = arcpy.MakeFeatureLayer_management(hexBins, "hexBinsLyr") # arcpy.AddMessage("\n" + arcpy.GetMessages()) counties_lyr = arcpy.MakeFeatureLayer_management(counties, "counties_lyr") # arcpy.AddMessage("\n" + arcpy.GetMessages()) # Select hexagons that intersect with the counties layer arcpy.SelectLayerByLocation_management(hexBinsLyr, "intersect", counties_lyr, "", "", "") # Switch selection arcpy.SelectLayerByLocation_management(hexBinsLyr, "", "", "", "SWITCH_SELECTION", "") # arcpy.AddMessage("\n" + arcpy.GetMessages()) # Delete the selection # http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/delete-features.htm arcpy.DeleteFeatures_management(hexBinsLyr) # arcpy.AddMessage("\n" + arcpy.GetMessages()) # Add a Unique ID integer field to use later in OLS regression arcpy.AddField_management(hexBinsLyr, "UID", "SHORT", "", "", "", "UID") # arcpy.AddMessage("\n" + arcpy.GetMessages()) # Calculate the UID field from the FID field arcpy.CalculateField_management(hexBinsLyr, "UID", "!OBJECTID!", "PYTHON") # arcpy.AddMessage("\n" + arcpy.GetMessages()) # Add the output to the MXD arcpy.SetParameterAsText(7, hexBinsLyr) # arcpy.AddMessage("\nTessellation hexagons generated:\n" + hexBins) # Delete local variables del outputName, output, description, extent, area, spatial_ref, \ hexBins, counties_lyr # Return hexBins to global scope return hexBinsLyr
def GenerateHexGrid(self, gridName): hex_extent = self.GetWorkSpaceExtent() spatial_ref = arcpy.SpatialReference(4326) arcpy.GenerateTessellation_management(gridName, hex_extent, "HEXAGON", "0.5 SquareKilometers", spatial_ref)