def is_arc_licensed():

    try:
        import arcpy
    except RuntimeError:
        print "arc not licensed"
        return False
    else:
        print "arc is licensed for " + arcpy.ProductInfo()
        return not arcpy.ProductInfo() == "NotInitialized"
def validateLicense():
    """
    Check that the current license is using advanced.
    :return:
    """
    if not arcpy.ProductInfo() == 'ArcInfo':
        if arcpy.CheckProduct('ArcInfo') == 'available':
            arcpy.SetProduct('ArcInfo')
        else:
            raise Exception('An advanced license was not available')
Example #3
0
 def checkout(name):
     try:
         if name == Products.ARCINFO:   import arcinfo
         if name == Products.ARCEDITOR: import arceditor
         if name == Products.ARCVIEW:   import arcview
         import archook
         archook.get_arcpy()
         import arcpy
         return arcpy if arcpy.ProductInfo() != "NotInitialized" else None
     except Exception as e:
         logging.debug(e)
         return None
Example #4
0
  def execute(self, parameters, messages):
    
    inFeatures = parameters[0].valueAsText
    outFeatures = parameters[1].valueAsText
    geometryType = parameters[2].valueAsText

    if (not geometryType):
      geometryType = "ENVELOPE"

    # arcinfo user use [MinimumBoundingGeometry_management]
    if (arcpy.ProductInfo() != "ArcInfo" or geometryType != "CONVEX_HULL"):
      arcpy.MinimumBoundingGeometry_management(inFeatures,
                                             outFeatures,
                                             geometryType)
      return

    # only CONVEX_HULL
    inDesc = arcpy.Describe(inFeatures)
    if (inDesc.dataType == "FeatureLayer"):
      inDesc = inDesc.featureClass
      

    dirName, fcName = os.path.split(outFeatures)

    #create output
    arcpy.CreateFeatureclass_management(dirName, fcName, "POLYGON", inFeatures, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", inDesc.spatialReference)

    fields = arcpy.ListFields(outFeatures)
    fieldList = []
    
    for field in fields:
      #messages.addMessage("{0} is a type of {1} with a length of {2}"
      #   .format(field.name, field.type, field.length))
      if (field.type != "OID" or field.type != "Geometry" ):
        fieldList.append(field.name)
    fieldList.append("SHAPE@")

    # for Progress step count
    result = arcpy.GetCount_management(inFeatures)
    count = int(result.getOutput(0))
    arcpy.SetProgressor("step", "Inserting ...", 0, count, 1)

    with arcpy.da.InsertCursor(outFeatures, fieldList) as inCursor:
      with arcpy.da.SearchCursor(inFeatures, fieldList) as cursor:
        for row in cursor:
          if (isinstance(row[-1] , arcpy.Polygon)):
            insertRow = row[:-1] + (row[-1].convexHull(),)
            inCursor.insertRow( insertRow )
          #step count
          arcpy.SetProgressorPosition()
    def createConnFile(self):
        '''
        Turns out this is actually much simpler than was originally
        thought.  Don't need a schema or a password to create the
        the sde file.  This makes things much simpler
        '''
        # connFileFullPath = self.secrets.getConnectionFilePath()
        connDirPath, connFilePath = os.path.split(self.connFile2Create)
        self.logger.debug("connDir: %s", connDirPath)
        self.logger.debug("connFile: %s", connFilePath)

        self.logger.debug("Connection file: %s", self.connFile2Create)

        suffix = os.path.splitext(self.connFile2Create)[1]
        if not suffix:
            self.connFile2Create = '{0}.sde'.format(self.connFile2Create)

        if not os.path.exists(self.connFile2Create):
            # if not arcpy.Exists(connFileFullPath):
            self.logger.debug('trying to create conn file ...')
            # for direct connect use:
            # CreateDatabaseConnection_management
            # host:port/serviceName, most of the time its just host/serviceName
            connStr = self.getConnectionString()

            self.logger.debug("importing arcpy...")
            import arcpy  # @UnresolvedImport

            self.logger.debug("arcpy imported")
            arcpy.SetProduct('arcinfo')
            self.logger.debug('arcpy product info %s', arcpy.ProductInfo())

            self.logger.debug("arcpytool report: %s",
                              arcpy.CreateDatabaseConnection_management)
            self.logger.debug(
                'inspect %s',
                inspect.getargspec(arcpy.CreateDatabaseConnection_management))

            # seems to require a value for username and password even through they are invalid
            arcpy.CreateDatabaseConnection_management(connDirPath,
                                                      connFilePath,
                                                      self.dbtype.upper(),
                                                      connStr, 'DATABASE_AUTH',
                                                      'junk', 'junk',
                                                      'DO_NOT_SAVE_USERNAME',
                                                      self.sde)
            self.logger.info('connection file %s has been created',
                             self.connFile2Create)
Example #6
0
def setup_arcpyenvironment():
    '''
    Setup the ArcPy stuff, required for
    to run ArcMap jobs in Python.

    ArcPy requires us to setup an environment.
    :return:
    '''

    # Prepare the ArcGIS environment
    arcpy.AddMessage(arcpy.ProductInfo())
    arcpy.CheckOutExtension("Spatial")
    arcpy.env.parallelProcessingFactor = "100%"

    # Setup workspace and project management:
    arcpy.env.workspace = "in_memory"
    arcpy.env.overwriteOutput = True
Example #7
0
def send_msg(message, messagetype="message"):
    """ output messages to stdout as well as arcpy """
    if messagetype.lower() == "message":
        arcpy.AddMessage(message)
    if messagetype.lower() == "warning":
        arcpy.AddWarning(message)
    if messagetype.lower() == "error":
        if arcpy.ProductInfo() != "ArcServer":
            arcpy.AddError("Failed. " + message)
        else:
            arcpy.AddMessage("Failed. " + message)
        # set the result_output parameter
        arcpy.SetParameterAsText(20, ("Failed. " + message))
    if messagetype.lower() == "success":
        # set the result_output parameter in case of success
        arcpy.AddMessage("Success. " + message)
        arcpy.SetParameterAsText(20, ("Success. " + message))

    print(message)
Example #8
0
def validate_user_table(userTableDescribe):
    """ validates user table schema requirements """
    # do these checks only when run in desktop while publishing
    # this check is not required on server after publishing as gp service
    if arcpy.ProductInfo() == "ArcServer":
        return True
    #Commented out to shave .05 seconds off
    #if not arcpy.Exists(user_table):
    #send_msg("User table does not exist", "error")
    #return False
    # check if user table has globalid enabled
    if not userTableDescribe.hasGlobalID:
        send_msg("User table does not have globalids enabled.", "error")
        return False

    upperPath = userTableDescribe.path.upper()
    if ('.SDE' in upperPath or '.GDS' in upperPath):
        return True
    return False
Example #9
0
def validate_user_table():
    """ validates user table schema requirements """
    # do these checks only when run in desktop while publishing
    # this check is not required on server after publishing as gp service
    if arcpy.ProductInfo() == "ArcServer":
        return True
    desc = arcpy.Describe(user_table)
    # check if user table has globalid enabled
    if not desc.hasGlobalID:
        send_msg("User table does not have globalids enabled.", "error")
        return False
    # check if user table is an enterprise gdb
    workspace_props = arcpy.Describe(desc.path)
    if workspace_props.workspaceFactoryProgID == "esriDataSourcesGDB.SdeWorkspaceFactory.1":
        return True
    else:
        send_msg("User table must be sourced from an enterprise geodatabase",
                 "error")
        return False
Example #10
0
def CreateDefaultKeys(InDB):
    #add default keys to the upc_key table
    fields = ['KeyName','KeyValue']
    cur = arcpy.da.InsertCursor(os.path.join(InDB,'upc_key'),fields)
    
    cur.insertRow(('BaseGeom_bnd','add a base geometry'))
    cur.insertRow(('BaseGeom_cent','add a base geometry'))
    cur.insertRow(('BaseGeom_id','add a base geometry'))
#     cur.insertRow(('Subarea_bnd',UPConfig['Subarea_bnd']))
#     cur.insertRow(('Subarea_id',UPConfig['Subarea_id']))
#     cur.insertRow(('Subarea_search',UPConfig['Subarea_search']))
    if arcpy.ProductInfo() == 'ArcInfo':
        cur.insertRow(('DistMode','GenerateNear'))
    else:
        cur.insertRow(('DistMode','RasterEuc'))
#     if 'Redev' in UPConfig.keys():
#         cur.insertRow(('Redev',UPConfig['Redev']))
#         cur.insertRow(('Redev_pop',UPConfig['Redev_pop']))
#         cur.insertRow(('Redev_emp',UPConfig['Redev_emp']))
    del cur
Example #11
0
def validate_inputs():
    """ validate and initialize user table """
    # this check is not required on server after publishing as gp service
    if arcpy.ProductInfo() != "ArcServer":
        desc = arcpy.Describe(user_table)
        # check if user table has globalid enabled
        if not desc.hasGlobalID:
            send_msg("User table does not have globalids enabled.", "error")
            return False

        # check if user table is an enterprise gdb
        workspace_props = arcpy.Describe(desc.path)
        if workspace_props.workspaceFactoryProgID not in [
                "esriDataSourcesGDB.SdeWorkspaceFactory.1",
                "esriDataSourcesGDB.SdeWorkspaceFactory"
        ]:
            send_msg(
                "User table must be sourced from an enterprise geodatabase",
                "error")
            return False

    # check if test login email exists
    if test_mode == "true":
        send_msg("=" * 30)
        send_msg("TEST MODE")
        send_msg("-" * 30)
        whereclause = "UPPER({0})='{1}'".format(user_email_field,
                                                test_login_email.upper())
        rowcount = 0
        with arcpy.da.SearchCursor(in_table=user_table,
                                   field_names=[user_email_field],
                                   where_clause=whereclause) as cursor:
            rowcount = len([i for i in cursor])
            if rowcount == 0:
                send_msg("Test login email does not exist.", "error")
                return False
            elif rowcount == 1:
                return True
    else:
        return True
Example #12
0
def send_msg(
    message,
    messagetype="message",
):
    """ output messages to stdout as well as arcpy """
    if messagetype.lower() == "message":
        arcpy.AddMessage(message)
    if messagetype.lower() == "warning":
        arcpy.AddWarning(message)
    if messagetype.lower() == "error":
        if arcpy.ProductInfo() != "ArcServer":
            arcpy.AddError("Failed. " + message)
        else:
            arcpy.AddMessage("Failed. " + message)
        # set the result_output parameter
        out_message = {"status": "Failed", "description": message}
        arcpy.SetParameterAsText(28, out_message)
    if messagetype.lower() == "success":
        # set the result_output parameter in case of success
        out_message = {"status": "Success", "description": message}
        arcpy.SetParameterAsText(28, out_message)
    print(message)
def createSDE(database_type, instance, database, account_authentication,
              dbms_admin, dbms_admin_pwd, schema_type, gdb_admin,
              gdb_admin_pwd, tablespace, license):
    # Get the current product license
    product_license = arcpy.ProductInfo()
    # Checks required license level
    if product_license.upper() == "ARCVIEW" or product_license.upper(
    ) == 'ENGINE':
        print(
            "\n" + product_license + " license found!" +
            " Creating an enterprise geodatabase requires an ArcGIS for Desktop Standard or Advanced, ArcGIS Engine with the Geodatabase Update extension, or ArcGIS for Server license."
        )
        sys.exit("Re-authorize ArcGIS before creating enterprise geodatabase.")
    else:
        print("\n" + product_license +
              " license available!  Continuing to create...")
        arcpy.AddMessage("++++++++++++++++++")

    try:
        arcpy.AddMessage("Creating enterprise geodatabase...")
        arcpy.CreateEnterpriseGeodatabase_management(
            database_platform=database_type,
            instance_name=instance,
            database_name=database,
            account_authentication=account_authentication,
            database_admin=dbms_admin,
            database_admin_password=dbms_admin_pwd,
            sde_schema=schema_type,
            gdb_admin_name=gdb_admin,
            gdb_admin_password=gdb_admin_pwd,
            tablespace_name=tablespace,
            authorization_file=license)
        for i in range(arcpy.GetMessageCount()):
            arcpy.AddReturnMessage(i)
        arcpy.AddMessage("++++++++++++++++++")
    except:
        for i in range(arcpy.GetMessageCount()):
            arcpy.AddReturnMessage(i)
Example #14
0
def hasLicense(*licenses):
    """Is the required license in use?"""
    return arcpy.ProductInfo() in licenses
Example #15
0
def ImportBaseGeom(UPlanGDB, InSHP, DescName, CentInside):
    '''
    *Imports a Base Geometry layer to the GDB 
    *Adds a field called up_polyid and calculates it equal to the ObjectID
    *Creates a Centroid layer from the Base Geometry layer and names it BaseGeom_cent
    *Adds a record to the upc_layers table for this Base Geometry layer and the centroid layer
    *updates the upc_keys table with the new base geometry info
    *updates pickle file
    
    Calls: 
    AddToUPC_Layers
    
    Called by:
    The Import Base Geometry Toolbox
    
    Arguments:
    UPlanGDB: The UPlan GDB (where the layer will be imported)
    InSHP: The shapefile to import
    DescName: The descriptive name of the layer
    CentInsdie = True if you want to force the centroid inside of the polygons, false for true centroid
    
    Returns: None
    '''
    arcpy.env.workspace = UPlanGDB
    arcpy.env.overwriteOutput = True

    #Register it with the layer tracker
    if os.path.basename(InSHP)[-4:] == ".shp":
        SHPName = os.path.basename(InSHP)[:-4]
        #if the shp has a field named "OBJECTID", it needs to be deleted
        #(SHPs use FID, so if OBJECT ID is there, it's from copying form a GDB
        #when copied back into the UPlan GDB it creates OBJECTID_1 instead of OBJECTID if OBJECTID isn't deleted)
        if FieldExist(InSHP, 'OBJECTID'):
            arcpy.DeleteField_management(InSHP, 'OBJECTID')
    else:
        SHPName = os.path.basename(InSHP)
    AddToUPC_Layers(UPlanGDB, SHPName, DescName, 'BaseGeom_bnd')

    #delete up_polyid field if it exists
    if FieldExist(InSHP, 'up_polyid'):
        arcpy.DeleteField_management(InSHP, 'up_polyid')

    #Import the shp to the GDB
    arcpy.FeatureClassToFeatureClass_conversion(InSHP, UPlanGDB, SHPName)

    #Add int field called "up_polyid" and calc = to object ID
    arcpy.AddField_management(SHPName, "up_polyid", "LONG")
    arcpy.CalculateField_management(SHPName, "up_polyid", "!OBJECTID!",
                                    "PYTHON_9.3")

    #Create centroid layer and drop all fields except for "up_polyid" (and shape and objectid).
    if arcpy.ProductInfo() == 'ArcInfo':
        if CentInside:
            CentType = 'INSIDE'
        else:
            CentType = 'CENTROID'
        arcpy.FeatureToPoint_management(SHPName, "poly_cent", CentType)
    else:
        CreateCentroids(UPlanGDB, SHPName)

    Fields = arcpy.ListFields("poly_cent")
    FieldList = []
    for Field in Fields:
        if not Field.required:
            if not Field.name == "up_polyid":
                FieldList.append(Field.name)
    arcpy.DeleteField_management("poly_cent", FieldList)

    #add centroid layer to layer tracker table
    AddToUPC_Layers(UPlanGDB, 'poly_cent',
                    'Base Geometry Centroids (Created from: ' + SHPName + ')',
                    'BaseGeom_cent')

    #update upc_key table with base geometry info
    UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_bnd', SHPName)
    UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_cent', 'poly_cent')
    UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_id', 'up_polyid')

    #update pickle file
    DBSplit = uiut.SplitPath(UPlanGDB)
    UPConfig = {}
    UPConfig = upc.ReadUPConfigFromGDB(DBSplit[0], DBSplit[1])
    uiut.MakePickle(UPConfig, UPlanGDB)
Example #16
0
def export_map_document(product_location,
                        mxd,
                        map_doc_name,
                        data_frame,
                        outputdirectory,
                        export_type,
                        production_xml=None):
    """Exports MXD to chosen file type"""

    try:
        export = export_type.upper()
        product = arcpy.ProductInfo()
        arcpy.AddMessage("Product: " + product)

        if product == 'ArcServer':
            filename_prefixe = "_ags_"
        else:
            filename_prefixe = ""

        if export == "PDF":
            filename = filename_prefixe + map_doc_name + ".pdf"
            outfile = os.path.join(outputdirectory, filename)

            # Export to PDF optional parameters
            data_frame = "PAGE_LAYOUT"
            df_export_width = 640
            df_export_height = 480
            resolution = 300
            image_quality = "BEST"
            colorspace = "RGB"
            compress_vectors = True
            image_compression = "ADAPTIVE"
            picture_symbol = "RASTERIZE_BITMAP"
            convert_markers = False
            embed_fonts = True
            layers_attributes = "LAYERS_ONLY"
            georef_info = True
            jpeg_compression_quality = 80

            # Run the export tool
            arcpy.mapping.ExportToPDF(mxd, outfile, data_frame,
                                      df_export_width, df_export_height,
                                      resolution, image_quality, colorspace,
                                      compress_vectors, image_compression,
                                      picture_symbol, convert_markers,
                                      embed_fonts, layers_attributes,
                                      georef_info, jpeg_compression_quality)
            arcpy.AddMessage("PDF is located: " + outfile)
            arcpy.AddMessage(filename)
            return filename

        elif export == 'JPEG':
            filename = filename_prefixe + map_doc_name + ".jpg"
            outfile = os.path.join(outputdirectory, filename)

            # Export to JEPG optional parameters
            data_frame = "PAGE_LAYOUT"
            df_export_width = 640
            df_export_height = 480
            resolution = 96
            world_file = False
            color_mode = "24-BIT_TRUE_COLOR"
            jpeg_quality = 100
            progressive = False

            # Run the export tool
            arcpy.mapping.ExportToJPEG(mxd, outfile, data_frame,
                                       df_export_width, df_export_height,
                                       resolution, world_file, color_mode,
                                       jpeg_quality, progressive)

            arcpy.AddMessage("JPEG is located: " + outfile)
            return filename

        elif export == 'TIFF':
            filename = filename_prefixe + map_doc_name + ".tif"
            outfile = os.path.join(outputdirectory, filename)

            # Export to JPEG optional parameters
            data_frame = "PAGE_LAYOUT"
            df_export_width = 640
            df_export_height = 480
            resolution = 96
            world_file = False
            color_mode = "24-BIT_TRUE_COLOR"
            tiff_compression = "LZW"

            # Run the export tool
            arcpy.mapping.ExportToTIFF(mxd, outfile, data_frame,
                                       df_export_width, df_export_height,
                                       resolution, world_file, color_mode,
                                       tiff_compression)
            arcpy.AddMessage("TIFF is located: " + outfile)
            return filename

        elif export == "MAP PACKAGE":
            filename = filename_prefixe + map_doc_name + ".mpk"
            outfile = os.path.join(outputdirectory, filename)
            dfextent = data_frame.extent
            mxd = mxd.filePath
            arcpy.AddMessage(mxd)

            # Export to MPK optional parameters
            convert_data = "CONVERT"
            convert_arcsde_data = "CONVERT_ARCSDE"
            apply_extent_to_arcsde = "ALL"
            arcgisruntime = "DESKTOP"
            reference_all_data = "NOT_REFERENCED"
            version = "ALL"

            # Run the export tool
            arcpy.PackageMap_management(mxd, outfile, convert_data,
                                        convert_arcsde_data, dfextent,
                                        apply_extent_to_arcsde, arcgisruntime,
                                        reference_all_data, version)
            arcpy.AddMessage("MPK is located: " + outfile)
            return filename

        elif export == 'LAYOUT GEOTIFF':
            filename = filename_prefixe + map_doc_name + ".tif"
            outfile = os.path.join(outputdirectory, filename)

            # Export to Layout GeoTIFF optional parameters:
            resolution = 96
            world_file = False
            color_mode = "24-BIT_TRUE_COLOR"
            tiff_compression = "LZW"

            # Run the export tool
            arcpyproduction.mapping.ExportToLayoutGeoTIFF(
                mxd, outfile, data_frame, resolution, world_file, color_mode,
                tiff_compression)
            arcpy.AddMessage("Layout GeoTIFF is located: " + outfile)
            return filename

        elif export == 'PRODUCTION PDF' or export == 'MULTI-PAGE PDF':
            filename = filename_prefixe + map_doc_name + ".pdf"
            outfile = os.path.join(outputdirectory, filename)
            setting_file = os.path.join(product_location, production_xml)

            if os.path.exists(setting_file) == True:
                arcpyproduction.mapping.ExportToProductionPDF(
                    mxd, outfile, setting_file)
                arcpy.AddMessage("Production PDF is located: " + outfile)
            else:
                arcpy.AddMessage(
                    "Production PDF is the default exporter for a Multi-page PDF."
                )
                arcpy.AddWarning(
                    "Color mapping rules file doesn't exist, using "
                    "standard ExportToPDF exporter with default "
                    "settings.")
                arcpy.mapping.ExportToPDF(mxd, outfile)
                arcpy.AddMessage("PDF is located: " + outfile)
            return filename

        else:
            arcpy.AddError("The exporter : " + export + " is not supported, "
                           "please contact your system administrator.")

    except arcpy.ExecuteError:
        arcpy.AddError(arcpy.GetMessages(2))
    except Exception as ex:
        arcpy.AddError(ex.message)
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        arcpy.AddError("Traceback info:\n" + tbinfo)

class CustomError(Exception):
    pass


try:

    # Get the original overwrite output setting so we can reset it at the end.
    OverwriteOutput = arcpy.env.overwriteOutput
    # It's okay to overwrite stuff in this tool
    arcpy.env.overwriteOutput = True

    # Check if they have the necessary license.
    # The snap_edit tool requires at least Standard (ArcEditor)
    ArcLicense = arcpy.ProductInfo()
    if ArcLicense != "ArcEditor" and ArcLicense != "ArcInfo":
        message = "To run this tool, you must have the Desktop Standard (ArcEditor) \
    or Advanced (ArcInfo) license.  Your license type is: %s." % ArcLicense
        arcpy.AddError(message)
        raise CustomError

# ----- Collect user inputs -----

# Location of FD where network dataset will be created
    outFD = arcpy.GetParameterAsText(0)
    # Streets feature class
    Streets = arcpy.GetParameterAsText(1)
    # SQL expression for selecting street features where pedestrians are allowed
    SelectExpression = arcpy.GetParameterAsText(2)
    # Max distance for which stops will be snapped to streets
Example #18
0
import arcpy
licensed = arcpy.ProductInfo()
if licensed == 'ArcInfo':
    testfile = open('C:\Program Files (x86)\ArcGIS\Desktop10.6\licensed','w')
    testfile.write(licensed)
    testfile.close()
Example #19
0
import arcpy
from arcpy import env
env.workspace = "C:/EsriPress/Python/Data/Exercise05"
in_features = "parks.shp"
out_featureclass = "Results/park_centroid.shp"
if arcpy.ProductInfo() == "ArcInfo":
    arcpy.FeatureToPoint_management(in_features, out_featureclass)
else:
    print "An ArcInfo license is not available"
#import arcview
#import arceditor
import arcinfo
import arcpy

print(arcpy.ProductInfo()) # prints ArcView, ArcEditor or ArcInfo
Example #21
0
def checkCoverage(maskA, maskB, inputFile=None):

    # Checks the coverage between polygons mask A and mask B

    # Set temporary variables
    prefix = os.path.join(arcpy.env.scratchGDB, "cover_")
    maskACopy = prefix + "maskACopy"
    maskBCopy = prefix + "maskBCopy"
    maskACoverage = prefix + "maskACoverage"

    # Copy maskA and maskBCopy to temporary variables
    arcpy.CopyFeatures_management(maskA, maskACopy)
    arcpy.CopyFeatures_management(maskB, maskBCopy)

    percOut = 0

    if checkLicenceLevel('Advanced') or arcpy.ProductInfo() == "ArcServer":

        # Check if the fields Area_ha exist in maskACopy and maskBCopy
        # If they do, delete them

        maskAFields = arcpy.ListFields(maskACopy)
        for field in maskAFields:
            if field.name == "Area_ha":
                arcpy.DeleteField_management(maskACopy, "Area_ha")

        maskBFields = arcpy.ListFields(maskBCopy)
        for field in maskBFields:
            if field.name == "Area_ha":
                arcpy.DeleteField_management(maskBCopy, "Area_ha")

        # Delete fields area_ha in in maskACopy and maskBCopy

        # Calculate total area of the coverage (study area or contributing area)
        arcpy.AddField_management(maskBCopy, "Area_ha", "DOUBLE")
        exp = "!SHAPE.AREA@HECTARES!"
        arcpy.CalculateField_management(maskBCopy, "Area_ha", exp,
                                        "PYTHON_9.3")

        maskBCopyVal = 0
        for row in arcpy.da.SearchCursor(maskBCopy, ["Area_ha"]):
            maskBCopyVal += float(row[0])

        # Use symmetrical difference to find which areas in maskA and maskBCopyCopy do not overlap
        arcpy.SymDiff_analysis(maskACopy, maskBCopy, maskACoverage)

        # Check if there are non-overlapping areas in the land use and coverage
        rows = [row for row in arcpy.da.SearchCursor(maskACoverage, "*")]
        numFeatures = len(rows)

        if numFeatures != 0:  # There is a discrepancy between mask A and mask B

            # Calculate the area of the polygons in the discrepancy
            arcpy.AddField_management(maskACoverage, "Sliver", "DOUBLE")
            exp = "!SHAPE.AREA@HECTARES!"
            arcpy.CalculateField_management(maskACoverage, "Sliver", exp,
                                            "PYTHON_9.3")

            area = 0
            for row in arcpy.da.SearchCursor(maskACoverage, ["Sliver"]):
                area += float(row[0])

            # Calculate percentage of the coverage area that the discrepancy covers
            percOut = float(area) / float(maskBCopyVal) * 100.0

            if percOut > 2.5:
                warning = 'Input data coverage is less tha 97.5 percent of the study area'
                log.warning(warning)

                warning = 'This may cause discrepancies in later calculations'
                log.warning(warning)

                warning = 'Please check this input: ' + str(inputFile)
                log.warning(warning)

        # Delete temporary masks
        arcpy.Delete_management(maskACopy)
        arcpy.Delete_management(maskBCopy)
        arcpy.Delete_management(maskACoverage)

    else:
        log.warning(
            'Coverage discrepancies between soil, land use, and coverage extent not checked as advanced license not present.'
        )
        log.warning(
            'Please ensure the soil and land use shapefile cover at least 97.5 percent of the coverage extent'
        )
Example #22
0
def NearByGroup(in_features, group_fields, near_features, search_radius=""):

    # Error if sufficient license is not available
    if arcpy.ProductInfo().lower() not in ['arcinfo']:
        arcpy.AddError("An ArcGIS for Desktop Advanced license is required.")
        sys.exit()

    # Read field values from input features
    uniq_values = set()
    scur = arcpy.SearchCursor(in_features, "", "", ";".join(group_fields))
    try:
        for row in scur:
            value = tuple()
            for field in group_fields:
                value += (row.getValue(field),)
            uniq_values.add(value)
    except:""
    finally:
        if scur:
            del scur

    # Add fields to Input
    arcpy.management.AddField(in_features, "NEAR_OID", "LONG")
    arcpy.management.AddField(in_features, "NEAR_DISTN", "DOUBLE")
    arcpy.management.AddField(in_features, "NEAR_FCLS", "TEXT")

    # Make a selection based on the values
    arcpy.management.MakeFeatureLayer(in_features, "input_lyr")
    near_features_list = []
    for each in near_features:
        arcpy.management.MakeFeatureLayer(each, "{0}_lyr".format(os.path.splitext(os.path.basename(each))[0]))
        near_features_list.append("{0}_lyr".format(os.path.splitext(os.path.basename(each))[0]))

    # Set the progress bar
    arcpy.SetProgressor("step", "Processing...", 0, len(uniq_values), 1)
    for uniq_value in uniq_values:
        expr = ""
        for combo in zip(uniq_value, group_fields):
            val = "'{0}'".format(combo[0]) if type(combo[0]) == str or type(combo[0]) == unicode else combo[0]
            expr += """{0} = {1} AND """.format(combo[1], val)
        expr = expr[:-5]
        # Select the input features
        arcpy.management.SelectLayerByAttribute("input_lyr", "", expr)
        for each in near_features_list:
            arcpy.management.SelectLayerByAttribute(each, "", expr)

        # Run the Near process
        arcpy.analysis.Near("input_lyr", near_features_list, search_radius)

        # Calculate the values into the NEAR_FID and NEAR_DISTN fields
        arcpy.management.CalculateField("input_lyr", "NEAR_OID", "!NEAR_FID!", "PYTHON")
        arcpy.management.CalculateField("input_lyr", "NEAR_DISTN", "!NEAR_DIST!", "PYTHON")
        if len(near_features) > 1:
            arcpy.management.CalculateField("input_lyr", "NEAR_FCLS", """getpath(!NEAR_FC!)""", "PYTHON", """def getpath(layer):\n    try:\n        return arcpy.Describe(str(layer)).catalogPath\n    except:\n        return 'None'""")
        else:
            arcpy.management.CalculateField("input_lyr", "NEAR_FCLS", """r'{0}'""".format(arcpy.Describe(near_features[0]).catalogPath), "PYTHON")
        arcpy.SetProgressorPosition()

    # Clean up
    arcpy.management.DeleteField("input_lyr", "NEAR_FID;NEAR_DIST;NEAR_FC")
    for each in ["input_lyr"] + near_features_list:
        try:
            arcpy.management.Delete(each)
        except:
            ""
        arcpy.GetMessages(2)
    except Exception as e:
        arcpy.AddMessage(e.args[0])


##    except ineditsessionerror:
##        arcpy.AddMessage("You may not be in an editing session while this tool is active")
##        arcpy.AddMessage("Please discontinue the edit session before operating this tool")
# End do_analysis function

# This test allows the script to be used from the operating
# system command prompt (stand-alone), in a Python IDE,
# as a geoprocessing script tool, or as a module imported in
# another script
if __name__ == '__main__':
    # Arguments are optional
    argv = tuple(
        arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount()))

