Esempio n. 1
0
def createMZ_Route(inCurvesRemovedPath, outFeatureClass, surfaceForZ,
                   routeIdField):
    """Input the shapefile with curves removed, calc Z values and create the routes with temp M values that
    will be overridden"""

    extStatus = arcpy.CheckOutExtension("3D")
    arcpy.AddMessage("3D analyst: " + str(extStatus))
    outPolyLineZ = outFeatureClass + "LineZ"
    tempOutsList.append(outPolyLineZ)
    outRouteMZ = outFeatureClass
    rtPartNumField = "PrtNum"
    rtPartCalcExp = "!" + routeIdField + "![-1:]"

    arcpy.InterpolateShape_3d(surfaceForZ, inCurvesRemovedPath, outPolyLineZ,
                              '', 1, '', True)
    arcpy.AddMessage(arcpy.GetMessages())
    arcpy.CreateRoutes_lr(outPolyLineZ, routeIdField, outRouteMZ)
    arcpy.AddMessage(arcpy.GetMessages())
    arcpy.AddField_management(outRouteMZ, rtPartNumField, "SHORT")
    arcpy.CalculateField_management(outRouteMZ, rtPartNumField, rtPartCalcExp,
                                    "PYTHON_9.3")

    arcpy.AddMessage(
        "Routes with Z values and M place holders have been created")
    extStatus = arcpy.CheckInExtension("3D")
    arcpy.AddMessage("3D analyst: " + str(extStatus))
    return [outRouteMZ, rtPartNumField]
def convert_routes_to_lr(routes_fc, routes_id_field, out_fc):
    arcpy.CreateRoutes_lr(in_line_features=routes_fc,
                          route_id_field=routes_id_field,
                          out_feature_class=out_fc,
                          measure_source="LENGTH",
                          from_measure_field="",
                          to_measure_field="",
                          coordinate_priority="UPPER_LEFT",
                          measure_factor="1",
                          measure_offset="0",
                          ignore_gaps="IGNORE",
                          build_index="INDEX")
    return out_fc
Esempio n. 3
0
def addReachDist(given_stream):
    """
    Calculates the ReachDist attribute using StreamID
    :param given_stream:
    :return:
    """
    print "Adding Reach Distance Attribute..."
    fields = [f.name for f in arcpy.ListFields(given_stream)]
    if 'ReachID' not in fields:
        arcpy.AddField_management(given_stream, "ReachID", "LONG")
    with arcpy.da.UpdateCursor(given_stream, ['FID', 'ReachID']) as cursor:
        for row in cursor:
            row[1] = row[0]
            cursor.updateRow(row)

    # get distance along route (LineID) for segment midpoints
    midpoints = arcpy.FeatureVerticesToPoints_management(given_stream, 'in_memory/midpoints', "MID")

    seg_network_dissolve = arcpy.Dissolve_management(given_stream, 'in_memory/seg_network_dissolve', 'StreamID', '',
                                                     'SINGLE_PART', 'UNSPLIT_LINES')

    arcpy.AddField_management(seg_network_dissolve, 'From_', 'DOUBLE')
    arcpy.AddField_management(seg_network_dissolve, 'To_', 'DOUBLE')
    with arcpy.da.UpdateCursor(seg_network_dissolve, ['SHAPE@Length', 'From_', 'To_']) as cursor:
        for row in cursor:
            row[1] = 0.0
            row[2] = row[0]
            cursor.updateRow(row)

    arcpy.CreateRoutes_lr(seg_network_dissolve, 'StreamID', 'in_memory/flowline_route', 'TWO_FIELDS', 'From_', 'To_')
    routeTbl = arcpy.LocateFeaturesAlongRoutes_lr(midpoints, 'in_memory/flowline_route', 'StreamID',
                                                  1.0,
                                                  os.path.join(os.path.dirname(given_stream), 'tbl_Routes.dbf'),
                                                  'RID POINT MEAS')

    distDict = {}
    # add reach id distance values to dictionary
    with arcpy.da.SearchCursor(routeTbl, ['ReachID', 'MEAS']) as cursor:
        for row in cursor:
            distDict[row[0]] = row[1]

    # populate dictionary value to output field by ReachID
    arcpy.AddField_management(given_stream, 'ReachDist', 'DOUBLE')
    with arcpy.da.UpdateCursor(given_stream, ['ReachID', 'ReachDist']) as cursor:
        for row in cursor:
            aKey = row[0]
            row[1] = distDict[aKey]
            cursor.updateRow(row)

    arcpy.Delete_management('in_memory')
Esempio n. 4
0
def editMileage():
    '''
    定义一个计算设备设施表里程的函数;
    计算之前确保管段表中起点里程和终点里程已经正确填写;
    
    基本思路:
    首先利用创建路径工具构建库中所有管线的路径
    然后将设备设施的路径通过定位要素工具计算出其里程(或起点里程和终点里程)
    最后将里程值写入每一个要素的对应里程字段中

    三通表计算完的里程需要检验(特别是对于管段编码号较大但里程值为0的或者管段编码值较小但里程值较大的情况需要重点检查),
    '''
    #利用管段起点里程和终点里程字段创建管线的路径
    arcpy.CreateRoutes_lr("T_PN_PIPESEGMENT_GEO","PLCODE","routes", "TWO_FIELDS", "MSTART","MEND")

    #获取管线地理要素集中的所有要素类
    featureClassList=arcpy.ListFeatureClasses("","","PIPEGEO")
    for FC in featureClassList:
        if FC!="T_PN_THREEORFOUR_GEO":
            try:
                #对所有不为空的点状要素进行里程计算
                if arcpy.Describe(FC).shapeType=="Point" and\
                   int(arcpy.GetCount_management(FC).getOutput(0))!=0:
                    
                    #获取输入要素的所有字段名称
                    fieldList=[]
                    fieldNameList=[]
                    fieldList=arcpy.ListFields(FC)  #获取表中字段列表
                    for FL in fieldList: #获取每一个表的所有字段名称
                        fieldNameList.append(FL.name)
                    if "MILEAGE" in fieldNameList:
                        #添加一个字段用于复制要素的OBJECTID
                        arcpy.AddField_management(FC,"OBJECTIDCOPY","TEXT")
                        # 将目标表中的OBJECTID字段计算到设备编号中
                        arcpy.CalculateField_management(FC,"OBJECTIDCOPY","!OBJECTID!","PYTHON")
                        #计算目标要素表的里程数据
                        arcpy.LocateFeaturesAlongRoutes_lr(FC,"routes","PLCODE","0.01 Kilometers","out")
                        #利用属性连接将输出含有里程数据的表与目标要素表连接
                        arcpy.JoinField_management(FC,"OBJECTIDCOPY","out","OBJECTIDCOPY","MEAS")
                        # 将属性连接后的字段计算成要素的管段编码
                        arcpy.CalculateField_management(FC,"MILEAGE","!MEAS!","PYTHON")
                        # 删除连接时多出的临时字段
                        arcpy.DeleteField_management(FC,["MEAS","OBJECTIDCOPY"])
                        # 删除中间文件
                        arcpy.Delete_management("out")     
            except Exception,e:
                print featureClassCodeDictionary[feature],"编辑里程时出错,错误信息:",e.message
                pass
            continue
Esempio n. 5
0
def measureLines(inLayer, idField, Mlines, lengthVar, fromFld, toFld, cp):
    #measures the lines in Zlines and creates a new featureclass that is
    #M and Z aware
    try:
        arcpy.AddMessage('Measuring the length of the line(s) in ' + inLayer)
        arcpy.CreateRoutes_lr(inLayer, idField, Mlines, lengthVar, fromFld,
                              toFld, cp)
    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = tbinfo + '\n' + str(sys.exc_type) + ': ' + str(sys.exc_value)
        arcpy.AddError(pymsg)
        raise SystemError
    finally:
        arcpy.AddMessage(Mlines + ' written to ' + arcpy.env.scratchWorkspace)
Esempio n. 6
0
def create_linear_reference():
    arcpy.AddField_management(OutputBrautTemp, "start", "FLOAT", "", "", "",
                              "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.AddField_management(OutputBrautTemp, "end", "FLOAT", "", "", "", "",
                              "NULLABLE", "NON_REQUIRED", "")
    arcpy.CalculateField_management(OutputBrautTemp, "start", "0",
                                    "PYTHON_9.3")
    arcpy.AddMessage(
        "Done adding and populating fields START and END to braut_temp feature class"
    )
    arcpy.CalculateField_management(OutputBrautTemp, "end",
                                    "!shape.length@meters!", "PYTHON_9.3")
    arcpy.env.MTolerance = "0.001"  # set tolerance for M coordinate
    arcpy.CreateRoutes_lr(OutputBrautTemp, "BRAUT", OutputBraut2dnafn,
                          "TWO_FIELDS", "start", "end", "UPPER_LEFT", "1", "0",
                          "IGNORE", "INDEX")
