Esempio n. 1
0
def DeleteDeletedRows(FMIS_DEL, FMIS_LOAD):
    #delete rows from the FMIS table programmed to be deleted according to the CPMS view
    MakeTableView_management(FMIS_DEL, "RowstoDelete")
    MakeTableView_management(FMIS_LOAD, "DeleteThese")
    delcount = GetCount(FMIS_DEL)
    #delete rows from SDE CIIMS that are removed from CANSYS CIIMS
    #search cursor to match the crossing ID in the delete view
    SetLogHistory(False)
    DeleteList = []
    with da.SearchCursor("RowstoDelete",
                         "PROJECT_NUMBER") as delcur:  # @UndefinedVariable
        for row in delcur:
            DelXID = ("{0}".format(row[0]))
            DeleteList.append(DelXID)
    print "list completed"
    for record in DeleteList:
        #print DelXID + " is being deleted from the FMIS table"
        #add the the crossing ID for the row to be deleted to a selection set
        delsel = "PROJECT_NUMBER LIKE '%s'" % record
        #print delsel
        SelectLayerByAttribute_management("DeleteThese", "ADD_TO_SELECTION",
                                          delsel)
    #delete the selected rows
    DeleteRows_management("DeleteThese")
    del FMIS_DEL, FMIS_LOAD, delsel
    print "Delete function completed"
Esempio n. 2
0
def DirectionalUrbanClass():
    #do the same as for State Sys but for Non State Urban Classified Highways (Nusys)
    FeatureVerticesToPoints_management("RoadCenterlinesC",
                                       "in_memory/UrbanPoints", "MID")
    FeatureClassToFeatureClass_conversion(
        NSND,
        "in_memory",
        "NSND_NPD",
        where_clause="NETWORK_DIRECTION IN ( 'SB' , 'WB' )")
    LocateFeaturesAlongRoutes_lr("UrbanPoints", "NSND_NPD", "NE_UNIQUE",
                                 "200 Feet", "in_memory/UrbanPointsMeasures",
                                 "RID POINT MEAS", "ALL", "DISTANCE", "ZERO",
                                 "FIELDS", "M_DIRECTON")
    Delete_management("UrbanPoints")
    #is the query stil valid for non state system?  Explore hte NE Unique.  State System did not use the county number prefix

    SelectLayerByAttribute_management(
        "UrbanPointsMeasures", "NEW_SELECTION",
        """SUBSTRING( "RID", 4, 7) NOT  LIKE SUBSTRING("NON_State_System_LRSKey" ,4, 7)"""
    )

    DeleteRows_management(in_rows="UrbanPointsMeasures")
    MakeRouteEventLayer_lr("NSND_NPD",
                           "NE_UNIQUE",
                           "UrbanPointsMeasures",
                           "rid POINT MEAS",
                           "UrbanPointEvents",
                           offset_field="Distance",
                           add_error_field="ERROR_FIELD",
                           add_angle_field="ANGLE_FIELD",
                           angle_type="NORMAL",
                           complement_angle="ANGLE",
                           offset_direction="RIGHT",
                           point_event_type="POINT")
    MakeFeatureLayer_management("UrbanPointEvents", "UNPD_ID",
                                """"Distance">=0""")
    SelectLayerByLocation_management("UNPD_ID", "INTERSECT",
                                     "RoadCenterlinesC", "1 Feet",
                                     "NEW_SELECTION")

    #at this point, there are a lot of false positives, places with single carriagway roads and nusys divided
    #we need to incorporate the check overlap process to identify where their are single and dual carriagways here
    #starting with the technique to find sausages or dual carraigeways
    #SpatialJoin_analysis("UNPD_ID", "RoadCenterlinesC", "in_memory/ValidateSausages120", "JOIN_ONE_TO_ONE", "KEEP_ALL", '#', "INTERSECT", "120 Feet", "Distance")

    #this Spatial Join step is improving the results, removing most false positives.  It still shows overlapping segments
    #it would be improved even more, potentially, by testing the non-primary direction against dissolve somehow.
    #except, the calculate method is by segment to the source, a dissolve would complicate the process of calculating back to the source data
    #we are looking for count grater than 0 of the offset point to hte segment, so a dissolved segment should work
    import EliminateOverlaps
    from EliminateOverlaps import CollectorDissolve
    #set the roadcenterline input and dissolve output for RoadCenterline dissolve for this subroutine
    roadcenterlines = "RoadCenterlinesC"
    ClassOutput = r"in_memory/RMC2"
    CollectorDissolve()
    SpatialJoin_analysis("UNPD_ID", "RMC2dissolve",
                         "in_memory/ValidateSausages120", "JOIN_ONE_TO_ONE",
                         "KEEP_ALL", '#', "INTERSECT", "120 Feet", "Distance")
Esempio n. 3
0
def Report():
    OverlayRouteEvents_lr(
        connection1 + "MAINTENANCE_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END",
        connection1 + "LANECLASS_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END",
        "UNION", connection1 + "CCL_Report_M",
        "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END", "NO_ZERO", "FIELDS", "INDEX")
    DissolveRouteEvents_lr(connection1 + "CCL_Report_M",
                           "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
                           "CITYNO;MAINT_DESC;CITY_NAME;Lanes",
                           connection1 + "CCL_Report_D",
                           "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
                           "CONCATENATE", "INDEX")
    #cleanup border errors - make feature layers based on City, city number, and CCLLRS and delete where they are not consistent between Maintenance and Resolution sections
    if Exists(connection1 + "CCL_Report"):
        MakeTableView_management(connection1 + "CCL_Report", "Report_Clean1",
                                 "CCL_LRS2 <> CCL_LRS")
        DeleteRows_management("Report_Clean1")
    LocateFeaturesAlongRoutes_lr(LineFeatureClass,
                                 connection1 + "CCL_LRS_ROUTE", NewRouteKey,
                                 "#", connection1 + "RES_SECTION_CCL",
                                 "CCL_LRS LINE CCL_BEGIN CCL_END", "ALL",
                                 "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")
    OverlayRouteEvents_lr(
        connection1 + "RES_SECTION_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END",
        connection1 + "CCL_Report_D", "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
        "INTERSECT", connection1 + "CCL_Report",
        "CCL_LRS LINE CCL_BEGIN CCL_END", "NO_ZERO", "FIELDS", "INDEX")
    MakeRouteEventLayer_lr(connection1 + "CCL_LRS_ROUTE", "CCL_LRS",
                           connection1 + "CCL_Report",
                           "CCL_LRS LINE CCL_BEGIN CCL_END",
                           "City Connecting Links Mapping", "#", "ERROR_FIELD",
                           "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT",
                           "POINT")
    print "add mapping fields for lane miles"
    AddField_management("City Connecting Links Mapping", "CenterlineMiles",
                        "DOUBLE")
    CalculateField_management("City Connecting Links Mapping",
                              "CenterlineMiles", '[CCL_END]-[CCL_BEGIN]', "VB")
    AddField_management("City Connecting Links Mapping", "LaneMiles", "DOUBLE")
    CalculateField_management("City Connecting Links Mapping", "LaneMiles",
                              '([CCL_END]-[CCL_BEGIN])*[Lanes]', "VB")
    AddField_management(connection1 + "CITY_CONNECTING_LINK_CENTERLINE",
                        "CenterlineMiles", "DOUBLE")
    MakeFeatureLayer_management(
        connection1 + "CITY_CONNECTING_LINK_CENTERLINE", 'Res_centerline')
    CalculateField_management("Res_centerline", "CenterlineMiles",
                              '[END_CNTY_LOGMILE]-[BEG_CNTY_LOGMILE]', "VB")
    Dissolve_management("Res_centerline", connection1 + "CCL_LEGEND",
                        "CITY;LRS_KEY;CITYNUMBER;CCL_LRS",
                        "CenterlineMiles SUM", "MULTI_PART", "DISSOLVE_LINES")
    AddField_management(connection1 + "CCL_LEGEND", "CCL_LEGEND", "TEXT", "#",
                        "#", "50")
    legendexp = 'str(!CCL_LRS![3]) +"-" + str(!CCL_LRS![6:9]).lstrip("0")+"........"+ str(!SUM_CenterlineMiles!)'
    MakeFeatureLayer_management(connection1 + "CCL_LEGEND", 'LegendCalc')
    CalculateField_management("LegendCalc", "CCL_LEGEND", legendexp,
                              "PYTHON_9.3", "#")
Esempio n. 4
0
def DeleteIt():
    for fc in KSdb_fc_list:
        print "deleting " + str(
            fc) + " for rows with steward = " + deletesteward
        deletefrom = final + fd2 + r'/' + fc
        lyrname = fc + "del"
        selection = "STEWARD = " + deletesteward + "OR STEWARD IS NULL"
        MakeFeatureLayer_management(deletefrom, lyrname, selection)
        DeleteRows_management(lyrname)
        del lyrname

    for tbl in KSdb_Tbl_list:
        print "deleting " + str(
            tbl) + " for rows with steward = " + deletesteward
        deletefrom = final + tbl
        selection = "STEWARD = " + deletesteward + "OR STEWARD IS NULL"
        lyrname = tbl + "del"
        MakeTableView_management(deletefrom, lyrname, selection)
        DeleteRows_management(lyrname)
Esempio n. 5
0
def PointDelete(
    fc, layer_name, tbl, table_name
):  #delete rows from SDE CIIMS that are removed from CANSYS CIIMS
    MakeFeatureLayer_management(fc, layer_name)
    MakeTableView_management(tbl, table_name)
    AddJoin_management(layer_name, "CROSSINGID", table_name, "CROSSINGID",
                       "KEEP_ALL")
    SelectLayerByAttribute_management(
        layer_name, "NEW_SELECTION",
        "CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGID IS NULL")
    DeleteRows_management("Static_Crossings")
    del fc, layer_name, tbl, table_name
    print "Delete function completed"
Esempio n. 6
0
def Restart():
    from arcpy import DeleteRows_management
    targetpath = currentPathSettings.EntDB + '/' + currentPathSettings.EDBName + '.' + currentPathSettings.EDBO + '.' + currentPathSettings.EFD
    print targetpath
    env.workspace = targetpath
    fclist = ListFeatureClasses()
    for fc in fclist:
        print fc
        #DeleteRows_management(fc)
    targetpath = currentPathSettings.EntDB
    env.workspace = targetpath
    tablelist = ListTables()
    for table in tablelist:
        print table
        DeleteRows_management(table)
Esempio n. 7
0
def PointDelete(
    fc, layer_name, deltbl, table_name
):  #delete rows from SDE CIIMS that are removed from CANSYS CIIMS
    MakeFeatureLayer_management(fc, layer_name)
    MakeTableView_management(deltbl, table_name)
    with da.SearchCursor(deltbl, "CROSSINGID") as delcur:
        for row in delcur:
            DelXID = ("{0}".format(row[0]))
            print DelXID + " is being deleted from the CIIMS table"
            #AddJoin_management(layer_name,"CROSSINGID", deltbl, "CROSSINGID", "KEEP_ALL")
            delsel = "CROSSINGID LIKE '" + str(row)[3:10] + "'"
            SelectLayerByAttribute_management(layer_name, "ADD_TO_SELECTION",
                                              delsel)
    DeleteRows_management(layer_name)
    del fc, layer_name, deltbl, table_name
    print "Delete function completed"