if arcpy.CheckProduct(
        "ArcEditor") == "AlreadyInitialized" or arcpy.CheckProduct(
            "ArcInfo") == "AlreadyInitialized":
    arcpy.AddMessage("Current License Level: " + arcpy.ProductInfo())
    do_analysis(*argv)
else:
    arcpy.AddMessage("Current License Level: " + arcpy.ProductInfo())
    arcpy.AddMessage(
        "Error: ArcEditor (ArcGIS for Desktop Standard) or ArcInfo (ArcGIS for Desktop Advanced) Required"
    )
    arcpy.AddMessage("Program Terminated")
### Version check
##version = str(arcpy.GetInstallInfo()['Version'])
##if version.find("10.") > 0:
##    ArcGIS10 = True
##else:
##    ArcGIS10 = False
#### Convert version string to a float value (needed for numeric comparison)
##versionFlt = float(version[0:4])
##if versionFlt < 10.5:
##    arcpy.AddError("\nThis tool requires ArcGIS version 10.5 or greater. Exiting...\n")
##    sys.exit()

# Main - wrap everything in a try statement
try:
    #--------------------------------------------------------------------- Check for ArcInfo License exit if not available
    if not arcpy.ProductInfo() == "ArcInfo":
        arcpy.AddError("\nThis tool requires an ArcInfo/Advanced license level for ArcGIS Desktop. Exiting...\n")
        sys.exit()

    # Check out Spatial Analyst License
    if arcpy.CheckExtension("Spatial") == "Available":
        arcpy.CheckOutExtension("Spatial")
    else:
        arcpy.AddError("\nSpatial Analyst Extension not enabled. Please enable Spatial Analyst from the Tools/Extensions menu. Exiting...\n")
        sys.exit()

    arcpy.SetProgressorLabel("Setting Variables")
    #------------------------------------------------------------------------  Input Parameters
    InputContours = arcpy.GetParameterAsText(0)
    dam = arcpy.GetParameterAsText(1)
    #outputPoolName = arcpy.GetParameterAsText(2)
        print(" \n%s: error: \n%s\n" %
              (sys.argv[0], "DBMS type (--DBMS) must be specified."))
        parser.print_help()
        sys.exit(3)

    if (license == ""):
        print " \n%s: error: \n%s\n" % (
            sys.argv[0], "Authorization file (-l) must be specified.")
        parser.print_help()
        sys.exit(3)

    if (database_type == "SQLSERVER"):
        database_type = "SQL_SERVER"

    # Get the current product license
    product_license = arcpy.ProductInfo()

    if (license == ""):
        print " \n%s: error: %s\n" % (
            sys.argv[0], "Authorization file (-l) must be specified.")
        parser.print_help()
        sys.exit(3)

    # Local variables
    instance_temp = instance.replace("\\", "_")
    instance_temp = instance_temp.replace("/", "_")
    instance_temp = instance_temp.replace(":", "_")
    Conn_File_NameT = instance_temp + "_" + database + "_" + username

    if os.environ.get("TEMP") == None:
        temp = "c:\\temp"