def generate_3d_features():
    number_of_features = str(arcpy.GetCount_management(InputFeatureClass))
    arcpy.AddMessage(number_of_features + " segments found")
    arcpy.InterpolateShape_3d(InputDEM, InputFeatureClass,
                              OutputBraut3d)  # convert 2D to 3D
    arcpy.AddMessage("Feature class braut3d created")
    arcpy.CalculateField_management(OutputBraut3d, "ID", "!OBJECTID!",
                                    "PYTHON_9.3")  # populate fields
    arcpy.CalculateField_management(OutputBraut3d, "start", "0", "PYTHON_9.3")
    arcpy.CalculateField_management(OutputBraut3d, "end",
                                    "!shape.length@meters!", "PYTHON_9.3")
    arcpy.AddMessage("Fields ID, START and END populated")
    arcpy.env.MTolerance = "0.001"  # set tolerance for M coordinate
    arcpy.CreateRoutes_lr(OutputBraut3d, "ID", OutputBraut3did, "TWO_FIELDS",
                          "start", "end", "UPPER_LEFT", "1", "0", "IGNORE",
                          "INDEX")
    arcpy.AddMessage("Feature class braut3did created")
Esempio n. 8
0
@author: kyleg
'''
import arcpy
#export working railroad files to shapefiles

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "ActiveLines2013"
arcpy.CalculateField_management("ActiveLines2013", "LRSKEY",
                                """[RAILROAD] &"_" & [SUBDIVISIO]""", "VB",
                                "#")

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "ActiveLines2013"
arcpy.CreateRoutes_lr(
    "ActiveLines2013", "LRSKEY",
    "//gisdata/arcgis/GISdata/KDOT/BTP/Projects/RAIL/R1.shp", "ONE_FIELD",
    "LENGTHMILE", "#", "UPPER_RIGHT", "1", "0", "IGNORE", "INDEX")

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "ActiveStations", "R1"
arcpy.LocateFeaturesAlongRoutes_lr(
    "ActiveStations", "R1", "LRSKEY", "1 Miles",
    "//gisdata/arcgis/gisdata/kdot/btp/projects/rail/stationlraf",
    "LRSKEY POINT MEAS", "FIRST", "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")

#Make Route event layer

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "ActiveLines2013", "ActiveStationsSnap"
arcpy.SplitLineAtPoint_management(
    "ActiveLines2013", "ActiveStationsSnap",
Esempio n. 9
0
in_layer = arcpy.GetParameterAsText(0)
#route_name = arcpy.GetParameterAsText(1)

homeDir = path.dirname(in_layer)  # GDB FC input
if not homeDir:
    homeDir = env.workspace  # Map Layer input

diveDir = 'C:\\CurrentProjects\\Hump18HI\\GIS\\2018HI Hump_DivesV2.gdb'

in_name = path.basename(in_layer)
route_name = diveDir + '\\Routes\\rt_' + in_name[3:]

try:

    # create route in gdb
    arcpy.CreateRoutes_lr(in_layer, "ptt", route_name, "TWO_FIELDS", "FromLoc",
                          "ToLoc")
    #                            "TWO_FIELDS", "FromLocation", "ToLocation")

    # gather data from track
    sql_clause = (None, 'ORDER BY "FromLoc" ASC')
    #sql_clause = (None, 'ORDER BY "FromLocation" ASC')
    #    fields = ("FromLocation", "ToLocation", "ptt", "SHAPE@XY", "OBJECTID")
    fields = ("FromLoc", "ToLoc", "ptt", "SHAPE@XY", "OBJECTID")
    with arcpy.da.SearchCursor(in_layer, fields, None, sr, None,
                               sql_clause) as sCur:
        row = sCur.next()
        # get begin & end dates
        startLoc = row[0]
        ptt = row[2]
        locs = [[row[4] - 1, row[3]]]  # [OID,(Lon,Lat)]
        for row in sCur:
def SLEM(Line, Distance, Output, TempFolder, TF):

    CopyLine = arcpy.CopyFeatures_management(Line,
                                             "%ScratchWorkspace%\CopyLine")

    fieldnames = [f.name for f in arcpy.ListFields(CopyLine)]

    #/identification of the polyline type : raw, UGOs, sequenced UGOs, or AGOs
    k = 0
    if "Rank_AGO" in fieldnames:
        k = 3
    elif "Order_ID" in fieldnames:
        k = 2
    elif "Rank_UGO" in fieldnames:
        k = 1

    arcpy.AddMessage(k)

    ################################
    ########## Raw polyline ########
    ################################
    if k == 0:

        #/shaping of the segmented result
        arcpy.AddField_management(CopyLine, "Rank_UGO", "LONG", "", "", "", "",
                                  "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "Rank_UGO",
                                        "[" + fieldnames[0] + "]", "VB", "")
        arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB",
                                        "")
        arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "To_Measure",
                                        "!shape.length!", "PYTHON_9.3", "")

        #/conversion in routes
        LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO",
                                           "%ScratchWorkspace%\\LineRoutes",
                                           "TWO_FIELDS", "From_Measure",
                                           "To_Measure")

        #/creation of the event table
        PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%",
                                                      "PointEventTEMP", "", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")

        UPD_SL.UpToDateShapeLengthField(LineRoutes)

        rowslines = arcpy.SearchCursor(LineRoutes)
        rowsevents = arcpy.InsertCursor(PointEventTEMP)
        for line in rowslines:
            tempdistance = float(0)
            while (tempdistance < float(line.Shape_Length)):
                row = rowsevents.newRow()
                row.Rank_UGO = line.Rank_UGO
                row.To_M = tempdistance + float(Distance)
                row.Distance = tempdistance
                rowsevents.insertRow(row)
                tempdistance = tempdistance + float(Distance)
        del rowslines
        del rowsevents

        #/creation of the route event layer
        MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(
            LineRoutes, "Rank_UGO", PointEventTEMP,
            "Rank_UGO LINE Distance To_M",
            "%ScratchWorkspace%\\MakeRouteEventTEMP")
        Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP,
                                              "%ScratchWorkspace%\\Split", "",
                                              "0", "0", "0")
        Sort = arcpy.Sort_management(
            Split, Output,
            [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]])

        arcpy.DeleteField_management(Sort, "To_M")

        #/calculation of the "Distance" field
        UPD_SL.UpToDateShapeLengthField(Sort)

        rows1 = arcpy.UpdateCursor(Sort)
        rows2 = arcpy.UpdateCursor(Sort)
        line2 = rows2.next()
        line2.Distance = 0
        rows2.updateRow(line2)
        nrows = int(str(arcpy.GetCount_management(Sort)))
        n = 0
        for line1 in rows1:
            line2 = rows2.next()
            if n == nrows - 1:
                break
            if n == 0:
                line1.Distance = 0
            if line2.Rank_UGO == line1.Rank_UGO:
                line2.Distance = line1.Distance + line1.Shape_Length
                rows2.updateRow(line2)
            if line2.Rank_UGO != line1.Rank_UGO:
                line2.Distance = 0
                rows2.updateRow(line2)

            n += 1

        #/deleting of the temporary files
        if str(TF) == "true":
            arcpy.Delete_management(Split)
            arcpy.Delete_management(CopyLine)
            arcpy.Delete_management(LineRoutes)
            arcpy.Delete_management(PointEventTEMP)

    ##################
    ###### UGO #######
    ##################
    if k == 1:

        #/shaping of the segmented result
        arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB",
                                        "")
        arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "To_Measure",
                                        "!shape.length!", "PYTHON_9.3", "")

        #/conversion in routes
        LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO",
                                           "%ScratchWorkspace%\\LineRoutes",
                                           "TWO_FIELDS", "From_Measure",
                                           "To_Measure")

        #/creation of the event table
        PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%",
                                                      "PointEventTEMP", "", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")

        UPD_SL.UpToDateShapeLengthField(LineRoutes)

        rowslines = arcpy.SearchCursor(LineRoutes)
        rowsevents = arcpy.InsertCursor(PointEventTEMP)
        for line in rowslines:
            tempdistance = float(0)
            while (tempdistance < float(line.Shape_Length)):
                row = rowsevents.newRow()
                row.Rank_UGO = line.Rank_UGO
                row.To_M = tempdistance + float(Distance)
                row.Distance = tempdistance
                rowsevents.insertRow(row)
                tempdistance = tempdistance + float(Distance)
        del rowslines
        del rowsevents

        #/creation of the route event layer
        MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(
            LineRoutes, "Rank_UGO", PointEventTEMP,
            "Rank_UGO LINE Distance To_M",
            "%ScratchWorkspace%\\MakeRouteEventTEMP")
        Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP,
                                              "%ScratchWorkspace%\\Split", "",
                                              "0", "0", "0")
        Sort = arcpy.Sort_management(
            Split, Output,
            [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]])

        arcpy.DeleteField_management(Sort, "To_M")

        #/calculation of the "Distance" field
        UPD_SL.UpToDateShapeLengthField(Sort)

        rows1 = arcpy.UpdateCursor(Sort)
        rows2 = arcpy.UpdateCursor(Sort)
        line2 = rows2.next()
        line2.Distance = 0
        rows2.updateRow(line2)
        nrows = int(str(arcpy.GetCount_management(Sort)))
        n = 0
        for line1 in rows1:
            line2 = rows2.next()
            if n == nrows - 1:
                break
            if n == 0:
                line1.Distance = 0
            if line2.Rank_UGO == line1.Rank_UGO:
                line2.Distance = line1.Distance + line1.Shape_Length
                rows2.updateRow(line2)
            if line2.Rank_UGO != line1.Rank_UGO:
                line2.Distance = 0
                rows2.updateRow(line2)

            n += 1

        #/deleting of the temporary files
        if str(TF) == "true":
            arcpy.Delete_management(Split)
            arcpy.Delete_management(CopyLine)
            arcpy.Delete_management(LineRoutes)
            arcpy.Delete_management(PointEventTEMP)

    ################################
    ######### Sequenced UGO ########
    ################################
    if k == 2:

        #/shaping of the segmented result
        arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB",
                                        "")
        arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "To_Measure",
                                        "!Shape_Length!", "PYTHON_9.3", "")

        #/conversion in routes
        LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO",
                                           "%ScratchWorkspace%\\LineRoutes",
                                           "TWO_FIELDS", "From_Measure",
                                           "To_Measure")
        arcpy.AddField_management(LineRoutes, "Order_ID", "LONG", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        Sort = arcpy.Sort_management(Line, "%ScratchWorkspace%\\Sort",
                                     [["Rank_UGO", "ASCENDING"]])

        rows1 = arcpy.UpdateCursor(LineRoutes)
        rows2 = arcpy.SearchCursor(Sort)

        for line1 in rows1:
            line2 = rows2.next()
            line1.Order_ID = line2.Order_ID
            rows1.updateRow(line1)

        #/creation of the event table
        PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%",
                                                      "PointEventTEMP", "", "")
        arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Order_ID", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")

        UPD_SL.UpToDateShapeLengthField(LineRoutes)

        rowslines = arcpy.SearchCursor(LineRoutes)
        rowsevents = arcpy.InsertCursor(PointEventTEMP)
        for line in rowslines:
            tempdistance = float(0)
            while (tempdistance < float(line.Shape_Length)):
                row = rowsevents.newRow()
                row.To_M = tempdistance + float(Distance)
                row.Order_ID = line.Order_ID
                row.Rank_UGO = line.Rank_UGO
                row.Distance = tempdistance
                rowsevents.insertRow(row)
                tempdistance = tempdistance + float(Distance)
        del rowslines
        del rowsevents

        MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(
            LineRoutes, "Rank_UGO", PointEventTEMP,
            "Rank_UGO LINE Distance To_M",
            "%ScratchWorkspace%\\MakeRouteEventTEMP")
        Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP,
                                              "%ScratchWorkspace%\\Split", "",
                                              "0", "0", "0")
        Sort = arcpy.Sort_management(
            Split, Output,
            [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]])

        arcpy.DeleteField_management(Sort, "To_M")

        #/calculation of the "Distance" field
        UPD_SL.UpToDateShapeLengthField(Sort)

        rows1 = arcpy.UpdateCursor(Sort)
        rows2 = arcpy.UpdateCursor(Sort)
        line2 = rows2.next()
        line2.Distance = 0
        rows2.updateRow(line2)
        nrows = int(str(arcpy.GetCount_management(Split)))
        n = 0
        for line1 in rows1:
            line2 = rows2.next()
            if n >= nrows - 1:
                break
            if n == 0:
                line1.Distance = 0
            if line2.Rank_UGO == line1.Rank_UGO:
                line2.Distance = line1.Distance + line1.Shape_Length
                rows2.updateRow(line2)
            if line2.Rank_UGO != line1.Rank_UGO:
                line2.Distance = 0
                rows2.updateRow(line2)

            n += 1
        #/deleting of the temporary files
        if str(TF) == "true":
            arcpy.Delete_management(Split)
            arcpy.Delete_management(CopyLine)
            arcpy.Delete_management(LineRoutes)
            arcpy.Delete_management(PointEventTEMP)

    #############
    #### AGO ####
    #############
    if k == 3:

        #/shaping of the segmented result
        arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB",
                                        "")
        arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        try:
            arcpy.CalculateField_management(CopyLine, "To_Measure",
                                            "!shape.length!", "PYTHON_9.3", "")
        except:
            arcpy.CalculateField_management(CopyLine, "To_Measure",
                                            "!forme.length!", "PYTHON_9.3", "")

        #/conversion in routes
        LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_AGO",
                                           "%ScratchWorkspace%\\LineRoutes",
                                           "TWO_FIELDS", "From_Measure",
                                           "To_Measure")
        arcpy.AddField_management(LineRoutes, "Order_ID", "LONG", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(LineRoutes, "Rank_UGO", "LONG", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(LineRoutes, "AGO_Val", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")

        UPD_SL.UpToDateShapeLengthField(LineRoutes)

        Ext.Export(CopyLine, TempFolder, "ExportTable")

        fichier = open(TempFolder + "\\ExportTable.txt", 'r')
        Order_ID = []
        Rank_UGO = []
        Dist = []
        Rank_AGO = []
        AGO_Val = []

        head = fichier.readline().split('\n')[0].split(';')
        iOrder_ID = head.index("Order_ID")
        iRank_UGO = head.index("Rank_UGO")
        iRank_AGO = head.index("Rank_AGO")
        iAGO_Val = head.index("AGO_Val")

        for l in fichier:
            Order_ID.append(int(l.split('\n')[0].split(';')[iOrder_ID]))
            Rank_UGO.append(int(l.split('\n')[0].split(';')[iRank_UGO]))
            Rank_AGO.append(float(l.split('\n')[0].split(';')[iRank_AGO]))
            AGO_Val.append(
                float(l.split('\n')[0].split(';')[iAGO_Val].replace(',', '.')))

        p = 0
        rows1 = arcpy.UpdateCursor(LineRoutes)
        for line1 in rows1:
            line1.Order_ID = Order_ID[p]
            line1.Rank_UGO = Rank_UGO[p]
            line1.Rank_AGO = Rank_AGO[p]
            line1.AGO_Val = AGO_Val[p]
            rows1.updateRow(line1)
            p += 1

        #/creation of the event table
        PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%",
                                                      "PointEventTEMP", "", "")
        arcpy.AddField_management(PointEventTEMP, "Distance_From_Start",
                                  "DOUBLE", "", "", "", "", "NULLABLE",
                                  "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "",
                                  "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Order_ID", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_AGO", "LONG", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "AGO_Val", "DOUBLE", "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")

        rowslines = arcpy.SearchCursor(LineRoutes)
        rowsevents = arcpy.InsertCursor(PointEventTEMP)
        for line in rowslines:
            tempdistance = float(0)
            while (tempdistance < float(line.Shape_Length)):
                row = rowsevents.newRow()
                row.Distance_From_Start = tempdistance
                row.To_M = tempdistance + float(Distance)
                row.Order_ID = line.Order_ID
                row.Rank_UGO = line.Rank_UGO
                row.Rank_AGO = line.Rank_AGO
                row.AGO_Val = line.AGO_Val
                rowsevents.insertRow(row)
                tempdistance = tempdistance + float(Distance)
        del rowslines
        del rowsevents

        MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(
            LineRoutes, "Rank_AGO", PointEventTEMP,
            "Rank_AGO LINE Distance_From_Start To_M",
            "%ScratchWorkspace%\\MakeRouteEventTEMP")
        Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP,
                                              "%ScratchWorkspace%\\Split", "",
                                              "0", "0", "0")
        arcpy.AddField_management(Split, "Distance", "LONG", "", "", "", "",
                                  "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(Split, "Distance",
                                        "!Distance_From_Start!", "PYTHON_9.3",
                                        "")
        arcpy.DeleteField_management(Split, ["To_M", "Distance_From_Start"])
        Sort = arcpy.Sort_management(
            Split, Output,
            [["Order_ID", "ASCENDING"], ["Rank_UGO", "ASCENDING"],
             ["Rank_AGO", "ASCENDING"], ["Distance", "ASCENDING"]])

        UPD_SL.UpToDateShapeLengthField(Sort)

        #/deleting of the temporary files
        if str(TF) == "true":
            arcpy.Delete_management(Split)
            arcpy.Delete_management(CopyLine)
            arcpy.Delete_management(LineRoutes)
            arcpy.Delete_management(PointEventTEMP)

    return Sort
Esempio n. 11
0
SectionBendPoints = "C:\\PROJECTS\\UMAR\\Default.gdb\\SectionBendPoints"

# Process: Densify
arcpy.Densify_edit(SectionLine, "DISTANCE", Distance, "0.1 Meters", "10")

# Process: Feature Vertices To Points (2)
arcpy.FeatureVerticesToPoints_management(Output_Feature_Class__3_, Output_Feature_Class__6_, "ALL")

# 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")
                insertCursor.insertRow((row[0], int(row[4])))

                currentStation = 0
                while currentStation < row[2]:
                    insertCursor.insertRow((row[0], currentStation * interval))
                    #AddMsgAndPrint("Added: ID: " + str(row[1]) + " -- " + str(currentStation*interval))
                    currentStation += 1
                del insertCursor, currentStation

        # Create Route(s) lyr and define events along each route
        AddMsgAndPrint("\nCreating Stations")
        routes = arcpy.CreateScratchName("routes",
                                         data_type="FeatureClass",
                                         workspace="in_memory")
        arcpy.CreateRoutes_lr(lineTemp, "ID", routes, "TWO_FIELDS", "FROM_PT",
                              "LENGTH_FT", "UPPER_LEFT", "1", "0", "IGNORE",
                              "INDEX")

        stationEvents = arcpy.CreateScratchName("stationEvents",
                                                data_type="FeatureClass",
                                                workspace="in_memory")
        arcpy.MakeRouteEventLayer_lr(routes, "ID", stationTable,
                                     "ID POINT STATION", stationEvents, "",
                                     "NO_ERROR_FIELD", "NO_ANGLE_FIELD",
                                     "NORMAL", "ANGLE", "LEFT", "POINT")
        arcpy.AddField_management(stationEvents, "STATIONID", "TEXT", "", "",
                                  "25", "", "NULLABLE", "NON_REQUIRED")
        arcpy.CalculateField_management(stationEvents, "STATIONID",
                                        "str(!STATION!) + '_' + str(!ID!)",
                                        "PYTHON3")
Esempio n. 13
0
    desc = arcpy.Describe(lineLayer)
    idField = desc.OIDFieldName
    addAndCalc(lineLayer, 'ORIG_FID', '[' + idField + ']')

    #interpolate the line to add z values
    zLine = lineLayer + '_z'
    arcpy.AddMessage('Getting elevation values for the cross-section in  ' +
                     lineLayer)
    arcpy.InterpolateShape_3d(dem, lineLayer, zLine)
    arcpy.AddMessage('   ' + zLine + ' written to ' +
                     arcpy.env.scratchWorkspace)

    #measure it and turn it into a route
    zmLine = lineLayer + '_zm'
    arcpy.AddMessage('Measuring the length of ' + zLine)
    arcpy.CreateRoutes_lr(zLine, 'ORIG_FID', zmLine, 'LENGTH', '#', '#', cp)
    arcpy.AddMessage('   ' + zmLine + ' written to ' +
                     arcpy.env.scratchWorkspace)

    #figure out where the collar elevation is coming from, a user specified field or to be
    #calculated by interpolation from the DEM and stored in 'zDEM'
    if not bhzField == '':
        zField = bhzField
        zBoreholes = bhLayer
    else:
        #interpolate Z values for the boreholes
        # first, select the borehole location points based on the buffer distance
        #to minimize the processing time
        arcpy.SelectLayerByLocation_management(bhLayer, 'WITHIN_A_DISTANCE',
                                               zmLine, buff)
        zBoreholes = outName + '_zBoreholes'
Esempio n. 14
0
	def arcpy_create_routes_linear_refernancing_from(self, arg_input_path, route_id_field="ROAD", start_column="START_TRUE_DIST", end_column="END_TRUE_DIST"):
		arg_input_path = Path(arg_input_path)
		thou_shalt("Create Linear Referanceable Layer {} from {}".format(self.shortened_name_with_context(), arg_input_path.shortened_name_with_context()),
			lambda: arcpy.CreateRoutes_lr(str(arg_input_path), route_id_field, str(self), "TWO_FIELDS", start_column, end_column)
		)
		return self
Esempio n. 15
0
  f = open(outlog_path + str(location) + '_transect_analysis_log.txt','w')
  f.write('Site: ' + str(location) + '\n')
  f.write('site | year | transect | distance\n')
  
  #Check to see if the output table already exists (delete it, if it does)
  if arcpy.Exists(out_table):
      arcpy.Delete_management(out_table)
  create_out_table(path, location)
  
  # Transects for each individual research site
  transects = path + str(location) + '_transects'
      
  # Convert starting points of transects to point feature class
  if arcpy.Exists(temproute):
      arcpy.Delete_management(temproute)
  arcpy.CreateRoutes_lr(transects, "TRANSECT_ID", temproute, "LENGTH", coordinate_priority=alcona_dir)
 
  # List of years in site to analyze from
  #years = alcona_years
  
  # Loop through all the years for any given site
  for year in years:
      toshore = path + location + '_shoreline_' + str(year)
  
      if arcpy.Exists(toshore):
          print str(location) + ' - ' + str(year)
          
          # Create search cursor to insert new data into the table
          ins_cur = arcpy.InsertCursor(out_table)
          
          # Convert shoreline verticies to points to calculate minimum and mean distances
Esempio n. 16
0
def line_route_points(output_workspace, line, station_distance, route_id_field,
                      fields):
    """
    Converts an input line feature class into a route, densifies its vertices, 
    and ctreates a point feature class in the output_workspace. 
    
    Writes all outputs to the specified output_workspace. 
    
    Args:
    output_workspace  -- Path to the output workspace.
    line              -- Path to a line feature class
    station_distance  -- (numeric) Distance between output line station points 
                         (in the linear units of the line feature class)
    route_id_field    -- (string) of the field name of the route identifier
    fields            -- (string) a list of fields from the line feature class
                         to join to the output line_points feature class (e.g., 
                         ["bank","ReachName"]). 
    
    Outputs:
    <line>_points     -- a point feature class named the same as the line 
                         input feature class written to the output_workspace
    """
    # Set line name
    line_name = arcpy.Describe(line).baseName

    # 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(line)]
    if "from_measure" not in field_names:
        arcpy.AddField_management(in_table=line,
                                  field_name="from_measure",
                                  field_type="DOUBLE")

    # Set the value of the line `from_measure` to zero in units meters
    arcpy.CalculateField_management(in_table=line,
                                    field="from_measure",
                                    expression="0",
                                    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=line,
                                  field_name="to_measure",
                                  field_type="DOUBLE")

    # Set the value of the line `to_measure` to the length of the
    # line in units meters
    arcpy.CalculateField_management(in_table=line,
                                    field="to_measure",
                                    expression="!shape.length@meters!",
                                    expression_type="PYTHON_9.3")
    arcpy.AddMessage("Added route 'from_measure' and 'to_measure' fields to " +
                     str(line_name))

    # Densify vertices of the line feature class using the Densify tool.
    line_densify_name = line_name + "_densify"
    line_densify = os.path.join(output_workspace, line_densify_name)
    arcpy.CopyFeatures_management(in_features=line,
                                  out_feature_class=line_densify)
    arcpy.Densify_edit(in_features=line_densify,
                       densification_method="DISTANCE",
                       distance=station_distance)

    # Convert the line feature class to a route
    line_densify_route_name = line_name + "_densify_route"
    line_densify_route = os.path.join(output_workspace,
                                      line_densify_route_name)
    arcpy.CreateRoutes_lr(in_line_features=line_densify,
                          route_id_field=route_id_field,
                          out_feature_class=line_densify_route,
                          measure_source="TWO_FIELDS",
                          from_measure_field="from_measure",
                          to_measure_field="to_measure")
    arcpy.AddMessage("Converted " + str(line_name) + " to a route")

    # Convert line feature vertices to points
    line_points_name = line_name + "_points"
    line_points = os.path.join(output_workspace, line_points_name)
    arcpy.FeatureVerticesToPoints_management(in_features=line_densify_route,
                                             out_feature_class=line_points)
    arcpy.AddMessage("Converted " + str(line_name) + " to points")

    # Add x, y, z, and m values to the `line_points` feature class
    arcpy.AddGeometryAttributes_management(Input_Features=line_points,
                                           Geometry_Properties="POINT_X_Y_Z_M",
                                           Length_Unit="METERS")

    # Set the first m-value for each line to zero. The `create route`
    # tool sets the first record to NULL.
    mNull_wc = """{0} IS NULL""".format(
        arcpy.AddFieldDelimiters(line_points, "POINT_M"))

    with arcpy.da.UpdateCursor(line_points, "POINT_M",
                               where_clause=mNull_wc) as cursor:
        for row in cursor:
            row[0] = 0
            cursor.updateRow(row)

    # Delete un-needed fields
    arcpy.DeleteField_management(in_table=line_points,
                                 drop_field=["ORIG_FID", "POINT_Z"])

    # Join fields from the `line` to the `line_points` feature class
    arcpy.JoinField_management(in_data=line_points,
                               in_field=route_id_field,
                               join_table=line,
                               join_field=route_id_field,
                               fields=fields)
    arcpy.AddMessage("Joined fields from " + str(line_name))

    # Cleanup
    arcpy.Delete_management(in_data=line_densify)
    arcpy.Delete_management(in_data=line_densify_route)

    arcpy.AddMessage("Converted " + str(line_name) + " to points")

    # Return the path to the feature class
    return line_points
Esempio n. 17
0
def XSCreateStationPoints(output_workspace, cross_section, dem, dem_units,
                          detrend_dem, station_distance):
    # 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("Cross Section: "
                     "{}".format(arcpy.Describe(cross_section).baseName))
    arcpy.AddMessage("DEM: {}".format(arcpy.Describe(dem).baseName))
    arcpy.AddMessage("DEM vertical units: {}".format(dem_units))
    if detrend_dem is True:
        arcpy.AddMessage("Detrended DEM: "
                         "{}".format(arcpy.Describe(detrend_dem).baseName))
    arcpy.AddMessage("Station distance: {0}".format(str(station_distance)))

    # Set cross_section name
    xs_name = arcpy.Describe(cross_section).baseName

    # 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(cross_section)]
    if "from_measure" not in field_names:
        arcpy.AddField_management(in_table=cross_section,
                                  field_name="from_measure",
                                  field_type="DOUBLE")

    # Set the value of the cross section `from_measure` to zero in units meters
    arcpy.CalculateField_management(in_table=cross_section,
                                    field="from_measure",
                                    expression="0",
                                    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=cross_section,
                                  field_name="to_measure",
                                  field_type="DOUBLE")

    # Set value of xs `to_measure` to length of xs in units meters
    arcpy.CalculateField_management(in_table=cross_section,
                                    field="to_measure",
                                    expression="!shape.length@meters!",
                                    expression_type="PYTHON_9.3")

    # Densify vertices of the cross_section fc
    arcpy.AddMessage("Densifying cross section vertices...")
    xs_densify = os.path.join(output_workspace, xs_name + "_densify")
    arcpy.CopyFeatures_management(in_features=cross_section,
                                  out_feature_class=xs_densify)
    arcpy.Densify_edit(in_features=xs_densify,
                       densification_method="DISTANCE",
                       distance=station_distance)

    # Convert the cross_section fc to a route
    arcpy.AddMessage("Creating cross section routes...")
    xs_densify_route = os.path.join(output_workspace,
                                    xs_name + "_densify_route")
    arcpy.CreateRoutes_lr(in_line_features=xs_densify,
                          route_id_field="Seq",
                          out_feature_class=xs_densify_route,
                          measure_source="TWO_FIELDS",
                          from_measure_field="from_measure",
                          to_measure_field="to_measure")

    # Convert cross section feature vertices to points
    arcpy.AddMessage("Converting cross section vertices to points...")
    xs_points = os.path.join(output_workspace, xs_name + "_points")
    arcpy.FeatureVerticesToPoints_management(in_features=xs_densify_route,
                                             out_feature_class=xs_points)

    # Add x, y, z, and m values to the `cross_section_points` feature class
    arcpy.AddGeometryAttributes_management(Input_Features=xs_points,
                                           Geometry_Properties="POINT_X_Y_Z_M",
                                           Length_Unit="METERS")

    # Set the first m-value for each xs to zero (because the `create route`
    # tool sets it to NULL).
    arcpy.AddMessage("Setting XS NULL m-values to zero...")
    arcpy.CalculateField_management(in_table=xs_points,
                                    field="POINT_M",
                                    expression="setNull2Zero(!POINT_M!)",
                                    code_block="""def setNull2Zero(m):
                                                        if m is None: 
                                                            return 0
                                                        else:
                                                            return m""",
                                    expression_type="PYTHON_9.3")

    # Delete un-needed fields
    arcpy.DeleteField_management(in_table=xs_points,
                                 drop_field=["ORIG_FID", "POINT_Z"])

    # Add a field to hold the linear referencing `route_units`
    arcpy.AddField_management(in_table=xs_points,
                              field_name="POINT_M_units",
                              field_type="TEXT")

    # Set the `route_units` field to "meter"
    arcpy.CalculateField_management(in_table=xs_points,
                                    field="POINT_M_units",
                                    expression="'m'",
                                    expression_type="PYTHON_9.3")

    # Join fields from the `cross_section` fc to `cross_section_points` fc
    fields = ["ReachName", "Watershed_Area_SqMile", "km_to_mouth"]
    arcpy.JoinField_management(in_data=xs_points,
                               in_field="Seq",
                               join_table=cross_section,
                               join_field="Seq",
                               fields=fields)

    # Add elevations to the `cross_section_points` feature class
    arcpy.AddMessage("Adding DEM surface information...")

    ## DEM
    arcpy.AddMessage("DEM: {}".format(dem))
    arcpy.AddSurfaceInformation_3d(in_feature_class=xs_points,
                                   in_surface=dem,
                                   out_property="Z",
                                   z_factor=1.0)

    ## Change `Z` field name to `DEM_Z`
    arcpy.AlterField_management(in_table=xs_points,
                                field="Z",
                                new_field_name="DEM_Z")

    ## Create and set the value of the dem_units field
    arcpy.AddField_management(in_table=xs_points,
                              field_name="dem_units",
                              field_type="TEXT")
    arcpy.CalculateField_management(in_table=xs_points,
                                    field="dem_units",
                                    expression="'{}'".format(dem_units),
                                    expression_type="PYTHON_9.3")

    ## Detrend
    if detrend_dem:
        arcpy.AddMessage("Detrend DEM: {}".format(detrend_dem))
        ## Add detrended elevations to the `cross_section_points` feature class
        arcpy.AddSurfaceInformation_3d(in_feature_class=xs_points,
                                       in_surface=detrend_dem,
                                       out_property="Z",
                                       z_factor=1.0)

        ## Change `Z` field name to `Detrend_DEM_Z`
        arcpy.AlterField_management(in_table=xs_points,
                                    field="Z",
                                    new_field_name="Detrend_DEM_Z")

    # Return
    arcpy.SetParameter(6, xs_points)

    # Cleanup
    arcpy.Delete_management(in_data=xs_densify)
    arcpy.Delete_management(in_data=xs_densify_route)
    return
    "Segmentation of the inFC according to the inflection points - Step " +
    str(ncurrentstep) + "/" + str(nstep))
CopyinFC = arcpy.CopyFeatures_management(inFC, "%ScratchWorkspace%\\CopyinFC")

UPD_SL.UpToDateShapeLengthField(CopyinFC)

arcpy.AddField_management(CopyinFC, "Start", "DOUBLE", "", "", "", "",
                          "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(CopyinFC, "End", "DOUBLE", "", "", "", "",
                          "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(CopyinFC, "Start", "0", "PYTHON_9.3", "")
arcpy.CalculateField_management(CopyinFC, "End", "!Shape_Length!",
                                "PYTHON_9.3", "")

Routes = arcpy.CreateRoutes_lr(CopyinFC, "Rank_UGO",
                               "%ScratchWorkspace%\\Routes", "TWO_FIELDS",
                               "Start", "End")
UPD_SL.UpToDateShapeLengthField(Routes)

LocateTABLE = arcpy.LocateFeaturesAlongRoutes_lr(
    InflectionPts, Routes, "Rank_UGO", "1", "%ScratchWorkspace%\\LocateTABLE",
    "RID POINT MEAS")
arcpy.LocateFeaturesAlongRoutes_lr(InflectionPts, Routes, "Rank_UGO", "1",
                                   "%ScratchWorkspace%\\LocateTABLE1",
                                   "RID POINT MEAS")

rows1 = arcpy.UpdateCursor(LocateTABLE)
rows2 = arcpy.UpdateCursor(LocateTABLE)
rows3 = arcpy.SearchCursor(Routes)
line2 = next(rows2)
nrows = int(str(arcpy.GetCount_management(LocateTABLE)))
Esempio n. 19
0
def SLEM(Line, Distance, Output, TF):
    
    CopyLine = arcpy.CopyFeatures_management(Line, r"in_memory\CopyLine")
    
    fieldnames = [f.name for f in arcpy.ListFields(CopyLine)]

    #/identification of the polyline type : raw, UGOs, sequenced UGOs, or AGOs
    k = 0
    if "Rank_AGO" in fieldnames :
        k = 3
    elif "Order_ID" in fieldnames :
        k = 2
    elif "Rank_UGO" in fieldnames :
        k = 1
            

    ################################
    ########## Raw polyline ########
    ################################
    if k == 0 :
        
        #/shaping of the segmented result
        arcpy.AddField_management(CopyLine, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "Rank_UGO", "!"+fieldnames[0]+"!", "PYTHON_9.3", "")
        arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "PYTHON_9.3", "")
        arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.CalculateField_management(CopyLine, "To_Measure", "!shape.length!", "PYTHON_9.3", "")
        
        #/conversion in routes
        LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO", r"in_memory\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure")

        #/creation of the event table
        PointEventTEMP = arcpy.CreateTable_management("in_memory", "PointEventTEMP", "", "")
        arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        
        UPD_SL.UpToDateShapeLengthField(LineRoutes)

        rowslines = arcpy.SearchCursor(LineRoutes)
        rowsevents = arcpy.InsertCursor(PointEventTEMP)
        for line in rowslines:
            tempdistance = float(line.Shape_Length)
            while (tempdistance > float(0)):
                row = rowsevents.newRow()
                row.Rank_UGO = line.Rank_UGO
                row.To_M = max(0, tempdistance - float(Distance))
                row.Distance = tempdistance
                rowsevents.insertRow(row)
                tempdistance = tempdistance - float(Distance)
        del rowslines
        del rowsevents

        #/creation of the route event layer
        MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(LineRoutes, "Rank_UGO", PointEventTEMP,
                                                         "Rank_UGO LINE Distance To_M",
                                                         r"in_memory\MakeRouteEventTEMP")
        Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, r"in_memory\Split", "", "0", "0", "0")
        Sort = arcpy.Sort_management(Split, Output, [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]])

        arcpy.DeleteField_management(Sort, "To_M")
        
        #/calculation of the "Distance" field
        UPD_SL.UpToDateShapeLengthField(Sort)
        
        rows1 = arcpy.UpdateCursor(Sort)
        rows2 = arcpy.UpdateCursor(Sort)
        line2 = rows2.next()
        line2.Distance = 0
        rows2.updateRow(line2)
        nrows = int(str(arcpy.GetCount_management(Sort)))
        n = 0
        for line1 in rows1 :
            line2 = rows2.next()          
            if n == nrows-1 :
                break
            if n == 0 :
                line1.Distance = 0
            if line2.Rank_UGO == line1.Rank_UGO :
                line2.Distance = line1.Distance + line1.Shape_Length
                rows2.updateRow(line2)
            if line2.Rank_UGO != line1.Rank_UGO :
                line2.Distance = 0
                rows2.updateRow(line2)
            
            n+=1
        
        #/deleting of the temporary files
        if str(TF) == "true" :
            arcpy.Delete_management(Split)
            arcpy.Delete_management(CopyLine)
            arcpy.Delete_management(LineRoutes)
            arcpy.Delete_management(PointEventTEMP)
    
    return Sort
Esempio n. 20
0
    ##        raise SystemError

    #environment variables
    arcpy.env.overwriteOutput = True
    scratchDir = arcpy.env.scratchWorkspace
    arcpy.env.workspace = scratchDir

    #add an rkey field to the table that consists of values from the OID
    desc = arcpy.Describe(xsecLayer)
    idField = desc.OIDFieldName
    addAndCalc(xsecLayer, 'ORIG_FID', '[' + idField + ']')

    #measure the cross section line
    mLine = xsecName + '_m'
    arcpy.AddMessage('Measuring the length of the cross section')
    arcpy.CreateRoutes_lr(xsecLayer, 'ORIG_FID', mLine, 'LENGTH', '#', '#', cp)
    arcpy.AddMessage('    ' + mLine + ' written to ' + scratchDir)

    #intersect the cross section with the lines layer
    #creates a table with only the original FIDs of the input features.
    #FID field is named FID_<fcname> so we can find it later to transfer attributes
    #otherwise, we get a ton of fields, which may be redundant if the same feature class
    #(with different selections or definition queries) is being passed for both
    #the cross-section layer and the lines layer, which was the case when I was developing,
    #that is, I was looking for intersections between the main cross-section line and
    #all other cross-sections
    intersectPts = outName + '_interPts'
    inList = linesLayer + ';' + xsecLayer
    arcpy.AddMessage(
        'Intersecting lines in {} with the line of cross-section'.format(
            linesLayer))
Esempio n. 21
0
arcpy.CopyFeatures_management('roadstraffic_lyr', roadstraffic_avg)
arcpy.Delete_management('roadstraffic_lyr')

#-----------------------------------------------------------------------------------------------------------------------
# INTERPOLATE
#   After joining the two sources, AADT values are interpolated for missing segments based on linear inverse distance
#   weighting from road segments with the same name.
#-----------------------------------------------------------------------------------------------------------------------

#Interpolate for designated lines
print('Interpolate AADT values along roads based on inverse distance weighting along lines')
arcpy.MakeFeatureLayer_management(roadstraffic_avg, 'roadstraffic_lyr')
arcpy.SelectLayerByAttribute_management('roadstraffic_lyr', 'NEW_SELECTION',
                                        "NOT (ARTDESCRIP = ' ' OR ARTDESCRIP = 'Not Designated')")
routes = 'roadstraffic_route'
arcpy.CreateRoutes_lr('roadstraffic_lyr', route_id_field='STNAME_ORD', out_feature_class=routes, measure_source='LENGTH',
                      ignore_gaps='NO_IGNORE')
arcpy.LocateFeaturesAlongRoutes_lr('roadstraffic_lyr', routes, route_id_field = 'STNAME_ORD', out_table= 'routes_loc')
#For each line with a designated type and no AADT, get the AADT of the closest segment on both sides on the same street name
arcpy.AddField_management('routes_loc', field_name='AADT_interp', field_type='double')
interpdicplus = defaultdict(list)
interpdicminus = defaultdict(list)
[f.name for f in arcpy.ListFields('routes_loc')]
with arcpy.da.UpdateCursor('routes_loc', ['CUSTOM_ID','STNAME_ORD','FMEAS', 'ARTDESCRIP', 'AADT_avg','AADT_interp']) as cursorOuter:
    for rowOuter in cursorOuter:
        if rowOuter[4] is None: #If AADT is null
            print(rowOuter[0])
            #For each segment of artierial street
            with arcpy.da.SearchCursor('routes_loc',['CUSTOM_ID', 'STNAME_ORD', 'FMEAS', 'ARTDESCRIP', 'AADT_avg']) as cursorInner:
                for rowInner in cursorInner:
                    #Loop over segments of the same street that have a non-zero AADT value
                    if rowOuter[1] == rowInner[1] and rowInner[4] > 0:
Esempio n. 22
0
def StreamNetworkPoints(output_workspace, stream_network, flow_accum, dem):
    # Check out the extension license
    arcpy.CheckOutExtension("Spatial")

    # Set environment variables
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = output_workspace

    # Get spatial reference of FAC
    spatial_ref = arcpy.Describe(flow_accum).spatialReference

    # List parameter values
    arcpy.AddMessage("Workspace: {}".format(arcpy.env.workspace))
    arcpy.AddMessage("Stream Network: "
                     "{}".format(arcpy.Describe(stream_network).baseName))
    arcpy.AddMessage("Flow Accumulation Model: "
                     "{}".format(arcpy.Describe(flow_accum).baseName))
    arcpy.AddMessage("Digital Elevation Model: "
                     "{}".format(arcpy.Describe(dem).baseName))

    ## Convert stream_network fc to a route
    # 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(stream_network)]
    if "from_measure" not in field_names:
        arcpy.AddField_management(in_table=stream_network,
                                  field_name="from_measure",
                                  field_type="DOUBLE")

    # Set the value of the cross section `from_measure` to zero in units meters
    arcpy.CalculateField_management(in_table=stream_network,
                                    field="from_measure",
                                    expression="!shape.length@kilometers!",
                                    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=stream_network,
                                  field_name="to_measure",
                                  field_type="DOUBLE")

    # Set the value of the cross section `to_measure` to the length of the
    # stream_network in units kilometers
    arcpy.CalculateField_management(in_table=stream_network,
                                    field="to_measure",
                                    expression="0",
                                    expression_type="PYTHON_9.3")

    # Convert stream_network fc into a route
    stream_network_route = os.path.join(output_workspace,
                                        "stream_network_route")
    arcpy.CreateRoutes_lr(in_line_features=stream_network,
                          route_id_field="ReachName",
                          out_feature_class=stream_network_route,
                          measure_source="TWO_FIELDS",
                          from_measure_field="from_measure",
                          to_measure_field="to_measure")
    arcpy.AddMessage("Stream network route created")
    # Convert stream network feature vertices to points
    stream_network_points = os.path.join(output_workspace,
                                         "stream_network_points")
    arcpy.FeatureVerticesToPoints_management(
        in_features=stream_network_route,
        out_feature_class=stream_network_points)
    arcpy.AddMessage("Converted the stream network to points")

    # Add x, y, z, and m values to the `cross_section_points` feature class
    arcpy.AddGeometryAttributes_management(
        Input_Features=stream_network_points,
        Geometry_Properties="POINT_X_Y_Z_M",
        Length_Unit="KILOMETERS")

    # Set the first m-value for each stream network to zero. The `create route`
    # tool sets it to NULL.
    arcpy.CalculateField_management(in_table=stream_network_points,
                                    field="POINT_M",
                                    expression="setNull2Zero(!POINT_M!)",
                                    code_block="""def setNull2Zero(m):
                                                        if m is None: 
                                                            return 0
                                                        else:
                                                            return m""",
                                    expression_type="PYTHON_9.3")

    # Delete un-needed fields
    arcpy.DeleteField_management(in_table=stream_network_points,
                                 drop_field=["ORIG_FID", "POINT_Z"])
    arcpy.AddMessage("Added stream network route lengths")

    # Add flow accumulation values to the stream_network_points fc
    arcpy.sa.ExtractMultiValuesToPoints(
        in_point_features=stream_network_points,
        in_rasters=[flow_accum],
        bilinear_interpolate_values="NONE")
    # Add a field to to the stream_network_points fc to hold watershed area
    # Check if the field already exists and if not add it
    field_names = [f.name for f in arcpy.ListFields(stream_network_points)]
    if "Watershed_Area_SqMile" not in field_names:
        arcpy.AddField_management(in_table=stream_network_points,
                                  field_name="Watershed_Area_SqMile",
                                  field_type="DOUBLE")
    # Convert flow accumulation cell counts to area in square miles
    cell_size = str(
        arcpy.GetRasterProperties_management(flow_accum, "CELLSIZEX"))
    # Expression to convert sq m to sq mi: 1 sq m = 0.0000003861 sq mile
    meters_sqmi = ("((float({0}) * float({0})) * 0.0000003861) * "
                   "!{1}!".format(cell_size,
                                  arcpy.Describe(flow_accum).baseName))

    if spatial_ref.linearUnitName == "Meter":
        arcpy.CalculateField_management(in_table=stream_network_points,
                                        field="Watershed_Area_SqMile",
                                        expression=meters_sqmi,
                                        expression_type="PYTHON_9.3")
    else:
        # Error
        arcpy.AddError("    Watershed linear unit not recognized."
                       " Area not calculated")
    # Delete un-needed fields
    arcpy.DeleteField_management(
        in_table=stream_network_points,
        drop_field=[arcpy.Describe(flow_accum).baseName])
    arcpy.AddMessage("Added stream network drainage area")

    # Add elevation values to the stream_network_points fc
    arcpy.sa.ExtractMultiValuesToPoints(
        in_point_features=stream_network_points,
        in_rasters=[dem],
        bilinear_interpolate_values="NONE")
    arcpy.AlterField_management(in_table=stream_network_points,
                                field=arcpy.Describe(dem).baseName,
                                new_field_name="Z",
                                new_field_alias="Z")
    arcpy.AddMessage("Added stream network elevations")

    # Return
    arcpy.SetParameter(4, stream_network_points)

    # Cleanup
    arcpy.Delete_management(in_data=stream_network_route)
Esempio n. 23
0
            pass
##   check for Z and M values
desc = arcpy.Describe(tempXsLine)
if desc.hasZ and desc.hasM:
    ZMline = tempXsLine
else:
    #Add Z values
    addMsgAndPrint('    getting elevation values for ' + shortName(tempXsLine))
    Zline = arcpy.CreateScratchName('xx', outFdsTag + '_Z', 'FeatureClass',
                                    scratch)
    arcpy.InterpolateShape_3d(dem, tempXsLine, Zline)
    #Add M values
    addMsgAndPrint('    measuring ' + shortName(Zline))
    ZMline = arcpy.CreateScratchName('xx', outFdsTag + '_ZM', 'FeatureClass',
                                     scratch)
    arcpy.CreateRoutes_lr(Zline, idField, ZMline, 'LENGTH', '#', '#',
                          startQuadrant)
## buffer line to get selection polygon
addMsgAndPrint('    buffering ' + shortName(tempXsLine) +
               ' to get selection polygon')
tempBuffer = arcpy.CreateScratchName('xx', outFdsTag + "xsBuffer",
                                     'FeatureClass', scratch)
arcpy.Buffer_analysis(ZMline, tempBuffer, bufferDistance, 'FULL', 'FLAT')

## get lists of feature classes to be projected
lineFCs = []
polyFCs = []
pointFCs = []
if projectAll:
    oldws = arcpy.env.workspace
    arcpy.env.workspace = gdb + '/GeologicMap'
    linefc = arcpy.ListFeatureClasses('*', 'Line')
    if type == "A1":
        return "AA001"
    elif type == "A2":
        return "A002"
    elif type == "A3":
        return "A003"
    elif type == "A4":
        return "A004"
    elif type == "A5":
        return "A005"
    elif type == "A6":
        return "A006"
    elif type == "A7":
        return "A007"
    else:
        return type"""


# Execute AddField
arcpy.AddField_management(roadsfull, "ROADT", "TEXT")
 
# Execute CalculateField 
arcpy.CalculateField_management(roadsfull, "ROADT", expression, "PYTHON", codeblock) 


rid = "ROADT" 
out_routes = "SLRoutes" 

# Execute CreateRoutes
arcpy.CreateRoutes_lr(roadsfull, rid, out_routes, "LENGTH", "#", "#",  "LOWER_LEFT")
Esempio n. 25
0
    count_pts = int(result.getOutput(0))

    # select segments that cross the area of interest
    arcpy.SelectLayerByLocation_management(track_name, "INTERSECT", sel_layer)
    result = arcpy.GetCount_management(track_name)
    count_path = int(result.getOutput(0))
    if count_path > 0:
        # save selection to a temp polyline class
        _select = 'in_memory\\sel_segments'
        arcpy.Select_analysis(track_name, _select)
        # create route from selection
        route_name = 'in_memory\\rt_' + track_name[3:]
        arcpy.CreateRoutes_lr(
            track_name,
            "name",
            route_name,  # MUST use trajectory!!
            "TWO_FIELDS",
            "from_loc",
            "to_loc")
        # extract FromLocation, ToLocation from temp points
        sql_clause = (None, 'ORDER BY "from_loc" ASC')
        fields = ("from_loc", "to_loc", "name")
        with arcpy.da.SearchCursor(_select, fields, None, sr, None,
                                   sql_clause) as sCur:
            row = sCur.next()
            startPath, endPath, ptt = row[0:4]
            for row in sCur:
                if not row == None:
                    pass
            endPath = row[1]
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")
UPD_SL.UpToDateShapeLengthField(BezierRoutes)

arcpy.AddMessage("    Locate inflection points on Bezier routes - 6/9")
InflectionPtTABLE = arcpy.LocateFeaturesAlongRoutes_lr(PtsForSplitting, BezierRoutes, "Rank_UGO", "1", "%ScratchWorkspace%\\InflectionPtTABLE", "RID POINT MEAS")


#/correction of the table : some errors have occured at confluences
rows1 = arcpy.UpdateCursor(InflectionPtTABLE)
rows2 = arcpy.UpdateCursor(InflectionPtTABLE)
rows3 = arcpy.SearchCursor(BezierRoutes)
line2 = next(rows2)
nrows = int(str(arcpy.GetCount_management(InflectionPtTABLE)))
    # Each inflection point has a MEAS field (distance from upstream)
Esempio n. 27
0
    idField = desc.OIDFieldName
    addAndCalc(lineLayer, 'ORIG_FID', '[' + idField + ']')

    #interpolate the line to add z values
    zLine = lineLyrName + '_z'
    arcpy.AddMessage('Getting elevation values for the cross-section in ' +
                     lineLyrName)
    arcpy.InterpolateShape_3d(dem, lineLayer, zLine)
    arcpy.AddMessage('   ' + zLine + ' written to ' +
                     arcpy.env.scratchWorkspace)

    #measure it and turn it into a route
    zmLine = lineLyrName + '_zm'
    if arcpy.Exists(zmLine): arcpy.Delete_management(zmLine)
    arcpy.AddMessage('Measuring the length of ' + zLine)
    arcpy.CreateRoutes_lr(zLine, 'ORIG_FID', zmLine, 'LENGTH', '#', '#', cp)
    arcpy.AddMessage('   ' + zmLine + ' written to ' +
                     arcpy.env.scratchWorkspace)

    #select points according to the section distance
    arcpy.SelectLayerByLocation_management(ptLayer, 'WITHIN_A_DISTANCE',
                                           zmLine, buff)
    zPts = outName + '_z'

    #figure out where the point elevation is coming from: a user specified field or to be
    #calculated by interpolation from the DEM and stored in 'zDEM'
    if not ptZField == '':
        arcpy.AddMessage('Using elevations stored in the field ' + ptZField)
        #if the elevation values are in the table, copy the selection to an
        #output fc
        zField = ptZField
Esempio n. 28
0
        #        arcpy.AddMessage(count_pts)

        # select segments that cross the area of interest
        arcpy.SelectLayerByLocation_management(track_name, "INTERSECT",
                                               sel_layer)
        result = arcpy.GetCount_management(track_name)
        count_path = int(result.getOutput(0))

        if count_path > 0:
            # save selection to a temp polyline class
            _select = 'in_memory\\sel_segments'
            arcpy.Select_analysis(track_name, _select)
            # create route from selection
            route_name = 'in_memory\\rt_' + track_name[3:]
            # ASSUMES track created by trajectory.py !!
            arcpy.CreateRoutes_lr(track_name, "ptt", route_name, "TWO_FIELDS",
                                  "FromLoc", "ToLoc")
            #            arcpy.CreateRoutes_lr(track_name, "name", route_name,
            #                                    "TWO_FIELDS", "from_loc", "to_loc")
            arcpy.AddMessage('Route created')

            # extract First & Last time reference from temp points
            sql_clause = (None, 'ORDER BY "from_loc" ASC')
            #            fields = ("from_loc", "to_loc", "name")
            fields = ("FromLoc", "ToLoc", "ptt")
            with arcpy.da.SearchCursor(_select, fields, None, sr, None,
                                       sql_clause) as sCur:
                row = sCur.next()
                startPath, endPath, ptt = row[0:4]
                for row in sCur:
                    if not row == None:
                        pass
Esempio n. 29
0
def crashReportLRS(GDBspot, csv, fatalwt, seriouswt, nonseriouswt, possiblewt,
                   IntersectionThreshold, SegmentThreshold):
    # workspace= "Z:/fullerm/Safety Locations/Safety.gdb"

    # Input parameters
    # GCAT file/location
    GCATfile = csv  # csv created after mapping fields with schemaCleaner
    # Intermediate file/location?

    # Intersection polygon file/location
    # IntersectionFeatures = arcpy.GetParameterAsText(1)
    # IntersectionFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/Fed_Aid_2010_LucWoo_Intersection_Buffer_Dissolve"
    IntersectionFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/LMW_intersection_250ft_buffer_5Jul2017_3857"
    psFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/CS_IP_Merge_copy_clip_16june2017_LMW_3857"
    psThreshold = str(0)
    countyFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/County_FLOWHHMS_Clipped_3857"
    # Segment polygon file/location
    # SegmentFeatures = arcpy.GetParameterAsText(2)
    SegmentFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/LMW_segments_70ft_buffer_5Jul2017_3857"
    # output file name/location for the spatial join
    # GDBspot = arcpy.GetParameterAsText(1)  # user input location for gdb and result excel tables

    # psThreshold = arcpy.GetParameterAsText(8)
    # output file name/location for excel table
    # TableFolder = arcpy.GetParameterAsText(4)
    # rdinv = arcpy.GetParameterAsText(8)
    rdinv = "C:/Users/fullerm/Documents/ArcGIS/Projects/Safety Report Script/Safety Report Script.gdb/Road_Inventory_CopyFeatures"
    # create geodatabase
    TimeDate = datetime.now()
    TimeDateStr = "CrashLocations" + TimeDate.strftime('%Y%m%d%H%M') + "_LRS"

    outputGDB = arcpy.CreateFileGDB_management(GDBspot, TimeDateStr)
    arcpy.env.workspace = str(outputGDB).replace('/', '\\')
    # I kept getting errors because arcmap sets the field type based on first dozen features and some ids were numeric
    '''fldmppng = arcpy.FieldMappings()
    fldmppng.addTable(GCATfile)

    nameFI = fldmppng.findFieldMapIndex("LOCAL_REPORT_NUMBER_ID")
    fldmp = fldmppng.getFieldMap(nameFI)
    fld = fldmp.outputField

    fld.name = "LOCAL_REPORT_NUMBER_ID"
    fld.aliasName = "LOCAL_REPORT_NUMBER_ID"
    fld.type = "String"
    fldmp.outputField = fld
    fldmppng.replaceFieldMap(nameFI,fldmp)'''
    # convert GCAT txt file to gdb table and add to map
    NewTable = arcpy.TableToTable_conversion(GCATfile, outputGDB, "OHMI_data")
    arcpy.TableSelect_analysis(NewTable, "oh_table",
                               '"NLF_COUNTY_CD" <> \'Monroe\' ')
    ohTable = arcpy.CopyRows_management("oh_table", "ohTable")
    arcpy.TableSelect_analysis(NewTable, "mi_table",
                               '"NLF_COUNTY_CD" = \'Monroe\' ')
    miTable = arcpy.CopyRows_management('mi_table', 'miTable')
    # arcpy.SelectLayerByAttribute_management(NewTable,"CLEAR_SELECTION")
    rdlyr = arcpy.MakeFeatureLayer_management(rdinv, "rdlyr")
    rtloc = os.path.join(
        GDBspot, "Road_Inventory3456_CreateRoutes" + TimeDateStr + ".shp")
    lrs = arcpy.CreateRoutes_lr(rdlyr, "NLF_ID", rtloc, "TWO_FIELDS",
                                "CTL_BEGIN", "CTL_END")
    event_props = "NLFID POINT COUNTY_LOG_NBR"
    PointFile = arcpy.MakeRouteEventLayer_lr(lrs, "NLF_ID", ohTable,
                                             event_props, "Crash_Events")
    # creating this extra feature class and working from it instead of the event layer
    # decreased script tool runtime from ~8 min to ~2 min
    arcpy.SelectLayerByAttribute_management(PointFile, "clear_selection")

    pointOH = arcpy.FeatureClassToFeatureClass_conversion(
        PointFile, outputGDB, "GCAT_LUCWOO_lrs_points_" + TimeDateStr)
    # pointOH = arcpy.CopyFeatures_management(PointFile, "LRS_Events_copy")
    mi_points = milocationsxy(miTable, outputGDB)
    pointcopy = arcpy.Merge_management([pointOH, mi_points], 'miohpointsmerge')

    dict = {
        'fatalities_count':
        "ODPS_TOTAL_FATALITIES_NBR<>0",
        'incapac_inj_count':
        "Incapac_injuries_NBR<>0 and ODPS_TOTAL_FATALITIES_NBR=0",
        'non_incapac_inj_count':
        "non_incapac_injuries_NBR<>0 and ODPS_TOTAL_FATALITIES_NBR=0 and incapac_injuries_nbr=0",
        'possible_inj_count':
        "possible_injuries_nbr<>0 and ODPS_TOTAL_FATALITIES_NBR=0 and non_incapac_injuries_nbr=0 and incapac_injuries_nbr=0"
    }
    fld_lst = [
        'SEVERITY_BY_TYPE_CD', 'fatalities_count', 'incapac_inj_count',
        'non_incapac_inj_count', 'possible_inj_count'
    ]

    # add fields for point layer

    for key in dict:
        arcpy.AddField_management(pointcopy, key, "LONG")
        '''arcpy.SelectLayerByAttribute_management(PointFile, "NEW_SELECTION", dict[key])
        arcpy.CalculateField_management(PointFile, key, 1)
        arcpy.SelectLayerByAttribute_management(PointFile, "Switch_selection")
        arcpy.CalculateField_management(PointFile, key, 0)'''
    # fillCountFields(pointcopy, fld_lst)
    with arcpy.da.UpdateCursor(pointcopy, fld_lst) as cursor:
        for row in cursor:
            if row[0] == 'Fatal Crashes':
                row[1] = 1
                row[2] = 0
                row[3] = 0
                row[4] = 0
            elif row[0] == 'Incapacitating Injury Crashes':
                row[1] = 0
                row[2] = 1
                row[3] = 0
                row[4] = 0
            elif row[0] == 'Non-Incapacitating Injury Crashes':
                row[1] = 0
                row[2] = 0
                row[3] = 1
                row[4] = 0
            elif row[0] == 'Possible Injury Crashes':
                row[1] = 0
                row[2] = 0
                row[3] = 0
                row[4] = 1
            else:
                row[1] = 0
                row[2] = 0
                row[3] = 0
                row[4] = 0
            cursor.updateRow(row)

    # Clear Selected Features
    arcpy.SelectLayerByAttribute_management(PointFile, "clear_selection")
    # PointFeatures2 = arcpy.CopyFeatures_management(PointFeatures,os.path.join(GDBspot, TimeDateStr + ".gdb\PointFeatures2"))
    PointFeatures = arcpy.FeatureClassToFeatureClass_conversion(
        pointcopy, outputGDB, "ohmi_points_copy" + TimeDateStr)
    ftype = {
        'Intersection': [IntersectionThreshold, IntersectionFeatures],
        'Segment': [SegmentThreshold, SegmentFeatures],
        'Subdivision': [psThreshold, psFeatures]
    }
    # field map and merge rules
    attchmnt = []
    writer = pandas.ExcelWriter(os.path.join(GDBspot, "Top_Locations.xlsx"),
                                engine='xlsxwriter')
    for f in ftype:

        # Create a new fieldmappings and add the two input feature classes.
        fieldmappings = arcpy.FieldMappings()
        fieldmappings.addTable(ftype[f][1])
        fieldmappings.addTable(PointFeatures)

        # First get the fieldmaps. POP1990 is a field in the cities feature class.
        # The output will have the states with the attributes of the cities. Setting the
        # field's merge rule to mean will aggregate the values for all of the cities for
        # each state into an average value. The field is also renamed to be more appropriate
        # for the output.
        addSumFlds(fieldmappings)

        # Run the Spatial Join tool, using the defaults for the join operation and join type
        loc = os.path.join(GDBspot, TimeDateStr + ".gdb\\" + f + "Join_LRS")
        Join = arcpy.SpatialJoin_analysis(ftype[f][1], PointFeatures, loc,
                                          "Join_one_to_one", "keep_all",
                                          fieldmappings)

        arcpy.AddField_management(Join, "PDO_", "LONG")
        arcpy.AddField_management(Join, "EPDO_Index", "DOUBLE")
        # CRLRS_EPDO_index(Join)
        CursorFlds = [
            'PDO_', 'EPDO_Index', 'Join_Count', 'sum_fatalities_count',
            'sum_incapac_inj_count', 'sum_non_incapac_inj_count',
            'sum_possible_inj_count'
        ]

        # determine PDO  and EPDO Index/Rate
        with arcpy.da.UpdateCursor(Join, CursorFlds) as cursor:
            for row in cursor:
                try:
                    row[0] = row[2] - int(row[3]) - int(row[4]) - int(
                        row[5]) - int(row[6])
                except:
                    row[0] = 0  # null or divide by zero are the major exceptions we are handling here
                try:
                    row[1] = (float(row[3]) * fatalwt + float(row[4]) *
                              seriouswt + float(row[5]) * nonseriouswt +
                              float(row[6]) * possiblewt +
                              float(row[0])) / float(row[2])
                except:
                    row[1] = 0  # null or divide by zero are the major exceptions we are handling here
                cursor.updateRow(row)

        # delete unnecessary fields
        keepFlds = [
            'OBJECTID', 'Shape', 'Shape_Area', 'Shape_Length', 'Name',
            'NAMELSAD', 'COUNTY', 'COUNTY_NME', 'Join_Count',
            'sum_fatalities_count', 'sum_incapac_inj_count',
            'sum_non_incapac_inj_count', 'sum_possible_inj_count', 'PDO_',
            'EPDO_Index', 'Fed_Aid_Buffer_Segments_2_Name', 'Length_ft',
            'County'
        ]
        # lstFlds = arcpy.ListFields(Join)

        dropFlds = [
            x.name for x in arcpy.ListFields(Join) if x.name not in keepFlds
        ]
        # delete fields
        arcpy.DeleteField_management(Join, dropFlds)

        # select high crash locations
        JoinLayer = arcpy.MakeFeatureLayer_management(
            Join,
            os.path.join(GDBspot, TimeDateStr + ".gdb\\" + f + "JoinLayer"))
        arcpy.AddMessage("{}".format(type(JoinLayer)))
        # arcpy.SelectLayerByAttribute_management(JoinLayer, "NEW_SELECTION", "Join_Count >=" + ftype[f][0])
        fld_nmes = [fld.name for fld in arcpy.ListFields(JoinLayer)]

        fld_nmes.remove(
            'Shape'
        )  # I think this field kept causing an exception: Data must be 1 dimensional
        arcpy.AddMessage("{}".format(fld_nmes))
        arcpy.AddMessage("{}".format(
            type(
                os.path.join(GDBspot,
                             TimeDateStr + ".gdb\\" + f + "JoinLayer"))))
        # do this because political sud
        # fields can be list or tuple, list works when 'Shape' field removed
        n = arcpy.da.FeatureClassToNumPyArray(JoinLayer,
                                              fld_nmes,
                                              where_clause="Join_Count  >=" +
                                              ftype[f][0],
                                              skip_nulls=False,
                                              null_value=0)

        df = pandas.DataFrame(n)

        CRLRS_excel_export(df, f, writer)

    writer.save()
    return os.path.join(GDBspot, "Top_Locations.xlsx")
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