Esempio n. 8
0
def PointDelete(fc, layer_name, deltbl, table_name):
    #delete rows from SDE CIIMS that are removed from CANSYS CIIMS
    MakeFeatureLayer_management(fc, layer_name)
    MakeTableView_management(deltbl, table_name)
    #search cursor to match the crossing ID in the delete view
    with da.SearchCursor(deltbl, "CROSSINGID") as delcur:  # @UndefinedVariable
        for row in delcur:
            DelXID = ("{0}".format(row[0]))
            print DelXID + " is being deleted from the CIIMS table"
            #add the the crossing ID for the row to be deleted to a selection set
            delsel = "CROSSINGID LIKE '" + str(row)[3:10] + "'"
            SelectLayerByAttribute_management(layer_name, "ADD_TO_SELECTION",
                                              delsel)
    #delete the selected rows
    DeleteRows_management(layer_name)
    del fc, layer_name, deltbl, table_name
    print "Delete function completed"
Esempio n. 9
0
def ClearSteward(StewardID):
    from arcpy import DeleteFeatures_management, DeleteRows_management, GetCount_management
    targetpath = currentPathSettings.EntDB + '/' + currentPathSettings.EDBName + '.' + currentPathSettings.EDBO
    #\\gisdata\arcgis\GISdata\DASC\NG911\Final\[email protected]\NG911.GEO.NG911\NG911.GEO.RoadCenterline
    print targetpath
    env.workspace = targetpath
    where_clause = "STEWARD LIKE '" + StewardID + "'"

    MakeFeatureLayer_management(targetpath + ".RoadCenterline",
                                "Steward_Delete_Roads", where_clause)
    MakeTableView_management(targetpath + ".RoadAlias", "Steward_Delete_Alias",
                             where_clause)
    LineCount = GetCount_management("Steward_Delete_Roads")
    AliasCount = GetCount_management("Steward_Delete_Alias")
    print 'deleting ' + str(
        LineCount) + ' road center lines where ' + where_clause
    print 'deleting ' + str(AliasCount) + ' alias rows where ' + where_clause
    DeleteFeatures_management(in_features="Steward_Delete_Roads")
    DeleteRows_management(in_rows="Steward_Delete_Alias")
Esempio n. 10
0
def checkAddressPointFrequency(AddressPoints, gdb):
    from arcpy import Frequency_analysis, MakeTableView_management, DeleteRows_management, GetCount_management, Delete_management, Exists
    from os.path import join

    AP_Freq = join(gdb, "AP_Freq")
    fl = "fl"

    #remove the frequency table if it exists already
    if Exists(AP_Freq):
        Delete_management(AP_Freq)

    #run frequency analysis
    Frequency_analysis(
        AddressPoints, AP_Freq,
        "MUNI;HNO;HNS;PRD;STP;RD;STS;POD;POM;ZIP;BLD;FLR;UNIT;ROOM;SEAT;LOC;LOCTYPE",
        "")

    #get count of records
    rFreq = GetCount_management(AP_Freq)
    rCount = int(rFreq.getOutput(0))

    #delete records
    #make where clause
    wc = "Frequency = 1 or LOCTYPE <> 'Primary'"

    #make feature layer
    MakeTableView_management(AP_Freq, fl, wc)

    #get count of the results
    result = GetCount_management(fl)
    count = int(result.getOutput(0))

    if rCount != count:
        #Delete
        DeleteRows_management(fl)
        userMessage(
            "Checked frequency of address points. Results are in table " +
            AP_Freq)
    elif rCount == count:
        Delete_management(AP_Freq)
        userMessage("All address points are unique.")
Esempio n. 11
0
def calibrationCCL():
    
    print "deriving CCL LRS starting points and calibrations"
    CCLEnd = "!"+schema+"CITY_CONNECTING_LINK_STATE.MAX_END_STATE_LOGMILE!- !"+schema+"CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!"
    CCLBeg = "!"+schema+"CITY_CONNECTING_LINK_STATE.MIN_BEG_STATE_LOGMILE! - !"+schema+"CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!"
    MakeFeatureLayer_management(LineFeatureClass, "CITY_CONNECTING_LINK_RESET")
    resln = "CITY_CONNECTING_LINK_RESET"
    AddField_management(resln,"CCL_BEGIN", "DOUBLE", 12, 3)
    AddField_management(resln,"CCL_END", "DOUBLE", 12, 3)
    AddJoin_management("CITY_CONNECTING_LINK_RESET","CCL_LRS",connection1+"CITY_CONNECTING_LINK_STATE_D","CCL_LRS","KEEP_ALL")
    CalculateField_management(resln, "CCL_BEGIN", CCLBeg, "PYTHON")
    CalculateField_management(resln, "CCL_END", CCLEnd, "PYTHON")
    print "calibrating LRS - point calibration method"
    statecalpoints = stateroutelyr+"_Point"
    print statecalpoints
    MakeFeatureLayer_management(statecalpoints, "smlrs_pt")
    print connection1+"CITY_CONNECTING_LINK_STATE_D"
    MakeFeatureLayer_management(connection1+"CITY_CONNECTING_LINK_STATE_D", "dissolved_res_sects")
    intersects = ["dissolved_res_sects", "smlrs_pt"]
    Intersect_analysis(intersects,connection0+"CALIBRATION_POINTS_CCL","ALL","#","POINT")
    print connection1+"CALIBRATION_POINTS_CCL"
    MakeFeatureLayer_management(connection1+"CALIBRATION_POINTS_CCL", "Calibrators")
    querystr = "Substring( CCL_LRS,4, 12)<> LRS_ROUTE"  
    SelectLayerByAttribute_management("Calibrators","NEW_SELECTION",querystr)
    DeleteRows_management("Calibrators")
    MakeFeatureLayer_management(connection1+"CITY_CONNECTING_LINK_STATE", "CCL_sections")
    DeleteIdentical_management("Calibrators","LRS_KEY;POINT_X;POINT_Y;POINT_M","#","0")
    AddField_management("CCL_sections","CCL_BEGIN","DOUBLE","#","#","#","#","NULLABLE","NON_REQUIRED","#")
    AddField_management("CCL_sections","CCL_BEGIN","DOUBLE","#","#","#","#","NULLABLE","NON_REQUIRED","#")
    AddJoin_management("CCL_sections","CCL_LRS","dissolved_res_sects","CCL_LRS","KEEP_ALL")
    CalculateField_management("CCL_sections",schema+"CITY_CONNECTING_LINK_STATE.CCL_BEGIN","!"+schema+"CITY_CONNECTING_LINK_STATE.MIN_BEG_STATE_LOGMILE!- !"+schema+"CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!","PYTHON","#")
    CalculateField_management("CCL_sections",schema+"CITY_CONNECTING_LINK_STATE.CCL_END","!"+schema+"CITY_CONNECTING_LINK_STATE.MAX_END_STATE_LOGMILE!- !"+schema+"CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!","PYTHON","#")
    AddField_management(connection1+"CALIBRATION_POINTS_CCL","CCL_MEASURE", "DOUBLE", 12, 3)
    CalculateField_management("Calibrators","CCL_MEASURE","!POINT_M!- !MIN_BEG_STATE_LOGMILE!","PYTHON","#")
    CreateRoutes_lr(LineFeatureClass,NewRouteKey,connection1+NewRoute+"base","TWO_FIELDS",NewBeg, NewEnd,"UPPER_LEFT","1","0","IGNORE","INDEX")
    CalibrateRoutes_lr(connection0+"/"+schema+"CCL_LRS_ROUTEbase","CCL_LRS",connection1+"CALIBRATION_POINTS_CCL","CCL_LRS","CCL_MEASURE",connection1+"CCL_LRS_ROUTE","DISTANCE","1 Feet","BETWEEN","NO_BEFORE","NO_AFTER","IGNORE","KEEP","INDEX")
    AddField_management(connection1+NewRoute, "NETWORKDATE", "DATE")
    CalculateField_management(connection1+NewRoute,"NETWORKDATE","datetime.datetime.now( )","PYTHON_9.3","#")
    MakeFeatureLayer_management(connection1+"CCL_LRS_ROUTE", NewRoute)
Esempio n. 12
0
def SecondaryDirectionFinder():
    #make a point at the center of each line segment for a highway
    #No way currently to do this for RML highways,  NUSYS provides the directional layer for C highwyas
    #FeatureToPoint_management("State Highways", "in_memory/HighwayPoints", "INSIDE")
    #maybe two ways to do this, this time, I'm using midpoint from feature to point - catch
    FeatureVerticesToPoints_management(
        in_features="RoadCenterlines",
        out_feature_class="in_memory/HighwayPoints",
        point_location="MID")

    FeatureClassToFeatureClass_conversion(
        SRND,
        "in_memory",
        "SRND_NPD",
        where_clause="NETWORK_DIRECTION IN ( 'SB' , 'WB' )")

    #CRND SEcondary Direction is the CRND layer where "NETWORK_DIRECTION IN ( 'SB' , 'WB' )"
    #Locate Features Along Routes will calculate a +/- offset distance of the dual carriageway points from the CRND centerline
    LocateFeaturesAlongRoutes_lr("HighwayPoints", "SRND_NPD", "NE_UNIQUE",
                                 "500 Feet", "in_memory/HighwayPointsMeasures",
                                 "RID POINT MEAS", "ALL", "DISTANCE", "ZERO",
                                 "FIELDS", "M_DIRECTON")
    # 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: "HighwayPointsMeasures"
    SelectLayerByAttribute_management(
        in_layer_or_view="HighwayPointsMeasures",
        selection_type="NEW_SELECTION",
        where_clause=
        """SUBSTRING( "RID", 1, 7) NOT  LIKE SUBSTRING("StateKey1", 1, 7)""")

    DeleteRows_management(in_rows="HighwayPointsMeasures")

    MakeRouteEventLayer_lr("SRND_NPD",
                           "NE_UNIQUE",
                           "HighwayPointsMeasures",
                           "rid POINT MEAS",
                           "HighwayPointEvents",
                           offset_field="Distance",
                           add_error_field="ERROR_FIELD",
                           add_angle_field="ANGLE_FIELD",
                           angle_type="NORMAL",
                           complement_angle="ANGLE",
                           offset_direction="RIGHT",
                           point_event_type="POINT")

    #select the secondary direction points
    MakeFeatureLayer_management("HighwayPointEvents", "NPD_ID",
                                """"Distance">=0""")

    # some random points, due to the 500 ft buffer I think, are getting included.  Select the event points that do not intersect a state highway road centerline feature
    #
    SelectLayerByLocation_management(in_layer="NPD_ID",
                                     overlap_type="INTERSECT",
                                     select_features="RoadCenterlines",
                                     search_distance="1 Feet",
                                     selection_type="NEW_SELECTION",
                                     invert_spatial_relationship="INVERT")

    FeatureClassToFeatureClass_conversion(
        "NPD_ID",
        "Database Connections/Conflation2012_sde.sde/Conflation.SDE.KANSAS_DOT",
        "Non_Primary_Divided_Highway", '"Distance">=0')
Esempio n. 13
0
OverlayRouteEvents_lr(
    connection1 + "MAINTENANCE_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END",
    connection1 + "LANECLASS_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END", "UNION",
    connection1 + "CCL_Report_M", "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
    "NO_ZERO", "FIELDS", "INDEX")

DissolveRouteEvents_lr(connection1 + "CCL_Report_M",
                       "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
                       "CITYNO;MAINT_DESC;CITY_NAME;Lanes",
                       connection1 + "CCL_Report_D",
                       "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END", "CONCATENATE",
                       "INDEX")