Example #26
0
    def get_replicas(self):
        """Get geodatabase replicas as ordered dict."""
        replicas_props = []
        if self.arcpy_found and self.is_gdb_enabled:
            # due to bug in arcpy, cannot use da.ListReplicas date properties
            # `lastSend` and `lastReceive` for file/personal geodatabases
            # because it crashes the Python process
            for replica in arcpy.da.ListReplicas(self.path):
                od = OrderedDict()
                for k, v in GDB_REPLICA_PROPS.items():
                    if (self.wkspc_type != 'Enterprise geodatabase') and (
                            k in ('lastReceive', 'lastSend')):
                        od[v] = 'Not available'
                    else:
                        prop_value = getattr(replica, k, '')
                        if isinstance(
                                prop_value, datetime.datetime
                        ) and prop_value.year == ESRI_GDB_REPLICA_INF_DATE:
                            od[v] = ''
                        else:
                            if prop_value is not None:
                                od[v] = prop_value
                            else:
                                od[v] = ''

                # need at least Standard license of ArcGIS Desktop
                # to run this GP tool
                if arcpy.ProductInfo() in ('ArcEditor', 'ArcInfo'):
                    if not hasattr(arcpy, 'ExportReplicaSchema_management'):
                        # ArcGIS Pro at 1.2 did not have this GP tool
                        return
                    replica_schema_xml = os.path.join(tempfile.gettempdir(),
                                                      'ReplicaSchema.xml')
                    arcpy.ExportReplicaSchema_management(
                        in_geodatabase=self.path,
                        output_replica_schema_file=replica_schema_xml,
                        in_replica=replica.name,
                    )
                    with open(replica_schema_xml, 'r') as fh:
                        schema_xml_data = fh.readlines()[0]
                    try:
                        os.remove(replica_schema_xml)
                    except Exception:
                        pass
                    xml = ElementTree.fromstring(schema_xml_data)
                    od['Creation date'] = xml.find('WorkspaceDefinition').find(
                        'GPReplica').find('CreationDate').text.replace(
                            'T', ' ')

                    datasets = xml.find('WorkspaceDefinition').find(
                        'GPReplica').find('GPReplicaDescription').find(
                            'GPReplicaDatasets')
                    datasets_pairs = sorted(((d.find('DatasetName').text,
                                              d.find('TargetName').text)
                                             for d in datasets.getchildren()),
                                            key=lambda pair: pair[0].lower())

                    od['Datasets'] = '<br>'.join([
                        '{0} -> {1}'.format(i[0], i[1]) for i in datasets_pairs
                    ])

                replicas_props.append(od)
        return replicas_props
