def EliminateSamllArea(shp): # try: # arcpy.AddField_management(shp,"Area","LONG",9,2) # except: # print "Area field already exists!" # # print "Calculating Area field..." # arcpy.CalculateField_management(shp,"Area", '!SHAPE.AREA!',"PYTHON_9.3") # print "finish Calculate Area field!" temp = os.path.join(os.path.dirname(shp),"temp") arcpy.MakeFeatureLayer_management(shp,"shp") # sql = '"Area" < 400' arcpy.SelectLayerByAttribute_management("shp","NEW_SELECTION", '"Area" = 0') print "Eliminating..." Eliminate = os.path.join(temp,"Eliminate.shp" ) print Eliminate arcpy.Eliminate_management("shp", Eliminate, "AREA") flag = True i = 1 while flag: print i arcpy.CalculateField_management(Eliminate,"Area", '!SHAPE.AREA!',"PYTHON_9.3") arcpy.MakeFeatureLayer_management(Eliminate,"Eliminate") arcpy.SelectLayerByAttribute_management("Eliminate","NEW_SELECTION",'"Area" = 0') FirstSelectionCount = arcpy.GetCount_management("Eliminate") print "FirstSelectionCount:",int(FirstSelectionCount.getOutput(0)) arcpy.SelectLayerByLocation_management("Eliminate", "SHARE_A_LINE_SEGMENT_WITH", "Eliminate", "", "NEW_SELECTION") SecondSelectionCount = arcpy.GetCount_management("Eliminate") print "SecondSelectionCount:",int(SecondSelectionCount.getOutput(0)) Chacount = int(SecondSelectionCount.getOutput(0)) - int(FirstSelectionCount.getOutput(0)) print "Chacount:",Chacount i = i + 1 if Chacount > 0: flag = True Eliminate = os.path.join(os.path.join(os.path.dirname(shp),"temp"),"Eliminate" + str(i) + ".shp" ) arcpy.Eliminate_management("Eliminate", Eliminate, "AREA") else: flag = False arcpy.Copy_management(Eliminate, os.path.join(os.path.dirname(shp),"Result")) print Eliminate
def Eliminate(inFeatures, outFeatureClass, expression, numero): nombre = arcpy.ValidateTableName( "M" + str(datetime.datetime.now().strftime("%b_%S")) + str(random.randrange(0, 50000)) + str(numero), "in_memory") templfeatures = arcpy.ValidateTableName( "blocklayer2" + "_" + str(datetime.datetime.now().strftime("%b_%S")) + str(random.randrange(0, 50000)) + str(numero), "in_memory") print inFeatures ## arcpy.MakeFeatureLayer_management(inFeatures, templfeatures) ## if ciclo==1: ## nombre = arcpy.ValidateTableName("M" + str(random.randrange(0,50000)),"in_memory") ## path = nombre arcpy.MakeFeatureLayer_management(grilla, templGrilla) arcpy.MakeFeatureLayer_management(inFeatures, templfeatures) ## arcpy.CopyFeatures_management(templfeatures, path) print "layer temporal1" fc_grilla = arcpy.SelectLayerByAttribute_management( templGrilla, "NEW_SELECTION", "PageNumber = %s" % str(numero)) print "selecionando grilla" fc_select = arcpy.SelectLayerByLocation_management( templfeatures, "have_their_center_in", templGrilla) print "seleccionando por centroides" arcpy.CopyFeatures_management(templfeatures, "in_memory" + "\\" + nombre) arcpy.MakeFeatureLayer_management("in_memory" + "\\" + nombre, "in_memory" + "\\" + nombre + "_lyr") arcpy.AddField_management(in_table="in_memory" + "\\" + nombre + "_lyr", field_name="Area", field_type="DOUBLE") arcpy.CalculateField_management(in_table="in_memory" + "\\" + nombre + "_lyr", field="Area", expression="!SHAPE.area!", expression_type="PYTHON") print "layer temporal2" print "layer seleccione" fc_filtro = arcpy.SelectLayerByAttribute_management( "in_memory" + "\\" + nombre + "_lyr", "NEW_SELECTION", expression) print "corriendo eliminate primer ciclo" ## try: arcpy.Eliminate_management(in_features="in_memory" + "\\" + nombre + "_lyr", out_feature_class=outFeatureClass, selection="LENGTH", ex_where_clause="", ex_features=capa_exclusion)
def elimSmallPolys(fc, outName, clusTol): """Runs Eliminate on all features in fc with area less than clusTol. This merges all small features to larger adjacent features. Arguments: fc {feature class} -- feature class to run operation over outName {feature class} -- reference to feature class clusTol {float} -- custer tolerance for specifying minimum shape area Returns: feature class -- reference to feature class that has had eliminate management run """ lyr = arcpy.MakeFeatureLayer_management(fc) arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", '"Shape_Area" < ' + str(clusTol)) out = arcpy.Eliminate_management(lyr, outName, 'LENGTH') arcpy.Delete_management(lyr) return out
def Eliminate(inFeatures, outFeatureClass, numero): nombre = arcpy.ValidateTableName( "M" + str(datetime.datetime.now().strftime("%b_%S")) + str(random.randrange(0, 50000)) + str(numero), "in_memory") templfeatures = arcpy.ValidateTableName( "blocklayer2" + "_" + str(datetime.datetime.now().strftime("%b_%S")) + str(random.randrange(0, 50000)) + str(numero), "in_memory") arcpy.MakeFeatureLayer_management(grilla, templGrilla) arcpy.MakeFeatureLayer_management(inFeatures, templfeatures) fc_grilla = arcpy.SelectLayerByAttribute_management( templGrilla, "NEW_SELECTION", "PageNumber = %s" % str(numero)) fc_select = arcpy.SelectLayerByLocation_management( templfeatures, "have_their_center_in", templGrilla) arcpy.CopyFeatures_management(templfeatures, "in_memory" + "\\" + nombre) arcpy.MakeFeatureLayer_management("in_memory" + "\\" + nombre, "in_memory" + "\\" + nombre + "_lyr") arcpy.AddField_management(in_table="in_memory" + "\\" + nombre + "_lyr", field_name="Area", field_type="DOUBLE") arcpy.CalculateField_management(in_table="in_memory" + "\\" + nombre + "_lyr", field="Area", expression="!SHAPE.area!", expression_type="PYTHON") fc_filtro = arcpy.SelectLayerByAttribute_management( "in_memory" + "\\" + nombre + "_lyr", "NEW_SELECTION", expresion_seleccion) arcpy.Eliminate_management(in_features="in_memory" + "\\" + nombre + "_lyr", out_feature_class=outFeatureClass, selection=selection, ex_where_clause=ex_where_clause, ex_features=capa_exclusion)
# Arcpy crashes on the instruction below: it seems you cannot union a layer with itself and keep gaps? # Do this manually instead and then restart try: arcpy.Union_analysis([["HLU_Manerase",1]], "HLU_Manerase_union", "ALL", 0, "NO_GAPS") except: print("Please do a manual union of HLU_Manerase with itself, keeping gaps. This crashed in Arcpy") exit() if elim_HLU_slivers: # Eliminate and then delete slivers (just deleting creates gaps). arcpy.MultipartToSinglepart_management("HLU_Manerase_union", "HLU_Manerase_union_sp") print(" Eliminating HLU slivers") arcpy.MakeFeatureLayer_management("HLU_Manerase_union_sp", "HLU_elim_layer") arcpy.SelectLayerByAttribute_management("HLU_elim_layer", where_clause="(Shape_Area <1)") arcpy.Eliminate_management("HLU_elim_layer", "HLU_Manerase_union_sp_elim") print(" Deleting remaining standalone slivers") arcpy.CopyFeatures_management("HLU_Manerase_union_sp_elim", "HLU_Manerase_union_sp_elim_del") arcpy.MakeFeatureLayer_management("HLU_Manerase_union_sp_elim_del", "HLU_del_layer") arcpy.SelectLayerByAttribute_management("HLU_del_layer", where_clause="Shape_Area < 1") arcpy.DeleteFeatures_management("HLU_del_layer") print(" Deleting larger gaps") arcpy.CopyFeatures_management("HLU_Manerase_union_sp_elim_del", "HLU_Manerase_union_sp_elim_del2") arcpy.MakeFeatureLayer_management("HLU_Manerase_union_sp_elim_del2", "HLU_del_gap_layer") arcpy.SelectLayerByAttribute_management("HLU_del_gap_layer", where_clause="(FID_HLU_Manerase=-1)") arcpy.DeleteFeatures_management("HLU_del_gap_layer") print(" Deleting identical HLU polygons") arcpy.CopyFeatures_management("HLU_Manerase_union_sp_elim_del2", "HLU_Manerase_union_sp_elim_delid")
def simplify(self): try: # Init WorkSpase # arcpy.env.overwriteOutput = 1 duongDanNguon = "C:/Generalize_25_50/50K_Process.gdb" duongDanDich = "C:/Generalize_25_50/50K_Final.gdb" urlFile = '/ConfigSimplify.json' _algorithm = "BEND_SIMPLIFY" _tolerance = "50 Meters" _error_option = "NO_CHECK" _collapsed_point_option = "NO_KEEP" _checkExitLayer = False if arcpy.Exists(duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM") and arcpy.Exists(duongDanNguon + "/PhuBeMat/PhuBeMat_Full"): #arcpy.CopyFeatures_management(duongDanNguon + "/PhuBeMat/PhuBeMat_LocMatNuoc", duongDanNguon + "/PhuBeMat/PhuBeMat") _checkExitLayer = True #Doc file config s1 = inspect.getfile(inspect.currentframe()) s2 = os.path.dirname(s1) urlFile = s2 + urlFile arcpy.AddMessage("\n# Doc file cau hinh: \"{0}\"".format(urlFile)) if os.path.exists(urlFile): fileConfig = open(urlFile) listLayerConfig = json.load(fileConfig) fileConfig.close() ############################### Simplify Polygon ######################################## arcpy.Integrate_management([[duongDanNguon + "/PhuBeMat/PhuBeMat", 1]], "2 Meters") arcpy.AddMessage("\n# Bat dau Simplify Polygon") listPolygon = [] fieldMappings = arcpy.FieldMappings() enableFields = [] inputsMerge = [] for objConfig in listLayerConfig: if objConfig["LayerType"] == "Polygon" and objConfig["RunStatus"] == "True": if not(_checkExitLayer == False and objConfig["LayerName"] == "PhuBeMat_Full"): temp = { "LayerType": objConfig["LayerType"], "DatasetName": objConfig["DatasetName"], "LayerName": objConfig["LayerName"], "featureLayer": "in_memory\\" + objConfig["LayerName"] + "_Layer", "featureCopy": "in_memory\\" + objConfig["LayerName"] + "_Copy", "featureCopyLayer": "in_memory\\" + objConfig["LayerName"] + "_Copy_Layer", "FID_XXX": "FID_" + objConfig["LayerName"] } listPolygon.append(temp) elif objConfig["LayerType"] == "Polyline" and objConfig["RunStatus"] == "True" and objConfig["LayerName"] <> "DuongBinhDo": if not(_checkExitLayer == False and objConfig["LayerName"] == "SongSuoiL_KenhMuongL_SnapPBM"): arcpy.AddMessage("\n# Buffer lop: \"{0}\"".format(objConfig["LayerName"])) layerPath = duongDanNguon + "/" + objConfig["DatasetName"] + "/" + objConfig["LayerName"] arcpy.Buffer_analysis(in_features = layerPath, out_feature_class = layerPath + "_Buffer", buffer_distance_or_field = "0.1 Meters", line_side = "RIGHT") temp = { "LayerType": objConfig["LayerType"], "DatasetName": objConfig["DatasetName"], "LayerName": objConfig["LayerName"] + "_Buffer", "featureLayer": "in_memory\\" + objConfig["LayerName"] + "_Buffer_Layer", "featureCopy": "in_memory\\" + objConfig["LayerName"] + "_Buffer_Copy", "featureCopyLayer": "in_memory\\" + objConfig["LayerName"] + "_Buffer_Copy_Layer", "FID_XXX": "FID_" + objConfig["LayerName"] } listPolygon.append(temp) for element in listPolygon: arcpy.AddMessage("\n# Xu ly lop: {0}".format(element["LayerName"])) layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] arcpy.MakeFeatureLayer_management(layerPath, element["featureLayer"]) arcpy.AddField_management(element["featureLayer"], element["FID_XXX"], "LONG") with arcpy.da.UpdateCursor(element["featureLayer"], ["OID@", element["FID_XXX"]]) as cursor: for row in cursor: row[1] = row[0] cursor.updateRow(row) arcpy.CopyFeatures_management(layerPath, element["featureCopy"]) arcpy.MakeFeatureLayer_management(element["featureCopy"], element["featureCopyLayer"]) ## Field Mappings ## enableFields.append(element["FID_XXX"]) fieldMappings.addTable(element["featureCopyLayer"]) inputsMerge.append(element["featureCopyLayer"]) for field in fieldMappings.fields: if field.name not in enableFields: fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name)) ## Merge ## arcpy.AddMessage("\n# Merge Polygon...") outPathMerge = "in_memory\\outPathMergeTemp" #outPathMerge = "C:/Generalize_25_50/50K_Process.gdb/DanCuCoSoHaTang/outPathMergeTemp" arcpy.Merge_management (inputsMerge, outPathMerge, fieldMappings) ## Simplify Polygon ## arcpy.AddMessage("\n# Simplify Polygon...") outPathSimplify = "in_memory\\outPathSimplifyTemp" #outPathSimplify = "C:/Generalize_25_50/50K_Process.gdb/DanCuCoSoHaTang/outPathSimplifyTemp" arcpy.SimplifyPolygon_cartography(in_features = outPathMerge, out_feature_class = outPathSimplify, algorithm = _algorithm, tolerance = _tolerance, minimum_area = "0 SquareMeters", error_option = _error_option, collapsed_point_option = _collapsed_point_option) ## MakeLayerFeature ## outPathSimplifyLayer = "in_memory\\outPathSimplifyTempLayer" arcpy.MakeFeatureLayer_management(outPathSimplify, outPathSimplifyLayer) ## Update Shape Feature Class ## arcpy.AddMessage("\n# Update Shape Feature Class:") for element in listPolygon: arcpy.AddMessage("\n\t# Update {0}...".format(element["LayerName"])) ### MakeLayerFeature ### layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] arcpy.MakeFeatureLayer_management(layerPath, element["featureLayer"]) ### Select ### strQuery = element["FID_XXX"] + " IS NOT NULL" arcpy.SelectLayerByAttribute_management(outPathSimplifyLayer, "NEW_SELECTION", strQuery) ### Copy To Table Temp ### outTableTemp = "in_memory\\outTableTemp" arcpy.CopyFeatures_management(outPathSimplifyLayer, outTableTemp) ### ... ### with arcpy.da.UpdateCursor(element["featureLayer"], ["OID@", "SHAPE@"]) as cursor: for row in cursor: found = False with arcpy.da.UpdateCursor(outTableTemp, [element["FID_XXX"], "SHAPE@"]) as cursorSub: for rowSub in cursorSub: if row[0] == rowSub[0]: found = True row[1] = rowSub[1] cursor.updateRow(row) cursorSub.deleteRow() break if found == False: cursor.deleteRow() arcpy.AddMessage("\n# Hoan thanh Simplify Polygon!!!") ############################################## Simplify Line ############################# arcpy.AddMessage("\n# Bat dau Simplify Line") listPolyLine = [] fieldMappingLine = arcpy.FieldMappings() enableFieldLine = [] inputsMergeLine = [] for objConfig in listLayerConfig: if objConfig["LayerType"] == "Polyline" and objConfig["RunStatus"] == "True": if not(_checkExitLayer == False and objConfig["LayerName"] == "SongSuoiL_KenhMuongL_SnapPBM"): temp = { "LayerType": objConfig["LayerType"], "DatasetName": objConfig["DatasetName"], "LayerName": objConfig["LayerName"], "featureLayer": "in_memory\\" + objConfig["LayerName"] + "_Layer", "featureCopy": "in_memory\\" + objConfig["LayerName"] + "_Copy", "featureCopyLayer": "in_memory\\" + objConfig["LayerName"] + "_Copy_Layer", "FID_XXX": "FID_" + objConfig["LayerName"] } listPolyLine.append(temp) for element in listPolyLine: arcpy.AddMessage("\n# Xu ly lop: {0}".format(element["LayerName"])) layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] if element["LayerName"] == "DuongBinhDo": arcpy.AddField_management(layerPath, "OLD_OBJECTID", "LONG", None, None, None,"OLD_OBJECTID", "NULLABLE") arcpy.CalculateField_management(layerPath, "OLD_OBJECTID", "!OBJECTID!", "PYTHON_9.3") arcpy.MakeFeatureLayer_management(layerPath, element["featureLayer"]) arcpy.AddField_management(element["featureLayer"], element["FID_XXX"], "LONG") with arcpy.da.UpdateCursor(element["featureLayer"], ["OID@", element["FID_XXX"]]) as cursor: for row in cursor: row[1] = row[0] cursor.updateRow(row) arcpy.CopyFeatures_management(layerPath, element["featureCopy"]) arcpy.MakeFeatureLayer_management(element["featureCopy"], element["featureCopyLayer"]) ## Field Mappings ## enableFieldLine.append(element["FID_XXX"]) fieldMappingLine.addTable(element["featureCopyLayer"]) inputsMergeLine.append(element["featureCopyLayer"]) for field in fieldMappingLine.fields: if field.name not in enableFieldLine: fieldMappingLine.removeFieldMap(fieldMappingLine.findFieldMapIndex(field.name)) ## Merge ## arcpy.AddMessage("\n# Merge Polyline...") outPathMerge = "in_memory\\outPathMergeTemp" arcpy.Merge_management (inputsMergeLine, outPathMerge, fieldMappingLine) ## Simplify Polyline ## arcpy.AddMessage("\n# Simplify Polyline...") outPathSimplify = "in_memory\\outPathSimplifyTemp" ''' arcpy.MakeFeatureLayer_management(duongDanNguon + "/ThuyHe/SongSuoiA", "ThuyHe_SongSuoiA_Lyr") arcpy.MakeFeatureLayer_management(duongDanNguon + "/ThuyHe/MatNuocTinh", "ThuyHe_MatNuocTinh_Lyr") arcpy.MakeFeatureLayer_management(duongDanNguon + "/ThuyHe/KenhMuongA", "ThuyHe_KenhMuongA_Lyr") in_barriers_Line = ["ThuyHe_SongSuoiA_Lyr", "ThuyHe_MatNuocTinh_Lyr", "ThuyHe_KenhMuongA_Lyr"] ''' arcpy.SimplifyLine_cartography(in_features = outPathMerge, out_feature_class = outPathSimplify, algorithm = _algorithm, tolerance = _tolerance, collapsed_point_option = _collapsed_point_option) ## MakeLayerFeature ## outPathSimplifyLayer = "in_memory\\outPathSimplifyTempLayer" arcpy.MakeFeatureLayer_management(outPathSimplify, outPathSimplifyLayer) ## Update Shape Feature Class ## arcpy.AddMessage("\n# Update Shape Feature Class:") for element in listPolyLine: if element["LayerType"] == "Polyline": arcpy.AddMessage("\n\t# Update {0}...".format(element["LayerName"])) ### MakeLayerFeature ### layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] arcpy.MakeFeatureLayer_management(layerPath, element["featureLayer"]) ### Select ### strQuery = element["FID_XXX"] + " IS NOT NULL" arcpy.SelectLayerByAttribute_management(outPathSimplifyLayer, "NEW_SELECTION", strQuery) ### Copy To Table Temp ### outTableTemp = "in_memory\\outTableTemp" arcpy.CopyFeatures_management(outPathSimplifyLayer, outTableTemp) ### ... ### with arcpy.da.UpdateCursor(element["featureLayer"], ["OID@", "SHAPE@"]) as cursor: for row in cursor: found = False with arcpy.da.UpdateCursor(outTableTemp, [element["FID_XXX"], "SHAPE@"]) as cursorSub: for rowSub in cursorSub: if row[0] == rowSub[0]: found = True row[1] = rowSub[1] cursor.updateRow(row) cursorSub.deleteRow() break if found == False: cursor.deleteRow() arcpy.AddMessage("\n# Hoan thanh Simplify Polyline!!!") ############################################## Snap Line to Polygon ############################# arcpy.AddMessage("\n# Bat dau Snap") for elementPolygon in listPolygon: if elementPolygon["LayerType"] == "Polyline": lineLayerName = elementPolygon["LayerName"][:elementPolygon["LayerName"].find('_Buffer')] if (lineLayerName <> "DuongBinhDo"): arcpy.AddMessage("\n\t# Snap: {0}".format(lineLayerName)) layerBufferPath = duongDanNguon + "/" + elementPolygon["DatasetName"] + "/" + elementPolygon["LayerName"] layerLinePath = duongDanNguon + "/" + elementPolygon["DatasetName"] + "/" + lineLayerName arcpy.Snap_edit(layerLinePath, [[layerBufferPath, "EDGE", self.snap_distance]]) ############## Snap Other if _checkExitLayer: arcpy.AddMessage("\n\t# Snap other: {0}".format("PhuBeMat")) arcpy.Integrate_management([[duongDanNguon + "/PhuBeMat/PhuBeMat", 1]], "2 Meters") arcpy.Densify_edit(duongDanNguon + "/PhuBeMat/PhuBeMat", "DISTANCE","2 Meters",None ,None) arcpy.Snap_edit(duongDanNguon + "/PhuBeMat/PhuBeMat", [[duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM", "EDGE", "35 Meters"]]) arcpy.Integrate_management([[duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM", 1],[duongDanNguon + "/PhuBeMat/PhuBeMat", 2]], "2 Meters") arcpy.Erase_analysis(in_features = duongDanNguon + "/PhuBeMat/PhuBeMat_Full", erase_features = duongDanNguon + "/PhuBeMat/PhuBeMat", out_feature_class = duongDanNguon + "/PhuBeMat/PhuBeMat_Lo") arcpy.CalculateField_management(duongDanNguon + "/PhuBeMat/PhuBeMat_Lo", "maNhanDang", '"temp123"', "PYTHON_9.3") arcpy.Append_management([duongDanNguon + "/PhuBeMat/PhuBeMat_Lo"], duongDanNguon + "/PhuBeMat/PhuBeMat", "NO_TEST",None,None) arcpy.MultipartToSinglepart_management(duongDanNguon + "/PhuBeMat/PhuBeMat", duongDanNguon + "/PhuBeMat/PhuBeMat2") arcpy.MakeFeatureLayer_management(duongDanNguon + "/PhuBeMat/PhuBeMat2", "PhuBeMat_Temp_Lyr") arcpy.SelectLayerByAttribute_management("PhuBeMat_Temp_Lyr", "NEW_SELECTION", "maNhanDang = 'temp123'") arcpy.Eliminate_management(in_features = "PhuBeMat_Temp_Lyr", out_feature_class = duongDanNguon + "/PhuBeMat/PhuBeMat3", selection = "LENGTH") arcpy.Densify_edit(duongDanNguon + "/ThuyHe/SongSuoiL", "DISTANCE","2 Meters",None ,None) arcpy.Snap_edit(duongDanNguon + "/ThuyHe/SongSuoiL", [[duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM", "EDGE", "2 Meters"]]) arcpy.CopyFeatures_management(duongDanNguon + "/PhuBeMat/PhuBeMat3", duongDanNguon + "/PhuBeMat/PhuBeMat") ############################################## Copy to final ############################# for element in listPolygon: if element["LayerType"] == "Polygon": if element["LayerName"] <> "PhuBeMat_Full": layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] layerFinalPath = duongDanDich + "/" + element["DatasetName"] + "/" + element["LayerName"] arcpy.DeleteField_management(layerPath, [element["FID_XXX"]]) arcpy.CopyFeatures_management(layerPath, layerFinalPath) for element in listPolyLine: if element["LayerType"] == "Polyline": if element["LayerName"] <> "SongSuoiL_KenhMuongL_SnapPBM": layerPath = duongDanNguon + "/" + element["DatasetName"] + "/" + element["LayerName"] layerFinalPath = duongDanDich + "/" + element["DatasetName"] + "/" + element["LayerName"] arcpy.DeleteField_management(layerPath, [element["FID_XXX"]]) arcpy.CopyFeatures_management(layerPath, layerFinalPath) #arcpy.AddMessage("\n# Hoan thanh!!!") else: arcpy.AddMessage("\n# Khong tim thay file cau hinh: \"{0}\"".format(urlFile)) except OSError as error: arcpy.AddMessage("Error" + error.message) except ValueError as error: arcpy.AddMessage("Error" + error.message) except arcpy.ExecuteError as error: arcpy.AddMessage("Error" + error.message) finally: arcpy.Delete_management("in_memory")
### Preliminary processing # load parameters with open("code/parameters/general.toml") as conffile: general_params = toml.loads(conffile.read()) with open("code/parameters/remove-slivers.toml") as conffile: slivers_params = toml.loads(conffile.read()) # set environmental variables arcpy.env.parallelProcessingFactor = general_params['threads'] arcpy.env.overwriteOutput = True arcpy.env.workspace = os.path.dirname(sys.argv[1]) ### Main processing # calculate area arcpy.AddField_management(sys.argv[1], 'AREA', 'DOUBLE') arcpy.CalculateField_management(sys.argv[1], 'AREA', '!shape.area@SQUAREKILOMETERS!', 'PYTHON_9.3') # select slivers arcpy.MakeFeatureLayer_management(os.path.basename(sys.argv[1]), os.path.basename(sys.argv[1]) + '_layer') arcpy.SelectLayerByAttribute_management( os.path.basename(sys.argv[1]) + '_layer', "NEW_SELECTION", 'AREA < ' + str(slivers_params['minimum_size_in_square_kilometers'])) # remove slivers arcpy.Eliminate_management( os.path.basename(sys.argv[1]) + '_layer', sys.argv[2], slivers_params['selection'])
# Iterative elimination print('Total HUR#, Target elimn HRU#, Actual elimn HRU#, New HRU# ') while sel_count != 0 and elimn_count <= 15 and row_count_new > 1: # (1) eliminate hru_shp_tmp = 'elimn_trial'+str(elimn_count)+'.shp' hru_lyr_tmp = 'elimn_lyr'+str(elimn_count) elimn_count=elimn_count+1 hru_shp_tmp_new = 'elimn_trial'+str(elimn_count)+'.shp' hru_lyr_tmp_new = 'elimn_lyr'+str(elimn_count) arcpy.MakeFeatureLayer_management(hru_shp_tmp, hru_lyr_tmp) row_count = int(arcpy.GetCount_management(hru_lyr_tmp).getOutput(0)) arcpy.SelectLayerByAttribute_management(hru_lyr_tmp, "NEW_SELECTION", '"area_m" < '''+str(float(hru_area_thrsh))) arcpy.Eliminate_management(hru_lyr_tmp, hru_shp_tmp_new, "LENGTH") # (2) update area exp = "!SHAPE.AREA@SQUAREMETERS!" arcpy.CalculateField_management(hru_shp_tmp_new, hru_area_field, exp, "PYTHON_9.3") arcpy.MakeFeatureLayer_management(hru_shp_tmp_new,hru_lyr_tmp_new) row_count_new = int(arcpy.GetCount_management(hru_lyr_tmp_new).getOutput(0)) delta = row_count - row_count_new print(row_count, sel_count, delta, row_count_new) # (3) identify small HRUs arcpy.SelectLayerByAttribute_management(hru_lyr_tmp_new, "NEW_SELECTION", '"area_m" < '''+str(float(hru_area_thrsh))) sel_count = int(arcpy.GetCount_management(hru_lyr_tmp_new).getOutput(0)) # Save eliminated subbasin shapefile
]]) arcpy.Integrate_management( [[duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM", 1], [duongDanNguon + "/PhuBeMat/PhuBeMat", 2]], "2 Meters") arcpy.Erase_analysis(in_features=duongDanNguon + "/PhuBeMat/PhuBeMat_LocMatNuoc", erase_features=duongDanNguon + "/PhuBeMat/PhuBeMat", out_feature_class=duongDanNguon + "/PhuBeMat/PhuBeMat_Lo") arcpy.CalculateField_management(duongDanNguon + "/PhuBeMat/PhuBeMat_Lo", "maNhanDang", '"temp123"', "PYTHON_9.3") arcpy.Append_management([duongDanNguon + "/PhuBeMat/PhuBeMat_Lo"], duongDanNguon + "/PhuBeMat/PhuBeMat", "NO_TEST", None, None) arcpy.MultipartToSinglepart_management(duongDanNguon + "/PhuBeMat/PhuBeMat", duongDanNguon + "/PhuBeMat/PhuBeMat2") arcpy.MakeFeatureLayer_management(duongDanNguon + "/PhuBeMat/PhuBeMat2", "PhuBeMat_Temp_Lyr") arcpy.SelectLayerByAttribute_management("PhuBeMat_Temp_Lyr", "NEW_SELECTION", "maNhanDang = 'temp123'") arcpy.Eliminate_management(in_features="PhuBeMat_Temp_Lyr", out_feature_class=duongDanNguon + "/PhuBeMat/PhuBeMat3", selection="LENGTH") arcpy.Densify_edit(duongDanNguon + "/ThuyHe/SongSuoiL", "DISTANCE", "2 Meters", None, None) arcpy.Snap_edit(duongDanNguon + "/ThuyHe/SongSuoiL", [[ duongDanNguon + "/ThuyHe/SongSuoiL_KenhMuongL_SnapPBM", "EDGE", "2 Meters" ]]) arcpy.CopyFeatures_management(duongDanNguon + "/PhuBeMat/PhuBeMat3", duongDanNguon + "/PhuBeMat/PhuBeMat")
union_failed = False arcpy.MultipartToSinglepart_management("New_snap_union", "New_snap_union_sp") print(" Deleting identical polygons after snap and union") arcpy.CopyFeatures_management("New_snap_union_sp", "New_snap_union_sp_delid") arcpy.DeleteIdentical_management("New_snap_union_sp_delid", ["Shape"]) print(" Eliminating slivers after snap and union") arcpy.MakeFeatureLayer_management("New_snap_union_sp_delid", "Elim_layer") arcpy.SelectLayerByAttribute_management( "Elim_layer", where_clause="Shape_Area < " + str(sliver_size)) arcpy.Eliminate_management("Elim_layer", "New_snap_union_sp_delid_elim") arcpy.Delete_management("Elim_layer") print(" Deleting remaining standalone slivers") arcpy.CopyFeatures_management("New_snap_union_sp_delid_elim", "New_snap_union_sp_delid_elim_del") arcpy.MakeFeatureLayer_management( "New_snap_union_sp_delid_elim_del", "Del_layer") arcpy.SelectLayerByAttribute_management( "Del_layer", where_clause="Shape_Area < " + str(sliver_size)) arcpy.DeleteFeatures_management("Del_layer") arcpy.Delete_management("Del_layer") arcpy.CopyFeatures_management("New_snap_union_sp_delid_elim_del", "New_snap_clean") MyFunctions.check_and_repair("New_snap_clean")
" are real gaps, not slivers. Set 'first_part' and 'second_part' to 'False' and restart the code. This will improve the final shapes." ) expression = "Type = 'Gap' AND Shape_Area > 500 AND Shape_area < 2500" MyFunctions.select_and_copy("Desig_union_repair_sp_delid", "Type", expression, "'sliver'") exit() if elim_slivers: print("Eliminating sliver gaps") arcpy.MakeFeatureLayer_management("Desig_union_repair_sp_delid", "Elim_layer") expression = "(Type = 'Gap' AND Shape_Area < 500) OR Type = 'sliver'" arcpy.SelectLayerByAttribute_management("Elim_layer", where_clause=expression) arcpy.Eliminate_management("Elim_layer", "Desig_union_repair_sp_delid_elim") arcpy.Delete_management("Elim_layer") # Delete remaining (larger) gaps print("Deleting large gaps") arcpy.CopyFeatures_management("Desig_union_repair_sp_delid_elim", "Desig_union_repair_sp_delid_elim_del") arcpy.MakeFeatureLayer_management("Desig_union_repair_sp_delid_elim_del", "Del_layer") arcpy.SelectLayerByAttribute_management( "Del_layer", where_clause="Type = 'Gap' AND Shape_Area >= 500") arcpy.DeleteFeatures_management("Del_layer") arcpy.Delete_management("Del_layer") # Eliminate non-gap slivers print("Eliminating non-gap slivers")
#o16414_se_APN NOT LIKE '______95%' AND o16414_se_APN NOT LIKE '______99%' FinalOutput = r'D:\Clark_County_2017_ImageClassification_Project\Samples_models\AmendedData\Tree_Proc\Tree_amend_int.gdb' Scratch = r'D:\Clark_County_2017_ImageClassification_Project\Samples_models\AmendedData\Scratch.gdb' for fc in fcs: print "processing " + fc arcpy.RepairGeometry_management(fc) arcpy.MakeFeatureLayer_management(fc, 'lyr') arcpy.Dissolve_management('lyr', Scratch + '\\' + fc + '_dis', [fc[:-6] + '_APN']) arcpy.MakeFeatureLayer_management(Scratch + '\\' + fc + '_dis', 'DisLyr') arcpy.MultipartToSinglepart_management('DisLyr', Scratch + '\\' + fc + '_Sing') arcpy.MakeFeatureLayer_management(Scratch + '\\' + fc + '_Sing', 'SingLyr') arcpy.SelectLayerByAttribute_management( 'SingLyr', 'NEW_SELECTION', fc[:-6] + "_APN LIKE '______95%' OR " + fc[:-6] + "_APN LIKE '______99%'") arcpy.Eliminate_management('SingLyr', Scratch + '\\' + fc + '_elim') arcpy.MakeFeatureLayer_management(Scratch + '\\' + fc + '_elim', 'FinalLyr') arcpy.Dissolve_management('FinalLyr', FinalOutput + '\\' + fc, [fc[:-6] + '_APN'])
merged_roads, os.path.join(temp_dir, "prelim_zones.shp")) #======================================== # Eliminate Small Zones using two passes #======================================== print('Eliminating small zones (1st pass)...') arcpy.AddField_management(prelim_zones, 'area_sqm', 'FLOAT') arcpy.CalculateGeometryAttributes_management(prelim_zones, [['area_sqm', 'AREA']], '', 'SQUARE_METERS') zones_layer = arcpy.MakeFeatureLayer_management(prelim_zones, 'zones') query = """"area_sqm" < 5000""" arcpy.SelectLayerByAttribute_management(zones_layer, "NEW_SELECTION", query) zones_eliminated = arcpy.Eliminate_management( zones_layer, os.path.join(temp_dir, 'zones_eliminated_again.shp'), 'LENGTH') print('Eliminating small zones (2nd pass)...') arcpy.CalculateGeometryAttributes_management(zones_eliminated, [['area_sqm', 'AREA']], '', 'SQUARE_METERS') zones_layer2 = arcpy.MakeFeatureLayer_management(zones_eliminated, 'zones') #query = """"area_sqm" < 10000""" query = """"area_sqm" < 15000""" arcpy.SelectLayerByAttribute_management(zones_layer2, "NEW_SELECTION", query) zones_eliminated2 = arcpy.Eliminate_management( zones_layer2, os.path.join(temp_dir, 'zones_eliminated_again2.shp'), 'LENGTH') #==========================
arcpy.SelectLayerByAttribute_management(tmpSelLyr, "NEW_SELECTION", expr) #get the non-slivers from this class expr = '"Shape_Area" > 899.99 AND "Class" = ' + cl_str arcpy.SelectLayerByAttribute_management(tmpSelLyr, "ADD_TO_SELECTION", expr) #write out the selection outFeat = "hyp_backOut_" + cl_str arcpy.CopyFeatures_management(tmpSelLyr, outFeat) #now use eliminate on this feature class inFeat = "hyp_backOut_" + cl_str tmpSelLyr2 = "tmpSelectSet2" expr = '"Shape_Area" < 900' arcpy.MakeFeatureLayer_management(inFeat, tmpSelLyr2) arcpy.SelectLayerByAttribute_management(tmpSelLyr2, "NEW_SELECTION", expr) outFeat = "hyp_backOut_Elim_" + cl_str arcpy.Eliminate_management(tmpSelLyr2, outFeat) man.Delete(inFeat) # Heidi's notes indicate there may be cases where slivers # are present but not adjacent the next higher up class # and in these cases they should be deleted. tmpSelLyr3 = "tmpSelectSet3" #get any remaining slivers and remove them. expr = '"Shape_Area" < 900' arcpy.MakeFeatureLayer_management(outFeat, tmpSelLyr3) arcpy.SelectLayerByAttribute_management(tmpSelLyr3, "NEW_SELECTION", expr) if int(arcpy.GetCount_management(tmpSelLyr3).getOutput(0)) > 0: arcpy.DeleteFeatures_management(tmpSelLyr3) # merge pieces back up (dissolve), original PRO seems to have this polysToDiss = "hyp_backOut_Elim_" + cl_str outFeatFin = "hyp_backOut_dissolve_" + cl_str arcpy.Dissolve_management(polysToDiss, outFeatFin, "ORIG_FID", "",
arcpy.SelectLayerByLocation_management("Trees_NONROW_lyr", "BOUNDARY_TOUCHES", "Trees_ROW_Single_lyr", "", "NEW_SELECTION", "INVERT") arcpy.Select_analysis("Trees_NONROW_lyr", "TREES_NONROW_NONAOI") print "creating the last compiled_clean trees feature class" arcpy.Append_management("Trees_ROW_Single_lyr", "TREES_NONROW_AOI", "NO_TEST") arcpy.MakeFeatureLayer_management("TREES_NONROW_AOI", "TREES_NONROW_AOI_lyr") arcpy.SelectLayerByAttribute_management( "TREES_NONROW_AOI_lyr", "NEW_SELECTION", "ParcelField = 717 OR ParcelField = 725") arcpy.Eliminate_management( "TREES_NONROW_AOI_lyr", ws + '\\' + fc[:-9] + '_intermediateData') arcpy.MakeFeatureLayer_management( ws + '\\' + fc[:-9] + '_intermediateData', "intermediateData_lyr") arcpy.Append_management("intermediateData_lyr", "TREES_NONROW_NONAOI", "NO_TEST") arcpy.CopyFeatures_management( "TREES_NONROW_NONAOI", FinalOutput + '\\' + fc[:-9] + '_TREES_correctROW') except: arcpy.Select_analysis(fc, "Trees_select", "gridcode = 1") arcpy.RepairGeometry_management("Trees_select") arcpy.Dissolve_management(
arcpy.PeukerDouglas_TDEM(fel_tif, "0.4", "0.1", "0.05", "8", ss_tif) # Process: Peuker Douglas Stream Definition 21. 6th input is #cells tributary needed to form stream. arcpy.PeukerDouglasStreamDef_TDEM(fel_tif, p_tif, "0.4", "0.1", "0.05", "50", "true", Outletmv, "", ad8_tif, "8", ss21_tif, ssa21_tif, src21_tif, drp21_txt, "true", "5", "500", "10", "true") # Process: Stream Reach And Watershed 22 arcpy.StreamReachAndWatershed_TDEM(fel_tif, p_tif, ad8_tif, src21_tif, Outletmv, "false", "8", ord22_tif, tree22_txt, coord22_txt, net22_shp, w22_tif) #post processing #TauDEM toolbox has a builtin raster --> shapefile converter, but it just calls raster --> polygon and doesn't merge tiny pieces. # Process: Raster to Polygon arcpy.RasterToPolygon_conversion(w22_tif, subwatersheds_shp, "SIMPLIFY", "Value") #convert raster to polygon # Process: Add Geometry Attributes arcpy.AddGeometryAttributes_management(subwatersheds_shp, "AREA", "", "ACRES", "") #make temp layer for editing arcpy.MakeFeatureLayer_management(subwatersheds_shp, select_sub) #merge polygons with area < 1 acre with the adjacent polygon with longest shared side length. May want to change area threshold arcpy.SelectLayerByAttribute_management(select_sub, "NEW_SELECTION", '"POLY_AREA" < 1.0') # "LENGTH" = merge with adjacent of longest sidelength arcpy.Eliminate_management(select_sub, out_sub, "LENGTH", "", "") print "done"
FeatureClass = temp cun = FeatureClass FeatureClass_Layer = cun FeatureClass_Layer__2_ = FeatureClass_Layer cun_Eliminate = FeatureClass_Layer__2_ Name = temp arcpy.env.worksapce = temp # Process: Iterate Feature Classes # arcpy.IterateFeatureClasses_mb(temp, "", "", "NOT_RECURSIVE") fcs = arcpy.ListFeatureClasses() for fc in fcs: # Process: Calculate Field arcpy.CalculateField_management(fc, "TBMJ", "\"!SHAPE.AREA!\"", "PYTHON_9.3", "") # Process: Make Feature Layer arcpy.MakeFeatureLayer_management( fc, FeatureClass_Layer, "", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;Id Id VISIBLE NONE;DSMC DSMC VISIBLE NONE;QXMC QXMC VISIBLE NONE;XZMC XZMC VISIBLE NONE;CUNMC CUNMC VISIBLE NONE;CUNDM CUNDM VISIBLE NONE;GCUNDM GCUNDM VISIBLE NONE;TBLXDM TBLXDM VISIBLE NONE;TBLXMC TBLXMC VISIBLE NONE;SFYF SFYF VISIBLE NONE;YFBH YFBH VISIBLE NONE;YFDKBH YFDKBH VISIBLE NONE;XZB XZB VISIBLE NONE;YZB YZB VISIBLE NONE;TBMJ TBMJ VISIBLE NONE;QZW QZW VISIBLE NONE;QZWMJ QZWMJ VISIBLE NONE;QCYZW QCYZW VISIBLE NONE;QCYZWMJ QCYZWMJ VISIBLE NONE;CBZW CBZW VISIBLE NONE;CBZWMJ CBZWMJ VISIBLE NONE;CCYZW CCYZW VISIBLE NONE;CCYZWMJ CCYZWMJ VISIBLE NONE;XBZW XBZW VISIBLE NONE;XBZWMJ XBZWMJ VISIBLE NONE;XCYZW XCYZW VISIBLE NONE;XCYZWMJ XCYZWMJ VISIBLE NONE;SNYD SNYD VISIBLE NONE;SNFZCS SNFZCS VISIBLE NONE;SNZWMC SNZWMC VISIBLE NONE;SNZWMJ SNZWMJ VISIBLE NONE;JCYX JCYX VISIBLE NONE;NHXM NHXM VISIBLE NONE;BZ BZ VISIBLE NONE;area area VISIBLE NONE;Shape_Leng Shape_Leng VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE" ) # Process: Select Layer By Attribute arcpy.SelectLayerByAttribute_management(FeatureClass_Layer, "NEW_SELECTION", "\"TBMJ\" <=66.7") # Process: Eliminate Eliminate_Result = os.path.join(result, fc) arcpy.Eliminate_management(FeatureClass_Layer, Eliminate_Result, "AREA", "", "")
delFC(thlb) #Select THLB for TSA and create temp dataset print "Selecting %s for %s from %s..." % (select, tsa, THLB_data) arcpy.Select_analysis(THLB_data, tempTHLB, select) #Repair TSA THLB arcpy.RepairGeometry_management(tempTHLB) arcpy.CalculateField_management(tempTHLB, "CFLB_FACT", "round(!CFLB_FACT!, 4)", "PYTHON") arcpy.CalculateField_management(tempTHLB, "THLB_FACT", "round(!THLB_FACT!, 4)", "PYTHON") shapeName = getShapeFieldName(tempTHLB) arcpy.MakeFeatureLayer_management(tempTHLB, tempTHLBlyr, shapeName + " < 5") try: arcpy.Eliminate_management(tempTHLBlyr, tempTHLB2) except Exception: tempTHLB2 = tempTHLB print "No Sliver smaller than 5 meteres found" arcpy.Delete_management(tempTHLBlyr) arcpy.MakeFeatureLayer_management(tempTHLB2, tempTHLBlyr2, "THLB_FACT > 0 or CFLB_FACT > 0") arcpy.Dissolve_management(tempTHLBlyr2, thlbDiss, [ "THLB_FACT", "CFLB_FACT", "TSA_NAME", "TSA_NUMBER", "TSR_VERSION", "TSR_REPORT_DATE" ], "", "SINGLE_PART") #Calculate Area Fields arcpy.AddField_management(thlbDiss, "area_ha", "DOUBLE") arcpy.AddField_management(thlbDiss, "thlb_ha", "DOUBLE") shapeName = getShapeFieldName(thlbDiss)
def main(fcInputCenterline, fcInputPolygon, fcSegmentedPolygons, workspaceTemp, dblPointDensity=10.0, dblJunctionBuffer=120.00): arcpy.AddMessage("GNAT Divide Polygon By Segment Tool") arcpy.AddMessage("GNAT DPS: Saving Polygon Results to: " + fcSegmentedPolygons) arcpy.AddMessage("GNAT DPS: Saving Temporary Files to: " + workspaceTemp) arcpy.env.OutputMFlag = "Disabled" arcpy.env.OutputZFlag = "Disabled" arcpy.AddMessage("arcpy M Output Flag: " + str(arcpy.env.OutputMFlag)) ## Copy Centerline to Temp Workspace fcCenterline = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_Centerline") arcpy.CopyFeatures_management(fcInputCenterline, fcCenterline) ## Build Thiessan Polygons arcpy.AddMessage("GNAT DPS: Building Thiessan Polygons") arcpy.env.extent = fcInputPolygon ## Set full extent to build Thiessan polygons over entire line network. arcpy.Densify_edit(fcCenterline, "DISTANCE", str(dblPointDensity) + " METERS") fcTribJunctionPoints = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_TribJunctionPoints") # All Segment Junctions?? #gis_tools.findSegmentJunctions(fcCenterline,fcTribJunctionPoints,"ALL") arcpy.Intersect_analysis(fcCenterline, fcTribJunctionPoints, output_type="POINT") fcThiessanPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanPoints") arcpy.FeatureVerticesToPoints_management(fcCenterline, fcThiessanPoints, "ALL") lyrThiessanPoints = gis_tools.newGISDataset("Layer", "lyrThiessanPoints") arcpy.MakeFeatureLayer_management(fcThiessanPoints, lyrThiessanPoints) arcpy.SelectLayerByLocation_management(lyrThiessanPoints, "INTERSECT", fcTribJunctionPoints, str(dblJunctionBuffer) + " METERS", "NEW_SELECTION") fcThiessanPoly = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanPoly") arcpy.CreateThiessenPolygons_analysis(lyrThiessanPoints, fcThiessanPoly, "ONLY_FID") fcThiessanPolyClip = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_TheissanPolyClip") arcpy.Clip_analysis(fcThiessanPoly, fcInputPolygon, fcThiessanPolyClip) ### Code to Split the Junction Thiessan Polys ### arcpy.AddMessage("GNAT DPS: Split Junction Thiessan Polygons") lyrTribThiessanPolys = gis_tools.newGISDataset("Layer", "lyrTribThiessanPolys") arcpy.MakeFeatureLayer_management(fcThiessanPolyClip, lyrTribThiessanPolys) arcpy.SelectLayerByLocation_management(lyrTribThiessanPolys, "INTERSECT", fcTribJunctionPoints, selection_type="NEW_SELECTION") fcSplitPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_SplitPoints") arcpy.Intersect_analysis([lyrTribThiessanPolys, fcCenterline], fcSplitPoints, output_type="POINT") arcpy.AddMessage("GNAT DPS: Moving Starting Vertices of Junction Polygons") geometry_functions.changeStartingVertex(fcTribJunctionPoints, lyrTribThiessanPolys) arcpy.AddMessage("GNAT DPS: Vertices Moved.") fcThiessanTribPolyEdges = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_ThiessanTribPolyEdges") arcpy.FeatureToLine_management(lyrTribThiessanPolys, fcThiessanTribPolyEdges) fcSplitLines = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_SplitLines") arcpy.SplitLineAtPoint_management(fcThiessanTribPolyEdges, fcSplitPoints, fcSplitLines, "0.1 METERS") fcMidPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_MidPoints") arcpy.FeatureVerticesToPoints_management(fcSplitLines, fcMidPoints, "MID") arcpy.Near_analysis(fcMidPoints, fcTribJunctionPoints, location="LOCATION") arcpy.AddXY_management(fcMidPoints) fcTribToMidLines = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_TribToMidLines") arcpy.XYToLine_management(fcMidPoints, fcTribToMidLines, "POINT_X", "POINT_Y", "NEAR_X", "NEAR_Y") ### Select Polys by Centerline ### arcpy.AddMessage("GNAT DPS: Select Polygons By Centerline") fcThiessanEdges = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanEdges") arcpy.FeatureToLine_management(fcThiessanPolyClip, fcThiessanEdges) fcAllEdges = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_AllEdges") arcpy.Merge_management([fcTribToMidLines, fcThiessanEdges, fcCenterline], fcAllEdges) # include fcCenterline if needed fcAllEdgesPolygons = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_AllEdgesPolygons") arcpy.FeatureToPolygon_management(fcAllEdges, fcAllEdgesPolygons) fcAllEdgesPolygonsClip = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_AllEdgesPolygonsClip") arcpy.Clip_analysis(fcAllEdgesPolygons, fcInputPolygon, fcAllEdgesPolygonsClip) fcPolygonsJoinCenterline = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_PolygonsJoinCenterline") arcpy.SpatialJoin_analysis(fcAllEdgesPolygonsClip, fcCenterline, fcPolygonsJoinCenterline, "JOIN_ONE_TO_MANY", "KEEP_ALL", match_option="SHARE_A_LINE_SEGMENT_WITH") fcPolygonsDissolved = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_PolygonsDissolved") arcpy.Dissolve_management(fcPolygonsJoinCenterline, fcPolygonsDissolved, "JOIN_FID", multi_part="SINGLE_PART") #fcSegmentedPolygons = gis_tools.newGISDataset(workspaceOutput,"SegmentedPolygons") lyrPolygonsDissolved = gis_tools.newGISDataset("Layer", "lyrPolygonsDissolved") arcpy.MakeFeatureLayer_management(fcPolygonsDissolved, lyrPolygonsDissolved) arcpy.SelectLayerByAttribute_management(lyrPolygonsDissolved, "NEW_SELECTION", """ "JOIN_FID" = -1 """) arcpy.Eliminate_management(lyrPolygonsDissolved, fcSegmentedPolygons, "LENGTH") arcpy.AddMessage("GNAT DPS: Tool Complete.") return
def main(huc_input, elev_input, strm_input, strm_seg, bf_input, outFGB, upstream_bool): # check data characteristics of input stream network for tool requirements check_req(strm_input, elev_input) # set environment parameters arcpy.AddMessage("Setting processing environment parameters...") arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput = True arcpy.env.workspace = outFGB arcpy.env.outputCoordinateSystem = elev_input arcpy.env.extent = elev_input arcpy.env.snapRaster = elev_input arcpy.env.cellSize = elev_input arcpy.env.mask = elev_input cellSizeResult = arcpy.GetRasterProperties_management( elev_input, "CELLSIZEX") cellSize = float(cellSizeResult.getOutput(0)) snap_dist = cellSize * 2 # metadata mWriter = Metadata.MetadataWriter("Delineate Catchments", "0.3") mWriter.createRun() # input parameters for metadata file mWriter.currentRun.addParameter("Watershed polygon feature class", huc_input) mWriter.currentRun.addParameter("DEM raster", elev_input) mWriter.currentRun.addParameter("Stream network polyline feature class", strm_input) mWriter.currentRun.addParameter("Segmented stream network feature class", strm_seg) mWriter.currentRun.addParameter( "Bankfull (or stream area) polygon feature class", bf_input) mWriter.currentRun.addParameter("Output file geodatabase", outFGB) mWriter.currentRun.addParameter("Upstream (i.e. overlapping) catchments?", upstream_bool) # check segmented stream network for LineOID field, and add if it's missing seg_oid = arcpy.Describe(strm_seg).OIDFieldName list_field = arcpy.ListFields(strm_seg, "LineOID") strm_seg_lyr = "strm_seg_lyr" arcpy.MakeFeatureLayer_management(strm_seg, strm_seg_lyr) if len(list_field) == 0: arcpy.AddField_management(strm_seg_lyr, "LineOID", "LONG") arcpy.CalculateField_management(strm_seg_lyr, "LineOID", "!" + seg_oid + "!", "PYTHON_9.3") # convert stream network to raster arcpy.AddMessage("Converting stream network to raster format...") strm_ras = sR.convert_ras(huc_input, bf_input, strm_input) # recondition DEM using stream network arcpy.AddMessage("Reconditioning DEM with stream network...") dem_rec = dR.dem_recnd(elev_input, huc_input, strm_ras, outFGB) # calculate flow direction and flow accumulation arcpy.AddMessage("Calculating flow direction and accumulation...") fd = FlowDirection(dem_rec, "NORMAL") fa = FlowAccumulation(fd, "", "FLOAT") # Plot segment endpoints as pour points arcpy.AddMessage("...plotting pour points.") seg_endpoints = end.main(strm_input, strm_seg, cellSize) arcpy.FeatureClassToFeatureClass_conversion(seg_endpoints, outFGB, "endpoints") # create blank polygon feature class to store catchments arcpy.AddMessage( "Creating blank polygon feature class to store catchments...") arcpy.CreateFeatureclass_management( outFGB, "catch_ply", "POLYGON", seg_endpoints) #the coord. sys. for pnt_input is applied arcpy.MakeFeatureLayer_management(outFGB + "\\catch_ply", "catch_ply_lyr") arcpy.MakeFeatureLayer_management(seg_endpoints, "seg_endpoints_lyr") # create field mappings for pour points fm = arcpy.FieldMappings() fm.addTable(seg_endpoints) # set up counters total_cnt = arcpy.GetCount_management("seg_endpoints_lyr") # if upstream boolean is selected, iterate through each point arcpy.AddMessage("Delineating catchments for each pour point...") if upstream_bool == 'true': with arcpy.da.SearchCursor(seg_endpoints, ["SHAPE@", "LineOID"]) as cursor: for row in cursor: try: arcpy.FeatureClassToFeatureClass_conversion( row[0], "in_memory", "pnt_tmp", "#", fm) arcpy.MakeFeatureLayer_management("in_memory\\pnt_tmp", "pnt_tmp_lyr") arcpy.CalculateField_management("pnt_tmp_lyr", "LineOID", row[1], "PYTHON_9.3") pnt_snap = SnapPourPoint("pnt_tmp_lyr", fa, snap_dist, "LineOID") wshd_ras = Watershed(fd, pnt_snap, "VALUE") arcpy.RasterToPolygon_conversion(wshd_ras, "in_memory\\wshd_ply", "NO_SIMPLIFY", "VALUE") arcpy.MakeFeatureLayer_management("in_memory\\wshd_ply", "wshd_ply_lyr") arcpy.AddField_management("wshd_ply_lyr", "LineOID", "LONG") arcpy.CalculateField_management("wshd_ply_lyr", "LineOID", row[1], "PYTHON_9.3") arcpy.Append_management("wshd_ply_lyr", "catch_ply_lyr", "NO_TEST") arcpy.AddMessage("Catchment delineated for " + str(round(row[1])) + " of " + str(total_cnt) + " records...") except: arcpy.AddMessage( "\nError delineating catchments for point #" + str(row[1]) + ": " + arcpy.GetMessages(2)) arcpy.AddMessage("Moving to next pour point...") #raise Exception continue # otherwise, create a "RCA-style" polygons else: arcpy.MakeFeatureLayer_management(seg_endpoints, "pnt_tmp_lyr") snap_dist = cellSize * 2 pnt_snap = SnapPourPoint("pnt_tmp_lyr", fa, snap_dist, "LineOID") wshd_ras = Watershed(fd, pnt_snap, "Value") arcpy.RasterToPolygon_conversion(wshd_ras, "in_memory\\wshd_ply", "NO_SIMPLIFY", "Value") arcpy.MakeFeatureLayer_management("in_memory\\wshd_ply", "wshd_ply_lyr") arcpy.AddField_management("wshd_ply_lyr", "LineOID", "LONG") arcpy.CalculateField_management("wshd_ply_lyr", "LineOID", "!gridcode!", "PYTHON_9.3") arcpy.Append_management("wshd_ply_lyr", "catch_ply_lyr", "NO_TEST") # final clean up of upstream catchment polygons arcpy.AddMessage("Removing slivers and dissolving watershed polygons...") arcpy.AddMessage("...repairing geometry.") arcpy.RepairGeometry_management("catch_ply_lyr") arcpy.AddField_management("catch_ply_lyr", "sqkm", "DOUBLE") arcpy.AddMessage("...calculating area of catchment polygons.") arcpy.CalculateField_management("catch_ply_lyr", "sqkm", "!SHAPE.AREA@SQUAREKILOMETERS!", "PYTHON_9.3") arcpy.AddMessage("...selecting sliver polygons.") arcpy.SelectLayerByAttribute_management("catch_ply_lyr", "NEW_SELECTION", """"sqkm" <= 0.0001""") arcpy.AddMessage("...merging sliver polygons with largest neighbors.") arcpy.Eliminate_management("catch_ply_lyr", "in_memory\\catch_eliminate", "LENGTH") arcpy.MakeFeatureLayer_management("in_memory\\catch_eliminate", "catch_elim_lyr") arcpy.AddMessage( "...dissolving catchment polygons based on LineOID value.") arcpy.Dissolve_management("catch_elim_lyr", outFGB + "\\catch_final", "LineOID") arcpy.MakeFeatureLayer_management(outFGB + "\\catch_final", "catch_final_lyr") arcpy.Delete_management(outFGB + "\\catch_ply") # find errors arcpy.AddMessage("Adding error_code field...") ### TEMP arcpy.AddField_management("catch_final_lyr", "error_code", "SHORT") # error_code = 1; polygons are "too small" arcpy.AddField_management("catch_final_lyr", "sqkm", "DOUBLE") arcpy.CalculateField_management("catch_final_lyr", "sqkm", "!SHAPE.AREA@SQUAREKILOMETERS!", "PYTHON_9.3") arcpy.SelectLayerByAttribute_management("catch_final_lyr", "NEW_SELECTION", """"sqkm" <= 0.02 """) arcpy.CalculateField_management("catch_final_lyr", "error_code", "1", "PYTHON_9.3") arcpy.SelectLayerByAttribute_management("catch_final_lyr", "CLEAR_SELECTION") # error_code = 2; polygons are "too thin" arcpy.AddField_management("catch_final_lyr", "thinness", "DOUBLE") arcpy.CalculateField_management( "catch_final_lyr", "thinness", """(4*3.14*!SHAPE.AREA!)/(math.pow(!SHAPE.LENGTH!,2))""", "PYTHON_9.3") arcpy.SelectLayerByAttribute_management("catch_final_lyr", "NEW_SELECTION", """"thinness" < 0.090""") arcpy.CalculateField_management("catch_final_lyr", "error_code", "2", "PYTHON_9.3") arcpy.SelectLayerByAttribute_management("catch_final_lyr", "CLEAR_SELECTION") arcpy.DeleteField_management("catch_final_lyr", "thinness") # Outputs and stop processing clock for metadata mWriter.currentRun.addOutput("Output catchment area polygons", outFGB + r"\catch_final") mWriter.currentRun.addOutput("Output endpoints", outFGB + "\endpoints") mWriter.currentRun.addOutput("Output reconditioned DEM", outFGB + r"\dem_recond") mWriter.finalizeRun() # Write the metadata file d = datetime.datetime.now() outPath = os.path.dirname(outFGB) metadataFile = "{0}{1}{2}{3}{4}{5}{6}{7}".format(outPath, r"\metadata_", d.year, d.month, d.day, d.hour, d.minute, ".xml") mWriter.writeMetadataFile(metadataFile) return
eu = "eu" ik = "ng" zone = r'C:\havplan\havplan_data\kategorier\final_24_07\havplan_temalag\eksisterende_udviklingszone_sngprt.shp' 1 #def unique_values(table, field): # with arcpy.da.SearchCursor(table, [field]) as cursor: # unique = sorted({row[0] for row in cursor}) # sektlist = [] # for i in unique: # splitted = i.split(", ") # for x in splitted: # if x not in sektlist: # sektlist.append(x) # return (sektlist) # #sektors = (unique_values(zone, "sektor_fnl")) #for sektor in sektors: # # arcpy.SelectLayerByAttribute_management(zone, "", '"POLY_AREA" < 500 AND "sektor_fnl" LIKE {}'.format("'%{}%'".format(sektor))) # arcpy.Eliminate_management(zone, ) # print (os.path.join(os.path.dirname(zone), "test_eliminate_fiskeri.shp")) tmpfile = "in_memory/tmpfile" arcpy.MakeFeatureLayer_management(zone, tmpfile) arcpy.SelectLayerByAttribute_management( tmpfile, "", '"POLY_AREA" < 500 AND "sektor_fnl" LIKE {}'.format("'%Fiskeri%'")) output = (os.path.join(os.path.dirname(zone), "test_eliminate_fiskeri.shp")) arcpy.Eliminate_management(tmpfile, output, "", '"sektor_fnl" NOT LIKE {}'.format("'%Fiskeri%'"))
#loop through LOC_ID to address slivers that resulted from its subdivision for ID, status in locIDlst: print "Next polyon" #select the lots within the subdivided polygon arcpy.SelectLayerByAttribute_management("newFC_lyr", "NEW_SELECTION", """ "LOC_ID" = '{0}' """.format(ID)) #save it to its own file tempFC3 arcpy.CopyFeatures_management("newFC_lyr", "tempFC3.shp") #make a layer for tempFC3 arcpy.MakeFeatureLayer_management("tempFC3.shp", "tempFC3_lyr") #select subdivided lots for that polygon that do not meet the minimum size arcpy.SelectLayerByAttribute_management("tempFC3_lyr", "NEW_SELECTION", '"SHAPE_AREA" < {0}'.format(sizeToMerge)) selected = arcpy.Describe("tempFC3_lyr").FIDSet if selected == '': continue #merge slivers withinin this polygon with a lots that shares its LOC_ID and longest border and save to holding file tempFC4 arcpy.Eliminate_management(in_features="tempFC3_lyr", out_feature_class="tempFC4.shp", selection="LENGTH") #append the lots in the holding file tempFC4 to the final file (taxmap_AVAILABLE_postsubdivide) arcpy.Append_management(inputs="tempFC4.shp", target=finalFC, schema_type="TEST") arcpy.Delete_management("newFC_lyr") arcpy.Delete_management("tempFC_lyr") print "Finished removing sliver lots" #------------------------------------------------------# #### Delete unneeded files and layers arcpy.Delete_management("SelectedParcel_Update_lyr") arcpy.Delete_management("SelectedParcels_multipart_lyr") arcpy.Delete_management("Parcels_available_forSubdivision_lyr")
def Eliminate(inFeatures, outFeatureClass, expression, ciclo): nombre = "M" + str(random.randrange(0, 5000)) templfeatures = "blocklayer2" + "_" + str(random.randrange(0, 5000)) print inFeatures arcpy.MakeFeatureLayer_management(inFeatures, templfeatures) if ciclo == 1: nombre = "M" + str(random.randrange(0, 5000)) arcpy.MakeFeatureLayer_management(grilla, templGrilla) arcpy.MakeFeatureLayer_management(inFeatures, templfeatures) path = r"in_memory\%s" % nombre print "layer temporal1" fc_grilla = arcpy.SelectLayerByAttribute_management( templGrilla, "NEW_SELECTION", "PageNumber = %s" % str(numero)) print "selecionando grilla" fc_select = arcpy.SelectLayerByLocation_management( templfeatures, "have_their_center_in", templGrilla) print "seleccionando por centroides" arcpy.CopyFeatures_management(templfeatures, path) arcpy.MakeFeatureLayer_management(path, path + ".lyr") arcpy.AddField_management(in_table=path + ".lyr", field_name="Area_ha", field_type="DOUBLE") arcpy.CalculateField_management(in_table=path + ".lyr", field="Area_ha", expression="!SHAPE.area!", expression_type="PYTHON") print "layer temporal2" print "layer seleccione" fc_filtro = arcpy.SelectLayerByAttribute_management( path + ".lyr", "NEW_SELECTION", expression) print "corriendo eliminate primer ciclo" arcpy.Eliminate_management(path + ".lyr", outFeatureClass, "LENGTH", "") if ciclo == 0: arcpy.MakeFeatureLayer_management(templfeatures, templfeatures + ".lyr") arcpy.AddField_management(in_table=templfeatures + ".lyr", field_name="Area_ha", field_type="DOUBLE") arcpy.CalculateField_management(in_table=templfeatures + ".lyr", field="Area_ha", expression="!SHAPE.area!", expression_type="PYTHON") print "seleccionando ciclos alternos" fc_filtro = arcpy.SelectLayerByAttribute_management( templfeatures, "NEW_SELECTION", expression) print "corriendo eliminate ciclos alternos" arcpy.Eliminate_management(templfeatures, outFeatureClass, "LENGTH", "") if ciclo == 2: arcpy.MakeFeatureLayer_management(templfeatures, templfeatures + ".lyr") arcpy.CalculateField_management(in_table=templfeatures + ".lyr", field="Area_ha", expression="!SHAPE.area!", expression_type="PYTHON") print "seleccionando ciclos alternos" fc_filtro = arcpy.SelectLayerByAttribute_management( templfeatures, "NEW_SELECTION", expression) print "corriendo eliminate ciclos alternos" arcpy.Eliminate_management(templfeatures, outFeatureClass, "LENGTH", "")
with open("code/parameters/remove-slivers.toml") as conffile: slivers_params = toml.loads(conffile.read()) # set environmental variables arcpy.env.parallelProcessingFactor = general_params['threads'] arcpy.env.overwriteOutput = True arcpy.env.workspace = os.path.dirname(sys.argv[1]) # storing system cmds input_PTH = sys.argv[1] output_PTH = sys.argv[2] ### Main processing # calculate area arcpy.AddField_management(input_PTH, 'AREA', 'DOUBLE') arcpy.CalculateField_management(input_PTH, 'AREA', '!shape.area@SQUAREKILOMETERS!', 'PYTHON_9.3') # select slivers arcpy.MakeFeatureLayer_management(os.path.basename(input_PTH), os.path.basename(input_PTH) + '_layer') arcpy.SelectLayerByAttribute_management( os.path.basename(input_PTH) + '_layer', "NEW_SELECTION", 'AREA < ' + str(slivers_params['minimum_size_in_square_kilometers'])) # remove slivers arcpy.Eliminate_management( os.path.basename(input_PTH) + '_layer', output_PTH, slivers_params['selection'])
# select areas of topo data and assign value expression = str('"' + "FID_TopoBND_Seg_" + inSegment + "_" + inYear + '" ' + "> -1") arcpy.AddMessage("expression is...") arcpy.AddMessage(expression) arcpy.SelectLayerByAttribute_management(outUnionLayer, "NEW_SELECTION", expression) FieldValue = '"2"' #2 is for topo arcpy.CalculateField_management(outUnionLayer, FieldName, FieldValue, "PYTHON_9.3") # # select features smaller than threshold and eliminate SizeThreshold = str("3") expression = str('"' + "Shape_Area" + '" ' + "<" + SizeThreshold) arcpy.AddMessage("expression is...") arcpy.AddMessage(expression) arcpy.SelectLayerByAttribute_management(outUnionLayer, "NEW_SELECTION", expression) outUnionElim = str(workspaceGDB + "\\" + "Seg_" + inSegment + "_" + inYear + "_" + "UnionElim") arcpy.Eliminate_management(outUnionLayer, outUnionElim, "LENGTH") # Dissolve outUnionDissolve = str(workspaceGDB + "\\" + "Seg_" + inSegment + "_" + inYear + "_" + CellSizeName + "_SourcePoly") DissolveField = str('"Source_' + inYear + '"') arcpy.Dissolve_management(outUnionElim, outUnionDissolve, DissolveField, "","SINGLE_PART", "DISSOLVE_LINES") # # convert to raster outSourceRaster = str(outFolder + "\\" + "Seg_" + inSegment + "_" + inYear + "_" + CellSizeName + "_Source.tif") valueField = str('Source_' + inYear) arcpy.PolygonToRaster_conversion(outUnionDissolve, valueField, outSourceRaster,"CELL_CENTER", "NONE", CellSize) # # Add text field to polygon # FieldName = str("Source_" + inYear + "_text") arcpy.AddField_management(outUnionDissolve, FieldName, "TEXT") #
temp = r"D:\0_PROJECTS\3_QingHai_MaDuo_Project\MaDuoNewBorderTask0107\Result\temp" fcs = arcpy.ListFeatureClasses("*","All","") print "start" for fc in fcs: print fc # Set local variables inFeatures = fc tempLayer = os.path.join(temp,str(fc[:-4]) + "blocklayer") expression = '"SampleName" = \'1\'' #设置筛选条件,注意转义字符 用 \ 转义 #exclusionExpression = '' # Execute MakeFeatureLayer arcpy.MakeFeatureLayer_management(inFeatures, tempLayer) print "Step 1 finished!" # Execute SelectLayerByAttribute to define features to be eliminated arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION", expression) print "Step 2 finished!" # Execute Eliminate outFC_name = str(fc[:-4]) outFeatureClass = os.path.join(outFeaturePath,outFC_name) arcpy.Eliminate_management(tempLayer, outFeatureClass, "AREA",) print "" print "All finished"
def main(fcInputCenterline, fcInputPolygon, fcSegmentedPolygons, dblPointDensity=10.0, dblJunctionBuffer=100.00, workspaceTemp="in_memory"): # Manage Environments env_extent = arcpy.env.extent env_outputmflag = arcpy.env.outputMFlag env_outputzflag = arcpy.env.outputZFlag arcpy.env.outputMFlag = "Disabled" arcpy.env.outputZFlag = "Disabled" arcpy.env.extent = fcInputPolygon ## Set full extent to build Thiessan polygons over entire line network. # Copy centerline to temporary workspace fcCenterline = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_Centerline") arcpy.CopyFeatures_management(fcInputCenterline, fcCenterline) if "FromID" not in [ field.name for field in arcpy.ListFields(fcCenterline) ]: arcpy.AddField_management(fcCenterline, "FromID", "LONG") arcpy.CalculateField_management( fcCenterline, "FromID", "!{}!".format(arcpy.Describe(fcCenterline).OIDFieldName), "PYTHON_9.3") # Build Thiessan polygons arcpy.AddMessage("GNAT DPS: Building Thiessan polygons") arcpy.Densify_edit(fcCenterline, "DISTANCE", "{} METERS".format(dblPointDensity)) fcTribJunctionPoints = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_TribJunctionPoints") arcpy.Intersect_analysis([fcCenterline], fcTribJunctionPoints, output_type="POINT") fcThiessanPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanPoints") arcpy.FeatureVerticesToPoints_management(fcCenterline, fcThiessanPoints, "ALL") lyrThiessanPoints = gis_tools.newGISDataset("Layer", "lyrThiessanPoints") arcpy.MakeFeatureLayer_management(fcThiessanPoints, lyrThiessanPoints) arcpy.SelectLayerByLocation_management( lyrThiessanPoints, "INTERSECT", fcTribJunctionPoints, "{} METERS".format(dblJunctionBuffer)) fcThiessanPoly = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanPoly") # arcpy.CreateThiessenPolygons_analysis(lyrThiessanPoints,fcThiessanPoly,"ONLY_FID") arcpy.CreateThiessenPolygons_analysis(lyrThiessanPoints, fcThiessanPoly, "ALL") # Clean polygons # lyrInputPolygon = gis_tools.newGISDataset("Layer", "lyrInputPolygon") # arcpy.MakeFeatureLayer_management(fcInputPolygon, lyrInputPolygon) arcpy.RepairGeometry_management(fcInputPolygon, "KEEP_NULL") fcThiessanPolyClip = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_TheissanPolyClip") arcpy.Clip_analysis(fcThiessanPoly, fcInputPolygon, fcThiessanPolyClip) # Split the junction Thiessan polygons arcpy.AddMessage("GNAT DPS: Split junction Thiessan polygons") lyrTribThiessanPolys = gis_tools.newGISDataset("Layer", "lyrTribThiessanPolys") arcpy.MakeFeatureLayer_management(fcThiessanPolyClip, lyrTribThiessanPolys) arcpy.SelectLayerByLocation_management(lyrTribThiessanPolys, "INTERSECT", fcTribJunctionPoints) fcSplitPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_SplitPoints") arcpy.Intersect_analysis([lyrTribThiessanPolys, fcCenterline], fcSplitPoints, output_type="POINT") arcpy.AddMessage("GNAT DPS: Moving starting vertices of junction polygons") geometry_functions.changeStartingVertex(fcTribJunctionPoints, lyrTribThiessanPolys) arcpy.AddMessage("GNAT DPS: Vertices moved") fcThiessanTribPolyEdges = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_ThiessanTribPolyEdges") arcpy.FeatureToLine_management(lyrTribThiessanPolys, fcThiessanTribPolyEdges) fcSplitLines = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_SplitLines") arcpy.SplitLineAtPoint_management(fcThiessanTribPolyEdges, fcSplitPoints, fcSplitLines, "0.1 METERS") fcMidPoints = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_MidPoints") arcpy.FeatureVerticesToPoints_management(fcSplitLines, fcMidPoints, "MID") arcpy.Near_analysis(fcMidPoints, fcTribJunctionPoints, location="LOCATION") arcpy.AddXY_management(fcMidPoints) fcTribToMidLines = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_TribToMidLines") arcpy.XYToLine_management(fcMidPoints, fcTribToMidLines, "POINT_X", "POINT_Y", "NEAR_X", "NEAR_Y") ### Select polygons by centerline ### arcpy.AddMessage("GNAT DPS: Select polygons by centerline") fcThiessanEdges = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_ThiessanEdges") arcpy.FeatureToLine_management(fcThiessanPolyClip, fcThiessanEdges) fcAllEdges = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_AllEdges") arcpy.Merge_management([fcTribToMidLines, fcThiessanEdges, fcCenterline], fcAllEdges) # include fcCenterline if needed fcAllEdgesPolygons = gis_tools.newGISDataset(workspaceTemp, "GNAT_DPS_AllEdgesPolygons") arcpy.FeatureToPolygon_management(fcAllEdges, fcAllEdgesPolygons) fcAllEdgesPolygonsClip = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_AllEdgesPolygonsClip") arcpy.Clip_analysis(fcAllEdgesPolygons, fcInputPolygon, fcAllEdgesPolygonsClip) fcPolygonsJoinCenterline = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_PolygonsJoinCenterline") arcpy.SpatialJoin_analysis(fcAllEdgesPolygonsClip, fcCenterline, fcPolygonsJoinCenterline, "JOIN_ONE_TO_MANY", "KEEP_ALL", match_option="SHARE_A_LINE_SEGMENT_WITH") fcPolygonsDissolved = gis_tools.newGISDataset( workspaceTemp, "GNAT_DPS_PolygonsDissolved") arcpy.Dissolve_management(fcPolygonsJoinCenterline, fcPolygonsDissolved, "FromID", multi_part="SINGLE_PART") lyrPolygonsDissolved = gis_tools.newGISDataset("Layer", "lyrPolygonsDissolved") arcpy.MakeFeatureLayer_management(fcPolygonsDissolved, lyrPolygonsDissolved) arcpy.SelectLayerByAttribute_management(lyrPolygonsDissolved, "NEW_SELECTION", """ "FromID" IS NULL """) arcpy.Eliminate_management(lyrPolygonsDissolved, fcSegmentedPolygons, "LENGTH") arcpy.AddMessage("GNAT DPS: Tool complete") # Reset env arcpy.env.extent = env_extent arcpy.env.outputMFlag = env_outputmflag arcpy.env.outputZFlag = env_outputzflag return
#Step 12 arcpy.SelectLayerByLocation_management('too_narrow_select',"HAVE_THEIR_CENTER_IN"\ ,'corner_sum_select','#',"NEW_SELECTION") #Step 13 arcpy.Update_analysis("MU_gen", "too_narrow_select", "MU_cornered", "BORDERS", "0.1 Meters") #Step 14 arcpy.MakeFeatureLayer_management("MU_cornered", "MU_cornered_select") arcpy.SelectLayerByLocation_management("MU_cornered_select","COMPLETELY_WITHIN",\ 'corner_sum_select') arcpy.CopyFeatures_management("too_narrow_select", 'truncated') #Step 15 Eliminate arcpy.Eliminate_management("MU_cornered_select", "MU_decornered", "LENGTH") arcpy.Delete_management("too_narrow") arcpy.Delete_management("too_narrow_sing") arcpy.Delete_management("corner_buff_4") arcpy.Delete_management("corner_inter") arcpy.Delete_management("corner_inter_sing") arcpy.Delete_management("corner_sum") arcpy.Delete_management("MU_cornered") ###******************* Part II ***************** #********************************************************* if query: arcpy.MakeFeatureLayer_management("MU_decornered", 'MU_decornered_select', query)
arcpy.env.workspace = workspace arcpy.env.overwriteOutput = True # Define variable to check for min value arcpy.CopyFeatures_management(path, "areas_new") arcpy.SelectLayerByAttribute_management( "areas_new", "NEW_SELECTION", '[RORI] in (SELECT min( [RORI] ) FROM areas_new)') check1 = arcpy.SearchCursor("areas_new") for row in check1: dv = row.getValue("RORI") del check1, row # Elimination process while dv < Ka: arcpy.Eliminate_management("areas_new", "new") arcpy.SelectLayerByAttribute_management("areas_new", "CLEAR_SELECTION") arcpy.SpatialJoin_analysis( "new", "areas_new", "new1", "JOIN_ONE_TO_ONE", "KEEP_ALL", "RORI RORI true true false 4 Long 0 0 ,First,#,new,RORI,-1,-1;RORI RORI2 true true false 4 Long 0 0 ,Sum,#,areas_new,RORI,-1,-1", "CONTAINS", "#", "#") arcpy.DeleteField_management("new1", ["Join_Count", "RORI_1", "TARGET_FID"]) arcpy.CopyFeatures_management("new1", path) # Delete unnecessary files arcpy.Delete_management(path2) arcpy.Delete_management(path3) arcpy.Delete_management(path4) # Check for the next iteration arcpy.CopyFeatures_management(path, "areas_new") arcpy.SelectLayerByAttribute_management(
def extractPriorities(link, curSurface, LCPlayer, fullBuild): mosList = [] ######## #workspace = 'C:\\workspace\\test' arcpy.AddMessage('Analyzing priority linkage areas...') arcpy.AddMessage('\tExtracting fuzzy member set of linkage bottlenecks...') fuzzyCurrent = arcpy.sa.FuzzyMembership(curSurface, arcpy.sa.FuzzyMSLarge(0.9, 1)) fuzzyCurrent.save('c:\\workspace\\fuzzcur.img') # curExtract = arcpy.sa.SetNull (curSurface, curSurface, 'Value = 0') fuzzySTD = arcpy.sa.FocalStatistics(curSurface, arcpy.sa.NbrCircle(1000, "MAP"), "STD", "DATA") fuzzySTD.save('c:\\workspace\\fuzzstd.img') #fuzzySTD = arcpy.sa.FocalStatistics(curSurface,arcpy.sa.NbrRectangle(3, 3, "CELL"),"STD", "DATA") fuzzySTD = arcpy.sa.FuzzyMembership(fuzzySTD, arcpy.sa.FuzzyLarge("", 1)) fuzzySTD.save('c:\\workspace\\fuzzstd2.img') arcpy.AddMessage( '\tExtracting fuzzy member set of linkage areas vulnerable to loss...') fuzzyLink = arcpy.sa.GreaterThan(link, fullBuild) fuzzyLink = arcpy.sa.Float(fuzzyLink) fuzzyLink.save('c:\\workspace\\fuzzlink.img') ## fuzzyLink = arcpy.sa.FuzzyMembership ( lost_link, arcpy.sa.FuzzyLarge(0.5, 5)) values = getCombinations(LCPlayer, 'PATH_RNK')[0] LCPlayer = arcpy.MakeFeatureLayer_management(LCPlayer, "lcpLayer") arcpy.AddMessage( '\tExtracting fuzzy member set of linkage path regions...') for v in values: if v == 1: string = 'Primary Linkage Path...' Spread = 10 Hedge = 'VERY' elif v == 2: string = 'Secondary Linkage Paths...' Spread = 5 Hedge = 'SOMEWHAT' elif v == 3: string = 'Potential Restoration Paths...' Spread = 5 Hedge = 'NONE' arcpy.AddMessage('\t\tProcessing ' + string) pName = 'prior_' + str(int(v)) arcpy.SelectLayerByAttribute_management("lcpLayer", "NEW_SELECTION", "PATH_RNK = " + str(v)) #### arcpy.AddWarning("pName: " + pName) cd = arcpy.sa.CostDistance(LCPlayer, curSurface) cd.save("c:\\workspace\\cd_" + str(v) + ".img") # Set the midpoint for fuzzy membership to 90% of the maximum value of the cost distance raster # NOTE: This could be a user input variable to allow control over selection tolerance. midPoint = float( arcpy.GetRasterProperties_management(cd, "MAXIMUM").getOutput(0)) midPoint = midPoint - (midPoint * 0.9) fuzzyDistance = arcpy.sa.FuzzyMembership( cd, arcpy.sa.FuzzySmall(midPoint, Spread), Hedge) #### fuzzyDistance.save('c:\\workspace\\fuzzDistance_' + str(v) + '.img') #fuzzyDistance.save(os.path.join('c:\\workspace\\fuzzy', 'fuzzdist_' + str(int(v)) + '.img')) overlay = arcpy.sa.FuzzyOverlay( [fuzzyCurrent, fuzzyDistance, fuzzySTD], 'AND') overlay = arcpy.sa.Slice(overlay, 2, 'EQUAL_INTERVAL') # overlay.save(os.path.join(workspace, 'fuzzyoverlay_' + str(int(v)) + '.img')) # Do vulnerable areas need to be enumerated separate from bottlenecks?????? overlay2 = arcpy.sa.FuzzyOverlay( [fuzzyLink, fuzzyDistance, fuzzyCurrent, fuzzySTD], 'AND') overlay2 = arcpy.sa.Slice(overlay2, 3, 'EQUAL_INTERVAL') overlay = arcpy.sa.Con(overlay2, 3, overlay, 'Value >= 2') #overlay = arcpy.sa.FuzzyOverlay ([overlay, overlay2], 'OR') overlay = arcpy.sa.ExtractByAttributes(overlay, 'Value >= 2') ## At Arcmap 10.2 the following line stopped working on the second pass of the for loop. It works fine if the commands are ## executed manually at the python command prompt. Another ArcGIS mystery. Workaround was to use Con to achieve the same result. ## overlay = arcpy.sa.Reclassify (overlay, 'Value', arcpy.sa.RemapValue([[2, int(v)],[3, int(v)]] )) overlay = arcpy.sa.Con(overlay, int(v), overlay, 'Value >= 2 AND Value <= 3') #overlay = arcpy.sa.Con(overlay, int(v)) arcpy.MakeRasterLayer_management(overlay, pName) mosList.append(pName) ## for var in [overlay, overlay2]: ## arcpy.Delete_management(var) ## for var in [fuzzyCurrent, fuzzySTD, fuzzyLink, fuzzyDistance]: ## arcpy.Delete_management(var) arcpy.SelectLayerByAttribute_management(LCPlayer, "CLEAR_SELECTION", "") arcpy.MosaicToNewRaster_management(mosList, workspace, "bneck_mos", "", "", "", "1", "MINIMUM") arcpy.CalculateStatistics_management(os.path.join(workspace, "bneck_mos")) ## # Add Cleanup routines. Memory could get full!!!!!! ## for var in [overlay, overlay2, fuzzyCurrent, fuzzySTD, fuzzyLink, fuzzyDistance]: ## arcpy.Delete_management(var) ### add filters here........................... arcpy.AddMessage('\tCleaning priority patch boundaries...') b_neck = arcpy.sa.BoundaryClean(os.path.join(workspace, "bneck_mos"), "ASCEND", "TWO_WAY") # b_neck.save('c:\\workspace\\test\\b_neck_clean.img') arcpy.AddMessage('\tExtracting restoration areas...') r_patch = arcpy.sa.BooleanAnd(b_neck, link) #r_patch.save('c:\\workspace\\test\\r_patch_bool.img') r_patch = arcpy.sa.ExtractByAttributes(r_patch, 'Value = 0') ## r_patch.save('c:\\workspace\\test\\r_patch_extract.img') arcpy.AddMessage('\tExtracting areas vulnerable to loss...') v_patch = arcpy.sa.GreaterThan(link, fullBuild) ## v_patch.save('c:\\workspace\\test\\v_patch_GT.img') #v_patch = arcpy.sa.Con(v_patch, b_neck, "", 'Value = 1') #v_patch.save('c:\\workspace\\test\\v_patch_Con.img') v_patch = arcpy.sa.ExtractByAttributes(v_patch, 'Value = 1') ## v_patch.save('c:\\workspace\\test\\v_patch_extract.img') arcpy.AddMessage('\tExtracting secure areas...') b_patch = arcpy.sa.Reclassify(b_neck, 'Value', arcpy.sa.RemapRange([[1, 3, 2]])) ### ## b_patch = arcpy.sa.Times(b_patch, 1) ## b_patch.save('c:\\workspace\\test\\b_patch_reclass.img') ### ## workspace2 = 'c:\\workspace\\test' ## b_neck.save(os.path.join(workspace2, 'bneck_mos.img')) arcpy.MosaicToNewRaster_management([r_patch, v_patch, b_patch], workspace, "prior_mos", "", "", "", "1", "MINIMUM") p_patch = arcpy.RasterToPolygon_conversion( os.path.join(workspace, "prior_mos"), 'in_memory\\p_patch', 0) b_neck = arcpy.RasterToPolygon_conversion(b_neck, 'in_memory\\b_neck', 0) ## p_patch = arcpy.RasterToPolygon_conversion (os.path.join(workspace, "prior_mos"), 'c:\\workspace\\p_patch', 0) ## b_neck = arcpy.RasterToPolygon_conversion (b_neck, 'c:\\workspace\\b_neck', 0) try: arcpy.Delete_management(r_patch) arcpy.Delete_management(v_patch) arcpy.Delete_management(b_patch) except: pass outP = arcpy.Intersect_analysis([b_neck, p_patch], 'in_memory\\outP') # outP = arcpy.Intersect_analysis ([b_neck, p_patch], 'c:\\workspace\\outP') arcpy.AddMessage('\tFormatting priority attributes...') ## Beginning with ArcGIS 10.2, field names produced from prior processing steps changed from "grid_code" and "grid_code_1" ## to "gridcode" and "gridcode_1" respectively. The following code checks field names and assigns the correct string to a ## variable for subsequent processing. Thanks ESRI for once again making random changes that break scripts with no performance ## benefit. fields = arcpy.ListFields(outP) for field in fields: if field.name == "grid_code": gc = "grid_code" elif field.name == "gridcode": gc = "gridcode" elif field.name == "grid_code_1": gc_1 = "grid_code_1" elif field.name == "gridcode_1": gc_1 = "gridcode_1" table = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'MiscFiles', 'priorityjointable.dbf')) arcpy.JoinField_management(outP, gc, table, "PATH_RANK", "PATH_TYPE") arcpy.JoinField_management(outP, gc_1, table, "PATCH_RANK", "PATCH_TYPE") arcpy.AddField_management(outP, "rnk", "SHORT") arcpy.CalculateField_management(outP, "rnk", '[' + gc + '] & [' + gc_1 + ']', "VB", "") if len(connectList) > 0: arcpy.JoinField_management(outP, 'rnk', table, "RNK", "PRIORITY_R") else: arcpy.JoinField_management(outP, 'rnk', table, "RNK", "REST_RNK") ## arcpy.AddField_management ('pLayer', 'PATH_TYPE', 'TEXT', "", "", 15) ## arcpy.AddField_management ('pLayer', 'PATCH_TYPE', 'TEXT', "", "", 15) # Get the fields from the input fields = arcpy.ListFields(outP) # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Iterate through the fields and set them to fieldinfo for field in fields: if field.name == gc: fieldinfo.addField(field.name, "PATH_RANK", "VISIBLE", "") elif field.name == "PATH_TYPE": fieldinfo.addField(field.name, "PATH_TYPE", "VISIBLE", "") elif field.name == gc_1: fieldinfo.addField(field.name, "PATCH_RANK", "VISIBLE", "") elif field.name == "PATCH_TYPE": fieldinfo.addField(field.name, "PATCH_TYPE", "VISIBLE", "") elif field.name == "PRIORITY_R" or field.name == "REST_RNK": fieldinfo.addField(field.name, "PRIOR_RNK", "VISIBLE", "") else: fieldInfo = fieldinfo.addField(field.name, field.name, "HIDDEN", "") arcpy.MakeFeatureLayer_management(outP, 'pLayer', "", "", fieldinfo) ######### # arcpy.CopyFeatures_management("pLayer", "C:\\workspace\\pLayer.shp") arcpy.AddField_management("pLayer", "ACRES", "DOUBLE", "", "", "", "", "", "", "") arcpy.AddField_management("pLayer", "ACRES", "DOUBLE", "", "", "", "", "", "", "") arcpy.CalculateField_management("pLayer", "ACRES", "!shape.area@acres!", "PYTHON", "") arcpy.SelectLayerByAttribute_management("pLayer", "NEW_SELECTION", "ACRES < 5") arcpy.Eliminate_management("pLayer", "in_memory\\pLayer", "AREA") arcpy.Delete_management("pLayer") arcpy.MakeFeatureLayer_management("in_memory\\pLayer", 'outpLayer') arcpy.SelectLayerByAttribute_management("outpLayer", "NEW_SELECTION", "ACRES >= 5") return ('outpLayer')