#cleanup border errors - make feature layers based on City, city number, and CCLLRS and delete where they are not consistent between Maintenance and Resolution sections
MakeTableView_management(connection1 + "CCL_Report", "Report_Clean1",
                         "CCL_LRS2 <> CCL_LRS")
DeleteRows_management("Report_Clean1")

LocateFeaturesAlongRoutes_lr(LineFeatureClass, connection1 + "CCL_LRS_ROUTE",
                             NewRouteKey, "#", connection1 + "RES_SECTION_CCL",
                             "CCL_LRS LINE CCL_BEGIN CCL_END", "ALL",
                             "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")

OverlayRouteEvents_lr(
    connection1 + "RES_SECTION_CCL", "CCL_LRS LINE CCL_BEGIN CCL_END",
    connection1 + "CCL_Report_D", "CCL_LRS LINE CCL_MA_BEGIN CCL_MA_END",
    "UNION", connection1 + "CCL_Report", "CCL_LRS LINE CCL_BEGIN CCL_END",
    "NO_ZERO", "FIELDS", "INDEX")

MakeRouteEventLayer_lr(connection1 + "CCL_LRS_ROUTE", "CCL_LRS",
                       connection1 + "CCL_Report",
                       "CCL_LRS LINE CCL_BEGIN CCL_END",
Esempio n. 14
0
def CRB():
    print "querying the shared.NON_STATE_SYSTEM to obtain only urban classified primary C routes with mileage that should be counted and for resolution segments."
    MakeFeatureLayer_management(
        NonState, "NonStateCP",
        "((LRS_KEY LIKE '%C%' OR LRS_ROUTE_PREFIX = 'C') AND (MILEAGE_COUNTED = -1)) OR (LRS_DIR_OF_TRAVEL = 'P' and SURFACE = 'Propose')"
    )

    print "querying the shared.NON_STATE_SYSTEM to obtain only urban classified NonPrimary C routes with mileage that should be counted and for resolution segments."
    MakeFeatureLayer_management(
        NonState, "NonStateCNP",
        "(LRS_KEY LIKE '%C%' OR LRS_ROUTE_PREFIX = 'C') AND (MILEAGE_COUNTED = 0) AND (LRS_DIR_OF_TRAVEL = 'S') and (COUNTY_NUMBER <> 0)"
    )

    print "shared.Non_State_System has unique IDS that we desire to keep as persistent IDs for comparison with GeoMedia, so we are spatially intersecting the state boundary to keep them as is."
    Buffer_analysis(StateBnd, "State_Boundary_1Mile", "5280 Feet", "FULL",
                    "ROUND", "NONE", "", "PLANAR")
    MakeFeatureLayer_management("State_Boundary_1Mile", "StateBnd")
    Intersect_analysis("NonStateCP #;StateBnd #",
                       "Non_State_Classified_Primary", "ALL", "-1 Unknown",
                       "LINE")
    Intersect_analysis("NonStateCNP #;StateBnd #",
                       "Non_State_Classified_NonPrimary", "ALL", "-1 Unknown",
                       "LINE")

    NonStateCP_fx = r'Non_State_Classified_Primary'
    NonStateCNP_fx = r'Non_State_Classified_NonPrimary'

    MakeFeatureLayer_management(NonStateCP_fx, "Non_State_Classified_Primary")
    MakeFeatureLayer_management(NonStateCNP_fx,
                                "Non_State_Classified_NonPrimary")

    CP_ET = 'CP_NON_STATE_EVENTS'
    CNP_ET = 'CNP_NON_STATE_EVENTS'

    MakeFeatureLayer_management(NUSYS, "Nusys_Extract",
                                "NSEC_SUB_CLASS <> 'R'")

    print "creating primary C Non_State_Routes by the Shape Length"
    MakeFeatureLayer_management(
        "Non_State_Classified_Primary", "BackwardSegsCP",
        "LRS_BACKWARDS = -1 AND (MILEAGE_COUNTED = -1 OR (LRS_DIR_OF_TRAVEL = 'P' and SURFACE = 'Propose'))"
    )
    FlipLine_edit("BackwardSegsCP")
    Dissolve_management("Non_State_Classified_Primary", "CPRouteShapeLength",
                        "NQR_DESCRIPTION", "", "MULTI_PART", "DISSOLVE_LINES")
    AddField_management("CPRouteShapeLength", "BCM", "DOUBLE", "", "", "", "",
                        "NULLABLE", "NON_REQUIRED", "")
    CalculateField_management("CPRouteShapeLength", "BCM", "0", "VB", "")
    AddField_management("CPRouteShapeLength", "ECM", "DOUBLE", "", "", "", "",
                        "NULLABLE", "NON_REQUIRED", "")
    CalculateField_management("CPRouteShapeLength", "ECM",
                              "!Shape.length@miles!", "Python")

    CreateRoutes_lr("CPRouteShapeLength", "NQR_DESCRIPTION",
                    destdb + "\CP_ShapeLengthRoute", "TWO_FIELDS", "BCM",
                    "ECM", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")
    #Flip them back to the original direction
    FlipLine_edit(in_features="BackwardSegsCP")
    LocateFeaturesAlongRoutes_lr("Non_State_Classified_Primary",
                                 "CP_ShapeLengthRoute", "NQR_DESCRIPTION",
                                 "0 Feet", "CP_NON_STATE_EVENTS",
                                 "RID LINE FMEAS TMEAS", "FIRST", "DISTANCE",
                                 "ZERO", "FIELDS", "M_DIRECTON")
    AddField_management("CP_NON_STATE_EVENTS", "AdjBegin", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "AdjEnd", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "CHG_BEGLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "CHG_ENDLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "NEW_BEGLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "NEW_ENDLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "AdjLength", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CP_NON_STATE_EVENTS", "CHANGE", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")

    CalculateField_management("CP_NON_STATE_EVENTS",
                              "AdjBegin",
                              "round( [FMEAS] , 3 )",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "AdjEnd",
                              "round( [TMEAS] , 3 )",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "CHG_BEGLOG",
                              "[AdjBegin] - [LRS_BEG_CNTY_LOGMILE]",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "CHG_ENDLOG",
                              "[AdjEnd] - [LRS_END_CNTY_LOGMILE]",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "NEW_BEGLOG",
                              "[AdjBegin]",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "NEW_ENDLOG",
                              "[AdjEnd]",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "AdjLength",
                              "[AdjEnd] - [AdjBegin]",
                              "VB",
                              code_block="")
    CalculateField_management("CP_NON_STATE_EVENTS",
                              "CHANGE",
                              "abs([LENGTH] - [AdjLength])",
                              "VB",
                              code_block="")

    MakeRouteEventLayer_lr("CP_ShapeLengthRoute", "NQR_DESCRIPTION",
                           "CP_NON_STATE_EVENTS", "RID LINE FMEAS TMEAS",
                           "CP_LRS_Review_Events", "", "ERROR_FIELD",
                           "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT",
                           "POINT")
    print "CP-Rte Builder script completed successfully"

    print "creating NonPrimary C Non_State_Routes by the Shape Length"
    MakeFeatureLayer_management(
        "Non_State_Classified_NonPrimary", "BackwardSegsCNP",
        "LRS_BACKWARDS = -1 AND LRS_DIR_OF_TRAVEL = 'S' and COUNTY_NUMBER <> 0"
    )
    FlipLine_edit("BackwardSegsCNP")
    Dissolve_management("Non_State_Classified_NonPrimary",
                        "CNPRouteShapeLength", "LRS_KEY", "", "MULTI_PART",
                        "DISSOLVE_LINES")
    AddField_management("CNPRouteShapeLength", "BCM", "DOUBLE", "", "", "", "",
                        "NULLABLE", "NON_REQUIRED", "")
    CalculateField_management("CNPRouteShapeLength", "BCM", "0", "VB", "")
    AddField_management("CNPRouteShapeLength", "ECM", "DOUBLE", "", "", "", "",
                        "NULLABLE", "NON_REQUIRED", "")
    CalculateField_management("CNPRouteShapeLength", "ECM",
                              "!Shape.length@miles!", "Python")

    CreateRoutes_lr("CNPRouteShapeLength", "LRS_KEY",
                    destdb + "\CNP_ShapeLengthRoute", "TWO_FIELDS", "BCM",
                    "ECM", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")
    #Flip them back to the original direction
    FlipLine_edit(in_features="BackwardSegsCNP")
    LocateFeaturesAlongRoutes_lr("Non_State_Classified_NonPrimary",
                                 "CNP_ShapeLengthRoute", "LRS_KEY", "0 Feet",
                                 "CNP_NON_STATE_EVENTS",
                                 "RID LINE FMEAS TMEAS", "FIRST", "DISTANCE",
                                 "ZERO", "FIELDS", "M_DIRECTON")
    AddField_management("CNP_NON_STATE_EVENTS", "AdjBegin", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "AdjEnd", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "CHG_BEGLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "CHG_ENDLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "NEW_BEGLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "NEW_ENDLOG", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "AdjLength", "DOUBLE", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNP_NON_STATE_EVENTS", "CHANGE", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")

    CalculateField_management("CNP_NON_STATE_EVENTS", "AdjBegin",
                              "round( [FMEAS] , 3 )", "VB", "")
    CalculateField_management("CNP_NON_STATE_EVENTS", "AdjEnd",
                              "round( [TMEAS] , 3 )", "VB", "")
    CalculateField_management("CNP_NON_STATE_EVENTS", "CHG_BEGLOG",
                              "[AdjBegin] - [LRS_BEG_CNTY_LOGMILE]", "VB", "")
    CalculateField_management("CNP_NON_STATE_EVENTS", "CHG_ENDLOG",
                              "[AdjEnd] - [LRS_END_CNTY_LOGMILE]", "VB", "")
    CalculateField_management("CNP_NON_STATE_EVENTS", "NEW_BEGLOG",
                              "[AdjBegin]", "VB", "")
    CalculateField_management("CNP_NON_STATE_EVENTS", "NEW_ENDLOG", "[AdjEnd]",
                              "VB", "")

    CalculateField_management("CNP_NON_STATE_EVENTS",
                              "AdjLength",
                              "[AdjEnd] - [AdjBegin]",
                              "VB",
                              code_block="")
    CalculateField_management("CNP_NON_STATE_EVENTS",
                              "CHANGE",
                              "abs([LENGTH] - [AdjLength])",
                              "VB",
                              code_block="")

    MakeRouteEventLayer_lr("CNP_ShapeLengthRoute", "LRS_KEY",
                           "CNP_NON_STATE_EVENTS", "RID LINE FMEAS TMEAS",
                           "CNP_LRS_Review_Events", "", "ERROR_FIELD",
                           "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT",
                           "POINT")
    AddField_management("CNPRouteShapeLength", "PersistentID", "LONG", "", "",
                        "", "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNPRouteShapeLength", "Pbeg", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNPRouteShapeLength", "Pend", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")

    CalculateField_management("CNPRouteShapeLength", "PersistentID", "!ID2!",
                              "PYTHON_9.3", "")
    TableToTable_conversion("CNPRouteShapeLength", "in_memory", "CNP_Events",
                            "")

    endpoints = ["beg", "end"]
    for pos in endpoints:
        out_event = "CNP_Events_" + pos
        print out_event
        out_lyr = "CNP_Events_Features_" + pos
        print out_lyr

        outfield = "P" + pos
        print outfield
        if pos == "beg":
            print "Will locate begin point"
            routesettings = "LRS_KEY POINT BCM"
        else:
            print "locating end point"
            routesettings = "LRS_KEY POINT ECM"
        MakeRouteEventLayer_lr("CNP_ShapeLengthRoute", "LRS_KEY", "CNP_Events",
                               routesettings, out_event, "", "ERROR_FIELD",
                               "ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT",
                               "POINT")
        LocateFeaturesAlongRoutes_lr(out_event, "CP_ShapeLengthRoute",
                                     "NQR_DESCRIPTION", "500 Feet", out_lyr,
                                     "RID POINT MEAS", "ALL", "DISTANCE",
                                     "ZERO", "FIELDS", "M_DIRECTON")
        AddJoin_management(out_lyr, "PersistentID", "CNPRouteShapeLength",
                           "PersistentID", "KEEP_ALL")
        selexp = out_lyr + ".RID <> CNPRouteShapeLength.LRS_KEY"
        print selexp
        SelectLayerByAttribute_management(out_lyr, "NEW_SELECTION", selexp)
        DeleteRows_management(out_lyr)
        RemoveJoin_management(out_lyr)
        AddJoin_management("CNPRouteShapeLength", "PersistentID", out_lyr,
                           "PersistentID", "KEEP_ALL")
        #expression = "[CNP_Events_Features_Begin.MEAS]"
        expression = "[" + out_lyr + ".MEAS]"
        print expression
        calcfield = "CNPRouteShapeLength." + outfield
        #CNPRouteShapeLength.Pbeg
        CalculateField_management("CNPRouteShapeLength", calcfield, expression,
                                  "VB", "")
        RemoveJoin_management("CNPRouteShapeLength", "")

    #test flipped routes and calculate mileage and flip flag
    AddField_management("CNPRouteShapeLength", "FlipTest", "LONG", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNPRouteShapeLength", "Adj_Beg", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    AddField_management("CNPRouteShapeLength", "Adj_End", "DOUBLE", "", "", "",
                        "", "NULLABLE", "NON_REQUIRED", "")
    SelectLayerByAttribute_management("CNPRouteShapeLength", "NEW_SELECTION",
                                      '"Pbeg" > "Pend"')
    CalculateField_management("CNPRouteShapeLength", "FlipTest", "1", "VB", "")
    CalculateField_management("CNPRouteShapeLength", "Adj_Beg", "!Pend!",
                              "Python", "")
    CalculateField_management("CNPRouteShapeLength", "Adj_End", "!Pbeg!",
                              "Python", "")
    SelectLayerByAttribute_management("CNPRouteShapeLength", "NEW_SELECTION",
                                      '"Pbeg" < "Pend"')
    CalculateField_management("CNPRouteShapeLength", "FlipTest", "0", "VB", "")
    CalculateField_management("CNPRouteShapeLength", "Adj_Beg", "!Pbeg!",
                              "Python", "")
    CalculateField_management("CNPRouteShapeLength", "Adj_End", "!Pend!",
                              "Python", "")
    SelectLayerByAttribute_management("CNPRouteShapeLength", "NEW_SELECTION",
                                      '"Adj_Beg" <= 0.003')
    CalculateField_management("CNPRouteShapeLength", "Adj_Beg", "0", "Python",
                              "")

    print "CNP-Rte Builder script completed successfully"
Esempio n. 15
0
def main():
    fc = GetParameterAsText(0)
    out_table = GetParameterAsText(1)
    out_fc = GetParameterAsText(2)

    for thingy in [out_table, out_fc]:
        if Exists(thingy):
            Delete_management(thingy)

    # --------------set up reporting table for new field names-----------------
    field_name = "PRENAME"
    field_count = "NEWCOUNT"
    schema_count = "PRECOUNT"
    new_name = "NEWNAME"

    #see if the output table exists
    if not Exists(out_table):
        CreateTable_management(dirname(out_table), basename(out_table))
    else:
        DeleteRows_management(out_table)

    #see if fields already exist, if not, create them
    if not fieldExists(out_table, field_name):
        AddField_management(out_table, field_name, "TEXT", "", "", 30)

    if not fieldExists(out_table, schema_count):
        AddField_management(out_table, schema_count, "LONG")

    if not fieldExists(out_table, field_count):
        AddField_management(out_table, field_count, "SHORT")

    if not fieldExists(out_table, new_name):
        AddField_management(out_table, new_name, "TEXT", "", "", 10)

    # loop through all fields
    all_fields = ListFields(fc)

    # create name dictionary of shortened shapefile names
    name_dictionary = {}
    shortList = []  # necessary for flagging repeated field names

    for fn in all_fields:
        short_name = fn.name
        if len(fn.name) > 10:
            short_name = fn.name[0:10]

        # make sure the shortened field name doesn't already exists
        if short_name not in shortList:
            shortList.append(short_name)
            name_dictionary[fn.name] = short_name
        else:
            i = 0
            while short_name in shortList and i < 100:
                short_name = short_name[0:7] + "_" + str(i)
                i += 1
            name_dictionary[fn.name] = short_name
            shortList.append(short_name)

    # -----next step, create new feature class & add all fields----------------
    # -----for text fields, make the length the proper length------------------

    desc = Describe(fc)
    geom_type = desc.shapeType
    SR = desc.spatialReference

    # create new feature class
    CreateFeatureclass_management(dirname(out_fc), basename(out_fc), geom_type,
                                  "", "", "", SR)

    # create list to hold the names of number fields (used later)
    numFields = []
    dateFields = []

    # get the name of the OID field while looping
    oid = ""

    # loop through string fields
    for f in all_fields:
        short_name = name_dictionary[f.name]
        data_type = f.type.upper()

        # check to see if the data type is "normal"
        if data_type in [
                "TEXT", "FLOAT", "DOUBLE", "SHORT", "LONG", "DATE", "BLOB",
                "RASTER", "GUID", "STRING", "INTEGER", "SMALLINTEGER"
        ]:

            # special track for string fields
            if data_type in ["STRING", "TEXT"]:

                # set counter at 0
                i = 0

                # set up search cursor on feature class just on that field
                with SearchCursor(fc, (f.name)) as rows:
                    for row in rows:
                        if row[0] is not None:
                            # loop through values to get the longest length
                            if len(row[0]) > i:
                                i = len(row[0])

                # make sure i isn't bigger than 254
                if i > 254:
                    i = 254

                # at this point, i equals the length of the longest field entry

                # insert the field name and the length into the output table
                cursor = InsertCursor(
                    out_table,
                    (field_name, field_count, schema_count, new_name))
                new_row = (f.name, i, f.length, short_name)
                cursor.insertRow(new_row)

                del row, rows, cursor, new_row

                # add a row to the new feature class
                AddField_management(out_fc, short_name, "TEXT", "", "", i)

            # track for numbers, GUIDs & dates
            else:
                AddField_management(out_fc, short_name, data_type)

                # if it's a number, record the field name in the num field list
                if data_type in [
                        "SHORT", "LONG", "INTEGER", "FLOAT", "DOUBLE"
                ]:
                    numFields.append(f.name)
                elif data_type in ["DATE"]:
                    dateFields.append(f.name)

                #make sure all fields are in the translation table
                cursor = InsertCursor(out_table, (field_name, new_name))
                new_row = (f.name, short_name)
                cursor.insertRow(new_row)
                del cursor, new_row

        elif data_type == "OID":
            AddField_management(out_fc, "LinkOID", "INTEGER")
            name_dictionary[f.name] = "LinkOID"  # add for field mapping
            oid = f.name

            # add link field for object ID to the mapping table
            cursor = InsertCursor(out_table, (field_name, new_name))
            new_row = (f.name, "LinkOID")
            cursor.insertRow(new_row)
            del cursor, new_row
        elif data_type == "GEOMETRY":
            pass
        else:
            print("Field " + f.name + " is type " + f.type +
                  ". It will not be copied over.")
            AddWarning("Field " + f.name + " is type " + f.type +
                       ". It will not be copied over.")
            del name_dictionary[f.name]

    # -----copy data into the new FC-------------------------------------------

    # set up field lists for search & insert cursors
    oldFields, newFields = [], []

    for field in name_dictionary.keys():
        oldFields.append(field)
        newFields.append(name_dictionary[field])

    # set up a text only version of the fields
    oldFieldsTextOnly = tuple(oldFields)
    newFieldsTextOnly = tuple(newFields)

    # add SHAPE to the original set of fields
    oldFields.append("SHAPE@")
    newFields.append("SHAPE@")

    # convert the new field list to a tuple, safety first
    newFields = tuple(newFields)  # this is the one with the shape field

    # create a list of the indexes of number & date fields
    numFieldsIndexList, dateFieldsIndexList = [], []
    for numF in numFields:
        numFieldsIndexList.append(oldFields.index(numF))
    for dateF in dateFields:
        dateFieldsIndexList.append(oldFields.index(dateF))

    # ran into an issue with invalid geometry, so here's the workaround
    invalidDict = {"point": 1, "polyline": 2, "polygon": 3}

    # set up reporting for records that didn't copy
    didNotCopy = []

    # fill new rows with old rows
    with SearchCursor(fc, oldFields) as rows:
        for row in rows:
            geomIndex = oldFields.index("SHAPE@")
            geom = row[geomIndex]
            objectID = str(row[oldFields.index(oid)])
            try:

                try:
                    # find the minimum number of required points
                    minNum = invalidDict[geom_type.lower()]

                    # get the count of points in the geometry
                    count = geom.pointCount

                    # if the count is smaller than the minimum number, there's a problem
                    if count < minNum:
                        wc = oid + " = " + objectID
                        # here, we won't copy the geometry, only the fields
                        userMessage("count smaller than min")
                        with SearchCursor(fc, oldFieldsTextOnly, wc) as rows2:
                            for row2 in rows2:
                                makeRow(out_fc, newFieldsTextOnly, row2,
                                        numFieldsIndexList,
                                        dateFieldsIndexList)
                        del row2, rows2, wc

                    else:
                        # excellent, the record is normal & will copy
                        makeRow(out_fc, newFields, row, numFieldsIndexList,
                                dateFieldsIndexList)

                except Exception as e:
                    userMessage(str(e))
                    # if we're in this area, it means the record has no geometry
                    wc = oid + " = " + objectID
                    with SearchCursor(fc, oldFieldsTextOnly, wc) as rows2:
                        for row2 in rows2:
                            makeRow(out_fc, newFieldsTextOnly, row2,
                                    numFieldsIndexList, dateFieldsIndexList)
                    del row2, rows2, wc

            except Exception as e:
                userMessage(str(e))
                # for whatever reason, the record did not copy
                userMessage("Error copying record ObjectID " + objectID)
                didNotCopy.append(objectID)

    if didNotCopy != []:
        userMessage("These records did not copy- %s %s" %
                    (oid, ", ".join(didNotCopy)))

    userMessage("Skinny shapefile complete.")
Esempio n. 16
0
def MakeSniceBoundaries():
    from arcpy import (FeatureClassToFeatureClass_conversion,
                       AddField_management, CalculateField_management,
                       Clip_analysis, SelectLayerByAttribute_management, gp,
                       RasterToPolygon_conversion, Dissolve_management,
                       FeatureToPoint_management, SpatialJoin_analysis, env,
                       Union_analysis, DeleteRows_management)

    env.overwriteOutput = 1
    SNIC = r'\\gisdata\arcgis\GISdata\Connection_files\ArcGIS103\sql2008\RO@sqlgisprod_GIS_cansys.sde\GIS_CANSYS.SHARED.SNIC'
    FeatureClassToFeatureClass_conversion(SNIC,
                                          "in_memory",
                                          "DAS1",
                                          "",
                                          "#",
                                          config_keyword="")
    AddField_management("DAS1", "DAS", "Text", "#", "#", "4")
    CalculateField_management(
        "DAS1",
        "DAS",
        "[SNICE_DISTRICT] & [SNICE_AREA] & [SNICE_SUB_AREA]",
        "VB",
        code_block="")
    gp.EucAllocation_sa("DAS1", "in_memory/EucAllo_Subarea", "", "", "1E-03",
                        "DAS", "", "")
    RasterToPolygon_conversion("EucAllo_Subarea", "in_Memory/DAS_Poly",
                               "SIMPLIFY", "Value")
    Dissolve_management("DAS1", "in_memory/DAS_Dissolve", "DAS", "",
                        "SINGLE_PART", "DISSOLVE_LINES")
    FeatureToPoint_management("DAS_Dissolve",
                              "in_memory/DAS_Dissolve_FeatureToPoint",
                              "INSIDE")
    SpatialJoin_analysis("DAS_Poly", "DAS_Dissolve_FeatureToPoint",
                         "in_memory/SNICE_AREA", "JOIN_ONE_TO_ONE", "KEEP_ALL",
                         "", "CONTAINS", "", "")
    #this is the time to look at the in memory process and edit any null DAS values, and review the changes

    #for some reason, SNICE boudary will not clip, it results in no values.  May be a projection issue or a extent issue
    Clip_analysis(
        r"in_memory/SNICE_AREA",
        r"Database Connections/GISPROD_Shared.sde/SHARED.STATE_BOUNDARY",
        r"in_memory/SNICE_AREA_CLIP")
    #for workaround Union the Euclidean Allocation areas and the state boundary, then select results outside state boundary and delete them

    Union_analysis(
        "in_memory/SNICE_AREA #;'Database Connections/SDEPROD_SHARED.sde/SHARED.STATE_BOUNDARY' #",
        "in_memory/SNICE_AREA_Union", "ALL", "", "GAPS")
    SelectLayerByAttribute_management(
        "SNICE_AREA_Union", "NEW_SELECTION",
        """"FID_STATE_BOUNDARY" = -1 OR "FID_SNICE_AREA" = -1""")
    DeleteRows_management("SNICE_AREA_Union")

    AddField_management("SNICE_AREA_Union", "D", "Text", "#", "#", "1")
    AddField_management("SNICE_AREA_Union", "A", "Text", "#", "#", "1")
    AddField_management("SNICE_AREA_Union", "S", "Text", "#", "#", "1")

    CalculateField_management("SNICE_AREA_Union", "D", "Left( [DAS],1)", "VB",
                              "")
    CalculateField_management("SNICE_AREA_Union", "A", "Mid( [DAS],2, 1)",
                              "VB", "")
    CalculateField_management("SNICE_AREA_Union", "S", "Right( [DAS], 1)",
                              "VB", "")

    Dissolve_management("SNICE_AREA_Union", "in_memory/KDOT_SNICE_District",
                        "D", "", "MULTI_PART", "DISSOLVE_LINES")
    Dissolve_management("SNICE_AREA_Union", "in_memory/KDOT_SNICE_Area", "D;A",
                        "", "MULTI_PART", "DISSOLVE_LINES")
    Dissolve_management("SNICE_AREA_Union", "in_memory/KDOT_SNICE_SubArea",
                        "D;A;S", "", "MULTI_PART", "DISSOLVE_LINES")
def createShortGradiculeLinesForEachCounty():
    # Get/use the same projection as the one used for the county roads.
    spatialReferenceProjection = Describe(
        sharedNonStateSystem).spatialReference

    env.workspace = sqlGdbLocation

    inputCountyGradicule = countyCountyGradicule
    bufferedCounties = 'bufferedCounties'
    countiesToCopy = 'countiesToCopy'
    gradiculeToCopy = 'gradiculeToCopy'
    loadedGradiculeCopy = 'loadedGradiculeCopy'
    loadedTempGradicule = 'loadedTempGradicule'
    #unBufferedCounties = 'unBufferedCounties'
    # Using the miniBuffered process changes it from
    # 1457 total output features to 1481 (at 2.1k)
    # total output features.
    miniBufferedCounties = 'miniBufferedCounties'
    loadedOutputGradicule = 'loadedOutputGradicule'
    tempCounties = r'in_memory\tempCounties'
    tempCountyGradicule = r'in_memory\tempCountyGradicule'
    tempCountyGradiculePostErase = r'in_memory\tempCountyGradiculePostErase'
    tempCountyGradiculeSinglePart = r'in_memory\tempCountyGradiculeSinglePart'
    bufferCursorFields = ["OBJECTID", "COUNTY_NAME"]

    MakeFeatureLayer_management(sharedCounties, countiesToCopy)

    MakeFeatureLayer_management(countyCountyGradicule, gradiculeToCopy)
    CopyFeatures_management(gradiculeToCopy, countyGradiculeCopied)
    MakeFeatureLayer_management(countyGradiculeCopied, loadedGradiculeCopy)

    # Might be worth dissolving based on COORD & County_Name prior
    # to removing the County_Name field, if that's a possibility.

    # Or better yet, just make it so that the Gradicule lines for
    # a particular county are eligible for intersecting and
    # erasing with that same county's polygon's.  All we're
    # trying to do here is make it so that the county's original
    # gradicule lines are about half of their original size.
    # Don't need to find out which gradicule lines are close to
    # the county or anything else like that. Just need to reduce
    # the size of the lines and keep the parts that are nearest
    # the county that they go with.

    # Remove the County_Name field so that the intersect can add it
    # back and populate it only where the county buffer actually
    # intersects the lines.
    #DeleteField_management(countyGradiculeCopied, "County_Name")

    # Elaine requested that this be 1000 Feet shorter.
    # I made it 2000 feet shorter, because it still seemed too big.
    Buffer_analysis(sharedCounties, countiesBuffered, "8000 Feet")
    Buffer_analysis(sharedCounties, countiesMiniBuffered, "1500 Feet")

    bufferedCountyPolygonList = list()
    outputFeatureList = list()

    # 1st SearchCursor
    newCursor = daSearchCursor(countiesBuffered, bufferCursorFields)
    for newRow in newCursor:
        bufferedCountyPolygonList.append(list(newRow))

    if 'newCursor' in locals():
        del newCursor
    else:
        pass

    MakeFeatureLayer_management(countiesBuffered, bufferedCounties)
    MakeFeatureLayer_management(countiesMiniBuffered, miniBufferedCounties)

    loadedCountiesFields = ListFields(bufferedCounties)

    for loadedCountiesField in loadedCountiesFields:
        print "A loadedCountiesField was found: " + str(
            loadedCountiesField.name)

    countyGradiculeFields = ListFields(loadedGradiculeCopy)

    for countyGradiculeField in countyGradiculeFields:
        print "A countyGradiculeField was found: " + str(
            countyGradiculeField.name)

    for listedRow in bufferedCountyPolygonList:
        print str(listedRow)
        selectCounty = listedRow[1]

        whereClause = """ "COUNTY_NAME" = '""" + str(selectCounty) + """' """
        print "The whereClause is " + str(whereClause)
        SelectLayerByAttribute_management(bufferedCounties, "NEW_SELECTION",
                                          whereClause)

        SelectLayerByAttribute_management(loadedGradiculeCopy, "NEW_SELECTION",
                                          whereClause)

        Intersect_analysis([loadedGradiculeCopy, bufferedCounties],
                           tempCountyGradicule, "ALL")

        MultipartToSinglepart_management(tempCountyGradicule,
                                         tempCountyGradiculeSinglePart)

        # Selects the same county as the other Select, but does it from the miniBufferedCounties
        # so that the lines which lay inside of the county and running just along its edges
        # are erased, as they should only exist as gradicules for the counties adjoining this
        # one, but not for this one itself.
        SelectLayerByAttribute_management(miniBufferedCounties,
                                          "NEW_SELECTION", whereClause)

        MakeFeatureLayer_management(tempCountyGradiculeSinglePart,
                                    loadedTempGradicule)

        SelectLayerByAttribute_management(loadedTempGradicule, "NEW_SELECTION",
                                          whereClause)

        secVerGradiculeFields = ListFields(loadedTempGradicule)

        #for secVerGradiculeField in secVerGradiculeFields:
        #    print "A secVerGradiculeField was found: " + str(secVerGradiculeField.name)

        Erase_analysis(loadedTempGradicule, miniBufferedCounties,
                       tempCountyGradiculePostErase, xyToleranceVal)

        fieldsToCopy = [
            "SHAPE@", "County_Number", "County_Name", "DIRECTION", "COORD"
        ]

        # 2nd SearchCursor
        newCursor = daSearchCursor(tempCountyGradiculePostErase, fieldsToCopy)
        for newRow in newCursor:
            outputFeatureList.append(newRow)

        if 'newCursor' in locals():
            del newCursor
        else:
            pass

    try:
        Delete_management(countyGradiculeShortWithUser)
    except:
        pass

    CreateFeatureclass_management(sqlGdbLocation, countyGradiculeShortNoPath,
                                  "POLYLINE", "", "", "",
                                  spatialReferenceProjection)

    AddField_management(countyGradiculeShortNoPath, "County_Number", "DOUBLE",
                        "", "", "")

    AddField_management(countyGradiculeShortNoPath, "County_Name", "TEXT", "",
                        "", "55")

    AddField_management(countyGradiculeShortNoPath, "DIRECTION", "TEXT", "",
                        "", "5")

    AddField_management(countyGradiculeShortNoPath, "COORD", "TEXT", "", "",
                        "30")

    print "First Intersected County Gradicule Row: " + str(
        outputFeatureList[0])

    newCursor = daInsertCursor(countyGradiculeShortPath, fieldsToCopy)
    counter = 1
    for outputFeature in outputFeatureList:
        rowToInsert = ([outputFeature])

        insertedOID = newCursor.insertRow(outputFeature)

        counter += 1

        print "Inserted Row with Object ID of " + str(insertedOID)

    # Load the feature class. Remove anything shorter than 850 feet.
    MakeFeatureLayer_management(countyGradiculeShortPath,
                                loadedOutputGradicule)

    # Select the rows that have geometry which is shorter than 850 feet.
    ## Note that Shape.STLength() returns units in the projection
    ## or coordinate system that it the feature class is stored in.
    whereClause = """ Shape.STLength() <  850 """
    print "The whereClause is " + str(whereClause)
    SelectLayerByAttribute_management(loadedOutputGradicule, "NEW_SELECTION",
                                      whereClause)

    # If there is at least one row selected, delete each selected row.
    if int(GetCount_management(loadedOutputGradicule).getOutput(0)) > 0:
        print str(GetCount_management(loadedOutputGradicule).getOutput(
            0)) + "rows selected."
        DeleteRows_management(loadedOutputGradicule)
    else:
        print "No rows were selected to delete."

    if 'newCursor' in locals():
        del newCursor
    else:
        pass
def thresholdRemoval(quarterOrHalf):
    # Change to look at the number of roads that are in the
    # county's erased polygon. -- the erased?
    # Then, if there are not at least that % of the
    # roads labeled as points in the pointIntersection
    # layer, remove ALL points for that county.

    # Rewrite this to test for the number of null points.

    # 1 through 100, percentage as integer. ## 25 seems to work well. Results in only 12 counties not having enough points.
    thresholdValue = 25
    # One county (42) doesn't have enough roads information for this script to do anything at all.

    if quarterOrHalf.lower() == "quarter":
        countyRoadNameRosette = countyRoadNameRosette_Q
    elif quarterOrHalf.lower() == "half":
        countyRoadNameRosette = countyRoadNameRosette_H
    else:
        print "quarterOrHalf variable not correctly defined."
        raise (Exception("quarterOrHalf error."))

    #makefeaturelayer1
    MakeFeatureLayer_management(countyRoadsFeature, "loadedCountyRoads")
    #makefeaturelayer2
    MakeFeatureLayer_management(countyRoadNameRosette, "loadedRoadNameRosette")

    for i in xrange(1, 106):
        roadThresholdWhereClause = """ "COUNTY_NUMBER" = """ + str(i) + """ """
        rosetteThresholdWhereClause = """ "COUNTY_NUMBER" = ' """ + str(
            i) + """ ' """

        #selectfeatures1
        SelectLayerByAttribute_management("loadedCountyRoads", "NEW_SELECTION",
                                          roadThresholdWhereClause)
        #selectfeatures2
        SelectLayerByAttribute_management("loadedRoadNameRosette",
                                          "NEW_SELECTION",
                                          rosetteThresholdWhereClause)

        #createfeaturelayer with whereclause, or do this then make a select clause.
        countyRoadsCount = GetCount_management("loadedCountyRoads")
        countyPointsCount = GetCount_management("loadedRoadNameRosette")

        countyRoadsCount = int(countyRoadsCount.getOutput(0))
        countyPointsCount = int(countyPointsCount.getOutput(0))

        if countyRoadsCount >= 1:
            if (float(countyPointsCount) / float(countyRoadsCount)) >= (
                    float(thresholdValue) /
                    float(100)) and countyPointsCount >= 20:
                print "Threshold value OK for County Number: " + str(i) + "."
                pass
            else:
                print "Threshold value not met for County Number: " + str(
                    i) + "."
                if countyPointsCount >= 1:
                    print "Removing road name rosette points from this county."
                    DeleteRows_management("loadedRoadNameRosette")
                else:
                    print "Would have deleted the points for this county, but none exist to delete."
        else:
            print "No County Roads found for County Number: " + str(i) + "."
Esempio n. 19
0
def StateHighwayCalibrate(theStateHighwaySegments):
    #theStateHighwaySegments is defined as the roadway segments intended for calibration to the EXOR measures
    #this function is being called by

    #here are the GIS routes with measures extracted regularly from EXOR using the FME extraction and python route reference tools from DT00ar60
    #these routes contain the current exor measures
    #Smlrs = r'Database Connections\RO@sqlgisprod_GIS_cansys.sde\GIS_CANSYS.SHARED.SMLRS'

    #need to define a route source represntative of the correct network year, about 2015
    #should have K-10 on 23rd street still

    Cmlrs = r'Database Connections\RO@sqlgisprod_GIS_cansys.sde\GIS_CANSYS.SHARED.CMLRS_2015'

    from arcpy import FeatureClassToFeatureClass_conversion, FeatureVerticesToPoints_management, LocateFeaturesAlongRoutes_lr, CalculateField_management
    from arcpy import env, MakeFeatureLayer_management, SelectLayerByAttribute_management, DeleteRows_management, AddJoin_management, AddField_management, RemoveJoin_management
    env.overwriteOutput = 1
    # Start by loading NG911 aggregated, conflated road centerlines to an in-memory feature class

    #FeatureClassToFeatureClass_conversion(Roads, "in_memory", "RoadCenterlines", "StateKey1 IS NOT NULL ")
    #FeatureClassToFeatureClass_conversion(Roads, "in_memory", "RoadCenterlines", "StateKey1 IS NOT NULL ")

    MakeFeatureLayer_management(theStateHighwaySegments,
                                "CalibrateRoadCenterlines")
    RoadCenterlines = "CalibrateRoadCenterlines"
    #these are the two linear referencing networks we're going to use to calibrate the state highway system
    #for iteration 2, no source data should refer to the state LRM, so we're only doing the County LRM
    Lrm_Dict = {'COUNTY': Cmlrs}

    #and this is the beginning and end of a line, for which we are going to create a vertex point
    End_List = ['START', 'END']

    # First,  create points at the begin and end of each road centerline segment using Vertices to Points.
    for end in End_List:
        i_end_output = "in_memory/CalibrationPoint" + str(end)
        FeatureVerticesToPoints_management(RoadCenterlines, i_end_output,
                                           str(end))

    #Iterate through the LRMs to bring them into memory and do the processing for each segment begin and end point!
    for key, value in Lrm_Dict.items():
        FeatureClassToFeatureClass_conversion(value, "in_memory",
                                              "LRM" + str(key))
        for end in End_List:
            outtable = "in_memory/" + str(end) + "_" + str(key)
            outstore = spampathfd + r"/" + str(end) + "_" + str(key)
            outproperties = str(key) + "_LRS POINT MEAS_" + str(key)
            if key == "STATE":
                lrskey = str(key) + "_NQR_DESCRIPTION"
            else:
                lrskey = "NQR_DESCRIPTION"
            LocateFeaturesAlongRoutes_lr(
                "in_memory/CalibrationPoint" + str(end),
                "in_memory/LRM" + str(key), lrskey, "500 Feet", outtable,
                outproperties, "ALL", "DISTANCE", "ZERO", "FIELDS",
                "M_DIRECTON")

            #that LFAR function located begin/end segment points to ALL ROUTES within 500 feet of the segment endpoint

            #for calibrating, we are only interested in the points and LFAR Results that where this query is NOT true:
            qNotThisRoad = '"COUNTY_LRS" <> "KDOT_LRS_KEY"'
            #so we will delete the records where this query is trye
            SelectLayerByAttribute_management(
                str(end) + "_" + str(key), "NEW_SELECTION", qNotThisRoad)
            DeleteRows_management(str(end) + "_" + str(key))
            #DeleteField_management(outtable, "Mileage_Length;Mileage_Logmile;ROUTE_PREFIX_TARGET;LRS_ROUTE_NUM_TARGET;LRS_UNIQUE_TARGET;Non_State_System_OBJECTID;LRS_BACKWARD;F_CNTY_2;T_CNTY_2;F_STAT_2;T_STAT_2;CountyKey2;MileFlipCheck;InLine_FID;SimLnFLag")

            #TableToTable_conversion(outtable, ConflationDatabase, outstore)
            #One Method, if using SQL Server, is to use table to table conversion to export to SQL server, then run these query in #CalcUsingSQLserver()
            #If not using SQL server this will suffice, although if there are multiple orig FID's to the original data source FID, there's no logic or handling to discern between the many to one relationship.
            #In the case of hte many to one, or duplicate Orig_FID in the measure table, it might be desirable to choose the closest result
            #A few of the duplicates I reviewed had identical measure values, if that's always the case, then handling the duplicates is unnecessary
            measfield = str(end) + "_" + str(key) + "_meas"
            try:
                AddField_management(theStateHighwaySegments, measfield,
                                    "DOUBLE", "", "", "", "", "NULLABLE",
                                    "NON_REQUIRED")
            except:
                print "could not add the field for calibrated measures"
            jointable = str(end) + "_" + str(key)
            AddJoin_management(theStateHighwaySegments, "OBJECTID", jointable,
                               "ORIG_FID", "KEEP_ALL")
            exp = "!" + jointable + ".MEAS_" + str(key) + "!"
            measfieldcalc = theStateHighwaySegments + "." + measfield
            CalculateField_management(theStateHighwaySegments, measfieldcalc,
                                      exp, "PYTHON")
            RemoveJoin_management(theStateHighwaySegments)

        # NEed to now test for direction again based on begin < end, handle flipping and assemble the routes
    from arcpy import CreateRoutes_lr
    CreateRoutes_lr("CalibrateRoadCenterlines", "KDOT_LRS_KEY",
                    "in_memory/Simplified_CreateRoutes_test1", "TWO_FIELDS",
                    "START_COUNTY_meas", "END_COUNTY_meas", "UPPER_LEFT", "1",
                    "0", "IGNORE", "INDEX")
def StateHighwayCalibrate():
    import datetime
    startDateTime = datetime.datetime.now()
    print("starting SHS calibration at " + str(startDateTime) +
          ", it should take about 15 minutes to calibrate state system routes")
    #Calibration process completed in 0:03:36.252839 hours, minutes, seconds
    from KhubCode25.KhubCode25Config import devorprod, dbname, dbownername, localProFileGDBWorkspace, KDOTConnections, Cmlrs, prodDataSourceSDE, devDataSourceSDE
    fileformatDateStr = startDateTime.strftime("%Y%m%d")
    #theStateHighwaySegments is defined as the roadway segments intended for calibration to the EXOR measures
    if devorprod == 'prod':
        database = prodDataSourceSDE

        print("running on " + devorprod)
    else:
        database = devDataSourceSDE

        print("running on " + devorprod)
    from arcpy import FeatureClassToFeatureClass_conversion, Delete_management, FeatureVerticesToPoints_management, LocateFeaturesAlongRoutes_lr, CreateFileGDB_management, env, MakeFeatureLayer_management, SelectLayerByAttribute_management, DeleteRows_management, MakeTableView_management
    env.overwriteOutput = 1
    try:
        CreateFileGDB_management(localProFileGDBWorkspace,
                                 "KhubRoadCenterlinesTemp" + fileformatDateStr,
                                 "CURRENT")
    except:
        Delete_management(localProFileGDBWorkspace,
                          "KhubRoadCenterlinesTemp" + fileformatDateStr)
        CreateFileGDB_management(localProFileGDBWorkspace,
                                 "KhubRoadCenterlinesTemp" + fileformatDateStr,
                                 "CURRENT")

    #stopped using in_memory after the upgrade to arcgis pro, it doesn't work like it used to do.
    #consider using in memory for not in non-pro script environment, but for this process, probably will not make much difference

    localfilegdb = localProFileGDBWorkspace + '\\' + 'KhubRoadCenterlinesTemp' + fileformatDateStr + '.gdb'
    sdegdb = KDOTConnections + r'\\' + database + r'\\' + dbname + "." + dbownername
    firstCut = sdegdb + ".All_Road_Centerlines"
    NextIter = "_D1"
    nextCut = firstCut + NextIter
    RoadsToCalibrate = [firstCut, nextCut]

    CMLRS = sdegdb + "." + Cmlrs
    Lrm_Dict = {'COUNTY': CMLRS}
    for sderoads in RoadsToCalibrate:
        if sderoads[-3:] == "_D1":
            MakeFeatureLayer_management(
                sderoads, "lyrStateSystemSource" + NextIter,
                "LRS_ROUTE_PREFIX IN ('I', 'U', 'K') And LRS_ROUTE_SUFFIX NOT IN ('Z', 'G')",
                None, "")
            RoadCenterlines = localfilegdb + "/lyrStateSystemSource" + NextIter
            End_List = ['START', 'END']
            # First,  create points at the begin and end of each road centerline segment using Vertices to Points.
            for end in End_List:
                end_name = end + NextIter
                i_end_output = localfilegdb + "/CalibrationPoint" + end_name
                try:
                    FeatureVerticesToPoints_management(RoadCenterlines,
                                                       i_end_output, str(end))
                    #this works in Pro
                except:
                    FeatureVerticesToPoints_management(
                        "lyrStateSystemSource" + NextIter, i_end_output,
                        str(end))
        #and this is the beginning and end of a line, for which we are going to create a vertex point
        #Iterate through the LRMs to bring them into memory and do the processing for each segment begin and end point!
                for key, value in Lrm_Dict.items():
                    FeatureClassToFeatureClass_conversion(
                        value, localfilegdb, "LRM" + str(key))
                    for end in End_List:
                        outtable = localfilegdb + r"/" + str(
                            end_name) + "_" + str(key)
                        outproperties = str(key) + "_LRS POINT MEAS_" + str(
                            key)
                        if key == "STATE":
                            lrskey = str(key) + "_NQR_DESCRIPTION"
                        else:
                            lrskey = "NQR_DESCRIPTION"
                        try:
                            LocateFeaturesAlongRoutes_lr(
                                localfilegdb + r"/CalibrationPoint" +
                                str(end_name), "LRM" + str(key), lrskey,
                                "500 Feet", outtable, outproperties, "ALL",
                                "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")
                            #this works in Pro
                        except:
                            LocateFeaturesAlongRoutes_lr(
                                localfilegdb + "/CalibrationPoint" +
                                str(end_name),
                                localfilegdb + r"/LRM" + str(key), lrskey,
                                "500 Feet", outtable, outproperties, "ALL",
                                "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")
                            #this works in non-Pro script environment

                        #that LFAR function located begin/end segment points to ALL ROUTES within 500 feet of the segment endpoint

                        #for calibrating, we are only interested in the points and LFAR Results that where this query is NOT true:
                        qNotThisRoad = 'SUBSTRING("COUNTY_LRS",0,10) <> SUBSTRING("KDOT_LRS_KEY",0,10)'
                        #so we will delete the records where this query is trye
                        try:
                            SelectLayerByAttribute_management(
                                str(end_name) + "_" + str(key),
                                "NEW_SELECTION", qNotThisRoad)
                            DeleteRows_management(
                                str(end_name) + "_" + str(key))
                            #this works in Pro Environment
                        except:
                            #SelectLayerByAttribute_management(localfilegdb+"/"+str(end)+"_"+str(key), "NEW_SELECTION", qNotThisRoad)
                            MakeTableView_management(
                                localfilegdb + "/" + str(end_name) + "_" +
                                str(key), "deleterows", qNotThisRoad, None, "")
                            DeleteRows_management("deleterows")
                            #this works in non-Pro script environment
                #this works in non-Pro script environment
        else:
            MakeFeatureLayer_management(
                sderoads, "lyrStateSystemSource",
                "LRS_ROUTE_PREFIX IN ('I', 'U', 'K') And LRS_ROUTE_SUFFIX NOT IN ('Z', 'G')",
                None, "")
            RoadCenterlines = localfilegdb + "/lyrStateSystemSource"
            End_List = ['START', 'END']
            for end in End_List:
                end_name = end
                print(end_name)
                i_end_output = localfilegdb + "/CalibrationPoint" + str(end)
                try:
                    FeatureVerticesToPoints_management(RoadCenterlines,
                                                       i_end_output, str(end))
                    #this works in Pro
                except:
                    FeatureVerticesToPoints_management("lyrStateSystemSource",
                                                       i_end_output, str(end))
        #and this is the beginning and end of a line, for which we are going to create a vertex point
        #Iterate through the LRMs to bring them into memory and do the processing for each segment begin and end point!
                for key, value in Lrm_Dict.items():
                    FeatureClassToFeatureClass_conversion(
                        value, localfilegdb, "LRM" + str(key))
                    for end in End_List:
                        outtable = localfilegdb + r"/" + str(
                            end_name) + "_" + str(key)
                        outproperties = str(key) + "_LRS POINT MEAS_" + str(
                            key)
                        if key == "STATE":
                            lrskey = str(key) + "_NQR_DESCRIPTION"
                        else:
                            lrskey = "NQR_DESCRIPTION"
                        try:
                            LocateFeaturesAlongRoutes_lr(
                                localfilegdb + r"/CalibrationPoint" +
                                str(end_name), "LRM" + str(key), lrskey,
                                "500 Feet", outtable, outproperties, "ALL",
                                "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")
                            #this works in Pro
                        except:
                            LocateFeaturesAlongRoutes_lr(
                                localfilegdb + "/CalibrationPoint" +
                                str(end_name),
                                localfilegdb + r"/LRM" + str(key), lrskey,
                                "500 Feet", outtable, outproperties, "ALL",
                                "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON")
                            #this works in non-Pro script environment

                        #that LFAR function located begin/end segment points to ALL ROUTES within 500 feet of the segment endpoint

                        #for calibrating, we are only interested in the points and LFAR Results that where this query is NOT true:
                        qNotThisRoad = 'SUBSTRING("COUNTY_LRS",0,10) <> SUBSTRING("KDOT_LRS_KEY",0,10)'
                        #so we will delete the records where this query is true
                        #It is possible that there will be multiple rows where this query is true, this result
                        #will calculate one value, not conditional on distance, min/max, just takes the first true result I guess
                        #in situations where there are multiple results, consider dissolving these points and keeping some stats
                        #then review stats to determine appropriate value, probably the closest result
                        try:
                            SelectLayerByAttribute_management(
                                str(end_name) + "_" + str(key),
                                "NEW_SELECTION", qNotThisRoad)
                            DeleteRows_management(
                                str(end_name) + "_" + str(key))
                            #this works in Pro Environment
                        except:
                            #SelectLayerByAttribute_management(localfilegdb+"/"+str(end)+"_"+str(key), "NEW_SELECTION", qNotThisRoad)
                            MakeTableView_management(
                                localfilegdb + "/" + str(end_name) + "_" +
                                str(key), "deleterows", qNotThisRoad, None, "")
                            DeleteRows_management("deleterows")
                            #this works in non-Pro script environment
    print(
        'Calibration process completed in {} hours, minutes, seconds.'.format(
            datetime.datetime.now() - startDateTime))
Esempio n. 21
0
def SecondaryDirectionFinder():
    #make a point at the center of each line segment for a highway
    #No way currently to do this for RML highways,  NUSYS provides the directional layer for C highwyas
    #FeatureToPoint_management("State Highways", "in_memory/HighwayPoints", "INSIDE")
    #maybe two ways to do this, this time, I'm using midpoint from feature to point - catch
    FeatureVerticesToPoints_management(
        "RoadCenterlines",
        out_feature_class="in_memory/HighwayPoints",
        point_location="MID")
    #FeatureToPoint_management(Roads, "in_memory/HighwayPoints", "INSIDE")
    FeatureClassToFeatureClass_conversion(
        SRND,
        "in_memory",
        "SRND_NPD",
        where_clause="NETWORK_DIRECTION IN ( 'SB' , 'WB' )")

    #CRND Secondary Direction is the CRND layer where "NETWORK_DIRECTION IN ( 'SB' , 'WB' )"
    #Locate Features Along Routes will calculate a +/- offset distance of the dual carriageway points from the CRND centerline
    LocateFeaturesAlongRoutes_lr("HighwayPoints", "SRND_NPD", "NE_UNIQUE",
                                 "500 Feet", "in_memory/HighwayPointsMeasures",
                                 "RID POINT MEAS", "ALL", "DISTANCE", "ZERO",
                                 "FIELDS", "M_DIRECTON")

    #All the Highway Points are no longer needed and can be freed from memory
    Delete_management("HighwayPoints")

    #since we located along all routes instead of the nearest routes, we need to select the rows that have the correct LRS key
    #SelectLayerByAttribute_management("in_memory/HighwayPointsMeasures", "NEW_SELECTION", """SUBSTRING( "RID", 1, 7) NOT  LIKE SUBSTRING("StateKey1", 4, 7)""")

    #Using State_System_LRSKey from Conflation just in case the StateKey1 has not yet been added or calculated.
    SelectLayerByAttribute_management(
        "HighwayPointsMeasures", "NEW_SELECTION",
        """SUBSTRING( "RID", 1, 7) NOT  LIKE SUBSTRING("State_System_LRSKey" ,4, 7)"""
    )
    DeleteRows_management(in_rows="HighwayPointsMeasures")
    # point events are located along the routes that KDOT says are divided on the dual carriageway
    MakeRouteEventLayer_lr("SRND_NPD",
                           "NE_UNIQUE",
                           "HighwayPointsMeasures",
                           "rid POINT MEAS",
                           "HighwayPointEvents",
                           offset_field="Distance",
                           add_error_field="ERROR_FIELD",
                           add_angle_field="ANGLE_FIELD",
                           angle_type="NORMAL",
                           complement_angle="ANGLE",
                           offset_direction="RIGHT",
                           point_event_type="POINT")

    #select the secondary direction points based on the offset direction
    MakeFeatureLayer_management("HighwayPointEvents", "NPD_ID",
                                """"Distance">=0""")

    # some random points, due to the 500 ft buffer I think, are getting included.  Select the event points that do not intersect a state highway road centerline feature
    #
    SelectLayerByLocation_management("NPD_ID", "INTERSECT", "RoadCenterlines",
                                     "1 Feet", "NEW_SELECTION")

    #sometimes (center turn lanes, left turn lanes, painted medians, median terminus locations)
    #there is a difference between what KDOT says is Divided and the dual carriagway geometry.
    #Consider this and determine how to handle divided highways in the route Keys/route structures.
    #this next step will only factor in the

    FeatureClassToFeatureClass_conversion(
        "NPD_ID",
        ConflationDatabase + "\\" + GeodatabaseName + ".SDE.KANSAS_DOT",
        "Non_Primary_Divided_Highway", '"Distance">=0')
Esempio n. 22
0
def calibrationCCL():

    print "deriving CCL LRS starting points and calibrations"
    CCLEnd = "!CCL.DBO.CITY_CONNECTING_LINK_STATE.MAX_END_STATE_LOGMILE!- !CCL.DBO.CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!"
    CCLBeg = "!CCL.DBO.CITY_CONNECTING_LINK_STATE.MIN_BEG_STATE_LOGMILE! - !CCL.DBO.CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!"
    MakeFeatureLayer_management(LineFeatureClass, "CITY_CONNECTING_LINK_RESET")
    resln = "CITY_CONNECTING_LINK_RESET"
    AddField_management(resln, "CCL_BEGIN", "DOUBLE", 12, 3)
    AddField_management(resln, "CCL_END", "DOUBLE", 12, 3)
    AddJoin_management("CITY_CONNECTING_LINK_RESET", "CCL_LRS",
                       connection1 + "CITY_CONNECTING_LINK_STATE_D", "CCL_LRS",
                       "KEEP_ALL")
    CalculateField_management(resln, "CCL_BEGIN", CCLBeg, "PYTHON")
    CalculateField_management(resln, "CCL_END", CCLEnd, "PYTHON")
    print "calibrating LRS - point calibration method"
    statecalpoints = stateroutelyr + "_Point"
    Intersect_analysis(
        "CCL.DBO.CITY_CONNECTING_LINK_STATE_D #;'Database Connections/SQL61_GIS_CANSYS_RO.sde/GIS_CANSYS.DBO.SMLRS_Point' #",
        "Database Connections/SQL61_GEOADMIN_CCL.sde/CALIBRATION_POINTS_CCL",
        "ALL", "#", "POINT")
    querystr = "Substring( CCL_LRS,4, 12)<> LRS_ROUTE"
    SelectLayerByAttribute_management("CCL.DBO.CALIBRATION_POINTS_CCL",
                                      "NEW_SELECTION", querystr)
    DeleteRows_management("CCL.DBO.CALIBRATION_POINTS_CCL")

    DeleteIdentical_management("CCL.DBO.CALIBRATION_POINTS_CCL",
                               "LRS_KEY;POINT_X;POINT_Y;POINT_M", "#", "0")
    AddField_management("CCL.DBO.CITY_CONNECTING_LINK_STATE", "CCL_BEGIN",
                        "DOUBLE", "#", "#", "#", "#", "NULLABLE",
                        "NON_REQUIRED", "#")
    AddField_management("CCL.DBO.CITY_CONNECTING_LINK_STATE", "CCL_BEGIN",
                        "DOUBLE", "#", "#", "#", "#", "NULLABLE",
                        "NON_REQUIRED", "#")
    AddJoin_management("CCL.DBO.CITY_CONNECTING_LINK_STATE", "CCL_LRS",
                       "CCL.DBO.CITY_CONNECTING_LINK_STATE_D", "CCL_LRS",
                       "KEEP_ALL")
    CalculateField_management(
        "CCL.DBO.CITY_CONNECTING_LINK_STATE",
        "CCL.DBO.CITY_CONNECTING_LINK_STATE.CCL_BEGIN",
        "!CCL.DBO.CITY_CONNECTING_LINK_STATE.MIN_BEG_STATE_LOGMILE!- !CCL.DBO.CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!",
        "PYTHON", "#")
    CalculateField_management(
        "CCL.DBO.CITY_CONNECTING_LINK_STATE",
        "CCL.DBO.CITY_CONNECTING_LINK_STATE.CCL_END",
        "!CCL.DBO.CITY_CONNECTING_LINK_STATE.MAX_END_STATE_LOGMILE!- !CCL.DBO.CITY_CONNECTING_LINK_STATE_D.MIN_BEG_STATE_LOGMILE!",
        "PYTHON", "#")
    inprops = str(ReferenceRouteKey + " POINT MEASURE")
    AddField_management(connection1 + "CALIBRATION_POINTS_CCL", "CCL_MEASURE",
                        "DOUBLE", 12, 3)
    CalculateField_management("CCL.DBO.CALIBRATION_POINTS_CCL", "CCL_MEASURE",
                              "!POINT_M!- !MIN_BEG_STATE_LOGMILE!", "PYTHON",
                              "#")
    CreateRoutes_lr(LineFeatureClass, NewRouteKey,
                    connection1 + NewRoute + "base", "TWO_FIELDS", NewBeg,
                    NewEnd, "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")
    CalibrateRoutes_lr(
        "Database Connections/SQL61_GEOADMIN_CCL.sde/CCL.DBO.CCL_LRS_ROUTEbase",
        "CCL_LRS",
        "Database Connections/SQL61_GEOADMIN_CCL.sde/CCL.DBO.CALIBRATION_POINTS_CCL",
        "CCL_LRS", "CCL_MEASURE",
        "Database Connections/SQL61_GEOADMIN_CCL.sde/CCL.DBO.CCL_LRS_ROUTE",
        "DISTANCE", "1 Feet", "BETWEEN", "NO_BEFORE", "NO_AFTER", "IGNORE",
        "KEEP", "INDEX")
    AddField_management(connection1 + NewRoute, "NETWORKDATE", "DATE")
    CalculateField_management(connection1 + NewRoute, "NETWORKDATE",
                              "datetime.datetime.now( )", "PYTHON_9.3", "#")
    MakeFeatureLayer_management(connection1 + "CCL_LRS_ROUTE", NewRoute)
Esempio n. 23
0
def TnA():
    try:
        env.workspace = stagews
        #copying oracle tables to memory
        print str(datetime.datetime.now()) + ' copying oracle tables to memory'
        FeatureClassToFeatureClass_conversion(sdeCDRS,"in_memory","Construction","#","ALERT_STATUS <>  3")
        MakeQueryTable_management(sdeCDRSWZ,"wz1","USE_KEY_FIELDS","KANROAD.CDRS_WZ_DETAIL.CDRS_WZ_DETAIL_ID",
                                """KANROAD.CDRS_WZ_DETAIL.CDRS_WZ_DETAIL_ID #;KANROAD.CDRS_WZ_DETAIL.CDRS_DETOUR_TYPE_ID #;
                                KANROAD.CDRS_WZ_DETAIL.WORK_ZONE_DESC #;KANROAD.CDRS_WZ_DETAIL.WORK_ZONE_SPEED_RESTRIC #;
                                KANROAD.CDRS_WZ_DETAIL.DETOUR_TYPE_TXT #;KANROAD.CDRS_WZ_DETAIL.DETOUR_SPEED_RESTRIC #;
                                KANROAD.CDRS_WZ_DETAIL.DETOUR_DESC #""", "#")
        TableToTable_conversion("wz1", 'in_memory', 'wz')
        #Joining the Oracle CDRS WZ table
        print str(datetime.datetime.now()) + " Joining the Oracle CDRS WZ table"
        MakeFeatureLayer_management("Construction", "ConstJoin")
        AddJoin_management("ConstJoin","CDRS_WZ_DETAIL_ID","wz","KANROAD_CDRS_WZ_DETAIL_CDRS_WZ_DETAIL_ID","KEEP_ALL")
        FeatureClassToFeatureClass_conversion("ConstJoin","in_memory","CDRS","#",'ConstJoin.ALERT_STATUS <  3', "#")
        #reformatting the Route name for US routes
        print str(datetime.datetime.now()) + " reformatting the Route name for US routes"
        AddField_management("CDRS", "RouteName", "TEXT", "#", "10")
        routenamed = '!Construction_BEG_LRS_ROUTE![0:1] +str(!Construction_BEG_LRS_ROUTE![3:6]).lstrip("0")'  # calculation expression
        #Calculate the Route names for User Display
        print routenamed
        CalculateField_management("CDRS", "RouteName", routenamed, "PYTHON_9.3","#") 
        AddField_management("CDRS", "STATUS", "TEXT", "#", "10")
        AddField_management("CDRS", "Alert_Status_I", "LONG", "#", "#")
        CalculateField_management("CDRS", "Alert_Status_I", '!Construction_ALERT_STATUS!' , "PYTHON_9.3", "#") 
        #Assigning projection for KanRoad CDRS Alert Route Layer
        print str(datetime.datetime.now()) + " Assigning projection for KanRoad CDRS Alert Route Layer"
        DefineProjection_management("CDRS", lambertCC)
        #reformatting the Route name for US routes
        print str(datetime.datetime.now()) + " reformatting the Route name for US routes"
        MakeFeatureLayer_management("CDRS", "ACTIVERoutes", '"Construction_ALERT_STATUS" =  2' )
        CalculateField_management("ACTIVERoutes","STATUS",'"Active"',"PYTHON_9.3","#") 
        
        MakeFeatureLayer_management("CDRS", "ClosedRoutes", '"Construction_ALERT_STATUS" =  2 AND "Construction_FEA_CLOSED" =  1')
        CalculateField_management("ClosedRoutes","STATUS",'"Closed"',"PYTHON_9.3","#") 
        
        MakeFeatureLayer_management("CDRS", "PlannedRoutes", '"Construction_ALERT_STATUS" =  1' )
        CalculateField_management("PlannedRoutes","STATUS",'"Planned"',"PYTHON_9.3","#")
        
        #copying joined oracle tables to memory for loading in Wichway Schema
        print str(datetime.datetime.now()) + " copying joined oracle tables to memory for loading in Wichway Schema"
        FeatureClassToFeatureClass_conversion(sdeKandriveConstruction, "in_memory", "CDRS_Segments", "#", "#")
        
        #delete rows in the destination feature class
        DeleteRows_management("CDRS_Segments")
        
        ###############################################################################################################
        # Maintainability information:
        # If you need to add another field to transfer between the two, just add it to the searchCursorFields and the
        # insertCursorFields lists and make sure that it is in the same position in the list order for both of
        # them.
        # Besides 'LoadDate', the order does not matter, so long as each field name in the
        # searchCursorFields has a counterpart in the insertCursorFields and vice versa.
        # 'LoadDate' should always be last for the insertCursorFields as it is appended to each row after all
        # of the other items from the searchCursorFields.
        ###############################################################################################################
        
        featuresToTransfer = list()
        
        # searchCursorFields go to "in_memory\CDRS". (Input table)
        searchCursorFields = ['SHAPE@', 'RouteName', 'Construction_BEG_STATE_LOGMILE', 'Construction_END_STATE_LOGMILE', 'Construction_BEG_COUNTY_NAME', 
                            'Construction_ALERT_DATE', 'Construction_COMP_DATE', 'Construction_ALERT_TYPE_TXT', 'Construction_ALERT_DESC_TXT',
                            'Construction_VERT_RESTRICTION', 'Construction_WIDTH_RESTRICTION', 'Construction_TIME_DELAY_TXT',
                            'Construction_PUBLIC_COMMENT', 'wz_KANROAD_CDRS_WZ_DETAIL_DETOUR_TYPE_TXT',
                            'wz_KANROAD_CDRS_WZ_DETAIL_DETOUR_DESC', 'Construction_CONTACT_NAME', 'Construction_CONTACT_PHONE', 
                            'Construction_CONTACT_EMAIL', 'Construction_ALERT_HYPERLINK',  'Alert_Status_I',
                            'Construction_FEA_CLOSED', 'STATUS', 'Construction_ALERT_DIREC_TXT', 'Construction_BEG_LONGITUDE',
                            'Construction_BEG_LATITUDE']
        
        # insertCursorFields go to sdeKandriveConstruction. (Output table)
        insertCursorFields = ['SHAPE@', 'RouteName', 'BeginMP', 'EndMP', 'County', 'StartDate', 'CompDate', 'AlertType', 'AlertDescription',
                            'HeightLimit', 'WidthLimit', 'TimeDelay', 'Comments', 'DetourType', 'DetourDescription',
                            'ContactName', 'ContactPhone', 'ContactEmail', 'WebLink',  'AlertStatus', 'FeaClosed', 'Status',
                            'AlertDirectTxt', 'X', 'Y', 'LoadDate']
        
        cdrsSearchCursor = daSearchCursor(r"in_memory\CDRS", searchCursorFields, """ "Alert_Status_I" <> 3""")
        
        for cdrsCursorItem in cdrsSearchCursor:
            featureItem = list(cdrsCursorItem)
            featureItem.append(starttime)
            featuresToTransfer.append(featureItem)
        
        ##Debug
        for feature in featuresToTransfer:
            print feature
        ##
        
        RemoveJoin_management("ConstJoin", "wz")
        
        #truncating CDRS segments in WICHWAY SPATIAL
        print str(datetime.datetime.now()) + " truncating CDRS segments in WICHWAY SPATIAL"
        
        TruncateTable_management(sdeKandriveConstruction)
        
        cdrsInsertCursor = daInsertCursor(sdeKandriveConstruction, insertCursorFields)
        
        for cdrsFeature in featuresToTransfer:
            insertOID = cdrsInsertCursor.insertRow(cdrsFeature)
            print "Inserted a row with the OID of: " + str(insertOID)
    
    except:
        print "An error occurred."
        errorItem = sys.exc_info()[1]
        print errorItem.args[0]
        try:
            del errorItem
        except:
            pass
        raise
    finally:
        try:
            del cdrsSearchCursor
        except:
            pass
        try:
            del cdrsInsertCursor
        except:
            pass