Example #27
0
def CheckArcInfoLicense():
    ArcLicense = arcpy.ProductInfo()
    if ArcLicense != "ArcInfo":
        arcpy.AddError("To run this tool, you must have the Desktop \
Advanced (ArcInfo) license.  Your license type is: %s." % ArcLicense)
        raise CustomError
Example #28
0
    installList = arcpy.ListInstallations()
    for install in installList:
        arcpy.AddMessage("Product: ArcGIS for " + install + " " +
                         arcpy.GetInstallInfo(install)["Version"] + " SP " +
                         arcpy.GetInstallInfo()["SPNumber"] + ", Build " +
                         arcpy.GetInstallInfo()["BuildNumber"])
        print("Product: ArcGIS for " + install + " " +
              arcpy.GetInstallInfo(install)["Version"] + " SP " +
              arcpy.GetInstallInfo()["SPNumber"] + ", Build " +
              arcpy.GetInstallInfo()["BuildNumber"])
        arcpy.AddMessage("Installed on: " +
                         arcpy.GetInstallInfo()["InstallDate"] + " " +
                         arcpy.GetInstallInfo()["InstallTime"])
        print("Installed on: " + arcpy.GetInstallInfo()["InstallDate"] + " " +
              arcpy.GetInstallInfo()["InstallTime"])
        arcpy.AddMessage("Using " + arcpy.ProductInfo() + " license level")
        print("Using " + arcpy.ProductInfo() + " license level")
        arcpy.AddMessage("Script Version 1.0")
        print("Script Version 1.0")
    arcpy.AddMessage("--------------------------------")

    ## This portion of the script will begin by checking the validity of the input files.
    ## It will then subsequently perform the first step of the script which is to subtract the
    ##Digital Terrain Model (DTM) from the Digital Surface Model (DSM).
    ##---------------------------------------------------------------------------------------

    arcpy.SetProgressorLabel("Validating input data...")

    if arcpy.Exists(inDSM) == True:
        print("Input DSM is valid")
        arcpy.AddMessage("Input DSM is valid")
