def simplify_then_densify(input_features): """ Simplify and densify the input features to normalize them for processing """ if os.path.basename( os.path.splitext(input_features)[0]).endswith("_projected"): if os.path.basename(input_features).endswith(".shp"): processed_output = (os.path.splitext(input_features)[0] ).rstrip("_projected") + "_processed.shp" else: processed_output = (os.path.splitext(input_features)[0] ).rstrip("_projected") + "_processed" else: if os.path.basename(input_features).endswith(".shp"): processed_output = os.path.splitext( input_features)[0] + "_processed.shp" else: processed_output = os.path.splitext( input_features)[0] + "_processed" print "Simplifying..." arcpy.SimplifyLine_cartography(input_features, processed_output, "BEND_SIMPLIFY", "100 Feet", collapsed_point_option="NO_KEEP") print "Densifying..." return arcpy.Densify_edit(processed_output, "Distance", "100 Feet")
def convertAltStreets(Project_Folder): arcpy.env.overwriteOutput = True Model_Inputs_gdb = os.path.join(Project_Folder, 'Model_Inputs.gdb') Model_Outputs_gdb = os.path.join(Project_Folder, 'Model_Outputs.gdb') streets_simple = os.path.join(Model_Outputs_gdb, 'Streets_Simple') altstreets = os.path.join(Model_Inputs_gdb, 'AltStreets') arcpy.env.workspace = Model_Inputs_gdb # Simplify AltStreets and Streets Lines # removes some of the nodes that make up the lines to make the files low resolution enough to be uploaded through mapmaker altstreets_simple = arcpy.SimplifyLine_cartography(in_features=altstreets, out_feature_class=os.path.join(Model_Outputs_gdb, "AltStreet_simple"), algorithm="POINT_REMOVE", tolerance="5 Feet", error_resolving_option="RESOLVE_ERRORS", collapsed_point_option="KEEP_COLLAPSED_POINTS", error_checking_option="CHECK", in_barriers=[])[0] # add ref_zlev and dom fields for alias classification and linking to streets file arcpy.AddFields_management(in_table=altstreets_simple, field_description=[ ["REF_ZLEV", "SHORT"], ["DOM", "LONG"]]) print('added fields to altstreets') arcpy.AddIndex_management(altstreets_simple, fields=[ "LINK_ID"], index_name="LINK_ID", unique="NON_UNIQUE", ascending="ASCENDING") print('added altstreet index') arcpy.JoinField_management(in_data=altstreets_simple, in_field="LINK_ID", join_table=streets_simple, join_field="LINK_ID", fields=["NUM_STNMES"]) print('joined altstreets to streets') # Filter out all of the altstreet rows that do not have multiple names altstreets_filter = arcpy.FeatureClassToFeatureClass_conversion( in_features=altstreets_simple, out_path=Model_Outputs_gdb, out_name="AltStreets_Filter", where_clause="NUM_STNMES > 1") print('altstreets filtered if less than 2') # Create Statistics Table from AltStreets_Simple # add in the count of all the street names added to the altstreets simple altstreet_stats = os.path.join(Model_Outputs_gdb, "Altstreets_Stats") arcpy.Statistics_analysis(in_table=altstreets_filter, out_table=altstreet_stats, statistics_fields=[ ["LINK_ID", "FIRST"]], case_field=["LINK_ID", "ST_NAME"]) # Join AltStreets_Simple with AltStreets_Stats arcpy.JoinField_management(in_data=altstreets_simple, in_field="LINK_ID", join_table=altstreet_stats, join_field="LINK_ID", fields=["NUM_STNMES"]) arcpy.CalculateField_management(in_table=altstreets_simple, field="Dom", expression="1", expression_type="PYTHON3", code_block="", field_type="TEXT") # Alias streetname identifier calculation (Alias == -9) # MapMaker REQUIRES it to be -9 in order to find it as an alias field arcpy.CalculateField_management(in_table=altstreets_simple, field="REF_ZLEV", expression="-9", expression_type="PYTHON3", code_block="", field_type="TEXT") # updated the schema to match mapmaker schema updateSchema(altstreets_simple) # returns altstreets_final gdb location return arcpy.FeatureClassToFeatureClass_conversion(in_features=altstreets_simple, out_path=Model_Outputs_gdb, out_name="AltStreets_Final")[0]
def Wygladzenie_lini_pow(): arcpy.Dissolve_management( "Rzeka_strum_L", "D:\mgr\M33033CD\Rzeka.shp" ) # polaczenie w jedno aby kolejna funkcja dzialala arcpy.SimplifyLine_cartography("D:\mgr\M33033CD\Rzeka.shp", "D:\mgr\M33033CD\Rzeka_L.shp", "BEND_SIMPLIFY", 200) arcpy.SimplifyPolygon_cartography("Las_A", "D:\mgr\M33033CD\Las.shp", "BEND_SIMPLIFY", 200)
''' contour_post.py Contour post processing Simplifies lines and smooths them based on user input regarding precision This is intended for use with DEM derived contours Steven Porter [email protected] 3/7/12 ''' import arcpy, sys contours = sys.argv[0] precision = sys.argv[2] output = sys.argv[3] from arcpy import env env.workspace = 'C:\\Windows\\Temp' #simplify line by removing unneeded vertices temp = arcpy.SimplifyLine_cartography(contours, "temp", precision) #smooth line of any sharp bends arcpy.SmoothLine_cartography(temp, output, "PAEK", precision ^ 2 * math.pi) #delete temp file arcpy.Delete_managment(temp)
def execute(self, parameters, messages): """The source code of your tool.""" arcpy.env.overwriteOutput = True arcpy.AddMessage( "Species distribution patterns around islands, generate a grid for analysis" ) for param in parameters: arcpy.AddMessage("Parameter: %s = %s" % (param.name, param.valueAsText)) # See http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000063000000 input_line = parameters[0].valueAsText output_directory = parameters[1].valueAsText distance = parameters[2].value simp_distance = parameters[3].value points_distance = parameters[4].value clean_up = parameters[5].value # 0 Describe files to set coordinate systems desc_input = arcpy.Describe(input_line) coord_system = desc_input.spatialReference arcpy.env.outputCoordinateSystem = coord_system # 1 Make output director and output files they do not exist if not os.path.exists(output_directory): os.makedirs(output_directory) arcpy.env.workspace = output_directory # 2 Buffer at distance around line arcpy.Buffer_analysis( in_features=input_line, out_feature_class=os.path.join(output_directory, "buffer.shp"), buffer_distance_or_field=str(distance) + " Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") # 3 Convert polygon to Line def polys_to_lines(fc, new_fc): # From http://gis.stackexchange.com/questions/129662/creating-lines-from-polygon-borders-with-polygon-attributes-using-arcgis-arcpy path, name = os.path.split(new_fc) sm = 'SAME_AS_TEMPLATE' arcpy.CreateFeatureclass_management(path, name, 'POLYLINE', fc, sm, sm, fc) fields = [ f.name for f in arcpy.ListFields(new_fc) if f.type not in ('OID', 'Geometry') ] # get attributes with arcpy.da.SearchCursor(fc, ['SHAPE@'] + fields) as rows: values = [(r[0].boundary(), ) + tuple(r[1:]) for r in rows] # insert rows with arcpy.da.InsertCursor(new_fc, ['SHAPE@'] + fields) as irows: for vals in values: irows.insertRow(vals) return new_fc polys_to_lines(os.path.join(output_directory, "buffer.shp"), os.path.join(output_directory, "lines.shp")) arcpy.MultipartToSinglepart_management( in_features=os.path.join(output_directory, "lines.shp"), out_feature_class=os.path.join(output_directory, "lines_explode.shp")) arcpy.SimplifyLine_cartography( in_features=os.path.join(output_directory, "lines_explode.shp"), out_feature_class=os.path.join(output_directory, "lines_explode_simplify.shp"), algorithm="BEND_SIMPLIFY", tolerance=str(simp_distance) + " Meters", error_resolving_option="FLAG_ERRORS", collapsed_point_option="KEEP_COLLAPSED_POINTS", error_checking_option="CHECK") # 4 Create points along the line arcpy.CreateFeatureclass_management(output_directory, "points_line.shp", 'POINT') arcpy.Project_management( in_dataset=os.path.join(output_directory, "lines_explode_simplify.shp"), out_dataset=os.path.join(output_directory, "lines_explode_simplify_proj.shp"), out_coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]", transform_method="", in_coor_system=coord_system, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.CreateFeatureclass_management(output_directory, "points_line_outer.shp", 'POINT') arcpy.CreateFeatureclass_management(output_directory, "points_line_inner.shp", 'POINT') def points_along_line(line_lyr, pnt_layer, pnt_dist, inn_out): # From https://geonet.esri.com/thread/95549 search_cursor = arcpy.da.SearchCursor(line_lyr, ['SHAPE@', 'FID']) insert_cursor = arcpy.da.InsertCursor(pnt_layer, 'SHAPE@') for row in search_cursor: if inn_out == "inner": if row[1] == 1: for dist in range(0, int(row[0].length), int(pnt_dist)): point = row[0].positionAlongLine(dist).firstPoint insert_cursor.insertRow([point]) elif inn_out == "outer": if row[1] == 0: for dist in range(0, int(row[0].length), int(pnt_dist)): point = row[0].positionAlongLine(dist).firstPoint insert_cursor.insertRow([point]) points_along_line( os.path.join(output_directory, "lines_explode_simplify_proj.shp"), os.path.join(output_directory, "points_line.shp"), points_distance, "inner") points_along_line( os.path.join(output_directory, "lines_explode_simplify_proj.shp"), os.path.join(output_directory, "points_line_outer.shp"), 1, "outer") points_along_line( os.path.join(output_directory, "lines_explode_simplify_proj.shp"), os.path.join(output_directory, "points_line_inner.shp"), 1, "inner") rows_list = [ row for row in arcpy.da.SearchCursor( os.path.join(output_directory, "points_line.shp"), "FID") ] delete_cursor = arcpy.da.UpdateCursor( os.path.join(output_directory, "points_line.shp"), "FID") delete_value = rows_list[-1][0] for row in delete_cursor: if row[0] == delete_value: delete_cursor.deleteRow() arcpy.DefineProjection_management( os.path.join(output_directory, "points_line.shp"), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) arcpy.DefineProjection_management( os.path.join(output_directory, "points_line_outer.shp"), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) arcpy.DefineProjection_management( os.path.join(output_directory, "points_line_inner.shp"), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) arcpy.Project_management( os.path.join(output_directory, "points_line.shp"), out_dataset=os.path.join(output_directory, "points_line_wgs.shp"), out_coor_system=coord_system, transform_method="", in_coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]", preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") def points_to_polygon_line(output_directory, points): sr = arcpy.Describe(os.path.join(output_directory, points)).spatialReference coords = np.array(list( list(x) for x in arcpy.da.FeatureClassToNumPyArray(os.path.join( output_directory, points), ["SHAPE@X", "SHAPE@Y"], "", sr, explode_to_points=True)), dtype="float64") arcpy.CreateFeatureclass_management(output_directory, "p2pl_li_" + points, "Polyline") arcpy.CreateFeatureclass_management(output_directory, "p2pl_pol_" + points, "Polygon") pnt = arcpy.Point() ary = arcpy.Array() cur = arcpy.InsertCursor( os.path.join(output_directory, "p2pl_pol_" + points)) for coord in coords: pnt.X = coord[0] pnt.Y = coord[1] ary.add(pnt) polygon = arcpy.Polygon(ary) feat = cur.newRow() feat.shape = polygon cur.insertRow(feat) del cur, pnt, ary, coord, polygon, feat pnt = arcpy.Point() ary = arcpy.Array() cur = arcpy.InsertCursor( os.path.join(output_directory, "p2pl_li_" + points)) for coord in coords: pnt.X = coord[0] pnt.Y = coord[1] ary.add(pnt) line = arcpy.Polyline(ary) feat = cur.newRow() feat.shape = line cur.insertRow(feat) del cur, pnt, ary, coord, line, feat arcpy.DefineProjection_management( os.path.join(output_directory, "p2pl_li_" + points), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) arcpy.DefineProjection_management( os.path.join(output_directory, "p2pl_pol_" + points), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) return points_to_polygon_line(output_directory, "points_line_inner.shp") points_to_polygon_line(output_directory, "points_line_outer.shp") # 5 Generate perpendicular lines desc = arcpy.Describe(os.path.join(output_directory, "points_line.shp")) sr = arcpy.SpatialReference(desc.spatialReference.factoryCode) dests = np.array(list( list(x) for x in arcpy.da.FeatureClassToNumPyArray( os.path.join(output_directory, "points_line_outer.shp"), ["SHAPE@X", "SHAPE@Y"], "", sr, True)), dtype="float64") arcpy.CreateFeatureclass_management(output_directory, "intersect_lines.shp", "Polyline") search_cursor = arcpy.da.SearchCursor( os.path.join(output_directory, "points_line.shp"), ['SHAPE@X', 'SHAPE@Y']) for row in search_cursor: origin = np.array((row[0], row[1]), dtype="float64") deltas = dests - origin distances = np.hypot(deltas[:, 0], deltas[:, 1]) min_dist = np.min(distances) wh = np.where(distances == min_dist) closest = dests[wh[0]] cur = arcpy.InsertCursor( os.path.join(output_directory, "intersect_lines.shp")) # Build array of start/ends line_array = arcpy.Array() # start point start = arcpy.Point() (start.ID, start.X, start.Y) = (1, origin.item( (0)), origin.item((1))) line_array.add(start) # end point end = arcpy.Point() (end.ID, end.X, end.Y) = (2, closest.item( (0, 0)), closest.item((0, 1))) line_array.add(end) # write our fancy feature to the shapefile feat = cur.newRow() feat.shape = line_array cur.insertRow(feat) # yes, this shouldn't really be necessary... line_array.removeAll() del origin arcpy.DefineProjection_management( os.path.join(output_directory, "intersect_lines.shp"), coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]" ) arcpy.Union_analysis(in_features=[ os.path.join(output_directory, "p2pl_pol_points_line_inner.shp"), os.path.join(output_directory, "p2pl_pol_points_line_outer.shp") ], out_feature_class=os.path.join( output_directory, "simplified_polygon.shp"), join_attributes="ALL", cluster_tolerance="", gaps="GAPS") with arcpy.da.UpdateCursor( os.path.join(output_directory, "simplified_polygon.shp"), "FID") as cursor: for row in cursor: if row[0] == 1: cursor.deleteRow() arcpy.Buffer_analysis(in_features=os.path.join(output_directory, "intersect_lines.shp"), out_feature_class=os.path.join( output_directory, "intersect_lines_buf.shp"), buffer_distance_or_field="1 Centimeters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") arcpy.Union_analysis(in_features=[ os.path.join(output_directory, "intersect_lines_buf.shp"), os.path.join(output_directory, "simplified_polygon.shp") ], out_feature_class=os.path.join( output_directory, "intersect_lines_buffered_polygon.shp"), join_attributes="ALL", cluster_tolerance="", gaps="GAPS") arcpy.Project_management( os.path.join(output_directory, "intersect_lines_buffered_polygon.shp"), out_dataset=os.path.join( output_directory, "intersect_lines_buffered_polygon_proj.shp"), out_coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]", transform_method="", in_coor_system=coord_system, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.CalculateAreas_stats( Input_Feature_Class=os.path.join( output_directory, "intersect_lines_buffered_polygon_proj.shp"), Output_Feature_Class=os.path.join( output_directory, "intersect_lines_buffered_polygon_areas.shp")) polygon_sizes = [] with arcpy.da.SearchCursor( os.path.join(output_directory, "intersect_lines_buffered_polygon_areas.shp"), "F_Area") as cursor: for row in cursor: polygon_sizes.append(int(row[0])) polygon_sizes = sorted(polygon_sizes, key=int, reverse=True) with arcpy.da.UpdateCursor( os.path.join(output_directory, "intersect_lines_buffered_polygon_areas.shp"), "F_Area") as cursor: for row in cursor: if int(row[0]) == int(polygon_sizes[0]): pass else: cursor.deleteRow() arcpy.MultipartToSinglepart_management(in_features=os.path.join( output_directory, "intersect_lines_buffered_polygon_areas.shp"), out_feature_class=os.path.join( output_directory, "grid_file1.shp")) arcpy.Project_management( os.path.join(output_directory, "grid_file1.shp"), out_dataset=os.path.join(output_directory, "grid_file.shp"), out_coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]", transform_method="", in_coor_system=coord_system, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.AddField_management(in_table=os.path.join( output_directory, "grid_file.shp"), field_name="GRID_ID", field_type="LONG", field_precision="", field_scale="", field_length="", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="") with arcpy.da.UpdateCursor( os.path.join(output_directory, "grid_file.shp"), ["FID", "GRID_ID"]) as cursor: for row in cursor: row[1] = row[0] cursor.updateRow(row) arcpy.Project_management( os.path.join(output_directory, "grid_file.shp"), out_dataset=os.path.join(output_directory, "grid_file_wgs.shp"), out_coor_system=coord_system, transform_method="", in_coor_system= "PROJCS['World_Mercator',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]]", preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.AddWarning( "The field name GRID_ID will likely not be sequential, and you will have to fix manually." ) arcpy.AddMessage( "After fixing the non-sequentially spaced names, you will have to manually join " "the layer back to the original point observations.") if clean_up: delete_list = [ os.path.join(output_directory, "buffer.shp"), os.path.join(output_directory, "lines.shp"), os.path.join(output_directory, "lines_explode.shp"), os.path.join(output_directory, "lines_explode_simplify.shp"), os.path.join(output_directory, "lines_explode_simplify_Pnt.shp"), os.path.join(output_directory, "intersect_lines_buffered_polygon_areas.shp"), os.path.join(output_directory, "intersect_lines_buffered_polygon_proj.shp"), os.path.join(output_directory, "lines_explode_simplify_proj.shp"), os.path.join(output_directory, "p2pl_li_points_line_inner.shp"), os.path.join(output_directory, "p2pl_li_points_line_outer.shp"), os.path.join(output_directory, "p2pl_pol_points_line_inner.shp"), os.path.join(output_directory, "p2pl_pol_points_line_outer.shp"), os.path.join(output_directory, "points_line.shp"), os.path.join(output_directory, "points_line_inner.shp"), os.path.join(output_directory, "points_line_outer.shp"), os.path.join(output_directory, "points_line_wgs.shp"), os.path.join(output_directory, "simplified_polygon.shp"), os.path.join(output_directory, "grid_file1.shp"), os.path.join(output_directory, "intersect_lines.shp"), os.path.join(output_directory, "intersect_lines_buf.shp"), os.path.join(output_directory, "intersect_lines_buffered_polygon.shp"), ] for i in delete_list: arcpy.Delete_management(i) return
pivot_point = '{0} {1}'.format(bdy_fc_centroid[0], bdy_fc_centroid[1]) # X Y out_raster = os.path.join(tmp_gdb, bdy_name + '_raster') out_raster_rotated = os.path.join(tmp_gdb, bdy_name + '_raster_r') tmp_fishnet_rotated = os.path.join(tmp_gdb, bdy_name + '_fishnet_r') # Convert to raster, rotate, and convert back to polyline (use 10m to keep our raster cells separate) arcpy.PolylineToRaster_conversion(tmp_fishnet_path, 'OID', out_raster, 'MAXIMUM_LENGTH', 'NONE', 10) arcpy.Rotate_management(out_raster, out_raster_rotated, rotation_val, pivot_point, 'NEAREST') arcpy.RasterToPolyline_conversion(out_raster_rotated, tmp_fishnet_rotated, 'ZERO', 0, 'SIMPLIFY') arcpy.AddMessage( 'Rotated data by specified value: {0} degrees'.format(rotation_val)) # Perform a real simplification on the layer - to tidy up the lines tmp_fishnet_rotated_simpl = tmp_fishnet_rotated + '_s' arcpy.SimplifyLine_cartography(tmp_fishnet_rotated, tmp_fishnet_rotated_simpl, 'POINT_REMOVE', 10) time.sleep(5) arcpy.AddMessage('Simplified/cleaned up data') arcpy.AddMessage('\n') # Clip rotated lines to input boundary # tmp_fishnet_clip = os.path.join(tmp_gdb, bdy_name + '_fishnet_r_s_c') arcpy.Clip_analysis(tmp_fishnet_rotated_simpl, bdy_fc_path, tmp_fishnet_clip) arcpy.AddMessage('>> Clipped new trap lines to input boundary') arcpy.AddMessage('\n') # Generate points along the clipped lines at set point intervals # tmp_pts_along_line = os.path.join(tmp_gdb, bdy_name + '_fishnet_r_s_c_pts') dist = '{0} meters'.format(point_interval) # US formatted spelling...
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")
def convertStreets(Project_Folder, us_counties): arcpy.env.overwriteOutput = True Model_Inputs_gdb = os.path.join(Project_Folder, 'Model_Inputs.gdb') Model_Outputs_gdb = os.path.join(Project_Folder, 'Model_Outputs.gdb') streets = os.path.join(Model_Inputs_gdb, 'Streets') zlevels = os.path.join(Model_Inputs_gdb, 'Zlevels') adminbound4 = os.path.join(Model_Inputs_gdb, 'Adminbndy4') arcpy.env.workspace = Model_Inputs_gdb # Simplify AltStreets and Streets Lines streets_simple = arcpy.SimplifyLine_cartography(in_features=streets, out_feature_class=os.path.join(Model_Outputs_gdb, "Streets_Simple"), algorithm="POINT_REMOVE", tolerance="5 Feet", error_resolving_option="RESOLVE_ERRORS", collapsed_point_option="KEEP_COLLAPSED_POINTS", error_checking_option="CHECK", in_barriers=[])[0] arcpy.AddFields_management(in_table=streets_simple, field_description=[["REF_ZLEV", "LONG", "", "", "", ""], ["NREF_ZLEV", "LONG", "", "", "", ""], ["PlaceCodeL", "LONG", "", "", "", ""], ["PlaceCodeR", "LONG", "", "", "", ""], ["PlaceNamL", "TEXT", "", "255", "", ""], ["PlaceNamR", "TEXT", "", "255", "", ""], ["CountyCodeL", "LONG", "", "", "", ""], ["CountyCodeR", "LONG", "", "", "", ""], ["CountyNamL", "TEXT", "", "255", "", ""], ["CountyNamR", "TEXT", "", "255", "", ""], ["StateCodeL", "LONG", "", "", "", ""], ["StateCodeR", "LONG", "", "", "", ""], ["StateAbbrL", "TEXT", "", "255", "", ""], ["StateAbbrR", "TEXT", "", "255", "", ""], ["OneWay", "SHORT", "", "", "", ""], ["Speed", "LONG", "", "", "", ""], ["CFCC", "TEXT", "", "255", "", ""], ["M_LINK_ID", "LONG", "", "", "", ""], ["OLD_LINK_ID", "LONG", "", "", "", ""]]) print('Fields added to Streets') # turning restrictions arcpy.JoinField_management(in_data=streets_simple, in_field="REF_IN_ID", join_table=zlevels, join_field="NODE_ID", fields=["Z_LEVEL"]) # return calculated z level turning restrictions arcpy.CalculateField_management(in_table=streets_simple, field="REF_ZLEV", expression="zlevCalc(!Z_LEVEL!)", expression_type="PYTHON3", code_block="""def zlevCalc(z): if(z != 0): return z else: return 0""", field_type="TEXT") arcpy.DeleteField_management( in_table=streets_simple, drop_field=["ZLEVEL"]) print('REF_ZLEV Calculated') # calculate NREF arcpy.JoinField_management(in_data=streets_simple, in_field="NREF_IN_ID", join_table=zlevels, join_field="NODE_ID", fields=["Z_LEVEL"]) arcpy.CalculateField_management(in_table=streets_simple, field="NREF_ZLEV", expression="zlevCalc(!Z_LEVEL!)", expression_type="PYTHON3", code_block="""def zlevCalc(z): if(z != 0): return z else: return 0""", field_type="TEXT") arcpy.DeleteField_management( in_table=streets_simple, drop_field=["ZLEVEL"]) print('NREF_ZLEV Calculated') # Calculate Cities/AdminBndry4 fields # calculate R_AREA Cities arcpy.JoinField_management(in_data=streets_simple, in_field="R_AREA_ID", join_table=adminbound4, join_field="AREA_ID", fields=["AREA_ID", "POLYGON_NM"]) arcpy.CalculateField_management( in_table=streets_simple, field="PlaceCodeR", expression="!AREA_ID!", expression_type="PYTHON3") arcpy.CalculateField_management(in_table=streets_simple, field="PlaceNameR", expression="placeNameCalc(!POLYGON_NM!)", expression_type="PYTHON3", code_block="""def placeNameCalc(name): if name == 'ST LOUIS': return 'ST LOUIS CITY' else: return name""") arcpy.DeleteField_management(in_table=streets_simple, drop_field=[ "AREA_ID", "POLYGON_NM"]) # calculate L_AREA Cities arcpy.JoinField_management(in_data=streets_simple, in_field="L_AREA_ID", join_table=adminbound4, join_field="AREA_ID", fields=["AREA_ID", "POLYGON_NM"]) arcpy.CalculateField_management( in_table=streets_simple, field="PlaceCodeL", expression_type="PYTHON3", expression="!AREA_ID!") arcpy.CalculateField_management(in_table=streets_simple, field="PlaceNameL", expression_type="PYTHON3", expression="placeNameCalc(!POLYGON_NM!)", code_block="""def placeNameCalc(name): if name == 'ST LOUIS': return 'ST LOUIS CITY' else: return name.upper()""") arcpy.DeleteField_management(in_table=streets_simple, drop_field=[ "AREA_ID", "POLYGON_NM"]) print('Cities Calculated') # Calculate County fields # CountyNameR, CountyNameL, CountyCodeL, CountyCodeR county_streets = arcpy.SpatialJoin_analysis( streets_simple, us_counties, "county_streets")[0] # US_COUNTIES needs to be TIGER or County level shapefile that has GEOID's arcpy.JoinField_management(in_data=streets_simple, in_field="LINK_ID", join_table=county_streets, join_field="LINK_ID", fields=["GEOID", "NAME"]) arcpy.CalculateField_management(in_table=streets_simple, field="CountyNameR", expression="placeNameCalc(!GEOID!, !NAME!)", expression_type="PYTHON3", code_block="""def placeNameCalc(geoid, name): if geoid == '29189': return 'ST LOUIS' elif geoid == '29510': return 'ST LOUIS CITY' elif geoid == '17163': return 'ST CLAIR' else: return name.upper()""") arcpy.CalculateField_management(in_table=streets_simple, field="CountyNameL", expression="placeNameCalc(!GEOID!, !NAME!)", expression_type="PYTHON3", code_block="""def placeNameCalc(geoid, name): if geoid == '29189': return 'ST LOUIS' elif geoid == '29510': return 'ST LOUIS CITY' elif geoid == '17163': return 'ST CLAIR' else: return name.upper()""") arcpy.CalculateField_management( in_table=streets_simple, field="CountyCodeR", expression="!GEOID!", expression_type="PYTHON3") arcpy.CalculateField_management( in_table=streets_simple, field="CountyCodeL", expression="!GEOID!", expression_type="PYTHON3") print("County Calculated") # Calculate State fields # StateAbbrL, StateAbbrR, StateCodeL, StateCodeR arcpy.CalculateField_management( in_table=streets_simple, field="StateCodeL", expression_type="PYTHON3", expression="!GEOID![0:2]") arcpy.CalculateField_management(in_table=streets_simple, field="StateAbbrL", expression_type="PYTHON3", expression="stateAbbr(!StateCodeL!)", code_block="""def stateAbbr(statecode): if statecode == 29: return 'MO' else: return 'IL' """) arcpy.CalculateField_management( in_table=streets_simple, field="StateCodeR", expression_type="PYTHON3", expression="!GEOID![0:2]") arcpy.CalculateField_management(in_table=streets_simple, field="StateAbbrR", expression_type="PYTHON3", expression="stateAbbr(!StateCodeR!)", code_block="""def stateAbbr(statecode): if statecode == 29: return 'MO' else: return 'IL' """) arcpy.DeleteField_management( in_table=streets_simple, drop_field=["GEOID", "NAME"]) # One Way Calculation # T = > # F = < # if blank is not a one way road and returns blank arcpy.CalculateField_management(in_table=streets_simple, field="OneWay", expression="oneWCalc(!DIR_TRAVEL!)", expression_type="PYTHON3", code_block="""def oneWCalc(dir): if(dir == "T"): return ">" elif(dir == "F"): return "<" else: return '' """) # calculated speed with to and from speeds # uses either to or from speed depending on direction for oneway speed calcs arcpy.CalculateField_management(in_table=streets_simple, field="Speed", expression="speedCalc(!DIR_TRAVEL!,!TO_SPD_LIM!,!FR_SPD_LIM!)", expression_type="PYTHON3", code_block="""def speedCalc(dir, toSpeed, fromSpeed): if(dir == 'T'): return toSpeed else: return fromSpeed """) print('OneWay Calculated') # Calculate Speeds based on category # Calculates speed fields that are empty with the speed calc field specs from HERE documentation arcpy.CalculateField_management(in_table=streets_simple, field="Speed", expression="nullSpeedCalc(!Speed!, !SPEED_CAT!)", expression_type="PYTHON3", code_block="""def nullSpeedCalc(speed, cat): if(speed is None): if(cat == '8'): return 15 elif(cat == '7'): return 20 elif(cat == '6'): return 25 elif(cat == '5'): return 35 """) print('Speed Calculated') # Calculate Functional Classes # TODO: REVIEW FUNCTIONAL CLASS CALCULATION # functional classes that adhear to the map maker specification arcpy.CalculateField_management(in_table=streets_simple, field="CFCC", expression="cfccCalc(!FUNC_CLASS!)", expression_type="PYTHON3", code_block="""def cfccCalc(fClass): if(fClass == 1): return 'A10' elif(fClass == 2): return 'A20' elif(fClass == 3): return 'A30' elif(fClass == 4 or fClass == 5): return 'A40' """) print('CFCC Calculated') # TODO: reassess calculation arcpy.CalculateFields_management(in_table=streets_simple, expression_type="PYTHON3", fields=[ ["M_LINK_ID", "!OBJECTID!"], ["OLD_LINK_ID", "!LINK_ID!"]], code_block="")[0] # updated the schema to match mapmaker schema updateSchema(streets_simple) return arcpy.FeatureClassToFeatureClass_conversion(in_features=streets_simple, out_path=Model_Outputs_gdb, out_name="Streets_Final")[0]
# Process: Add Surface Information arcpy.AddSurfaceInformation_3d(Output_Feature_Class__6_, NED10m1, "Z", "BILINEAR", "", "1", "0", "NO_FILTER") # Process: Add XY Coordinates (2) arcpy.AddXY_management(SectionLine_FeatureVerticesT1) # Process: Create Routes arcpy.CreateRoutes_lr(SectionLine, Route_Identifier_Field__2_, SectionRoute, "LENGTH", "", "", "UPPER_LEFT", Measure_Factor, "0", "IGNORE", "INDEX") arcpy.CreateRoutes_lr(SectionLine, Route_Identifier_Field__2_, SectionRoute, "LENGTH", "", "", direction, Measure_Factor, "0", "IGNORE", "INDEX") # Process: Locate Features Along Routes arcpy.LocateFeaturesAlongRoutes_lr(SectionLine_FeatureVerticesT1__3_, SectionRoute, Route_Identifier_Field__2_, "0 Meters", ElevationPoints, Output_Event_Table_Properties, "FIRST", "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON") # Process: Simplify Line arcpy.SimplifyLine_cartography(SectionLine, SectionLine_SimplifyLine, "POINT_REMOVE", "10 Meters", "FLAG_ERRORS", "NO_KEEP", "NO_CHECK") # Process: Feature Vertices To Points arcpy.FeatureVerticesToPoints_management(SectionLine_SimplifyLine, SectionLine_SimplifyLine_Fea, "ALL") # Process: Add Surface Information (2) arcpy.AddSurfaceInformation_3d(SectionLine_SimplifyLine_Fea, NED10m1, "Z", "BILINEAR", "", "1", "0", "NO_FILTER") # Process: Add XY Coordinates arcpy.AddXY_management(SectionLine_SimplifyLine_Fea) # Process: Locate Features Along Routes (2) arcpy.LocateFeaturesAlongRoutes_lr(SectionLine_SimplifyLine_Fea, SectionRoute, Route_Identifier_Field__2_, "0 Meters", SectionBends, Output_Event_Table_Properties__2_, "FIRST", "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON") # Process: Add Field arcpy.AddField_management(ElevationPoints, "SectionY", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
def StreamProfilePoints(output_workspace, flowline, dem, km_to_mouth, station_distance, calibration_points, point_id_field, measure_field, search_radius): # Check out the extension licenses arcpy.CheckOutExtension("3D") # Set environment variables arcpy.env.overwriteOutput = True arcpy.env.workspace = output_workspace # List parameter values arcpy.AddMessage("Workspace: {}".format(arcpy.env.workspace)) arcpy.AddMessage("flowline: {}".format(arcpy.Describe(flowline).baseName)) arcpy.AddMessage("km_to_mouth: {}".format(str(km_to_mouth))) arcpy.AddMessage("DEM: {}".format(arcpy.Describe(dem).baseName)) arcpy.AddMessage("Station distance: {}".format(str(station_distance))) if calibration_points: arcpy.AddMessage("Calibration points: {}".format(str(calibration_points))) arcpy.AddMessage("point_id_field: {}".format(str(point_id_field))) arcpy.AddMessage("measure_field: {}".format(str(measure_field))) arcpy.AddMessage("search_radius: {}".format(str(search_radius))) # Add a field to hold the linear referencing route from measure # Check if the field already exists and if not add it field_names = [f.name for f in arcpy.ListFields(flowline)] if "from_measure" not in field_names: arcpy.AddField_management(in_table = flowline, field_name = "from_measure", field_type = "DOUBLE") # Set the value of the flowline `from_measure` to the input parameter # `km_to_mouth` in units kilometers arcpy.CalculateField_management(in_table = flowline, field = "from_measure", expression = km_to_mouth, expression_type = "PYTHON_9.3") # Add a field to hold the linear referencing route to measure if "to_measure" not in field_names: arcpy.AddField_management(in_table = flowline, field_name = "to_measure", field_type = "DOUBLE") # Set the value of the flowline `to_measure` to the length of the flowline # in units kilometers plus the value of km_to_mouth expression = "!shape.length@kilometers! + {}".format(str(km_to_mouth)) arcpy.CalculateField_management(in_table = flowline, field = "to_measure", expression = expression, expression_type = "PYTHON_9.3") # Simplify the flowline to speed interpolation arcpy.SimplifyLine_cartography(in_features = flowline, out_feature_class = "flowline_simplify", algorithm = "POINT_REMOVE", tolerance = "1 Feet") arcpy.AddMessage("Simplified flowline: flowline_simplify") # Densify vertices of the flowline_simplify feature class using the Densify tool. arcpy.CopyFeatures_management(in_features = "flowline_simplify", out_feature_class = "flowline_densify") arcpy.Densify_edit(in_features = "flowline_densify", densification_method = "DISTANCE", distance = station_distance) arcpy.AddMessage("Densified verticies of flowline: flowline_densify") # Convert the flowline feature class to a route arcpy.CreateRoutes_lr(in_line_features = "flowline_densify", route_id_field = "ReachName", out_feature_class = "flowline_densify_route", measure_source = "TWO_FIELDS", from_measure_field = "from_measure", to_measure_field = "to_measure") arcpy.AddMessage("Converted densfied flowline to a route: " "flowline_densify_route") # Calibrate route if calibration_points: arcpy.CalibrateRoutes_lr(in_route_features = "flowline_densify_route", route_id_field = "ReachName", in_point_features = calibration_points, point_id_field = point_id_field, measure_field = measure_field, out_feature_class = "flowline_route_calibrate", calibrate_method = "DISTANCE", search_radius = search_radius) arcpy.CopyFeatures_management(in_features = "flowline_route_calibrate", out_feature_class = "flowline_densify_route") arcpy.AddMessage("Calibrated route") # Convert flowline feature vertices to points flowline_points = os.path.join(output_workspace, "flowline_points") arcpy.FeatureVerticesToPoints_management( in_features = "flowline_densify_route", out_feature_class = flowline_points) arcpy.AddMessage("Converted densified flowline route to points: " "flowline_points") # Add x, y, z, and m values to the `flowline_points` feature class arcpy.AddGeometryAttributes_management(Input_Features = flowline_points, Geometry_Properties = "POINT_X_Y_Z_M", Length_Unit = "METERS") # Set the first m-value for the flowline_points to zero. The `create # route` tool sets it to NULL. # Create code block that inserts the km_to_mouth value for the NULL record # (the first record) #arcpy.AddMessage("Set Null m-values to zero - start: {}".format(datetime.now().strftime("%H:%M:%S"))) codeBlock = """def setNull2Zero(m): if m is None: return {} else: return m""".format(km_to_mouth) arcpy.CalculateField_management(in_table = flowline_points, field = "POINT_M", expression = "setNull2Zero(!POINT_M!)", code_block = codeBlock, expression_type = "PYTHON_9.3") #arcpy.AddMessage("Set Null m-values to zero - end: {}".format(datetime.now().strftime("%H:%M:%S"))) # Delete un-needed fields arcpy.DeleteField_management(in_table = flowline_points, drop_field = ["ORIG_FID","POINT_Z"]) # Add elevations to the `flowline_points` feature class arcpy.AddSurfaceInformation_3d(in_feature_class = flowline_points, in_surface = dem, out_property = "Z", z_factor = 1.0) arcpy.AddMessage("Added geometry fields to flowline points.") # Return arcpy.SetParameter(9, flowline_points) # Cleanup arcpy.Delete_management(in_data = "flowline_simplify") arcpy.Delete_management(in_data = "flowline_simplify_Pnt") arcpy.Delete_management(in_data = "flowline_densify") arcpy.Delete_management(in_data = "flowline_route_calibrate") arcpy.Delete_management(in_data = "flowline_densify_route") return
def make_workspace_copy(inputfeatures, theworkspace, dotopologycheck, dosimplify, dosimplify_method, dosimplify_tolerance, thefield): """This function tests the input features for the topology error 'Must Be Single Part', and returns the Origin Feature's Object ID of the errant features to the calling module. Beware: the origing feature's object ID is that of the COPY of the input features. The object ID's of the copy may be different from the source "inputfeautures"!!!!. This is why the function passes back the name of the COPY so that the processing can continue on that feature class where the topologically errant features will be correctly identified by the values in the export topology errors geoprocessing tool.""" ## arcpy.AddMessage("funcs.make_workspace_copy") #Process the #roads with the simplify_line tool with the point_remove option at a tolerance of 0.001 meters so that redundant vertices on staight lines are removed. #If the user specifies their own parameters for simplify_line, THAT ARE NOT POINT_REMOVE AND THE TOLERANCE IS > 0.001 METERS, that is done additionally, #afterwards: #this section makes the feature class datasets, feature class names, and topology name: badfids = set() fdname = "KDOT_Topology_Check" #the feature dataset name for the topology check fdnamepath = theworkspace + "\\" + fdname #the complete pathname of the feature dataset tpname = "CheckTopology" #the name of the topology topology_name = fdnamepath + "\\" + tpname #the complete pathname of the topology ## arcpy.AddMessage("make_workspace_copy, fdnamepath: "+fdnamepath) ## arcpy.AddMessage("make_workspace_copy, topology_name: "+topology_name) fcname = arcpy.ParseTableName( inputfeatures, theworkspace) #Split the inputfeatures to find the name from the path. namelist = fcname.split( ", " ) #the feature class name without the path. Used in creating a copy in the feature dataset. ## arcpy.AddMessage('fcname = '+ namelist[2]) topology_featureclass = fdnamepath + '\\' + namelist[ 2] + '_check' #the copy of inputfeatures used for the topology check topology_featureclass_errors = namelist[ 2] + '_errors' # the basename used for the export topology errors tool ## arcpy.AddMessage(topology_featureclass) topology_featureclass_errors_line = fdnamepath + '\\' + namelist[ 2] + '_errors_line' #the output name of LINE errors from the export topology errors tool #Delete if the feature dataset currently exists: doesexistfd = arcpy.Exists(fdnamepath) try: if doesexistfd: arcpy.AddMessage( 'Previous topology check feature dataset exists. Now deleteing ' ) arcpy.Delete_management(fdnamepath) except arcpy.ExecuteError: print arcpy.GetMessages(2) except Exception as e: print e.args[0] #Re-create the topology feature dataset: arcpy.AddMessage('Generating the topology check scratch feature dataset') arcpy.CreateFeatureDataset_management(theworkspace, fdname, inputfeatures) #Make a copy of the input roads in the feature dataset that contains the topology: try: arcpy.AddMessage( 'Generating a copy of the input feature class in the scratch feature dataset' ) #This replaces the function "arcpy.CopyFeatures_management" so that we can retain the original FID: ## make_copies_of_features(inputfeatures, topology_featureclass, "Original_OID") make_copies_of_features(inputfeatures, topology_featureclass, thefield) ## arcpy.CopyFeatures_management(inputfeatures, topology_featureclass) except arcpy.ExecuteError: print arcpy.GetMessages(2) except Exception as e: print e.args[0] #Perform the topology check, if checked ON in input parameters: ## arcpy.AddMessage('make_workspace_copy, dotopology = ' + str(dotopologycheck)) ## if(dotopologycheck == True): if (str(dotopologycheck) == 'true'): arcpy.AddMessage('Creating the topology') arcpy.CreateTopology_management(fdnamepath, tpname) #Add the input roads to the topology arcpy.AddMessage( 'Adding the copy of the input features to the topology') arcpy.AddFeatureClassToTopology_management(topology_name, topology_featureclass, 1, 1) #Add a rule: arcpy.AddMessage('Adding rule "Must Be Single Part" to the topology') arcpy.AddRuleToTopology_management(topology_name, "Must Be Single Part (Line)", topology_featureclass) #Validate the topology: arcpy.AddMessage('Validating the topology') arcpy.ValidateTopology_management(topology_name) #Export the errant features to a feature class arcpy.AddMessage( 'Exporting the topologically-errant feature to feature class ' + topology_featureclass_errors) arcpy.ExportTopologyErrors_management(topology_name, fdnamepath, topology_featureclass_errors) arcpy.AddMessage("Completed exporting topology errors") #Extract the values from field "OriginObjectID". This is a field generated to identify the OID's of errant features: ## arcpy.AddMessage('Retrieving the object ID''s of the errant features') with arcpy.da.SearchCursor(topology_featureclass_errors_line, ["OriginObjectID"]) as cursor: for row in cursor: ## arcpy.AddMessage(str(row[0])) badfids.add(row[0]) #Perform at the least, the default line simplification of 0.001 meters or 0.00328084 feet #SimplifyLine(mergedFeatures, simplifiedFeatures, dosimplify_method, dosimplify_tolerance, "RESOLVE_ERRORS", "KEEP_COLLAPSED_POINTS", "CHECK") simplified_featureclass = fdnamepath + '\\_simplified_roads' arcpy.SimplifyLine_cartography(topology_featureclass, simplified_featureclass, dosimplify_method, dosimplify_tolerance, False, False, False) arcpy.AddMessage('completed creating a workspace copy....') ## arcpy.AddMessage('completed funcs.make_workspace_copy') return badfids, simplified_featureclass
# Lines with orientations Line_features = arcpy.GetParameterAsText(1) if Line_features == '#' or not Line_features: Line_features = "C:\\PROJECTS\\Pdwr_Mtn\\Pdwr_Mtn.gdb\\Stream_Line.shp" # provide a default value if unspecified # Location and name of Feature Class Where resulting segments will be stored Output_Line_features = arcpy.GetParameterAsText(2) # Place to put rose diagram pdf = PdfPages(arcpy.GetParameterAsText(8) + ".pdf") ############################################################################### # Straighten Lines arcpy.SimplifyLine_cartography(Line_features, Output_Line_features, "POINT_REMOVE", float(arcpy.GetParameterAsText(3)), "FLAG_ERRORS", "NO_KEEP", "NO_CHECK") arcpy.AddMessage("Simplifying Lines complete") # Create temporary input file for Split Line tool tempFC = arcpy.env.scratchGDB + os.path.sep + "tempFC" arcpy.CopyFeatures_management(Output_Line_features, tempFC) # Overwrite Output lines with line segments arcpy.SplitLine_management(tempFC, Output_Line_features) arcpy.Delete_management(tempFC) arcpy.AddMessage("Splitting Lines Complete") # Make a temporary feature class to hold line segment vertices tempVert = arcpy.env.scratchGDB + os.path.sep + "tempVert"