def ExportGISProdLyrs(owner_workspace, admin_workspace): #similar to CANP, only for layers in another geodatabase, like GISPROD. owner = "GIS_CANSYS.SHARED." outpre = owner_workspace + "/" + owner print "exporting initialized at " + str(datetime.datetime.now()) destConnection = owner_workspace #once again, this could be change to the admin workspace for lyr in mapping.ListLayers(mxd): if lyr.name in gisprodlist: try: #manipulate the layer name a little bit differently lyrname = lyr.name[7:] print lyrname + " exporting..." outlyrname = lyrname outlyrobj = outpre + outlyrname Unlock(admin_workspace) FeatureClassToFeatureClass_conversion(lyr, destConnection, outlyrname, "#", "#", "#") ChangePrivileges_management(outlyrobj, "readonly", "GRANT", "AS_IS") print lyrname + " exported to " + outlyrname + " " + str( datetime.datetime.now()) except ExecuteError: msgs = GetMessages(2) AddError(msgs) print msgs endingTime = datetime.datetime.now() ScriptStatusLogging('CANP_LRS_EXPORT.py', 'ExportGISProdLyrs Function', scriptFailure, startingTime, endingTime, GetMessages(2)) pass except (RuntimeError, TypeError, NameError): print "TypeError on item" + lyr.name endingTime = datetime.datetime.now() ScriptStatusLogging('CANP_LRS_EXPORT.py', 'ExportGISProdLyrs Function', scriptFailure, startingTime, endingTime, GetMessages(2)) pass except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) msgs = "ArcPy ERRORS:\n" + GetMessages(2) + "\n" print pymsg + "\n" print msgs endingTime = datetime.datetime.now() ScriptStatusLogging('CANP_LRS_EXPORT.py', 'ExportGISProdLyrs Function', scriptFailure, startingTime, endingTime, GetMessages(2)) else: #print lyr.name +" was not in the export list and will be skipped" return
def AttribFields(fc, tbl, layer_name, table_name, workspace): #Updates the crossing type attribute values in the GIS database from the CANSYS table. I believe this should work but needs to be tested more. try: MakeFeatureLayer_management(fc, layer_name) MakeQueryTable_management(tbl, table_name, "USE_KEY_FIELDS", "CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGID", "#", "#") AddJoin_management(layer_name, "CROSSINGID", table_name, "CROSSINGID", "KEEP_ALL") SelectLayerByAttribute_management( layer_name, "NEW_SELECTION", "CIIMS.Static_Crossings.CROSSINGTYPE <> vwcrossings3.CROSSINGTYPE") with da.Editor( workspace) as edit: # @UnusedVariable @UndefinedVariable CalculateField_management(layer_name, 'CIIMS.Static_Crossings.CROSSINGTYPE', '!vwcrossings3.CROSSINGTYPE!', 'PYTHON_9.3') CalculateField_management(layer_name, "CIIMS.Static_Crossings.LOADDATE", "datetime.datetime.now( )", "PYTHON_9.3", "#") del layer_name, fc, table_name, tbl print "attrib fields updated for crossing type" except ExecuteError: print(GetMessages(2)) endingTime = datetime.datetime.now() ScriptStatusLogging('POINT_UPDATE_PROD.py', 'CIIMS.Static_Crossings', scriptFailure, startingTime, endingTime, GetMessages(2))
def LatLongFields(fc, tbl, layer_name, table_name, workspace): #Updates the XY attributes values in the GIS database from the CANSYS table try: MakeFeatureLayer_management(fc, layer_name) MakeQueryTable_management(tbl, table_name, "USE_KEY_FIELDS", "CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGID", "#", "#") AddJoin_management(layer_name, "CROSSINGID", table_name, "CROSSINGID", "KEEP_ALL") #select the rows where the CIIMS position has been changed SelectLayerByAttribute_management( layer_name, "NEW_SELECTION", "CIIMS.Static_Crossings.CROSSINGLATITUDE <> vwcrossings3.CROSSINGLATITUDE OR CIIMS.Static_Crossings.CROSSINGLONGITUDE <> vwcrossings3.CROSSINGLONGITUDE" ) with da.Editor( workspace) as edit: # @UnusedVariable @UndefinedVariable CalculateField_management( layer_name, 'CIIMS.Static_Crossings.CROSSINGLATITUDE', '!vwcrossings3.CROSSINGLATITUDE!', 'PYTHON_9.3') CalculateField_management( layer_name, 'CIIMS.Static_Crossings.CROSSINGLONGITUDE', '!vwcrossings3.CROSSINGLONGITUDE!', 'PYTHON_9.3') CalculateField_management(layer_name, "CIIMS.Static_Crossings.LOADDATE", "datetime.datetime.now( )", "PYTHON_9.3", "#") del layer_name, fc, table_name, tbl except ExecuteError: print(GetMessages(2)) endingTime = datetime.datetime.now() ScriptStatusLogging('POINT_UPDATE_PROD.py', 'CIIMS.Static_Crossings', scriptFailure, startingTime, endingTime, GetMessages(2))
def PointGEOM(fc, tbl, workspace, layer_name, fields): #Updates the Geometry point location based on the XY attributes in the GIS table, run this after the XY attributes have been updated try: MakeFeatureLayer_management(fc, layer_name) #the tolerance is how close a lat/long field value must match the coordinate position Tolerance = 0.000001 #start the edit operation using the DA cursor edit = da.Editor(workspace) # @UndefinedVariable edit.startEditing() edit.startOperation() with da.UpdateCursor(fc, fields) as ucursor: # @UndefinedVariable for row in ucursor: #rows 0 and 1 are the lat long fields in the table point = Point(row[0], row[1]) #row 2 is the geometry lat long tuple, and needs to be split in to lat/long parts rowx, rowy = (row[2]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) #compare the lat long table values to the point location if (type(rowx) == float): intolX = abs(row[0] - rowx) intolY = abs(row[1] - rowy) if intolX < Tolerance and intolY < Tolerance: pass else: #if the shape needs to be adjusted, this will update the coordinate position from the feild info point = Point(row[0], row[1]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) print "these rows are outside the position tolerance:" print(rowvalues) ucursor.updateRow(rowvalues) #print (rowvalues) else: point = Point(row[0], row[1]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) print "these rows need to be calculated:" print(rowvalues) ucursor.updateRow(rowvalues) edit.stopOperation() edit.stopEditing(True) del layer_name, fc, fields, workspace print "point geometry updated" except ExecuteError: print(GetMessages(2)) endingTime = datetime.datetime.now() ScriptStatusLogging('POINT_UPDATE_PROD.py', 'CIIMS.Static_Crossings', scriptFailure, startingTime, endingTime, GetMessages(2))
def PostProcLRS(): #this will create the calibrated network LRMs and the calibration points which are useful for other referential methods MResolution = 0.0005 MTolerance = 0.001 env.MTolerance = MTolerance env.MResolution = MResolution try: #copying these layers, the routes measures are already calibrated, measures as they should be #the FC2Fc was changed in the document to show only the primary route, mitigating hte need for the V_LRSNETS view FeatureClassToFeatureClass_conversion( admin_workspace + "/GIS_CANSYS.DBO.V_LRSS_SDO_R", admin_workspace, "SMLRS", "DIRECTION in (1, 2)", "#", "#") ChangePrivileges_management(admin_workspace + "/GIS_CANSYS.DBO.SMLRS", "readonly", "GRANT", "AS_IS") FeatureClassToFeatureClass_conversion( admin_workspace + "/GIS_CANSYS.DBO.V_LRSC_SDO_R", admin_workspace, "CMLRS", "DIRECTION in (1, 2)", "#", "#") ChangePrivileges_management(admin_workspace + "/GIS_CANSYS.DBO.CMLRS", "readonly", "GRANT", "AS_IS") #Oracle EXOR require M values at ever vertex #over time EXOR measures have become a bit of a mess, because of non-functional route calibration tools prior to 2012-2013 #measures should be based on stationing and increase linearly along a project except at the location of an equation #assets are based on whatever section reference they are given, so if the section measures change, so does the asset location except: endingTime = datetime.datetime.now() ScriptStatusLogging('CANP_LRS_EXPORT.py', 'PostProcLRS Function', scriptFailure, startingTime, endingTime, GetMessages(2)) pass
def LatLongFields(fc, tbl, layer_name, table_name, workspace): #Updates the XY attributes values in the GIS database from the CANSYS table try: 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.Static_Crossings.CROSSINGLATITUDE <> CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGLATITUDE OR CIIMS.Static_Crossings.CROSSINGLONGITUDE <> CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGLONGITUDE" ) with da.Editor(workspace) as edit: CalculateField_management( layer_name, 'CIIMS.Static_Crossings.CROSSINGLATITUDE', '!CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGLATITUDE!', 'PYTHON_9.3') CalculateField_management( layer_name, 'CIIMS.Static_Crossings.CROSSINGLONGITUDE', '!CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGLONGITUDE!', 'PYTHON_9.3') CalculateField_management(layer_name, "CIIMS.Static_Crossings.LOADDATE", "datetime.datetime.now( )", "PYTHON_9.3", "#") del layer_name, fc, table_name, tbl except ExecuteError: print(GetMessages(2))
def process_k_value(self): """ Method for verifying the K value and passing it to the back-end processing Also enforces a range of values """ k = self.k_input.get() if k == "" or not k.isdigit(): tkMessageBox.showinfo( "Warning", "Integer K value between 2 - 30 is required!") elif k and not 2 <= int(k) <= 30: tkMessageBox.showinfo( "Warning", "Your Value: {}\nChoose value between 2 - 30".format(k)) else: self.update_status_message(True) k_val = int(k) if k_val: try: self.run_idw_process(k_val) self.create_image(self.frame, new=True) except: raise ValueError( "Unable to process IDW function! \n{}".format( GetMessages()))
def log_GP_msg(self, print_msg=True): """ logs the arcpy messages and prints them to the screen """ from arcpy import GetMessages msgs = GetMessages() try: self.log_msg(msgs, print_msg) except: self.log_msg("error getting arcpy message", print_msg)
def ExportCANPLyrs(): print "exporting initialized at " + str(datetime.datetime.now()) destConnection = r"D:\SQL61_GIS_CANSYS.sde" TableToTable_conversion("MAP_EXTRACT", r"Database Connections/SQL61_GIS_CANSYS.sde", "Map_Extract", "#", "#", "#") for lyr in mapping.ListLayers(mxd): if lyr.name in CANPlist: try: lyrname = lyr.name[11:] print lyrname + " exporting..." outlyrname = "V_" + lyrname outlyrobj = destConnection + "\\GIS_CANSYS.DBO." + outlyrname FeatureClassToFeatureClass_conversion(lyr, destConnection, outlyrname, "#", "#", "#") ChangePrivileges_management(outlyrobj, "readonly", "GRANT", "AS_IS") print lyrname + " exported to " + outlyrname + " " + str( datetime.datetime.now()) except ExecuteError: msgs = GetMessages(2) AddError(msgs) print msgs pass except (RuntimeError, TypeError, NameError): print "TypeError on item" + lyr.name pass except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string # pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) msgs = "ArcPy ERRORS:\n" + GetMessages(2) + "\n" print pymsg + "\n" print msgs else: #print lyr.name +" was not in the export list and will be skipped" pass
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except ExecuteError: msgs = "\n************* \n{}\n************* \n".format(GetMessages(2)) msgs+=func.__name__ print(msgs) log.exception(msgs)
def gateTestCleanup(): try: ######################################################### # DO NOT USE cx_Oracle DELETE on ESRI registered tables.# # Use ESRI tools to delete the rows instead. # ######################################################### deleteList = xrange(4581, 5639) gateTestConn = cx_Oracle.connect('gate/data@GATETEST') firstQuery = """SELECT * FROM GATE.PROCESS_LOG""" #cur1 = sdedev.cursor() #cur1.execute(delrows) cursor1 = gateTestConn.cursor() cursor1.execute(firstQuery) rows = cursor1.fetchall() for row in rows: if row[0] in deleteList: print row[0], row[1] delQuery = """DELETE FROM GATE.PROCESS_LOG WHERE PROCESS_LOG_ID=""" + """'""" + str(row[0]) + """'""" print delQuery cursor1.execute(delQuery) gateTestConn.commit() cursor1.close() gateTestConn.close() if 'cursor1' in locals(): del cursor1 else: pass if 'gateTestConn' in locals(): del gateTestConn else: pass except: print(GetMessages(2)) if 'cursor1' in locals(): del cursor1 else: pass if 'gateTestConn' in locals(): del gateTestConn else: pass
def __getDirectory(self, subDirectory, makeTemp=True): try: if os.path.exists(subDirectory): shutil.rmtree(subDirectory) os.makedirs(subDirectory) #temp dir if makeTemp: os.makedirs(os.path.join(subDirectory, "Temp")) return subDirectory except: x = GetMessages() return subDirectory
def MergeCatchment(self, inbasin): dstemp = None try: catchments = Config()["catchment"] env.workspace = self._TempLocation resultworkspace = os.path.join(self._WorkspaceDirectory, self.WorkspaceID + '.gdb', "Layers") downstreamcatchment = os.path.join(resultworkspace, catchments["downstream"]) dsDesc = Describe(downstreamcatchment) if not Exists(downstreamcatchment): raise Exception("downstream catchment doesn't exist") sr = dsDesc.spatialReference wrkingbasin = self.ProjectFeature(inbasin, sr) #strip downstream catchment from mask dstemp = Erase_analysis(wrkingbasin, downstreamcatchment, "dstemp") #remove any weird verticies associated with Erase globalbasin = os.path.join(resultworkspace, catchments["global"]) FeatureToPolygon_management(dstemp, globalbasin, cluster_tolerance="50 Meters", attributes="ATTRIBUTES", label_features="") #Delete multipolys created by Featuretopolygon maxOid = max({ key: value for (key, value) in arcpy.da.SearchCursor( globalbasin, ['OID@', 'Shape_Area']) }.iteritems(), key=operator.itemgetter(1))[0] with arcpy.da.UpdateCursor(globalbasin, 'OID@') as cursor: for row in cursor: if row[0] != maxOid: cursor.deleteRow() #https://gis.stackexchange.com/questions/152481/how-to-delete-selected-rows-using-arcpy if not Exists(globalbasin): raise Exception("Failed to create basin " + GetMessages()) return True except: tb = traceback.format_exc() self._sm("Merge basin Error " + tb, "ERROR") return False finally: #cleanup for fs in arcpy.ListFeatureClasses(): arcpy.Delete_management(fs) for rs in arcpy.ListRasters(): arcpy.Delete_management(rs)
def logGPMsg(self, printMsg=True): """ printMsg: boolean value whether or not to print the message to the screen Logs the geoprocessing messages and can also print them to the screen if necessary """ msgs = GetMessages() try: self.logMsg(msgs, printMsg) except: self.logMsg('Error getting arcpy message', printMsg)
def f(*args, **kwargs): try: resultValue = func(*args, **kwargs) except ExecuteError: UtilityClassFunctionality.printAndLog( "UtilityClass.captureAndPrintGeoprocessingErrors: Geoprocessing Error.\n{}" .format(GetMessages(2)), UtilityClassFunctionality.ERROR_LEVEL) return exit() except Exception as e: UtilityClassFunctionality.printAndLog( "UtilityClass.captureAndPrintGeoprocessingErrors: {}". format(e), UtilityClassFunctionality.ERROR_LEVEL) return exit() return resultValue
def getAliasIDs(inputRoadNamesList, inputRoadsAliasTableView): # Select the road alias entries that # match the names from the input list. # Then, for each matching entry, add that entry's SEGID to the returnList # and return the return list to the calling function so that it is able # to select the proper roads without having to create a join or a relate. inputRoadNamesString = """(""" inputRoadNamesLength = len(inputRoadNamesList) for x in xrange(inputRoadNamesLength): if (x != inputRoadNamesLength - 1): inputRoadNamesString += """'""" + inputRoadNamesList[x] + """',""" else: inputRoadNamesString += """'""" + inputRoadNamesList[x] + """')""" aliasWhereClause = ("""A_RD IN """ + inputRoadNamesString + """ OR """ + """LABEL IN """ + inputRoadNamesString + """ OR """ + """KDOT_ROUTENAME IN """ + inputRoadNamesString) segIDList = list() roadAliasSCFields = ['SEGID'] try: roadAliasSC = SearchCursor(inputRoadsAliasTableView, roadAliasSCFields, aliasWhereClause) for roadAliasItem in roadAliasSC: segIDList.append(roadAliasItem[0]) try: del roadAliasSC except: pass except: AddMessage((GetMessages(2))) # Do a search cursor with the whereclause instead of a Select, then append the # segIDs to a list and return the list of segIDs. Just test to make sure # that you're at least getting some segIDs on the test data part of the time # before implementing this. return segIDList
def main(): from arcpy import CheckExtension, CheckOutExtension, CheckInExtension, ExecuteError, GetMessages class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError subset_image_for_texture(in_image, in_polygon, area, out_image) CheckInExtension("ImageAnalyst") except LicenseError: print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def main(): from arcpy import CheckExtension, CheckOutExtension, CheckInExtension, ExecuteError, GetMessages, AddError class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError try: from PIL import Image except ModuleNotFoundError: from arcpy import AddError AddError( "PILLOW Library Not Detected. Install using Python Manager in ArcGIS Pro" ) print( "PILLOW Library Not Detected. Install using Python Manager in ArcGIS Pro" ) exit() i_list, extent = get_images_and_stats( in_mosaic ) # Obtain image statistics and info from mosaic for processing for i in i_list: # Check that output folder is not the path of i if out_folder == path.dirname(i[0]): AddError( "outFolder cannot be the same folder/directory as images referenced in the mosaic dataset" ) exit() if not path.exists(out_folder): makedirs(out_folder) texture_images(i_list, extent, in_texture, in_polygon, out_folder, method, blur_distance) # Generate Texture-Masked tiles CheckInExtension("ImageAnalyst") except LicenseError: AddError("Image Analyst license is unavailable") print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def main(): from arcpy import ExecuteError, GetMessages, CheckOutExtension, CheckExtension, CheckInExtension class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError seamless_texture(inImg, outImg) CheckInExtension("ImageAnalyst") except LicenseError: print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def main(): from arcpy import CheckExtension, CheckOutExtension, CheckInExtension, ExecuteError, GetMessages class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError create_mask(in_raster, in_polygon, out_raster) CheckInExtension("ImageAnalyst") except LicenseError: print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def las_tiles_to_numpy_pandas(in_lidar_folder, sr, lidar_format, returns, class_codes, format_for_library): class LicenseError(Exception): pass try: if CheckExtension("3D") == "Available": CheckOutExtension("3D") else: # raise a custom exception raise LicenseError if not lidar_format.startswith( "."): # Ensure lidar format contains a format decimal lidar_format = ".{}".format(lidar_format) supported_lidar_formats = [".las", ".zlas"] assert lidar_format in supported_lidar_formats, \ "LiDAR format {0} unsupported. Ensure LiDAR format is in {1}".format(lidar_format, supported_lidar_formats) lidar_tiles = [ f for f in listdir(in_lidar_folder) if f.endswith("{}".format(lidar_format)) ] if len(lidar_tiles) < 1: AddError("No LiDAR tiles detected in input directory") count = 0 for tile in lidar_tiles: AddMessage("processing lidar tile {0} of {1} : {2}".format( count + 1, len(lidar_tiles), tile)) lidar_tile = join(in_lidar_folder, tile) las_tile_to_numpy_pandas(lidar_tile, sr, returns, class_codes, format_for_library) count += 1 AddMessage("processing {} lidar tiles complete".format(count)) # Check back in 3D Analyst license CheckInExtension("3D") except LicenseError: AddError("3D Analyst license is unavailable") except ExecuteError: AddError("3D Analyst license is unavailable") print(GetMessages(2))
def PointGEOM(fc, tbl, workspace, layer_name, fields): #Updates the Geometry point location based on the XY attributes in the GIS table, run this after the XY attributes have been updated try: MakeFeatureLayer_management(fc, layer_name) Tolerance = 0.0000001 #start the edit operation edit = da.Editor(workspace) edit.startEditing() edit.startOperation() with da.UpdateCursor(fc, fields) as ucursor: for row in ucursor: point = Point(row[0], row[1]) rowx, rowy = (row[2]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) if (type(rowx) == float): intolX = abs(row[0] - rowx) intolY = abs(row[1] - rowy) if intolX < Tolerance and intolY < Tolerance: pass else: point = Point(row[0], row[1]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) print(rowvalues) ucursor.updateRow(rowvalues) #print (rowvalues) else: point = Point(row[0], row[1]) rowvalues = (row[0], row[1], point, datetime.datetime.now()) print "these rows are outside the position tolerance:" print(rowvalues) ucursor.updateRow(rowvalues) edit.stopOperation() edit.stopEditing(True) del layer_name, fc, fields, workspace print "point geometry updated" except ExecuteError: print(GetMessages(2))
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) msgs = "\n************* \n{}\n************* \n".format( GetMessages(2)) # Return python error messages for use in script tool or Python window AddError(pymsg) AddError(msgs) # Print Python error messages for use in Python / Python window log.exception(pymsg) log.exception(msgs)
def AttribFields(fc, tbl, layer_name, table_name, workspace): #Updates the crossing type attribute values in the GIS database from the CANSYS table. I believe this should work but needs to be tested more. try: 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.Static_Crossings.CROSSINGTYPE <> CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGTYPE" ) with da.Editor(workspace) as edit: CalculateField_management( layer_name, 'CIIMS.Static_Crossings.CROSSINGTYPE', '!CIIMS.CIIMS_VWCROSSINGGIS3.CROSSINGTYPE!', 'PYTHON_9.3') CalculateField_management(layer_name, "CIIMS.Static_Crossings.LOADDATE", "datetime.datetime.now( )", "PYTHON_9.3", "#") del layer_name, fc, table_name, tbl print "attrib fields updated for crossing type" except ExecuteError: print(GetMessages(2))
def PostProcCalibPts(): #this will create the calibrated network LRMs and the calibration points which are useful for other referential methods MResolution = 0.0005 MTolerance = 0.001 env.MTolerance = MTolerance env.MResolution = MResolution LRM_NAMES = ["SMLRS", "CMLRS"] try: for LRM in LRM_NAMES: print "converting " + LRM + " to point features " + str( datetime.datetime.now()) #this is rather expensive FeatureVerticesToPoints_management( admin_workspace + "/GIS_CANSYS.DBO." + LRM, admin_workspace + "/GIS_CANSYS.DBO." + LRM + "_Point", "ALL") print "adding calibration values to " + LRM + " point features " + str( datetime.datetime.now()) #this is VERY expensive and should be replaced by a da cursor that calculates SHAPE@M #for now this is easy and convenient to attribute the M value at every point AddXY_management(admin_workspace + "/GIS_CANSYS.DBO." + LRM + "_Point") #AddXY might take an hour total for both LRMS print "finished " + LRM + " LRM processing " + str( datetime.datetime.now()) ChangePrivileges_management( admin_workspace + "/GIS_CANSYS.DBO." + LRM + "_Point", "readonly", "GRANT", "AS_IS") except: endingTime = datetime.datetime.now() ScriptStatusLogging( 'CANP_LRS_EXPORT.py', 'PostProcCalibPts Function failed while attempting to calibrate ' + str(LRM), scriptFailure, startingTime, endingTime, GetMessages(2)) pass
def main(): from arcpy import CheckExtension, CheckOutExtension, CheckInExtension, ExecuteError, GetMessages, AddError class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError batch_create_tiled_ortho_mosaics(in_folder, image_format, num_bands, pixel_depth, product_definition, product_band_definitions, pixel_size, out_folder) CheckInExtension("ImageAnalyst") except LicenseError: AddError("Image Analyst license is unavailable") print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def main(): from arcpy import CheckExtension, CheckOutExtension, CheckInExtension, ExecuteError, GetMessages, AddMessage from arcpy.management import BuildPyramids class LicenseError(Exception): pass try: if CheckExtension("ImageAnalyst") == "Available": CheckOutExtension("ImageAnalyst") else: # raise a custom exception raise LicenseError try: from PIL import Image except ModuleNotFoundError: from arcpy import AddError AddError( "PILLOW Library Not Detected. Install using Python Manager in ArcGIS Pro" ) print( "PILLOW Library Not Detected. Install using Python Manager in ArcGIS Pro" ) exit() mask_image(in_image, in_mask, in_texture, out_image, method, blur_distance) AddMessage("Building Pyramids") BuildPyramids(out_image, -1, "NONE", "NEAREST", "DEFAULT", 75, "OVERWRITE") CheckInExtension("ImageAnalyst") except LicenseError: print("Image Analyst license is unavailable") except ExecuteError: print(GetMessages(2))
def geocodeHardcodedAddresses(): try: setGlobals() address = {} #single line input example ## address['SingleLine'] = '2020 SW 32nd Street, Topeka, KS 66611' ## xy = geoCodeAddress(address) ## if xy != None: ## print str(xy[0]) + ' ' + str(xy[1]) #parsed examples address['Street'] = '2020 SW 32nd Street' address['City'] = 'Topeka' address['State']= 'KS' address['Zip']= '66611' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] address['Street'] = '2010 SW 32nd Street' address['City'] = 'Topeka' address['State']= 'KS' address['Zip']= '66611' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] address['Street'] = '2020 SW 32nd terrace' address['City'] = 'Topeka' address['State']= 'KS' address['Zip']= '66611' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] address['Street'] = '5175 Tuttle Cove Rd' address['City'] = 'Manhattan' address['State']= 'KS' address['Zip']= '66502' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] address['Street'] = '2800 SW TOPEKA BLVD' address['City'] = 'TOPEKA' address['State']= 'KS' address['Zip']= '66611' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] address['Street'] = '' address['City'] = 'Topeka' address['State']= 'KS' address['Zip']= '' xy = geoCodeAddress(address) if xy != None: print str(xy[0]) + ' ' + str(xy[1]) + ' ' + address['Street'] + ' ' + address['City'] except: # Return any python specific errors and any error returned by the geoprocessor tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "Python Errors:\nTraceback Info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n" gpmsg = "GP ERRORS:\n" + GetMessages(2) + "\n" print pymsg print gpmsg AddError(pymsg) AddError(gpmsg)
def geoprocessMapExport(countyMapSizesLocation, mapsFolder, outputPDFParentFolder, optionsObject): """ Geoprocessing version of the CountyMapDataDrivenPagesToPDF script. """ # Probably fastest to just rewrite this from scratch/samples. Don't actually need most of what's # going on here except the ability to select a map based on the name/size list. # That's about it. The rest of the function is overly complicated for the process. # Not sure yet if I'm going to write a function to spawn map copies for each county which will # preset the map to that data driven page so that it's on the right one as soon as it opens, # or if I'm going to use the data driven pages to set the data driven page on each call of the # script after opening the proper map document. -- The second approach seems like a better one # but the first just has the disadvantage of creating many more map documents, which are essentially # just data taking up a bit of disk space. -- It is also slightly more difficult to maintain # as the documents have to be deleted/recreated when there is a change to any of the template # map documents. -- However, if there were to be multiple print calls of the same Map Scale for # different counties in a short period of time, the second approach is probably better, since it # assures a different map is used for each function call -- Possible solution is to preCopy the # template maps to a different location and append a randomly generated number which is highly # likely to be unique (and if not, should cause a failure, which can be checked for and allow a separate # attempt to copy the map) prior to actually opening them with the script and then exporting the pdf. # Only need to get the maps location, the requested scale, the requested county name, the table # to translate between the two of them, and the place to output the PDF. # In the county map road name border creation script # the results queue needs to have information # on the rows, stored in a list format # that can be transferred back easily. # Since the only output here should be the pdfs # there should be no need to pass any such # information back to the main process. # Instead, we can pass back completion/performance # information, if so desired. # Get the map size information from the geodatabase. ### This needs to be change away from a networked .gdb to the sql server instance. ### Of course, I also need to get my GIS automation data owner set up. ## ^^^ Use GEO for the gis automation data owner. ## searchFieldList = [ 'County', 'quarterInchSize', 'mapOrientationDir', 'halfInchSize' ] countyMapSizeInfo = list() cursor = da.SearchCursor(countyMapSizesLocation, searchFieldList) # @UndefinedVariable for row in cursor: countyMapSizeInfo.append(list(row)) if 'cursor' in locals(): del cursor else: pass if 'row' in locals(): del row else: pass # Sort the list based on the County Name values. countyMapSizeInfo = sorted(countyMapSizeInfo, key=lambda countySize: str(countySize[0])) # Generate the map's name from the countyMapSizeInfo, the county's name and the given scale. # The path is given from the maps folders location. # Then, just need to open the map, set it to the correct data driven page, and export a pdf. ## ^ May change this to opening the map which is already set to the correct data driven ## page in the future, but that requires a separate script to pre-make those and slightly ## complicates the process, so this is a better way to test first even though the performance ## might not be as good. ## See if you can just return the premade PDF's path. If that's possible, then it is ## probably a much better solution, performance-wise. try: countyToUse = optionsObject.countyName scaleToUse = optionsObject.mapScale except: # Defaults countyToUse = "Allen" scaleToUse = "quarterInch" try: # Defaults mapToUse = "countyMapHalf" + "9x12" + "V" + ".mxd" countyMapOutNamePDF = "Allen" + "CountyQt.pdf" for countyMapSizeItem in countyMapSizeInfo: if countyMapSizeItem[0].lower() == countyToUse.lower(): if (scaleToUse.lower() == "halfInch".lower()): mapToUse = "countyMapHalf" + countyMapSizeItem[ 3] + countyMapSizeItem[2] + ".mxd" countyMapOutNamePDF = countyToUse + "County.pdf" else: scaleToUse = "quarterInch" mapToUse = "countyMapQuarter" + countyMapSizeItem[ 1] + countyMapSizeItem[2] + ".mxd" countyMapOutNamePDF = countyToUse + "CountyQt.pdf" else: pass except: pass PDFOutpath = os.path.join(outputPDFParentFolder, scaleToUse, countyMapOutNamePDF) localCopyPath = os.path.join(env.scratchWorkspace, countyMapOutNamePDF) # @UndefinedVariable if os.path.isfile(PDFOutpath): shutil.copyfile(PDFOutpath, localCopyPath) return PDFOutpath else: try: print "Trying to open " + str(mapToUse) + " in " + str( mapsFolder) + "." mxd = mapping.MapDocument(os.path.join(mapsFolder, mapToUse)) dataDrivenPagesObject = mxd.dataDrivenPages ## Just need to open the mapToUse and set it's data driven ## page, then export, using the mapScale to direct the output. ddpPageIndex = dataDrivenPagesObject.getPageIDFromName(countyToUse) dataDrivenPagesObject.currentPageID = ddpPageIndex dataDrivenPagesObject.exportToPDF(PDFOutpath, "CURRENT") shutil.copyfile(PDFOutpath, localCopyPath) try: del mxd except: pass except Exception as exceptionInstance: # If an error occurred, print line number and error message tb = sys.exc_info()[2] print "Line %i" % tb.tb_lineno print exceptionInstance.message print "An error occurred." # Need to get the error from the arcpy object or result to figure out what's going on. print GetMessages() try: del exceptionInstance except: pass pythonGuess = PDFOutpath AddMessage("Python thinks the location is: " + pythonGuess) AddMessage("1") AddMessage("2") AddMessage("3") AddMessage("4") AddMessage("5") return PDFOutpath
def process(): class LicenseError(Exception): pass try: if CheckExtension("3D") == "Available": CheckOutExtension("3D") else: # raise a custom exception raise LicenseError # Constants - DO NOT MODIFY split_area = "split_area" orig_area = "orig_area" def calc_area(in_fc, field_name): AddField(in_fc, field_name, "DOUBLE") with da.UpdateCursor(in_fc, [field_name, "SHAPE@AREA"]) as cursor1: for r1 in cursor1: r1[0] = r1[1] cursor1.updateRow(r1) def field_exists(in_fc, in_field): from arcpy import ListFields if in_field in [f.name for f in ListFields(in_fc)]: return True else: return False def delete_field_if_exists(in_fc, in_field): if field_exists(in_fc, in_field): DeleteField(in_fc, in_field) assert field_exists(in_buildings, building_fid), \ "no attribute named {} in feature class".format(building_fid) for field in [tile_fid, file_name]: delete_field_if_exists(in_buildings, field) temp_fp = join("in_memory", "mp_fp") ddd.MultiPatchFootprint(in_buildings, temp_fp, "bldg_fid") calc_area(in_fc=temp_fp, field_name=orig_area) temp_isect = join("in_memory", "temp_isect") Intersect(r"{0} #;{1} #".format(temp_fp, in_tiles), temp_isect, "ALL", None, "INPUT") # Delete Temporary Multipatch Footprint Delete(temp_fp) calc_area(in_fc=temp_isect, field_name=split_area) temp_isect_asc = join("in_memory", "temp_isect_asc") Sort(temp_isect, temp_isect_asc, [[building_fid, "ASCENDING"]]) # Delete Temporary Intersect Feature Class Delete(temp_isect) fields = [building_fid, tile_fid, file_name, orig_area, split_area] # Generate a list of duplicates bldg_list = [] with da.SearchCursor(temp_isect_asc, building_fid) as cursor2: for row in cursor2: bldg_list.append(row[0]) duplicates = [ item for item, count in Counter(bldg_list).items() if count > 1 ] duplicates_list = [] for i in duplicates: duplicates_list.append([i, bldg_list.count(i)]) # TODO: Resolve why tile_fid is not showing up below when BuildingFID and TileFID are OID fields. "In_memory" issue ''' # \\ Begin Debug print code from arcpy import AddMessage fds = [f.name for f in arcpy.ListFields(temp_isect_asc) if f.name in fields] AddMessage(fds) nfds = [f.name for f in arcpy.ListFields(temp_isect_asc) if f.name not in fields] AddMessage(nfds) # End Debug pring code // ''' final_list = [] with da.SearchCursor(temp_isect_asc, fields) as cursor3: prev_area = -1 prev_item_list = [] item_count = 0 fcound = 0 for row in cursor3: if row[0] not in duplicates: final_list.append([row[0], row[1], row[2]]) else: area = row[3] - row[4] index = duplicates.index(row[0]) total_items = duplicates_list[index][1] if row[0] == duplicates[ 0] and item_count == 0: # Deal with first item differently item_count += 1 prev_area = area prev_item_list = [row[0], row[1], row[2]] elif item_count + 1 == total_items: # Deal with last item in list if prev_area <= area: prev_area = area prev_item_list = [row[0], row[1], row[2]] final_list.append(prev_item_list) item_count = 0 prev_area = -1 prev_item_list = [] elif item_count + 1 != total_items: if prev_area <= area: prev_area = area prev_item_list = [row[0], row[1], row[2]] item_count += 1 # Append results back to Input Feature Class AddField(in_buildings, tile_fid, "LONG") AddField(in_buildings, file_name, "TEXT") with da.UpdateCursor(in_buildings, [building_fid, tile_fid, file_name]) as cursor: for r in cursor: for i in final_list: if r[0] == i[0]: r[1] = int(i[1]) r[2] = str(i[2]) cursor.updateRow(r) Delete(temp_isect) del bldg_list del duplicates_list del duplicates # Check back in 3D Analyst license CheckInExtension("3D") except LicenseError: AddError("3D Analyst license is unavailable") print("3D Analyst license is unavailable") except ExecuteError: AddError("3D Analyst license is unavailable") print(GetMessages(2))