Example #29
0
def runSystemChecks(folder=None, rerun=False):

    import LUCI_PTFs.lib.progress as progress

    # Set overwrite output
    arcpy.env.overwriteOutput = True

    # Check spatial analyst licence is available
    if arcpy.CheckExtension("Spatial") == "Available":
        arcpy.CheckOutExtension("Spatial")
    else:
        raise RuntimeError(
            "Spatial Analyst license not present or could not be checked out")

    ### Set workspaces so that temporary files are written to the LUCI scratch geodatabase ###
    if arcpy.ProductInfo() == "ArcServer":
        log.info('arcpy.env.scratchWorkspace on server: ' +
                 str(arcpy.env.scratchWorkspace))

        # Set current workspace
        arcpy.env.workspace = arcpy.env.scratchGDB
    else:

        # If rerunning a tool, check if scratch workspace has been set. If it has, use it as it is (with temporary rasters and feature classes from the previous run).
        scratchGDB = None

        if rerun:
            xmlFile = progress.getProgressFilenames(folder).xmlFile

            if os.path.exists(xmlFile):
                scratchGDB = readXML(xmlFile, 'ScratchGDB')

                if not arcpy.Exists(scratchGDB):
                    log.error('Previous scratch GDB ' + str(scratchGDB) +
                              ' does not exist. Tool cannot be rerun.')
                    log.error('Exiting tool')
                    sys.exit()

        if scratchGDB is None:

            # Set scratch path from values in user settings file if values present
            scratchPath = None
            try:
                if os.path.exists(configuration.userSettingsFile):

                    tree = ET.parse(configuration.userSettingsFile)
                    root = tree.getroot()
                    scratchPath = root.find("scratchPath").text

            except Exception:
                pass  # If any errors occur, ignore them. Just use the default scratch path.

            # Set scratch path if needed
            if scratchPath is None:
                scratchPath = configuration.scratchPath

            # Create scratch path folder
            if not os.path.exists(scratchPath):
                os.makedirs(scratchPath)

            # Remove old date/time stamped scratch folders if they exist and if they do not contain lock ArcGIS lock files.
            for root, dirs, files in os.walk(scratchPath):
                for dir in dirs:

                    # Try to rename folder. If this is possible then no locks are held on it and it can then be removed.
                    try:
                        fullDirPath = os.path.join(scratchPath, dir)
                        renamedDir = os.path.join(scratchPath,
                                                  'ready_for_deletion')
                        os.rename(fullDirPath, renamedDir)
                    except Exception:
                        # import traceback
                        # log.warning(traceback.format_exc())
                        pass
                    else:
                        try:
                            shutil.rmtree(renamedDir)
                        except Exception:
                            # import traceback
                            # log.warning(traceback.format_exc())
                            pass

            # Create new date/time stamped scratch folder for the scratch GDB to live in
            dateTimeStamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
            scratchGDBFolder = os.path.join(scratchPath,
                                            'scratch_' + dateTimeStamp)
            if not os.path.exists(scratchGDBFolder):
                os.mkdir(scratchGDBFolder)

            # Create scratch GDB
            scratchGDB = os.path.join(scratchGDBFolder, 'scratch.gdb')
            if not os.path.exists(scratchGDB):
                arcpy.CreateFileGDB_management(os.path.dirname(scratchGDB),
                                               os.path.basename(scratchGDB))

            # Try to remove old scratch path if still exists
            try:
                shutil.rmtree(configuration.oldScratchPath, ignore_errors=True)
            except Exception:
                pass

        # Set scratch and current workspaces
        arcpy.env.scratchWorkspace = scratchGDB
        arcpy.env.workspace = scratchGDB

        # Scratch folder
        scratchFolder = arcpy.env.scratchFolder
        if not os.path.exists(scratchFolder):
            os.mkdir(scratchFolder)

        # Remove all in_memory data sets
        arcpy.Delete_management("in_memory")

    # Check disk space for disk with scratch workspace
    freeSpaceGb = 3
    if getFreeDiskSpaceGb(arcpy.env.scratchWorkspace) < freeSpaceGb:
        log.warning("Disk containing scratch workspace has less than " +
                    str(freeSpaceGb) +
                    "Gb free space. This may cause this tool to fail.")
import arcpy
print arcpy.ProductInfo()