def xytoline(make): root.filename = tkFileDialog.asksaveasfilename(initialdir="/", title="Ouput Feature Class:", filetypes=(("Shapefile", "*.shp*"), ("all", "*.*"))) arcpy.XYToLine_management(make, root.filename, "Easting1", "Northing1", "Easting2", "Northing2", "0", "", "PROJCS['NAD_1983_2011_StatePlane_Illinois_West_FIPS_1202_Ft_US',GEOGCS['GCS_NAD_1983_2011',DATUM['D_NAD_1983_2011',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',2296583.333333333],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-90.16666666666667],PARAMETER['Scale_Factor',0.9999411764705882],PARAMETER['Latitude_Of_Origin',36.66666666666666],UNIT['Foot_US',0.3048006096012192]];-16150900 -46131100 3048.00609601219;-100000 10000;-100000 10000;3.28083333333333E-03;0.001;0.001;IsHighPrecision") #Add fields (elev 1 and 2) arcpy.AddField_management(root.filename + '.shp', "Elevation1", "FLOAT", "", "2", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(root.filename + '.shp', "Elevation2", "FLOAT", "", "2", "", "", "NULLABLE", "NON_REQUIRED", "")
def DrawRadialFlows(): # Try to assign the spatial reference by using the Well-Known ID for the input try: spRef = arcpy.SpatialReference(int(wkid)) except: # If the SR cannot be created, assume 4326 arcpy.AddWarning( "Problem creating the spatial reference!! Assumed to be WGS84 (wkid: 4326)" ) spRef = arcpy.SpatialReference(4326) # Local variable: out_flows_fc = r"in_memory\FlowsLineXY" out_tbl = r"in_memory\tmpTable" out_Name = "Telecoupling Flows" if inTable and inTable != "#": try: # XY To Line arcpy.AddMessage('Creating Radial Flow Lines from Input File ...') arcpy.SetProgressorLabel( 'Creating Radial Flow Lines from Input File ...') if id_field and flow_amnt: arcpy.XYToLine_management(in_table=inTable, out_featureclass=out_flows_fc, startx_field=startX_field, starty_field=startY_field, endx_field=endX_field, endy_field=endY_field, line_type=lineType_str, id_field=id_field, spatial_reference=spRef) arcpy.CopyRows_management(inTable, out_tbl) ## JOIN output flows with attribute from input table arcpy.JoinField_management(out_flows_fc, id_field, out_tbl, id_field, flow_amnt) # Process: Create a feature layer from the joined feature class to send back as output to GP tools out_fl = arcpy.MakeFeatureLayer_management(out_flows_fc, out_Name) # Send string of (derived) output parameters back to the tool arcpy.SetParameter(9, out_fl) # Execute FeatureClassToGeodatabase arcpy.AddMessage("Converting Feature Class to Shapefile...") arcpy.FeatureClassToShapefile_conversion(out_flows_fc, arcpy.env.scratchFolder) except Exception: e = sys.exc_info()[1] arcpy.AddError('An error occurred: {}'.format(e.args[0]))
def LoadSect(lookuppath,storepath,arcpath,demname,NumSect): #Iterate through number of segments generated startx_field = "RBX" starty_field = "RBY" endx_field = "LBX" endy_field = "LBY" prj = arcpath + demname + "_fdemasc.prj" for j in np.arange(2,NumSect): #Create key to find text file lookup = "CS_{}".format(j) + ".txt" print("Creating Line from "+ lookup) #Assign parameters for ArcTool in_table = lookuppath + "CS_{}".format(j)+".txt" out_featureclass = storepath+"CS_{}".format(j)+".shp" arcpy.XYToLine_management(in_table, out_featureclass, startx_field, starty_field, endx_field, endy_field, "GEODESIC", "", prj)
def CreateLinesBuildings2Levee(accum_csv, Levee_failures_shp, temp_table, temp_point, Line_Features, Line_Features_WGS84, ConnectionLines): df_csv = pd.read_csv(accum_csv) field_names=['X_center', 'Y_center', 'LF'] arr = arcpy.da.TableToNumPyArray(Levee_failures_shp, (field_names)) df = pd.DataFrame(arr) merged_df = df_csv.merge(df, left_on='Levee_Failure', right_on="LF") columns_to_drop = ['Wert', 'MaxDepth', 'deg_of_loss', 'Damage', 'LF'] for columns in columns_to_drop: merged_df = merged_df.drop(columns, axis=1) merged_df = merged_df.rename(columns={'X_centroid': 'X_Build', 'Y_centroid': 'Y_Build', 'X_center': 'X_LF', 'Y_center': 'Y_LF'}) x = np.array(np.rec.fromrecords(merged_df.values)) names = merged_df.dtypes.index.tolist() x.dtype.names = tuple(names) if not arcpy.Exists(temp_table): arcpy.da.NumPyArrayToTable(x, temp_table) else: print ("Temporal table already exists. Cannot move on. Script aborted.") print ("ArcGIS Event layer will now be created.") EventLayer='Event_Layer_Lines' arcpy.MakeXYEventLayer_management(temp_table, "X_Build", "Y_Build", EventLayer, spatial_reference=arcpy.SpatialReference(21781)) arcpy.CopyFeatures_management(EventLayer, temp_point) print ("Start to create ArcGIS line feature class...") arcpy.XYToLine_management(temp_point, Line_Features, "X_Build", "Y_Build", "X_LF", "Y_LF", id_field="V25OBJECTI") arcpy.JoinField_management(Line_Features, "X_LF", temp_table ,"X_LF", fields = "Levee_Failure") out_coor_system = arcpy.SpatialReference(4326) arcpy.Project_management(Line_Features, Line_Features_WGS84, out_coor_system=out_coor_system) arcpy.FeaturesToJSON_conversion(Line_Features_WGS84, ConnectionLines, format_json="FORMATTED", geoJSON="GEOJSON") print ('Finished converting and exporting to geojson') print ('begin to delete temporary files') files_to_delete=[temp_table, temp_point, Line_Features, Line_Features_WGS84] for files in files_to_delete: if arcpy.Exists(files): arcpy.Delete_management(files) print (str(files) + (" successfully deleted.")) else: print ("File " + str(files) + " does not exist and cannot be deleted") print ("Successfully finished function")
def ejecutar(path_1): path = path_1 + "/temp" path_file = path_1 + "\\data" arcpy.env.workspace = path arcpy.env.overwriteOutput = True arcpy.MakeXYEventLayer_management(conversion_dbf, "LONG_N", "LAT_N", "NODOS", sr) arcpy.SaveToLayerFile_management("NODOS", path_file + "\\NODOS") arcpy.PackageLayer_management(path_file + "\\NODOS.lyr", path_file + "\\NODOS") #arcpy.ErasePoint_edit(path_file+"\\NODOS.lyr",path_1+"\\data\\mapa\\EliminarNaN.lyr",'INSIDE') #arcpy.PackageLayer_management(path_file + "\\"+V_ATRIBUTO_NAME+"EB.lyr", path_file + "\\"+V_ATRIBUTO_NAME +"EB") arcpy.XYToLine_management(path + "\\conversion.dbf", path_file + "\\Enlaces", "LONG_A", "LAT_A", "LONG_B", "LAT_B", "GEODESIC", "PERMISIONA")
def DrawRadialFlows(): # Try to assign the spatial reference by using the Well-Known ID for the input try: spRef = arcpy.SpatialReference(int(wkid)) except: # If the SR cannot be created, assume 4326 arcpy.AddWarning( "Problem creating the spatial reference!! Assumed to be WGS84 (wkid: 4326)" ) spRef = arcpy.SpatialReference(4326) # Local variable: out_flows_fc = r"in_memory\FlowsLineXY" out_tbl = r"in_memory\tmpTable" out_Name = "Telecoupling Flows" if inTable and inTable != "#": try: # XY To Line arcpy.AddMessage('Creating Radial Flow Lines from Input File ...') arcpy.SetProgressorLabel( 'Creating Radial Flow Lines from Input File ...') if id_field and flow_units: arcpy.XYToLine_management(in_table=inTable, out_featureclass=out_flows_fc, startx_field=startX_field, starty_field=startY_field, endx_field=endX_field, endy_field=endY_field, line_type=lineType_str, id_field=id_field, spatial_reference=spRef) arcpy.CopyRows_management(inTable, out_tbl) ## JOIN output flows with attribute from input table arcpy.JoinField_management(out_flows_fc, id_field, out_tbl, id_field, flow_units) except Exception: e = sys.exc_info()[1] arcpy.AddError('An error occurred: {}'.format(e.args[0])) return out_flows_fc
def LoadLines(lookuppath,storepath,arcpath,demname,NumSect,perform): if perform = True: #Iterate through number of segments generated startx_field = "Topx" starty_field = "Topy" endx_field = "Botx" endy_field = "Boty" prj = arcpath + filein + "_fdemasc.prj" for j in np.arange(1,NumSect+1): #Create key to find text file lookup = "CS_{}".format(j) + ".txt" print("Creating Line from "+ lookup) #Assign parameters for ArcTool in_table = lookuppath + "CS_{}".format(j)+".txt" out_featureclass = storepath+"CS_{}".format(j)+".shp" startx_field = "Topx" starty_field = "Topy" endx_field = "Botx" endy_field = "Boty" prj = arcpath + demname + "_fdemasc.prj" #Run ArcTool arcpy.XYToLine_management(in_table, out_featureclass, startx_field, starty_field, endx_field, endy_field, "GEODESIC", "", prj)
def split_at_vertices(in_fc, out_fc): """Unique segments retained when poly geometry is split at vertices. """ result = check_path(out_fc) if result[0] is None: print(result[1]) return result[1] gdb, name = result SR = getSR(in_fc) a, IFT, IFT_2 = fc_geometry(in_fc, SR) ag = Geo(a, IFT) # fr_to = ag.unique_segments() # geo method fr_to = ag.polys_to_segments() dt = np.dtype([('X_orig', 'f8'), ('Y_orig', 'f8'), ('X_dest', 'f8'), ('Y_dest', 'f8')]) od = uts(fr_to, dtype=dt) # ---- unstructured to structured tmp = "memory/tmp" if arcpy.Exists(tmp): arcpy.Delete_management(tmp) arcpy.da.NumPyArrayToTable(od, tmp) args = [tmp, out_fc] + list(od.dtype.names) + ["GEODESIC", "", SR] arcpy.XYToLine_management(*args) return
def ArcGISPoint2Line(Current_lv, DF_LeveeFailures, CompleteDataframe, ArcGISTablepath, ArcGISPointFC, ArcGISLineFCPath, ArcGISLineFC_joinPath, ArcGISLineFC_joinPath_WGS84, LeveeFailureLinesGeojson): DF_Completed = None Point = None start_time = time.time() #first of all, let's create an ArcGIS table from the panda dataframe FilesList = [ ArcGISTablepath, ArcGISPointFC, ArcGISLineFCPath, ArcGISLineFC_joinPath, ArcGISLineFC_joinPath_WGS84, LeveeFailureLinesGeojson ] print(psutil.virtual_memory()) for objects in FilesList: if arcpy.Exists(objects): arcpy.Delete_management(objects) print("The file " + objects + " was deleted") DF_Completed = CompleteDataframe x = np.array(np.rec.fromrecords(DF_Completed.values)) names = DF_Completed.dtypes.index.tolist() x.dtype.names = tuple(names) arcpy.da.NumPyArrayToTable(x, ArcGISTablepath) print("ArcGIS table was successfully written") print( "--- %s seconds have elapsed within the ArcGISPoint2Line method ---" % (time.time() - start_time)) X_CentroidOfLeveeFailure = DF_LeveeFailures.at[Current_lv, 'X_center'] Y_CentroidOfLeveeFailure = DF_LeveeFailures.at[Current_lv, 'Y_center'] arcpy.AddField_management(ArcGISTablepath, "X_levee", "long") arcpy.AddField_management(ArcGISTablepath, "Y_levee", "long") arcpy.CalculateField_management(ArcGISTablepath, "X_levee", X_CentroidOfLeveeFailure) arcpy.CalculateField_management(ArcGISTablepath, "Y_levee", Y_CentroidOfLeveeFailure) EventLayer = "Event_Layer" arcpy.MakeXYEventLayer_management( ArcGISTablepath, "X_centroid", "Y_centroid", EventLayer, spatial_reference=arcpy.SpatialReference(21781)) arcpy.CopyFeatures_management(EventLayer, ArcGISPointFC) print( "--- %s seconds have elapsed within the ArcGISPoint2Line method ---" % (time.time() - start_time)) print(psutil.virtual_memory()) #arcpy.env.overwriteOutput = True #arcpy.env.qualifiedFieldNames = False arcpy.XYToLine_management(ArcGISPointFC, ArcGISLineFCPath, "X_levee", "Y_levee", "X_centroid", "Y_centroid", id_field="V25OBJECTI") JoinedLineFC = arcpy.AddJoin_management(ArcGISLineFCPath, "V25OBJECTI", ArcGISTablepath, "V25OBJECTI") print("Successful 1") arcpy.CopyFeatures_management(JoinedLineFC, ArcGISLineFC_joinPath) print("Successful 2") arcpy.Project_management(ArcGISLineFC_joinPath, ArcGISLineFC_joinPath_WGS84, 4326) print("Successful 3") arcpy.FeaturesToJSON_conversion(ArcGISLineFC_joinPath_WGS84, LeveeFailureLinesGeojson, format_json="FORMATTED", geoJSON="GEOJSON") print("Successful 4") print( "Line Feature Class was successfully created, projected to WGS84 and exported as GEOJSON" ) print( "--- %s seconds have elapsed within the ArcGISPoint2Line method ---" % (time.time() - start_time))
# Import system modules import arcpy arcpy.env.workspace = '.' # Set local variables input_table = 'f_sh_0601_s0.csv' out_lines = 'f_sh_0601_s0' spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" #XY To Line arcpy.XYToLine_management(input_table, out_lines, 'ox', 'oy', 'dx', 'dy', spatial_reference=spRef)
Azline2="Azline2" arcpy.BearingDistanceToLine_management (LineSplit, Azline1, "X_mid", "Y_mid", "Distance", TransecLength_Unit, "AziLine_1", "DEGREES", "GEODESIC", "LineID", spatial_reference) arcpy.BearingDistanceToLine_management (LineSplit, Azline2, "X_mid", "Y_mid", "Distance", TransecLength_Unit, "AziLine_2", "DEGREES", "GEODESIC", "LineID", spatial_reference) #Create Azline and append Azline1 and Azline2 Azline="Azline" arcpy.CreateFeatureclass_management(env.workspace, "Azline", "POLYLINE", "", "", "", spatial_reference) arcpy.AddField_management (Azline, "LineID", "DOUBLE") arcpy.Append_management ([Azline1, Azline2], Azline, "NO_TEST") #Dissolve Azline Azline_Dissolve="Azline_Dissolve" arcpy.Dissolve_management (Azline, Azline_Dissolve,"LineID", "", "SINGLE_PART") #Add Fields to Azline_Dissolve FieldsNames2=["x_start", "y_start", "x_end", "y_end"] for fn2 in FieldsNames2: arcpy.AddField_management (Azline_Dissolve, fn2, "DOUBLE") #Calculate Azline_Dissolve fields arcpy.CalculateField_management (Azline_Dissolve, "x_start", "!Shape!.positionAlongLine(0,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management (Azline_Dissolve, "y_start", "!Shape!.positionAlongLine(0,True).firstPoint.Y", "PYTHON_9.3") arcpy.CalculateField_management (Azline_Dissolve, "x_end", "!Shape!.positionAlongLine(1,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management (Azline_Dissolve, "y_end", "!Shape!.positionAlongLine(1,True).firstPoint.Y", "PYTHON_9.3") #Generate output file arcpy.XYToLine_management (Azline_Dissolve, OutputTransect,"x_start", "y_start", "x_end","y_end", "", "", spatial_reference) #Delete General.gdb arcpy.Delete_management(General_GDB)
counter += 1 #rename table arcpy.CopyFeatures_management("lowOutFC", "joinedOutput") #move table to output location arcpy.TableToTable_conversion("joinedOutput.dbf", outLocation, "joinedTable") print("Joined the high table to the low table and saved result") #run xy to line env.workspace = outLocation env.qualifiedFieldNames = False env.overwriteOutput = True #spatial reference change see webpage for WKID: #https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/pdf/projected_coordinate_systems.pdf env.outputCoordinateSystem = arcpy.SpatialReference(2965) outFC = str(outLocation) + "/xyLineOutput" arcpy.XYToLine_management("joinedTable.dbf", outFC, "X", "Y", "X1", "Y1", "GEODESIC", msagID) print("XY to line is complete") print("File is located at " + str(outLocation)) #write irregularities to a file and delete them indata = "xyLineOutput.shp" xyRows1 = arcpy.da.UpdateCursor(indata, ["X", msagID]) xyRows2 = arcpy.da.UpdateCursor(indata, ["X1", msagID]) f = open(str(outLocation) + "/irregularities.csv", "a") for row in xyRows1: if int(row[0]) < 1: f.write(str(row[1])+",\n") xyRows1.deleteRow() for row in xyRows2: if int(row[0]) < 1:
##################################################################################################### ##################################################################################################### centroids=directory+"centroids_p.shp" out_centroids=workspace2+"near_centroids" centr_to_centr=workspace2+"centr_to_centr" # defining WGS84 CRS wgs84 = arcpy.SpatialReference(4326) # Create near table with location for radius of 300 km arcpy.GenerateNearTable_analysis(centroids, centroids, out_centroids, "300 Kilometers", "LOCATION", "NO_ANGLE", "ALL", "0", "GEODESIC") print "near table centroids done" arcpy.XYToLine_management(out_centroids, centr_to_centr, "FROM_X", "FROM_Y", "NEAR_X", "NEAR_Y", "GEODESIC", "", wgs84) print "near table to line centroids done" arcpy.AddField_management(centr_to_centr, "Parameter", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") print "generate Parameter field centroids done" arcpy.CalculateField_management(centr_to_centr, "Parameter", walk, "VB", "") print "calculate Parameter field centroids done" arcpy.DeleteField_management(centr_to_centr, "FROM_X;FROM_Y;NEAR_X;NEAR_Y") print "delete fields centroids done" arcpy.AddField_management(centr_to_centr, "Length", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") print "generate Length field centroids done" # to calculate c to c length, we dont need to project but can directly
def CommodityTrade(): countrySelection = arcpy.GetParameterAsText( 0) #the user's country selection commodityData = arcpy.GetParameterAsText( 1) #the OEC data set cleaned for the purposes of this tool direction = arcpy.GetParameterAsText( 2) #select whether interested in commodity export or import commodityItem = arcpy.GetParameterAsText( 3) #select commodity of interest from list startYear = arcpy.GetParameterAsText(4) #start year of analysis endYear = arcpy.GetParameterAsText(5) #end year of analysis outputName = arcpy.GetParameterAsText(6) #name of output feature class limit = arcpy.GetParameter( 7) #gives users the ability to limit trade partners numLimit = arcpy.GetParameterAsText( 8) #the number of trade partners to limit output to #remove commodity trade shapefile if if already exists from a previous run if os.path.exists( os.path.join(arcpy.env.scratchFolder, outputName + ".shp")): for filename in glob.glob( os.path.join(arcpy.env.scratchFolder, outputName + "*")): os.remove(filename) try: #make sure end year follows the start year startYear = int(startYear) endYear = int(endYear) if endYear < startYear: arcpy.AddMessage("End year must follow the start year.") sys.exit() #read CSV as a dataframe df = pd.read_csv(commodityData) #subset by year yearList = range(startYear, endYear + 1) df_year = df[df["year"].isin(yearList)] #subset by commodity df_comm_yr = df_year[df_year["comm_name"] == commodityItem] #subset by exporting or importing country if direction == "Export": df_trade = df_comm_yr[ df_comm_yr["cntry_orig"] == countrySelection] #select the exporting country if df_trade["export_val"].isnull().all(): arcpy.AddMessage("Nothing to map! " + countrySelection + " did not export " + commodityItem + " from " + str(startYear) + " to " + str(endYear) + ".") sys.exit() df_trade = df_trade[np.isfinite( df_trade["export_val"])] #remove NaN values from export_val df_trade["import_val"].fillna( 0, inplace=True) #import values not important. replace NaN with 0 else: df_trade = df_comm_yr[ df_comm_yr["cntry_dest"] == countrySelection] #select the importing country if df_trade["import_val"].isnull().all(): arcpy.AddMessage("Nothing to map! " + countrySelection + " did not import " + commodityItem + " from " + str(startYear) + " to " + str(endYear) + ".") sys.exit() df_trade = df_trade[np.isfinite( df_trade["import_val"])] #remove NaN values from import_val df_trade["export_val"].fillna( 0, inplace=True) #export values not important. replace NaN with 0 #limit the number of output linkages if limit is TRUE if limit: numLimit = int(numLimit) if direction == "Export": df_trade = df_trade.groupby("year", group_keys=False).apply( lambda g: g.nlargest(numLimit, "export_val") ) #group data by year and return the top trading partners as specified in numLimit else: df_trade = df_trade.groupby("year", group_keys=False).apply( lambda g: g.nlargest(numLimit, "import_val") ) #group data by year and return the top trading partners as specified in numLimit #count number of records in each year. if record count is below numLimit for any year, then below message is printed to inform user that there are fewer returned records than the number requested count_occur = df_trade.groupby(["year"]).size() count_occur = count_occur.to_frame(name="size").reset_index() if (count_occur["size"] == numLimit).all() == False: arcpy.AddMessage( "FYI, the number of returned trade partners is less than the specified limit for some or all analysis years." ) #create dbase table. this will be used to put Pandas DataFrame content within fc arr = np.array(np.rec.fromrecords(df_trade.values)) names = df_trade.dtypes.index.tolist() arr.dtype.names = tuple(names) arcpy.da.NumPyArrayToTable( arr, os.path.join(arcpy.env.scratchFolder, "df_table.dbf")) table = os.path.join(arcpy.env.scratchFolder, "df_table.dbf") #draw radial flows using dbase table wgs = arcpy.SpatialReference(4326) flowsOutputFC = os.path.join(arcpy.env.scratchFolder, outputName + ".shp") if direction == "Export": arcpy.XYToLine_management(in_table=table, out_featureclass=flowsOutputFC, startx_field="lon_origin", starty_field="lat_origin", endx_field="lon_dest", endy_field="lat_dest", line_type="GEODESIC", id_field="id", spatial_reference=wgs) else: arcpy.XYToLine_management(in_table=table, out_featureclass=flowsOutputFC, startx_field="lon_dest", starty_field="lat_dest", endx_field="lon_origin", endy_field="lat_origin", line_type="GEODESIC", id_field="id", spatial_reference=wgs) #join commodity trade data from dbase table to radial flows feature class arcpy.JoinField_management(in_data=flowsOutputFC, in_field="id", join_table=table, join_field="id") #drop unnecesary fields if direction == "Export": dropFields = [ "Unnamed__0", "id", "orgid", "origin", "dest", "comm_code", "import_val", "lat_origin", "lon_origin", "lat_dest", "lon_dest", "id_1", "lat_orig_1", "lon_orig_1", "lat_dest_1", "lon_dest_1" ] else: dropFields = [ "Unnamed__0", "id", "orgid", "origin", "dest", "comm_code", "export_val", "lat_origin", "lon_origin", "lat_dest", "lon_dest", "id_1", "lat_orig_1", "lon_orig_1", "lat_dest_1", "lon_dest_1" ] arcpy.DeleteField_management(flowsOutputFC, dropFields) #remove the dbase table from the scratch folder os.chdir(arcpy.env.scratchFolder) os.remove(os.path.join(arcpy.env.scratchFolder, 'df_table.dbf')) #add output to map arcpy.SetParameter(9, flowsOutputFC) except Exception: e = sys.exc_info()[1] arcpy.AddError('An error occurred: {}'.format(e.args[0]))
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
arcpy.CalculateField_management(gisMissingLinesView, 'temp_missing_lines.to_utm_x', '!UUSD.DBO.Fiber_Enclosures.POINT_X!', 'PYTHON_9.3') arcpy.CalculateField_management(gisMissingLinesView, 'temp_missing_lines.to_utm_y', '!UUSD.DBO.Fiber_Enclosures.POINT_Y!', 'PYTHON_9.3') arcpy.RemoveJoin_management(gisMissingLinesView) #Find all records that have all the from/to coordinates and map them arcpy.SelectLayerByAttribute_management( gisMissingLinesView, "NEW_SELECTION", "from_utm_x IS NOT NULL AND to_utm_x IS NOT NULL") arcpy.XYToLine_management(gisMissingLinesView, newGISLines, "from_utm_x", "from_utm_y", "to_utm_x", "to_utm_y", "GEODESIC", "cb_id", gisEnclosures) arcpy.DeleteRows_management(gisMissingLinesView) #New lines should NOT be appended to the existing dataset. #New Lines need to be manually reviewd and edited to provide spatial accuracy where possible. #Update email report newLinesCount = int( arcpy.GetCount_management(newGISLines).getOutput(0)) gisMissingCount = int( arcpy.GetCount_management(gisMissingLinesView).getOutput(0)) infile.write( "There are " + str(newLinesCount) + " to be added to the Fiber Lines feature class.\nThe temporary new lines feature class is located at \n\\ad.utah.edu\sys\FM\gis\uit_scheduled_tasks\Fiber_DataCompare.gdb\new_Lines.\n\n" )
break lineE = next(rowsE) if lineS.Rank_UGO == lineE.Rank_UGO : lineS.END_X = lineE.NEAR_X lineS.END_Y = lineE.NEAR_Y else : rowsS.deleteRow(lineS) rowsS.updateRow(lineS) n += 1 #/creation of the inflection line ncurrentstep+=1 arcpy.AddMessage("Creating the final Inflection line - Step " + str(ncurrentstep) + "/" + str(nstep)) arcpy.AddMessage(" Connecting inflection point by straight lines - 1/9") InflToLine = arcpy.XYToLine_management(PtsForInflLine, "%ScratchWorkspace%\\InflToLine", "NEAR_X", "NEAR_Y", "END_X", "END_Y", "GEODESIC", "Rank_UGO", SpatialRef) arcpy.AddMessage(" Dissolving all lines with the same \"Rank_UGO\" field - 2/9") Dissolve = arcpy.Dissolve_management(InflToLine, "%ScratchWorkspace%\\Dissolve", "Rank_UGO", "", "MULTI_PART", "DISSOLVE_LINES") arcpy.AddMessage(" Smoothing by Bezier interpolation - 3/9") Bezier = arcpy.SmoothLine_cartography(Dissolve, "%ScratchWorkspace%\\Bezier", "BEZIER_INTERPOLATION") arcpy.AddField_management(Bezier, "Start", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(Bezier, "End", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(Bezier, "Start", "0", "PYTHON_9.3", "") arcpy.CalculateField_management(Bezier, "End", "!Shape_Length!", "PYTHON_9.3", "") arcpy.AddMessage(" Converting in routes - 4/9") BezierRoutes = arcpy.CreateRoutes_lr(Bezier, "Rank_UGO", "%ScratchWorkspace%\\BezierRoutes", "TWO_FIELDS", "Start", "End") arcpy.AddMessage(" Up to date the \"Shape_Length\" field - 5/9")
"COUNT_pipe_material_") # Process: Table Select (2) arcpy.TableSelect_analysis(pipeconn__2_, pipeconn_could_not_draw, "latitude_1 IS NULL OR latitude_12 IS NULL") # Process: Table Select (3) arcpy.TableSelect_analysis( pipeconn__2_, pipeconn_to_draw_out, "(direction_ = 'Out ' AND latitude_1 IS NOT NULL AND latitude_12 IS NOT NULL)" ) # Process: XY To Line (2) arcpy.XYToLine_management( pipeconn_to_draw_out, pipeconn_lines_out, "longitude_1", "latitude_1", "longitude_12", "latitude_12", "0", "fulcrum_id", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision" ) # Process: Table Select arcpy.TableSelect_analysis( pipeconn__2_, pipeconn_to_draw_in, "(direction_ = 'In ' AND COUNT_TREKK_ID = 1 AND latitude_1 IS NOT NULL AND latitude_12 IS NOT NULL)" ) # Process: XY To Line arcpy.XYToLine_management( pipeconn_to_draw_in, pipeconn_lines_in, "longitude_12", "latitude_12", "longitude_1", "latitude_1", "0", "fulcrum_id", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision" )
out_fc = sys.argv[9] SR = sys.argv[10] # if origin == "Top left": y_offset = -y_offset orig = [origin_x, origin_y] arr = transect_lines(N, orig, dist, x_offset, y_offset, bearing, True) # ---- Create the table and the lines if create_output: tbl = out_fc arcpy.da.NumPyArrayToTable(arr, tbl) out_fc = tbl + "_t" arcpy.XYToLine_management(tbl, out_fc, 'X_from', 'Y_from', 'X_to', 'Y_to', spatial_reference=SR) ... # startx_field, starty_field, endx_field, endy_field, ... # {line_type}, {id_field}, {spatial_reference} # ==== Processing finished ==== # =========================================================================== # if __name__ == "__main__": """optional location for parameters"""
def AddMediaFlows(): # Local variable: out_layer_src = r"in_memory\source_lyr" out_layer_pnt = r"in_memory\country_lyr" out_media_flows = r"in_memory\media_lyr" # Get the value of the input parameter input_fc = arcpy.GetParameterAsText(0) input_fc_field = arcpy.GetParameterAsText(1) input_html = arcpy.GetParameterAsText(2) source_FeatureSet = arcpy.GetParameterAsText(3) is_checked_table = arcpy.GetParameter(4) input_flow_lyr = arcpy.GetParameterAsText(5) lineType_str = arcpy.GetParameterAsText(6) try: ### 1. Create temp feature layer from source point and add XY coordinates ### # Process: Make Feature Layer (temporary) arcpy.MakeFeatureLayer_management(in_features=source_FeatureSet, out_layer=out_layer_src) #### Add XY Coordinates To Point Layer #### arcpy.AddXY_management(out_layer_src) ### 2. The user should only add a single source location. If so, then store the XY coordinate values into a list object ### countRows = int(arcpy.GetCount_management(out_layer_src).getOutput(0)) if countRows > 1: arcpy.AddError("You need to specify ONLY ONE source location on the map!!") raise arcpy.ExecuteError else: with arcpy.da.SearchCursor(out_layer_src, ['POINT_X', 'POINT_Y']) as cursor: for row in cursor: srcPnt_XY = [row[0], row[1]] del cursor ### 3. Create Dictionary of Values Based on Selected Input Feature Field ### fNames_lst = [] # Search Cursor: the following only works with ArcGIS 10.1+ ### arcpy.SetProgressorLabel('Creating Dictionary From Input Feature ...') arcpy.AddMessage('Creating Dictionary From Input Feature ...') with arcpy.da.SearchCursor(input_fc, input_fc_field) as cursor: for row in cursor: fNames_lst.append(row[0]) del cursor ### 4. Read HTML input report file and parse its content grabbing desired tags ### arcpy.SetProgressorLabel('Reading and Parsing HTML File ...') arcpy.AddMessage('Reading and Parsing HTML File ...') soup = BeautifulSoup(open(input_html),"lxml") links_geo = soup.find_all(lambda tag: tag.name == 'p' and tag.get('class') == ['c1']) #links_p = soup.find_all('p') text_elements_geo = [links_geo[i].find_all(text=True) for i in range(len(links_geo))] ### 5. Initialize a dictionary to store frequency of geographic locations to be mapped: ### ### Keys ---> unique values of the selected input feature field; ### Values ---> count frequency of word match between parsed HTML string and dictionary key (e.g. country name) ### #country_parse = {k: None for k in country_lst} country_parse = {} for el in text_elements_geo: for geo in fNames_lst: if len(el) == 1 and geo in el[0]: if not geo in country_parse: country_parse[geo] = 1 else: country_parse[geo] += 1 arcpy.AddMessage('Dictionary Filled With Frequency Counts From Parsed HTML ...') ### 6. Create a temporary point layer from the input Polygon feature class ### arcpy.SetProgressorLabel('Creating Temporary Point Layer from Input Feature ...') arcpy.AddMessage('Creating Temporary Point Layer from Input Feature ...') ### Process: Feature To Point Layer ### arcpy.FeatureToPoint_management(input_fc, out_layer_pnt, "INSIDE") ### 7. Add Fields Required as Input by the XY To Line GP tool ### arcpy.SetProgressorLabel('Adding New Field ...') arcpy.AddMessage('Adding New Field ...') #### Add Fields to temporary feature layer ### arcpy.AddField_management(in_table=out_layer_pnt, field_name="FROM_X", field_type="DOUBLE") arcpy.AddField_management(in_table=out_layer_pnt, field_name="FROM_Y", field_type="DOUBLE") arcpy.AddField_management(in_table=out_layer_pnt, field_name="TO_X", field_type="DOUBLE") arcpy.AddField_management(in_table=out_layer_pnt, field_name="TO_Y", field_type="DOUBLE") arcpy.AddField_management(in_table=out_layer_pnt, field_name="Frequency", field_type="SHORT") #### Add XY Coordinates To Point Layer #### arcpy.AddXY_management(out_layer_pnt) ### 8. Fill Out Values for All Newly Added Fields in the Temp Point Feature Layer ### arcpy.SetProgressorLabel('Transferring Values from Dictionary to Feature Layer ...') arcpy.AddMessage('Transferring Values from Dictionary to Feature Layer ...') fields_selection = ['FROM_X', 'FROM_Y', input_fc_field, 'TO_X', 'TO_Y', 'POINT_X', 'POINT_Y', 'Frequency'] with arcpy.da.UpdateCursor(out_layer_pnt, fields_selection) as cursor: for row in cursor: if row[2] in country_parse.keys(): row[0] = srcPnt_XY[0] row[1] = srcPnt_XY[1] row[3] = row[5] row[4] = row[6] row[7] = country_parse[row[2]] # Update the cursor with the updated list cursor.updateRow(row) else: cursor.deleteRow() del cursor ### 9. Remove Unnecessary Fields From the Temp Point Feature Class ### fields = arcpy.ListFields(out_layer_pnt) keepFields = ['FROM_X', 'FROM_Y', 'TO_X', 'TO_Y', 'Frequency'] dropFields = [f.name for f in fields if f.name not in keepFields and f.type != 'OID' and f.type != 'Geometry'] # delete fields arcpy.DeleteField_management(out_layer_pnt, dropFields) ### 10. Export temp feature class to CSV and use to draw flow lines ### arcpy.SetProgressorLabel('Exporting Geographic Location to CSV Table ...') arcpy.AddMessage('Exporting Geographic Location to CSV Table ...') outTable_CSV = os.path.join(arcpy.env.scratchFolder, "Media_Flows_Table.csv") ExportToCSV(fc=out_layer_pnt, output=outTable_CSV) ### 11. If Merging Box is Checked, Merge Temp Point Feature Class To Copy of Input Flow Layer ### if is_checked_table: arcpy.SetProgressorLabel('Creating Media Information Radial Flow Lines ...') arcpy.AddMessage('Creating Media Information Radial Flow Lines ...') arcpy.XYToLine_management(in_table=outTable_CSV, out_featureclass=out_media_flows, startx_field='FROM_X', starty_field='FROM_Y', endx_field='TO_X', endy_field='TO_Y', line_type=lineType_str, id_field='Frequency', spatial_reference=out_layer_pnt) arcpy.SetProgressorLabel('Merging Media Information Flows With Existing Flow Layer ...') arcpy.AddMessage('Merging Media Information Flows With Existing Flow Layer ...') out_fc = os.path.join(arcpy.env.scratchGDB, "Merged_Flow_Lines") arcpy.Merge_management([out_media_flows, input_flow_lyr], out_fc) else: arcpy.SetProgressorLabel('Creating Media Information Radial Flow Lines ...') arcpy.AddMessage('Creating Media Information Radial Flow Lines ...') out_fc = os.path.join(arcpy.env.scratchGDB, "FlowLines") arcpy.XYToLine_management(in_table=outTable_CSV, out_featureclass=out_fc, startx_field='FROM_X', starty_field='FROM_Y', endx_field='TO_X', endy_field='TO_Y', line_type=lineType_str, id_field='Frequency', spatial_reference=out_layer_pnt) # Remove CSV Table os.remove(os.path.join(arcpy.env.scratchFolder, "Media_Flows_Table.csv")) arcpy.SetParameterAsText(7, out_fc) except Exception: e = sys.exc_info()[1] arcpy.AddError('An error occurred: {}'.format(e.args[0]))
def XSLayout(output_workspace, flowline, split_type, transect_spacing, transect_width, transect_width_unit): # Set environment variables arcpy.env.overwriteOutput = True arcpy.env.XYResolution = "0.00001 Meters" arcpy.env.XYTolerance = "0.0001 Meters" # Create "General" file geodatabase WorkFolder = os.path.dirname(output_workspace) General_GDB = WorkFolder + "\General.gdb" arcpy.CreateFileGDB_management(WorkFolder, "General", "CURRENT") arcpy.env.workspace = General_GDB # List parameter values arcpy.AddMessage("Output Workspace: {}".format(output_workspace)) arcpy.AddMessage("Workfolder: {}".format(WorkFolder)) arcpy.AddMessage("Flowline: " "{}".format(arcpy.Describe(flowline).baseName)) arcpy.AddMessage("Split Type: {}".format(split_type)) arcpy.AddMessage("XS Spacing: {}".format(transect_spacing)) arcpy.AddMessage("XS Width: {}".format(transect_width)) arcpy.AddMessage("XS Width Units: {}".format(transect_width_unit)) #Unsplit Line LineDissolve = "LineDissolve" arcpy.Dissolve_management(flowline, LineDissolve, "", "", "SINGLE_PART") LineSplit = "LineSplit" #Split Line if split_type == "Split at approximate distance": splitline(LineDissolve, LineSplit, transect_spacing) else: arcpy.SplitLine_management(LineDissolve, LineSplit) #Add fields to LineSplit FieldsNames = [ "LineID", "Direction", "Azimuth", "X_mid", "Y_mid", "AziLine_1", "AziLine_2", "Distance" ] for fn in FieldsNames: arcpy.AddField_management(LineSplit, fn, "DOUBLE") #Calculate Fields CodeBlock_Direction = """def GetAzimuthPolyline(shape): radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y)) degrees = radian * 180 / math.pi return degrees""" CodeBlock_Azimuth = """def Azimuth(direction): if direction < 0: azimuth = direction + 360 return azimuth else: return direction""" CodeBlock_NULLS = """def findNulls(fieldValue): if fieldValue is None: return 0 elif fieldValue is not None: return fieldValue""" arcpy.CalculateField_management(LineSplit, "LineID", "!OBJECTID!", "PYTHON_9.3") arcpy.CalculateField_management(LineSplit, "Direction", "GetAzimuthPolyline(!Shape!)", "PYTHON_9.3", CodeBlock_Direction) arcpy.CalculateField_management(LineSplit, "Direction", "findNulls(!Direction!)", "PYTHON_9.3", CodeBlock_NULLS) arcpy.CalculateField_management(LineSplit, "Azimuth", "Azimuth(!Direction!)", "PYTHON_9.3", CodeBlock_Azimuth) arcpy.CalculateField_management( LineSplit, "X_mid", "!Shape!.positionAlongLine(0.5,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( LineSplit, "Y_mid", "!Shape!.positionAlongLine(0.5,True).firstPoint.Y", "PYTHON_9.3") CodeBlock_AziLine1 = """def Azline1(azimuth): az1 = azimuth + 90 if az1 > 360: az1-=360 return az1 else: return az1""" CodeBlock_AziLine2 = """def Azline2(azimuth): az2 = azimuth - 90 if az2 < 0: az2+=360 return az2 else: return az2""" arcpy.CalculateField_management(LineSplit, "AziLine_1", "Azline1(!Azimuth!)", "PYTHON_9.3", CodeBlock_AziLine1) arcpy.CalculateField_management(LineSplit, "AziLine_2", "Azline2(!Azimuth!)", "PYTHON_9.3", CodeBlock_AziLine2) arcpy.CalculateField_management(LineSplit, "Distance", transect_width, "PYTHON_9.3") #Generate Azline1 and Azline2 spatial_reference = arcpy.Describe(flowline).spatialReference Azline1 = "Azline1" Azline2 = "Azline2" arcpy.BearingDistanceToLine_management(LineSplit, Azline1, "X_mid", "Y_mid", "Distance", transect_width_unit, "AziLine_1", "DEGREES", "GEODESIC", "LineID", spatial_reference) arcpy.BearingDistanceToLine_management(LineSplit, Azline2, "X_mid", "Y_mid", "Distance", transect_width_unit, "AziLine_2", "DEGREES", "GEODESIC", "LineID", spatial_reference) #Create Azline and append Azline1 and Azline2 Azline = "Azline" arcpy.CreateFeatureclass_management(arcpy.env.workspace, "Azline", "POLYLINE", "", "", "", spatial_reference) arcpy.AddField_management(Azline, "LineID", "DOUBLE") arcpy.Append_management([Azline1, Azline2], Azline, "NO_TEST") #Dissolve Azline Azline_Dissolve = "Azline_Dissolve" arcpy.Dissolve_management(Azline, Azline_Dissolve, "LineID", "", "SINGLE_PART") #Add Fields to Azline_Dissolve FieldsNames2 = ["x_start", "y_start", "x_end", "y_end"] for fn2 in FieldsNames2: arcpy.AddField_management(Azline_Dissolve, fn2, "DOUBLE") #Calculate Azline_Dissolve fields arcpy.CalculateField_management( Azline_Dissolve, "x_start", "!Shape!.positionAlongLine(0,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "y_start", "!Shape!.positionAlongLine(0,True).firstPoint.Y", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "x_end", "!Shape!.positionAlongLine(1,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "y_end", "!Shape!.positionAlongLine(1,True).firstPoint.Y", "PYTHON_9.3") #Generate output file out_transect_name = "xs_{}_{}".format(int(round(transect_spacing)), int(round(transect_width))) output_transect = os.path.join(output_workspace, out_transect_name) arcpy.XYToLine_management(Azline_Dissolve, output_transect, "x_start", "y_start", "x_end", "y_end", "", "", spatial_reference) # Create `Seq` field arcpy.AddField_management(in_table=output_transect, field_name="Seq", field_type="SHORT") arcpy.CalculateField_management(in_table=output_transect, field="Seq", expression="!OID!", expression_type="PYTHON_9.3") # Set the ReachName field unique_reaches = set( row[0] for row in arcpy.da.SearchCursor(flowline, "ReachName")) reach_name = list(unique_reaches)[0] arcpy.AddField_management(in_table=output_transect, field_name="ReachName", field_type="TEXT") arcpy.CalculateField_management(in_table=output_transect, field="ReachName", expression="'" + reach_name + "'", expression_type="PYTHON_9.3") # Return arcpy.SetParameter(6, output_transect) # Cleanup arcpy.Delete_management(General_GDB)
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
def main(input_fc=None, output_fc=None, closest=False, mode=settings.mode): # convert 'closest' string value closest_count = "" if closest == 'true': closest_count = 1 # does the input fc exist? if not arcpy.Exists(input_fc): utils.msg("Input, %s, doesn't exist.", mtype='error') sys.exit() input_fc_mem = 'in_memory/input_fc' try: utils.msg("Copying features into memory...") arcpy.CopyFeatures_management(input_fc, input_fc_mem) except Exception as e: utils.msg("Unable to copy features into memory.", mtype='error', exception=e) sys.exit() # add POINT_X and POINT_Y columns try: arcpy.AddXY_management(input_fc_mem) except Exception as e: message = "Error creating point coordinate values for {0}".format( input_fc) utils.msg(message, mtype='error', exception=e) sys.exit() # get the spatial reference of our input, determine the type desc = arcpy.Describe(input_fc_mem) sr = desc.spatialReference if sr.type not in ['Geographic', 'Projected']: utils.msg("This tools only works with geographic or projected data.", mtype='error') sys.exit() # yes, all this mucking about is necessary to get a row count row_count = int(arcpy.GetCount_management(input_fc_mem).getOutput(0)) if row_count < 500 or closest: near_table = 'in_memory/near_table' output_fc_mem = 'in_memory/output_fc' else: utils.msg( "Too many features (%i) to load pairwise into memory. Using disk, which decreases performance." % row_count) near_table = os.path.join(arcpy.env.scratchGDB, 'near_table') output_fc_mem = output_fc # create a look-up table which maps individual observations to all others. # FIXME: do we need to filter this in some way? by group, ...? try: # generates an output table with IN_FID, NEAR_FID, NEAR_X, NEAR_Y [...] utils.msg("Creating near table...") arcpy.GenerateNearTable_analysis(input_fc_mem, input_fc_mem, near_table, \ "", "LOCATION", "NO_ANGLE", "ALL") time.sleep(5) except Exception as e: utils.msg("Error creating near table.", mtype='error', exception=e) sys.exit() try: utils.msg("Joining attributes...") in_fields = arcpy.ListFields(input_fc_mem) # find the OID field. oid_field = [f.name for f in in_fields if f.type == 'OID'][0] # join back our 'true' x & y columns to the original data. arcpy.JoinField_management(near_table, "IN_FID", input_fc_mem, oid_field, ['POINT_X', 'POINT_Y']) # clean up arcpy.Delete_management(input_fc_mem) in_fields = arcpy.ListFields(near_table) oid_field = [f.name for f in in_fields if f.type == 'OID'][0] id_field = 'ID' # add an 'ID' field other than the OID so we can copy it over using XYToLine arcpy.AddField_management(near_table, id_field, "LONG") expression = "!{0}!".format(oid_field) arcpy.CalculateField_management(near_table, id_field, expression, "PYTHON_9.3") except Exception as e: utils.msg("Error joining data.", mtype='error', exception=e) sys.exit() # Now compute the lines between these locations. try: utils.msg("Computing pairwise geodesic lines...") arcpy.XYToLine_management(near_table, output_fc_mem, \ "NEAR_X", "NEAR_Y", "POINT_X", "POINT_Y", "GEODESIC", id_field) utils.msg("Remapping output columns...") arcpy.JoinField_management(output_fc_mem, id_field, near_table, id_field, ['IN_FID', 'NEAR_FID']) # column modifications necessary remap_cols = [ ('IN_FID', 'Source_ID', '!IN_FID!'), ('POINT_X', 'Source_X', '!POINT_X!'), ('POINT_Y', 'Source_Y', '!POINT_Y!'), ('NEAR_FID', 'Dest_ID', '!NEAR_FID!'), ('NEAR_X', 'Dest_X', '!NEAR_X!'), ('NEAR_Y', 'Dest_Y', '!NEAR_Y!'), (None, 'Distance_in_km', "!Shape.length@kilometers!" ) # output distance in kilometers ] fn = [f.name for f in arcpy.ListFields(output_fc_mem)] for (in_name, out_name, expr) in remap_cols: # create a new field for storing the coord arcpy.AddField_management(output_fc_mem, out_name, "DOUBLE") # copy the field over from the original arcpy.CalculateField_management(output_fc_mem, out_name, expr, "PYTHON_9.3") # delete the original field if in_name in fn: arcpy.DeleteField_management(output_fc_mem, in_name) # can't delete length; part of the data model which is # unfortunate -- it's degrees, and has no linear distance. # arcpy.DeleteField_management(output_fc_mem, "Shape_Length") # copy the final result back to disk. if output_fc_mem != output_fc: utils.msg("Writing results to disk...") output_fc = os.path.abspath(output_fc) arcpy.CopyFeatures_management(output_fc_mem, output_fc) utils.msg("Created shortest distance paths successfully: {0}".format( output_fc)) except Exception as e: utils.msg("Error creating geodesic lines.", mtype='error', exception=e) sys.exit() # clean up: remove intermediate steps. try: for layer in [near_table, output_fc_mem]: arcpy.Delete_management(layer) except Exception as e: utils.msg("Unable to delete temporary layer", mtype='error', exception=e) sys.exit()
tempsideyardcenter = r"/tempsideyardcenter.shp" arcpy.SpatialJoin_analysis(splitLineCenter, tempsideyardsfull1, tempsideyardcenter, "#", "KEEP_COMMON") #Draw lines from sides to center. arcpy.AddXY_management(tempsideyardcenter) arcpy.AddXY_management(buildingsCenter1) arcpy.JoinField_management(tempsideyardcenter, "PARCEL", buildingsCenter1, "PARCEL", ["POINT_X", "POINT_Y"]) temptempsideyardcenter = r"/tempsideyardcenter.dbf" #Now cut the yards into separate shapes that can be measured. cutlines = "/tempcutlines.shp" arcpy.XYToLine_management(temptempsideyardcenter, cutlines, "POINT_X", "POINT_Y", "POINT_X_1", "POINT_Y_1") tempdirtyyards = "/tempdirtyyards.shp" arcpy.FeatureToPolygon_management([outbox, cutlines], tempdirtyyards) tempdirtyyards1 = "/tempdirtyyards1.shp" arcpy.Erase_analysis(tempdirtyyards, testbldgs1, tempdirtyyards1) arcpy.AddField_management(tempdirtyyards, "area", "Double") expression1 = "{0}".format("!SHAPE.area@SQUAREFEET!") arcpy.CalculateField_management( tempdirtyyards1, "area", expression1, "PYTHON", )
def fishbone(address, centerlines, schemaFile): #determine the coordinate system of the files env.outputCoordinateSystem = arcpy.Describe(address + ".shp").spatialReference #run make feature layer so processes can be performed on the files addFC = "addressFC" centerFC = "centerFC" arcpy.MakeFeatureLayer_management(address + ".shp", addFC) arcpy.MakeFeatureLayer_management(centerlines + ".shp", centerFC) print("Created address and centerlines feature layers") #convert dbf of the address file to csv inTable = "addressFC.dbf" outTable = "addressdbfConvert1.csv" outLoc = schemaFile.iloc[0, 2] arcpy.TableToTable_conversion(inTable, outLoc, outTable) print("Converted address dbf") #remove the OID field that sometimes gets added from converting to csv os.chdir(outLoc) df = pd.read_csv(outTable, delimiter=",") outCSV = schemaFile.iloc[0, 5] try: df = df.drop("OID", axis=1) except: print("OID does not exist in the table") df.to_csv(outLoc + "/" + outCSV) print("Created csv file") #geocode the csv file using a premade address locator addressField = schemaFile.iloc[0, 6] zipField = schemaFile.iloc[0, 7] addressLocator = schemaFile.iloc[0, 8] addressFields = "Street " + addressField + ";ZIP " + zipField geocodeResult = "geocodeResult1" arcpy.GeocodeAddresses_geocoding(outCSV, addressLocator, addressFields, geocodeResult) print("Finished geocoding") #repair geometry on the addresses in the geocode output file tempLayer3 = "geocodeFC" arcpy.MakeFeatureLayer_management("geocodeResult1.shp", tempLayer3) arcpy.RepairGeometry_management(tempLayer3, "KEEP_NULL") #export to shapefile repairResult = "repairResult1" arcpy.CopyFeatures_management(tempLayer3, repairResult) print("Created new file with the repaired geometry") #check for scores that aren't 100 and save them in a file fc = outputLoc + "/" + repairResult + ".shp" fields = ["Score", "FID"] expression = "Score < 100" unmatchValues = arcpy.da.UpdateCursor(fc, fields, expression) for record in unmatchValues: print("Record number " + str(record[1]) + " has a match score of " + str(record[0])) f = open(str(outputLoc) + "/fishNotMatch.csv", "a") f.write(str(record[1]) + "," + str(record[0]) + ",\n") if int(record[0]) < 80: unmatchValues.deleteRow() #add x, y, and join fields in the results and address files repairResult = "repairResult1.shp" arcpy.AddField_management(repairResult, "NewXField1", "FLOAT", 9, 2) arcpy.AddField_management(repairResult, "NewYField1", "FLOAT", 9, 2) arcpy.AddField_management(repairResult, "JoinField1", "TEXT", "", "", 100) arcpy.AddField_management(addFC, "NewXField2", "FLOAT", 9, 2) arcpy.AddField_management(addFC, "NewYField2", "FLOAT", 9, 2) arcpy.AddField_management(addFC, "JoinField2", "TEXT", "", "", 100) print("Added fields") #calculate geometry on x and y arcpy.CalculateField_management(repairResult, "NewXField1", "!shape.extent.XMax!", "PYTHON_9.3") arcpy.CalculateField_management(repairResult, "NewYField1", "!shape.extent.YMax!", "PYTHON_9.3") arcpy.CalculateField_management(addFC, "NewXField2", "!shape.extent.XMax!", "PYTHON_9.3") arcpy.CalculateField_management(addFC, "NewYField2", "!shape.extent.YMax!", "PYTHON_9.3") #calculate join to equal the full address and zip code repairAddress = schemaFile.iloc[0, 9] repairZipcode = schemaFile.iloc[0, 10] expression1 = '!' + str(repairAddress) + '! + " " + !' + str( repairZipcode) + '!' expression2 = '!' + str(addressField) + '! + " " + !' + str(zipField) + '!' arcpy.CalculateField_management(repairResult, "JoinField1", expression1, "PYTHON_9.3") arcpy.CalculateField_management(addFC, "JoinField2", expression2, "PYTHON_9.3") print("Calculated fields") #join the two datasets and export them arcpy.MakeFeatureLayer_management(repairResult, "repairFC") arcpy.AddJoin_management("repairFC.shp", "JoinField1", "addressFC.dbf", "JoinField2") joinFile = "joinFile1" arcpy.CopyFeatures_management("repairFC", joinFile) print("Joined tables and exported file") #run xy to line arcpy.XYToLine_management("joinFile1.dbf", "outFish", "NewXField1", "NewYField1", "NewXField2", "NewYField2") print("XY to Line is complete") #delete temporary files arcpy.Delete_management("centerEDIT.shp") arcpy.Delete_management("addressdbfConvert1.csv") arcpy.Delete_management("geocodeTable.csv") arcpy.Delete_management("geocodeResult1.shp") arcpy.Delete_management("repairResult1.shp") arcpy.Delete_management("addressEDIT.shp") arcpy.Delete_management("joinFile1.shp") return
arcpy.AddField_management(xSecResultsTable, "ID", "LONG") arcpy.AddField_management(xSecResultsTable, "FROM_X", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "FROM_Y", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "TO_X", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "TO_Y", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "m", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "c", "DOUBLE") arcpy.AddField_management(xSecResultsTable, "s", "DOUBLE") fieldNameList = ["ID", "FROM_X", "FROM_Y", "TO_X", "TO_Y", "m", "c", "s"] cursor = arcpy.da.InsertCursor(xSecResultsTable, (fieldNameList)) for row in clRowVal: cursor.insertRow(row) arcpy.XYToLine_management(xSecResultsTable, xSecLineResults, "FROM_X", "FROM_Y", "TO_X", "TO_Y", "", "ID", xSecPoints) arcpy.AddMessage("Generating Points...") arcpy.GeneratePointsAlongLines_management(xSecLineResults, newXSecPoints, 'DISTANCE', '1 meters') if (deleteAndAppend == "true"): arcpy.AddMessage("Deleting and Appending...") with arcpy.da.UpdateCursor( xSecPoints, "ID", "ID >= " + str(xSecStartID) + " AND ID <= " + str(xSecEndID)) as cursor: for row in cursor: cursor.deleteRow() arcpy.Append_management(newXSecPoints, xSecPoints, 'NO_TEST') if (newPointXSecOut): arcpy.AddMessage("Copying features to " + newPointXSecOut)
def tableTo2PointLine(inputTable, inputStartCoordinateFormat, inputStartXField, inputStartYField, inputEndCoordinateFormat, inputEndXField, inputEndYField, outputLineFeatures, inputLineType, inputSpatialReference): ''' Creates line features from a start point coordinate and an endpoint coordinate. inputTable - Input Table inputStartCoordinateFormat - Start Point Format (from Value List) inputStartXField - Start X Field (longitude, UTM, MGRS, USNG, GARS, GEOREF)(from Input Table) inputStartYField - Start Y Field (latitude)(from Input Table) inputEndCoordinateFormat - End Point Format (from Value List) inputEndXField - End X Field (longitude, UTM, MGRS, USNG, GARS, GEOREF)(from Input Table) inputEndYField - End Y Field (latitude) (from Input Table) outputLineFeatures - Output Line inputLineType - Line Type (from Value List) inputSpatialReference - Spatial Reference, default is GCS_WGS_1984 returns line feature class inputStartCoordinateFormat and inputEndCoordinateFormat must be one of the following: * DD_1: Both longitude and latitude values are in a single field. Two values are separated by a space, a comma, or a slash. * DD_2: Longitude and latitude values are in two separate fields. * DDM_1: Both longitude and latitude values are in a single field. Two values are separated by a space, a comma, or a slash. * DDM_2: Longitude and latitude values are in two separate fields. * DMS_1: Both longitude and latitude values are in a single field. Two values are separated by a space, a comma, or a slash. * DMS_2: Longitude and latitude values are in two separate fields. * GARS: Global Area Reference System. Based on latitude and longitude, it divides and subdivides the world into cells. * GEOREF: World Geographic Reference System. A grid-based system that divides the world into 15-degree quadrangles and then subdivides into smaller quadrangles. * UTM_ZONES: The letter N or S after the UTM zone number designates only North or South hemisphere. * UTM_BANDS: The letter after the UTM zone number designates one of the 20 latitude bands. N or S does not designate a hemisphere. * USNG: United States National Grid. Almost exactly the same as MGRS but uses North American Datum 1983 (NAD83) as its datum. * MGRS: Military Grid Reference System. Follows the UTM coordinates and divides the world into 6-degree longitude and 20 latitude bands, but MGRS then further subdivides the grid zones into smaller 100,000-meter grids. These 100,000-meter grids are then divided into 10,000-meter, 1,000-meter, 100-meter, 10-meter, and 1-meter grids. inputLineType must be one of the following: * GEODESIC: * GREAT_CIRCLE: * RHUMB_LINE: * NORMAL_SECTION: ''' try: # get/set environment env.overwriteOutput = True deleteme = [] scratch = '%scratchGDB%' joinFieldName = "JoinID" startXFieldName = "startX" startYFieldName = "startY" endXFieldName = "endX" endYFieldName = "endY" if env.scratchWorkspace: scratch = env.scratchWorkspace inputSpatialReference = _checkSpatialRef(inputSpatialReference) copyRows = os.path.join(scratch, "copyRows") arcpy.CopyRows_management(inputTable, copyRows) originalTableFieldNames = _tableFieldNames(inputTable, joinExcludeFields) addUniqueRowID(copyRows, joinFieldName) #Convert Start Point arcpy.AddMessage("Formatting start point...") startCCN = os.path.join(scratch, "startCCN") arcpy.ConvertCoordinateNotation_management(copyRows, startCCN, inputStartXField, inputStartYField, inputStartCoordinateFormat, "DD_NUMERIC", joinFieldName) arcpy.AddField_management(startCCN, startXFieldName, "DOUBLE") arcpy.CalculateField_management(startCCN, startXFieldName, "!DDLon!", "PYTHON_9.3") arcpy.AddField_management(startCCN, startYFieldName, "DOUBLE") arcpy.CalculateField_management(startCCN, startYFieldName, "!DDLat!", "PYTHON_9.3") arcpy.JoinField_management(copyRows, joinFieldName, startCCN, joinFieldName, [startXFieldName, startYFieldName]) #Convert End Point arcpy.AddMessage("Formatting end point...") endCCN = os.path.join(scratch, "endCCN") arcpy.ConvertCoordinateNotation_management(copyRows, endCCN, inputEndXField, inputEndYField, inputEndCoordinateFormat, "DD_NUMERIC", joinFieldName) arcpy.AddField_management(endCCN, endXFieldName, "DOUBLE") arcpy.CalculateField_management(endCCN, endXFieldName, "!DDLon!", "PYTHON_9.3") arcpy.AddField_management(endCCN, endYFieldName, "DOUBLE") arcpy.CalculateField_management(endCCN, endYFieldName, "!DDLat!", "PYTHON_9.3") arcpy.JoinField_management(copyRows, joinFieldName, endCCN, joinFieldName, [endXFieldName, endYFieldName]) #XY TO LINE arcpy.AddMessage( "Connecting start point to end point as {0}...".format( inputLineType)) arcpy.XYToLine_management(copyRows, outputLineFeatures, startXFieldName, startYFieldName, endXFieldName, endYFieldName, inputLineType, joinFieldName, inputSpatialReference) #Join original table fields to output arcpy.AddMessage( "Joining fields from input table to output line features...") arcpy.JoinField_management(outputLineFeatures, joinFieldName, copyRows, joinFieldName, originalTableFieldNames) arcpy.DeleteField_management(outputLineFeatures, [ joinFieldName, startXFieldName, startYFieldName, endXFieldName, endYFieldName ]) return outputLineFeatures except arcpy.ExecuteError: # Get the tool error messages msgs = arcpy.GetMessages() arcpy.AddError(msgs) print(msgs) except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n" # Return python error messages for use in script tool or Python Window arcpy.AddError(pymsg) arcpy.AddError(msgs) # Print Python error messages for use in Python / Python Window print(pymsg + "\n") print(msgs) finally: if len(deleteme) > 0: # cleanup intermediate datasets if debug == True: arcpy.AddMessage("Removing intermediate datasets...") for i in deleteme: if debug == True: arcpy.AddMessage("Removing: " + str(i)) arcpy.Delete_management(i) if debug == True: arcpy.AddMessage("Done")
out_name=i + '_stoptable') stoplyr = arcpy.MakeXYEventLayer_management(table=env + '/BUSGTFS.gdb/' + i + '_stoptable', in_x_field='stop_lon', in_y_field='stop_lat', out_layer=i + '_stop', spatial_reference='4326') arcpy.management.CopyFeatures(stoplyr, env + '/BUSGTFS.gdb/' + i + '_stop') arcpy.TableToTable_conversion(in_rows=env + '/' + i + '/shape2.txt', out_path=env + '/BUSGTFS.gdb', out_name=i + '_shapetable') arcpy.XYToLine_management( in_table=env + '/BUSGTFS.gdb/' + i + '_shapetable', out_featureclass=env + '/BUSGTFS.gdb/' + i + '_shape', startx_field='shape_from_lon', starty_field='shape_from_lat', endx_field='shape_to_lon', endy_field='shape_to_lat', spatial_reference='4326') shapelyr = arcpy.MakeFeatureLayer_management(env + '/BUSGTFS.gdb/' + i + '_shape') shapelyr = arcpy.AddJoin_management(in_layer_or_view=shapelyr, in_field='OID', join_table=env + '/BUSGTFS.gdb/' + i + '_shapetable', join_field='OBJECTID') arcpy.Dissolve_management( in_features=shapelyr, out_feature_class=env + '/BUSGTFS.gdb/' + i + '_route', dissolve_field=[ str(i) + '_shapetable.' + x for x in [
arcpy.SetProgressorLabel("Calculating geometry of record " + str(iter3) + " of " + str(recordCount1) + "...") row[1] = row[0].positionAlongLine(0, True).firstPoint.X row[2] = row[0].positionAlongLine(0, True).firstPoint.Y row[3] = row[0].positionAlongLine(1, True).firstPoint.X row[4] = row[0].positionAlongLine(1, True).firstPoint.Y iter3 = iter3 + 1 arcpy.SetProgressorPosition() updateRows.updateRow(row) del updateRows del row #Generate output file OutputTransect = arcpy.XYToLine_management(Azline_Dissolve, "OutputTransect", "x_start", "y_start", "x_end", "y_end", "", "", spatial_reference) arcpy.AddMessage("Done creating transects across stream channel... ") ##------------------------------------------------------------------------------------------------------------------ ##------------------------------------------------------------------------------------------------------------------ #Copy OutputTransect so it doesn't have 'linesegment' field (used for joining later) OutputTransectNoLineSeg = arcpy.CopyFeatures_management( OutputTransect, "OutputTransectNoLineSeg") #Create progressor for script completion percentage labels in ArcMap [Record Count for OutputTransect] recordCount2 = int(arcpy.GetCount_management(OutputTransect).getOutput(0)) #Create endpoints at polyline ends of transects arcpy.AddMessage("Creating endpoints on transects... ")
def generate_transects(workspacePath, lineFL, splitType, distanceBetweenTransects, transectWidth, widthUnit, outputFC): import arcpy import math #Set environments arcpy.env.overwriteOutput = True arcpy.env.XYResolution = "0.00001 Meters" arcpy.env.XYTolerance = "0.0001 Meters" # Set local variables arcpy.env.workspace = workspacePath Lines = lineFL SplitType = splitType DistanceSplit = float(distanceBetweenTransects) TransecLength = transectWidth TransecLength_Unit = widthUnit OutputTransect = outputFC # Def splitline module ###START SPLIT LINE CODE IN A SAME DISTANCE### Source: http://nodedangles.wordpress.com/2011/05/01/quick-dirty-arcpy-batch-splitting-polylines-to-a-specific-length/ def splitline(inFC, FCName, alongDist): OutDir = arcpy.env.workspace outFCName = FCName outFC = OutDir + "/" + outFCName def distPoint(p1, p2): calc1 = p1.X - p2.X calc2 = p1.Y - p2.Y return math.sqrt((calc1**2) + (calc2**2)) def midpoint(prevpoint, nextpoint, targetDist, totalDist): newX = prevpoint.X + ((nextpoint.X - prevpoint.X) * (targetDist / totalDist)) newY = prevpoint.Y + ((nextpoint.Y - prevpoint.Y) * (targetDist / totalDist)) return arcpy.Point(newX, newY) def splitShape(feat, splitDist): # Count the number of points in the current multipart feature # partcount = feat.partCount partnum = 0 # Enter while loop for each part in the feature (if a singlepart feature # this will occur only once) # lineArray = arcpy.Array() while partnum < partcount: # Print the part number # #print "Part " + str(partnum) + ":" part = feat.getPart(partnum) #print part.count totalDist = 0 pnt = part.next() pntcount = 0 prevpoint = None shapelist = [] # Enter while loop for each vertex # while pnt: if not (prevpoint is None): thisDist = distPoint(prevpoint, pnt) maxAdditionalDist = splitDist - totalDist #print thisDist, totalDist, maxAdditionalDist if (totalDist + thisDist) > splitDist: while (totalDist + thisDist) > splitDist: maxAdditionalDist = splitDist - totalDist #print thisDist, totalDist, maxAdditionalDist newpoint = midpoint(prevpoint, pnt, maxAdditionalDist, thisDist) lineArray.add(newpoint) shapelist.append(lineArray) lineArray = arcpy.Array() lineArray.add(newpoint) prevpoint = newpoint thisDist = distPoint(prevpoint, pnt) totalDist = 0 lineArray.add(pnt) totalDist += thisDist else: totalDist += thisDist lineArray.add(pnt) #shapelist.append(lineArray) else: lineArray.add(pnt) totalDist = 0 prevpoint = pnt pntcount += 1 pnt = part.next() # If pnt is null, either the part is finished or there is an # interior ring # if not pnt: pnt = part.next() #if pnt: #print "Interior Ring:" partnum += 1 if (lineArray.count > 1): shapelist.append(lineArray) return shapelist if arcpy.Exists(outFC): arcpy.Delete_management(outFC) arcpy.Copy_management(inFC, outFC) #origDesc = arcpy.Describe(inFC) #sR = origDesc.spatialReference #revDesc = arcpy.Describe(outFC) #revDesc.ShapeFieldName deleterows = arcpy.UpdateCursor(outFC) for iDRow in deleterows: deleterows.deleteRow(iDRow) try: del iDRow del deleterows except: pass inputRows = arcpy.SearchCursor(inFC) outputRows = arcpy.InsertCursor(outFC) fields = arcpy.ListFields(inFC) numRecords = int(arcpy.GetCount_management(inFC).getOutput(0)) OnePercentThreshold = numRecords // 100 #printit(numRecords) iCounter = 0 iCounter2 = 0 for iInRow in inputRows: inGeom = iInRow.shape iCounter += 1 iCounter2 += 1 if (iCounter2 > (OnePercentThreshold + 0)): #printit("Processing Record "+str(iCounter) + " of "+ str(numRecords)) iCounter2 = 0 if (inGeom.length > alongDist): shapeList = splitShape(iInRow.shape, alongDist) for itmp in shapeList: newRow = outputRows.newRow() for ifield in fields: if (ifield.editable): newRow.setValue(ifield.name, iInRow.getValue(ifield.name)) newRow.shape = itmp outputRows.insertRow(newRow) else: outputRows.insertRow(iInRow) del inputRows del outputRows #printit("Done!") ###END SPLIT LINE CODE IN A SAME DISTANCE### # Create "General" file geodatabase WorkFolder = arcpy.env.workspace General_GDB = WorkFolder + "\General.gdb" arcpy.CreateFileGDB_management(WorkFolder, "General", "CURRENT") arcpy.env.workspace = General_GDB #Unsplit Line LineDissolve = "LineDissolve" arcpy.Dissolve_management(Lines, LineDissolve, "", "", "SINGLE_PART") LineSplit = "LineSplit" #Split Line if SplitType == "Split at approximate distance": splitline(LineDissolve, LineSplit, DistanceSplit) else: arcpy.SplitLine_management(LineDissolve, LineSplit) #Add fields to LineSplit FieldsNames = [ "LineID", "Direction", "Azimuth", "X_mid", "Y_mid", "AziLine_1", "AziLine_2", "Distance" ] for fn in FieldsNames: arcpy.AddField_management(LineSplit, fn, "DOUBLE") #Calculate Fields CodeBlock_Direction = """def GetAzimuthPolyline(shape): radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y)) degrees = radian * 180 / math.pi return degrees""" CodeBlock_Azimuth = """def Azimuth(direction): if direction < 0: azimuth = direction + 360 return azimuth else: return direction""" CodeBlock_NULLS = """def findNulls(fieldValue): if fieldValue is None: return 0 elif fieldValue is not None: return fieldValue""" arcpy.CalculateField_management(LineSplit, "LineID", "!OBJECTID!", "PYTHON_9.3") arcpy.CalculateField_management(LineSplit, "Direction", "GetAzimuthPolyline(!Shape!)", "PYTHON_9.3", CodeBlock_Direction) arcpy.CalculateField_management(LineSplit, "Direction", "findNulls(!Direction!)", "PYTHON_9.3", CodeBlock_NULLS) arcpy.CalculateField_management(LineSplit, "Azimuth", "Azimuth(!Direction!)", "PYTHON_9.3", CodeBlock_Azimuth) arcpy.CalculateField_management( LineSplit, "X_mid", "!Shape!.positionAlongLine(0.5,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( LineSplit, "Y_mid", "!Shape!.positionAlongLine(0.5,True).firstPoint.Y", "PYTHON_9.3") CodeBlock_AziLine1 = """def Azline1(azimuth): az1 = azimuth + 90 if az1 > 360: az1-=360 return az1 else: return az1""" CodeBlock_AziLine2 = """def Azline2(azimuth): az2 = azimuth - 90 if az2 < 0: az2+=360 return az2 else: return az2""" arcpy.CalculateField_management(LineSplit, "AziLine_1", "Azline1(!Azimuth!)", "PYTHON_9.3", CodeBlock_AziLine1) arcpy.CalculateField_management(LineSplit, "AziLine_2", "Azline2(!Azimuth!)", "PYTHON_9.3", CodeBlock_AziLine2) arcpy.CalculateField_management(LineSplit, "Distance", TransecLength, "PYTHON_9.3") #Generate Azline1 and Azline2 spatial_reference = arcpy.Describe(Lines).spatialReference Azline1 = "Azline1" Azline2 = "Azline2" arcpy.BearingDistanceToLine_management(LineSplit, Azline1, "X_mid", "Y_mid", "Distance", TransecLength_Unit, "AziLine_1", "DEGREES", "GEODESIC", "LineID", spatial_reference) arcpy.BearingDistanceToLine_management(LineSplit, Azline2, "X_mid", "Y_mid", "Distance", TransecLength_Unit, "AziLine_2", "DEGREES", "GEODESIC", "LineID", spatial_reference) #Create Azline and append Azline1 and Azline2 Azline = "Azline" arcpy.CreateFeatureclass_management(arcpy.env.workspace, "Azline", "POLYLINE", "", "", "", spatial_reference) arcpy.AddField_management(Azline, "LineID", "DOUBLE") arcpy.Append_management([Azline1, Azline2], Azline, "NO_TEST") #Dissolve Azline Azline_Dissolve = "Azline_Dissolve" arcpy.Dissolve_management(Azline, Azline_Dissolve, "LineID", "", "SINGLE_PART") #Add Fields to Azline_Dissolve FieldsNames2 = ["x_start", "y_start", "x_end", "y_end"] for fn2 in FieldsNames2: arcpy.AddField_management(Azline_Dissolve, fn2, "DOUBLE") #Calculate Azline_Dissolve fields arcpy.CalculateField_management( Azline_Dissolve, "x_start", "!Shape!.positionAlongLine(0,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "y_start", "!Shape!.positionAlongLine(0,True).firstPoint.Y", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "x_end", "!Shape!.positionAlongLine(1,True).firstPoint.X", "PYTHON_9.3") arcpy.CalculateField_management( Azline_Dissolve, "y_end", "!Shape!.positionAlongLine(1,True).firstPoint.Y", "PYTHON_9.3") #Generate output file arcpy.XYToLine_management(Azline_Dissolve, OutputTransect, "x_start", "y_start", "x_end", "y_end", "", "", spatial_reference) #Delete General.gdb arcpy.Delete_management(General_GDB)