コード例 #1
0
def Cleanup():
    LogMessage( " Start the module Cleanup")
    ## Switch back to DEFAULT version.  This is so we can delete the version.
    LogMessage("Switching back to parent version...")
    arcpy.ChangeVersion_management("BoundaryLayer", "TRANSACTIONAL", "sde.DEFAULT", "")
    arcpy.ChangeVersion_management("FiberLayer", "TRANSACTIONAL", "sde.DEFAULT", "")
    arcpy.ChangeVersion_management("StructureLayer", "TRANSACTIONAL", "sde.DEFAULT", "")
    

    ## Reconcile and post version.
    LogMessage("Reconciling/Posting version " + versionName)
    ##logFileName = "C:/TEMP/Google/GoogleLog-" + todayhhmm + ".txt"
    arcpy.ReconcileVersions_management(inWorkspace, "ALL_VERSIONS", parentVersion, "PW." + versionName,  "LOCK_ACQUIRED", "ABORT_CONFLICTS", "BY_OBJECT", "FAVOR_EDIT_VERSION", "POST", "DELETE_VERSION")
    LogMessage( "Finished")
    return
コード例 #2
0
def changeVersion(tableName, versionName):
    retVal = False
    try:
        arcpy.ChangeVersion_management(tableName, "TRANSACTIONAL", versionName)
        retVal = True
    except:
        addMessage("Failed to change version: " + tableName + ", " +
                   versionName)
    return retVal
コード例 #3
0
ファイル: bgimport.py プロジェクト: MikeTschudi/BG-Connector
 def _makeLayer(self, dataset, row, key):
     where_clause = dataset['pkfield'] + " = " + str(key)
     feature_class = os.path.join(self._stagingWorkspace(),
                                  dataset['table'])
     layer_name = "lyr" + str(uuid.uuid1()).replace("-", "")
     arcpy.MakeFeatureLayer_management(feature_class, layer_name,
                                       where_clause)
     arcpy.ChangeVersion_management(layer_name, 'TRANSACTIONAL',
                                    self._bgbaseEditVersion(), '')
     return layer_name
コード例 #4
0
ファイル: gzSupport.py プロジェクト: wptff/gzTools
def changeVersion(tableName, versionName):
    # change to a new version in the database
    retVal = False
    try:
        arcpy.ChangeVersion_management(tableName, "TRANSACTIONAL", versionName)
        retVal = True
    except:
        addMessageLocal("Failed to change version: " + tableName + ", " +
                        versionName)
    return retVal
コード例 #5
0
def get_version_milepoint_layer(production_ws,
                                production_ws_version,
                                wildcard=LRSN_FC_WILDCARD,
                                logger=None,
                                arcpy_messages=None):
    """
    Get the name of the LRSN from the Roads and Highways ALRS using the wildcard keyward argument, and return
    the feature class name. Generate a versioned FeatureLayer of the feature class, and return it.

    Arguments
    ---------
    :param production_ws: Filepath to the SDE file pointing to the correct database. The version of the SDE filepath
        does not matter as long as it exists. The function will change to the user's version.
    :param production_ws_version: The version name that's expected to exist in the production_ws

    Keyword Arguments
    -----------------
    :param wildcard: A wildcard expression that's passed to `arcpy.ListFeatureClasses` which identifies the
        network feature class from the R&H ALRS
    :param logger: Defaults to None. If set, should be Python logging module logger object.
    :param arcpy_messages: Defaults to None. If set, should refer to the arcpy.Messages variable that is present
        in the `execute` method of Python Toolboxes.

    Returns
    -------
    :returns tuple: Returns a tuple of the milepoint_fc variable (ALRS Network Feature class) and a versioned
        `arcpy.FeatureLayer` pointing to the milepoint_fc in the production_ws, referencing the production_ws_version
    """
    original_ws = arcpy.env.workspace
    arcpy.env.workspace = production_ws

    milepoint_fcs = [fc for fc in arcpy.ListFeatureClasses(wildcard)]
    if len(milepoint_fcs) == 1:
        milepoint_fc = milepoint_fcs[0]
    else:
        raise ValueError(
            'Too many or too few feature classes were selected while trying to find LRSN_Milepoint. '
            + 'Selected FCs: {}'.format(milepoint_fcs))
    log_it('found milepoint FC: {}'.format(milepoint_fc),
           level='warn',
           logger=logger,
           arcpy_messages=arcpy_messages)

    sde_milepoint_layer = arcpy.MakeFeatureLayer_management(
        milepoint_fc, 'milepoint_layer_{}'.format(int(time.time())))

    version_milepoint_layer = arcpy.ChangeVersion_management(
        sde_milepoint_layer,
        'TRANSACTIONAL',
        version_name=production_ws_version)

    arcpy.env.workspace = original_ws

    return milepoint_fc, version_milepoint_layer
コード例 #6
0
def stopEditReconcileVersion():
    edit.stopOperation()
    edit.stopEditing(True)
    for i in fcLSTglob:
        arcpy.ChangeVersion_management(
            i.split(".")[1], "TRANSACTIONAL", "SDE.DEFAULT")
    arcpy.ReconcileVersions_management(workspace, "", "SDE.DEFAULT",
                                       userglob + "." + version,
                                       "LOCK_ACQUIRED", "NO_ABORT",
                                       "BY_OBJECT", "FAVOR_EDIT_VERSION",
                                       "POST", "DELETE_VERSION")
    os.remove(workspace)
    print("versioned editing ended")
    arcpy.AddMessage("versioned editing ended")
コード例 #7
0
    def append_to_esri_source(self, input_fc, esri_output_fc,
                              input_where_clause):
        """
        Append to the esri source FC, including projecting to output coord system, creating a versioned FC of the
        output in SDE, deleting from that versioned FC based on a where_clause, appending the new data, and then
        posting that version
        :param input_fc: the input source data
        :param esri_output_fc: the output in SDE
        :param input_where_clause: where clause used to add to/delete from the output esri FC
        :return:
        """

        logging.info(
            'Starting vector_layer.append_to_esri_source for {0}'.format(
                self.name))

        fc_to_append = self.project_to_output_srs(input_fc, esri_output_fc)

        logging.debug('Creating a versioned FL from esri_service_output')
        arcpy.MakeFeatureLayer_management(esri_output_fc,
                                          "esri_service_output_fl")

        version_name = self.name + "_" + str(int(time.time()))

        sde_workspace = os.path.dirname(esri_output_fc)
        desc = arcpy.Describe(sde_workspace)
        if hasattr(desc,
                   "datasetType") and desc.datasetType == 'FeatureDataset':
            sde_workspace = os.path.dirname(sde_workspace)

        del desc

        if os.path.splitext(sde_workspace)[1] != '.sde':
            logging.error('Could not find proper SDE workspace. Exiting.')
            sys.exit(1)

        arcpy.CreateVersion_management(sde_workspace, "sde.DEFAULT",
                                       version_name, "PRIVATE")
        arcpy.ChangeVersion_management("esri_service_output_fl",
                                       'TRANSACTIONAL', 'gfw.' + version_name,
                                       '')

        if input_where_clause:
            logging.debug(
                'Deleting features from esri_service_output feature layer based on input_where_clause. '
                'SQL statement: {0}'.format(input_where_clause))

            arcpy.SelectLayerByAttribute_management("esri_service_output_fl",
                                                    "NEW_SELECTION",
                                                    input_where_clause)

            # Delete the features selected by the input where_clause
            arcpy.DeleteRows_management("esri_service_output_fl")

        else:
            logging.debug(
                'No where clause for esri_service_output found; deleting all features before '
                'appending from source')

            # commented out at this already happens below with wc
            # arcpy.MakeFeatureLayer_management("esri_service_output_fl", "fl_to_delete")
            # arcpy.DeleteRows_management("fl_to_delete")
            # arcpy.Delete_management("fl_to_delete")

            sde_sql_conn = arcpy.ArcSDESQLExecute(sde_workspace)
            esri_fc_name = os.path.basename(esri_output_fc) + '_evw'

            # Find the min and max OID values
            to_delete_oid_field = [
                f.name for f in arcpy.ListFields(esri_output_fc)
                if f.type == 'OID'
            ][0]

            sql = 'SELECT min({0}), max({0}) from {1}'.format(
                to_delete_oid_field, esri_fc_name)
            to_delete_min_oid, to_delete_max_oid = sde_sql_conn.execute(sql)[0]

            # If there are features to delete, do it
            if to_delete_min_oid and to_delete_max_oid:

                for wc in util.generate_where_clause(to_delete_min_oid,
                                                     to_delete_max_oid,
                                                     to_delete_oid_field,
                                                     1000):

                    logging.debug('Deleting features with {0}'.format(wc))
                    arcpy.MakeFeatureLayer_management("esri_service_output_fl",
                                                      "fl_to_delete", wc)

                    arcpy.DeleteRows_management("fl_to_delete")
                    arcpy.Delete_management("fl_to_delete")

            else:
                pass

        esri_output_pre_append_count = int(
            arcpy.GetCount_management("esri_service_output_fl").getOutput(0))
        input_feature_count = int(
            arcpy.GetCount_management(fc_to_append).getOutput(0))

        logging.debug('Starting to append to esri_service_output')

        # Find the min and max OID values
        to_append_oid_field = [
            f.name for f in arcpy.ListFields(fc_to_append) if f.type == 'OID'
        ][0]
        to_append_min_oid, to_append_max_oid = cartodb.ogrinfo_min_max(
            fc_to_append, to_append_oid_field)

        for wc in util.generate_where_clause(to_append_min_oid,
                                             to_append_max_oid,
                                             to_append_oid_field, 1000):

            logging.debug('Appending features with {0}'.format(wc))
            arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append", wc)

            arcpy.Append_management("fl_to_append", "esri_service_output_fl",
                                    "NO_TEST")
            arcpy.Delete_management("fl_to_append")

        logging.debug('Append finished, starting to reconcile versions')

        arcpy.ReconcileVersions_management(
            input_database=sde_workspace,
            reconcile_mode="ALL_VERSIONS",
            target_version="sde.DEFAULT",
            edit_versions='gfw.' + version_name,
            acquire_locks="LOCK_ACQUIRED",
            abort_if_conflicts="NO_ABORT",
            conflict_definition="BY_OBJECT",
            conflict_resolution="FAVOR_TARGET_VERSION",
            with_post="POST",
            with_delete="KEEP_VERSION",
            out_log="")

        logging.debug('Deleting temporary FL and temporary version')

        # For some reason need to run DeleteVersion_management here, will have errors if with_delete is used above
        arcpy.Delete_management("esri_service_output_fl")
        arcpy.DeleteVersion_management(sde_workspace, 'gfw.' + version_name)

        post_append_count = int(
            arcpy.GetCount_management(esri_output_fc).getOutput(0))

        if esri_output_pre_append_count + input_feature_count == post_append_count:
            logging.debug('Append successful based on sum of input features')
        else:
            logging.debug(
                'esri_output_pre_append_count: {0}\input_feature_count: {1}\npost_append_count{2}\n'
                'Append failed, sum of input features does not match. '
                'Exiting'.format(esri_output_pre_append_count,
                                 input_feature_count, post_append_count))
            sys.exit(1)

        return
コード例 #8
0
	def switch_version(self, version="dbo.DEFAULT"):
		"""Switches to the targeted version. Unless the child version is specifically called, it will change to the default parent version, dbo.DEFAULT"""
	
		for fc in self.FC:
			arcpy.ChangeVersion_management(fc, "TRANSACTIONAL", version, "")
コード例 #9
0
def CreateFeatures():
        ##Create a version off your personal version of PW.

        arcpy.CreateVersion_management(inWorkspace, parentVersion, versionName, "PROTECTED")
        LogMessage(" version created.")
        arcpy.MakeFeatureLayer_management(GoogleBoundary, "BoundaryLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleFiber, "FiberLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleStructure, "StructureLayer", "", "", "")
        LogMessage(" Layers created.")
        LogMessage("Changing version to " + versionName + "...")
        arcpy.ChangeVersion_management("BoundaryLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("FiberLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("StructureLayer", "TRANSACTIONAL", "PW." + versionName, "")

        LogMessage(" Start the CreateFeatures module.")        

        GoogleFolder = os.listdir(Google)
        for tempfolders in GoogleFolder:
            LogMessage (" " + tempfolders)
            PermitID = tempfolders
            query = "CODPermitNumber = " + "'" + PermitID + "'"
            path = Google +"/" + tempfolders
            LogMessage (" This is the path: "+ path)
            i = 0

##            # Select Layer By Attribute - Looks for existing features based on the permit
##            # ID and deletes them if found, for permit revisions.  
##            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("BoundaryLayer").getOutput(0))
##            # Show number of features found to be deleted.
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Permits to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("BoundaryLayer")
##                LogMessage (" Old Google Boundary deleted")
##            else:
##                LogMessage ("No features to delete")
##
##            # Finds the fiber features that need to be deleted before appending new revision. 
##            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("FiberLayer").getOutput(0))
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Fiber features to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("FiberLayer")
##                LogMessage (" Old Google Fibers were deleted")
##            else:
##                LogMessage ("No features to delete")
##
##            # Finds the structure features that need to be deleted before appending new revision. 
##            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", query)
##            count = int(arcpy.GetCount_management("StructureLayer").getOutput(0))
##            LogMessage (" " + str(count))
##            if count > 0:
##                LogMessage (str(count) + " Structure features to be deleted by attribute.")
##                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
##                arcpy.DeleteFeatures_management("StructureLayer")
##                LogMessage (" Old Google Structures were deleted")
##            else:
##                LogMessage ("No features to delete")
            

            #now walk the folder/file structure to find all the shapefiles and feature classes we need. Walk needs to be used when
            #you want it to see path and determine its structure. Otherwise it will see path as a string and look at each character.
            for files in os.walk(path):
                LogMessage( " Starting walkthrough ")
                try:
                    LogMessage( files )
                    fullpath = path
                    LogMessage( fullpath )
                    if os.path.isfile (fullpath + "/PERMITAREA.shp") is True:
                        NewAreaPolygon = fullpath + "/PERMITAREA.shp"
                        LogMessage(" PERMITAREA Found") ##shapefile that includes the GoogleBoundary for this PermitID
                        arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                        LogMessage(" Google boundary append complete")
                        
                    else:
                        LogMessage(" No boundary file found for " + PermitID) ##creates a message about the missing boundary file

##                    
##                    if os.path.isfile (fullpath + "/FIBERS.shp") is True:
##                        NewRunningLine = fullpath + "/FIBERS.shp"  ##shapefile that includes the GoogleFiber for this PermitID
##                        LogMessage(NewRunningLine)
##                        arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
##                        LogMessage(" Google fiber append complete!")
##                        
##                    else:
##                        LogMessage(" No google fiber layer found!")
##                
##                    if os.path.isfile (fullpath + "/STRUCTURES.shp") is True:
##                        NewStructure = fullpath + "/STRUCTURES.shp"  ##shapefile that includes the GoogleStructure for this PermitID
##                        LogMessage(NewStructure)
##                        arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
##                    else:
##                        LogMessage(" No Structure.shp")
                except:
                    LogMessage(" Problem with append")


            LogMessage (" Make feature layer and calculate permitID.")
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            link = "http://pwgis/permits/" + PermitID + ".PDF"
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitLink", "\"" + link + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("FiberLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("StructureLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            LogMessage(" Permit fields calculated.")
コード例 #10
0
        arcpy.AddMessage(
            u"Se han indicado los siguientes parámetros para realizar el montaje:"
        )
        arcpy.AddMessage(u"Versión: " + str(version))
        arcpy.AddMessage(u"Ubicación: " + str(ubic))
        arcpy.AddMessage("Planos: " + str(ingresados))
        arcpy.AddMessage(u"El montaje se está realizando... \n")

        try:

            #Intersect para obtener traslape
            SQLmos = "NUMERO_PLA = '" + "' OR NUMERO_PLA = '".join(
                ingresados.split(",")) + "'"
            arcpy.MakeFeatureLayer_management("SIRIGISADMIN.MOSAICO_PLANOS",
                                              "layer_mos", SQLmos)
            arcpy.ChangeVersion_management("layer_mos", "TRANSACTIONAL",
                                           version)
            arcpy.CopyFeatures_management("layer_mos",
                                          temp + "\\Mosaico_Catastral")
            arcpy.Intersect_analysis("layer_mos", temp + "\\Traslape")

            #Carga las capas del Intersect y la totalidad de los planos involucrados
            rutaMos = r'C:\Scripts\Montajes\Temp.gdb\Mosaico_Catastral'
            rutaTras = r'C:\Scripts\Montajes\Temp.gdb\Traslape'

            mosLayer = arcpy.mapping.Layer(rutaMos)
            arcpy.mapping.AddLayer(df, mosLayer, "TOP")
            arcpy.mapping.AddLayer(dfMapa, mosLayer, "TOP")

            trasLayer = arcpy.mapping.Layer(rutaTras)

            arcpy.mapping.AddLayer(df, trasLayer, "TOP")
コード例 #11
0
    def onClick(self):
        print "building tool button is executing"
        #Variables for Creating the polygon
        xCoordinate = float(tool.x)
        yCoordinate = float(tool.y)
        #I'll fix this variable name later. My bad.
        #bob is the buffer distance
        bob = float(combobox.buffDist)

        print "Centroid of buidling:(%r,%r) Side length: %r" % (
            xCoordinate, yCoordinate, bob)
        print __name__

        def doStuff():
            #Calculates and creates an array of vertex points, opens an
            #insert cursor, creates a polygon using the arra, and inserts
            #building into the appropriate database.
            vertices = []
            vertices.append(
                arcpy.Point((xCoordinate + bob), (yCoordinate + bob)))
            vertices.append(
                arcpy.Point((xCoordinate + bob), (yCoordinate - bob)))
            vertices.append(
                arcpy.Point((xCoordinate - bob), (yCoordinate - bob)))
            vertices.append(
                arcpy.Point((xCoordinate - bob), (yCoordinate + bob)))
            vertices.append(
                arcpy.Point((xCoordinate + bob), (yCoordinate + bob)))

            cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
            array = arcpy.Array(vertices)
            simpleBuilding = arcpy.Polygon(array)
            cursor.insertRow([simpleBuilding])

            del cursor
            print "Building should be drawn"

        def makeConnection(authenticationType, pathParent):
            #Creates connection file so code can edit .sde database
            #make this work when r"U:\tempstuff" already exists
            outFolderPath = pathParent
            outName = r"actualName" + str(button2.databaseX) + r".sde"
            databasePlatform = "SQL_Server"
            instance = button2.instanceX
            database = button2.databaseX
            username = "******"  #doesn't seem to matter
            password = "******"  #doesn't seem to matter
            version = button2.currentVersion

            arcpy.CreateDatabaseConnection_management(
                outFolderPath, outName, databasePlatform, instance,
                authenticationType, username, password, 'DO_NOT_SAVE_USERNAME',
                database, '', 'TRANSACTIONAL', version, '')

        fcName = button2.featureClass
        workspace = button2.versionWorkspace
        featureDataset = button2.featureDataset
        fc = os.path.join(workspace, featureDataset, fcName)

        #Action to take if layer is in a .sde version.
        if button2.isSDE == True:

            database = str(button2.databaseX)
            pathParent = r"U:\tempBuildingTool"
            path = pathParent + r"\actualName" + database + r".sde"

            if not os.path.isdir(r"U:\tempBuildingTool"):
                os.makedirs(pathParent)

            if not os.path.isfile(path):
                authenticationType = 'OPERATING_SYSTEM_AUTH'
                makeConnection(authenticationType, pathParent)

            workspace = pathParent + r"\actualName" + database + r".sde"
            fc = os.path.join(workspace, featureDataset, fcName)
            print workspace
            print "Feature class is %r" % fc
            print "initiating editing"

            #Catching all errors with the table view or using the same trick
            #with isSDE to make sure table view is only created when it needs
            #to be.

            #Occassionally there is a strange error that appears because the
            #the connection file is too old. If you're getting an error
            #regarding the tableview or if the building isn't being drawn and
            #even though you're not getting an error, try deleting the
            #connection file. The script will rebuild it for you.
            if not arcpy.Exists("editView" + database):
                try:
                    #occasionally fails because the connection file is too
                    #old. Just delete the connecton file and a new one will
                    #be made next time you run the tool
                    tableView = arcpy.MakeTableView_management(
                        fc, "editView" + database, "", workspace, '')
                    button2.isTableView = True
                except arcpy.ExecuteError:
                    pass

                arcpy.ChangeVersion_management((tableView), "TRANSACTIONAL",
                                               button2.currentVersion, '')

            try:
                edit = arcpy.da.Editor(workspace)
                edit.startEditing(True, True)
                edit.startOperation()

                doStuff()
                print "made it past function call."
                edit.stopOperation()
                print "Stopping editing"
                edit.stopEditing(True)
            except RuntimeError:
                print "Still in an edit session"

        #if no versions then the function runs without using the
        #connection file
        else:
            print "made it to else statemenet"
            fc = button2.featureClass
            doStuff()
            print "made it past function call"
コード例 #12
0
def CreateFeatures():
        ##Create a version off your personal version of PW.

        arcpy.CreateVersion_management(inWorkspace, parentVersion, versionName, "PROTECTED")
        LogMessage(" version created.")
        arcpy.MakeFeatureLayer_management(GoogleBoundary, "BoundaryLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleFiber, "FiberLayer", "", "", "")
        arcpy.MakeFeatureLayer_management(GoogleStructure, "StructureLayer", "", "", "")
        LogMessage(" Layers created.")
        LogMessage("Changing version to " + versionName + "...")
        arcpy.ChangeVersion_management("BoundaryLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("FiberLayer", "TRANSACTIONAL", "PW." + versionName, "")
        arcpy.ChangeVersion_management("StructureLayer", "TRANSACTIONAL", "PW." + versionName, "")

        LogMessage(" Start the CreateFeatures module.")        
        GoogleFolder = os.listdir(Google)
        for tempfolders in GoogleFolder:
            LogMessage (" " + tempfolders)
            PermitID = tempfolders
            query = "CODPermitNumber = " + "'" + PermitID + "'"
            ##dirs = os.listdir(tempfolders)
            path = Google +"/" + tempfolders
            LogMessage (" This is the path: "+ path)
            i = 0

            # Select Layer By Attribute - Looks for existing features based on the permit
            # ID and deletes them if found, for permit revisions.  
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("BoundaryLayer").getOutput(0))
            # Show number of features found to be deleted.
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Permits to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("BoundaryLayer")
                LogMessage (" Old Google Boundary deleted")
            else:
                LogMessage ("No features to delete")

            # Finds the fiber features that need to be deleted before appending new revision. 
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("FiberLayer").getOutput(0))
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Fiber features to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("FiberLayer")
                LogMessage (" Old Google Fibers were deleted")
            else:
                LogMessage ("No features to delete")

            # Finds the structure features that need to be deleted before appending new revision. 
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", query)
            count = int(arcpy.GetCount_management("StructureLayer").getOutput(0))
            LogMessage (" " + str(count))
            if count > 0:
                LogMessage (str(count) + " Structure features to be deleted by attribute.")
                LogMessage ("***********************I AM DELETING SOMETHING*****************************")
                arcpy.DeleteFeatures_management("StructureLayer")
                LogMessage (" Old Google Structures were deleted")
            else:
                LogMessage ("No features to delete")
            

            #now walk the folder/file structure to find all the shapefiles and feature classes we need.    
            for(path, dirs, files) in os.walk(path):
                
                for x in dirs:
                    LogMessage(" " + x)
                    try:
                        fullpath = path + "/" + x
                        
                        if os.path.isfile(fullpath + "/Lcp_Area.shp") is True:
                            NewAreaPolygon = fullpath + "/Lcp_Area.shp"
                            LogMessage(" Lcp_Area Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile(fullpath + "/Permit_Area_Polygon.shp") is True:
                            NewAreaPolygon = fullpath + "/Permit_Area_Polygon.shp"
                            LogMessage(" Permit_Area_Polygon found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile(fullpath + "/PermitAreaPolygon.shp") is True:
                            NewAreaPolygon = fullpath + "/PermitAreaPolygon.shp"
                            LogMessage(" PermitAreaPolygon found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/Lcp.shp") is True:
                            NewAreaPolygon = fullpath + "/Lcp.shp"
                            LogMessage(" Lcp Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/LCPAREA.shp") is True:
                            NewAreaPolygon = fullpath + "/LCPAREA.shp"
                            LogMessage(" LCPAREA found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        elif os.path.isfile (fullpath + "/RoughRunningLine_Buffer.shp") is True:
                            NewAreaPolygon = fullpath + "/RoughRunningLine_Buffer.shp"
                            LogMessage(" RoughRunningLine_Buffer Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                                
                        elif os.path.isfile (fullpath + "/Permit_Polygon.shp") is True:
                            NewAreaPolygon = fullpath + "/Permit_Polygon.shp"
                            LogMessage(" Permit_Polygon Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")

                        elif os.path.isfile (fullpath + "/PermitPolygon.shp") is True:
                            NewAreaPolygon = fullpath + "/PermitPolygon.shp"
                            LogMessage(" PermitPolygon Found") ##shapefile that includes the GoogleBoundary for this PermitID
                            arcpy.Append_management(NewAreaPolygon, "BoundaryLayer", "NO_TEST")
                            LogMessage(" Google boundary append complete")
                            
                        else:
                            LogMessage(" No boundary file found for " + PermitID) ##creates a message about the missing boundary file

                        
                        if os.path.isfile (fullpath + "/Rough_Running_Line.shp") is True:
                            NewRunningLine = fullpath + "/Rough_Running_Line.shp"  ##shapefile that includes the GoogleFiber for this PermitID
                            LogMessage(NewRunningLine)
                            arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
                            LogMessage(" Google fiber append complete!")
                                
                        elif os.path.isfile (fullpath + "/RoughRunningLine.shp") is True:
                            NewRunningLine = fullpath + "/RoughRunningLine.shp"  ##shapefile that includes the GoogleFiber for this PermitID
                            LogMessage(NewRunningLine)
                            arcpy.Append_management(NewRunningLine, "FiberLayer", "NO_TEST")
                            LogMessage(" Google fiber append complete!")
                            
                        else:
                            LogMessage(" No google fiber layer found!")
                    
                        if os.path.isfile (fullpath + "/Structure.shp") is True:
                            NewStructure = fullpath + "/Structure.shp"  ##shapefile that includes the GoogleStructure for this PermitID
                            LogMessage(NewStructure)
                            arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
                            LogMessage(" Google structure append complete!")
                        elif os.path.isfile (fullpath + "/Structures.shp") is True:
                            NewStructure = fullpath + "/Structures.shp"  ##shapefile that includes the GoogleStructure for this PermitID
                            LogMessage(NewStructure)
                            arcpy.Append_management(NewStructure, "StructureLayer", "NO_TEST")
                            LogMessage(" Google structure append complete!")
                        else:
                            LogMessage(" No Structure.shp")
                    except:
                        LogMessage(" Problem with append")


            LogMessage (" Make feature layer and calculate permitID.")
            arcpy.SelectLayerByAttribute_management("BoundaryLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            link = "http://pwgis/permits/" + PermitID + ".PDF"
            arcpy.CalculateField_management("BoundaryLayer", "CODPermitLink", "\"" + link + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("FiberLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("FiberLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            arcpy.SelectLayerByAttribute_management("StructureLayer", "NEW_SELECTION", "CODPermitNumber is null")
            arcpy.CalculateField_management("StructureLayer", "CODPermitNumber", "\"" + PermitID + "\"", "VB", "")
            LogMessage(" Permit fields calculated.")
コード例 #13
0
    def append_to_esri_source(self, input_fc, esri_output_fc,
                              input_where_clause):
        """
        Append to the esri source FC, including projecting to output coord system, creating a versioned FC of the
        output in SDE, deleting from that versioned FC based on a where_clause, appending the new data, and then
        posting that version
        :param input_fc: the input source data
        :param esri_output_fc: the output in SDE
        :param input_where_clause: where clause used to add to/delete from the output esri FC
        :return:
        """

        logging.info(
            'Starting vector_layer.append_to_esri_source for {0}'.format(
                self.name))

        fc_to_append = self.project_to_output_srs(input_fc, esri_output_fc)
        logging.info('appending {} to {}'.format(input_fc, esri_output_fc))

        logging.debug('Creating a versioned FL from esri_service_output')
        arcpy.MakeFeatureLayer_management(esri_output_fc,
                                          "esri_service_output_fl")

        version_name = self.name + "_" + str(int(time.time()))

        sde_workspace = os.path.dirname(esri_output_fc)
        desc = arcpy.Describe(sde_workspace)
        if hasattr(desc,
                   "datasetType") and desc.datasetType == 'FeatureDataset':
            sde_workspace = os.path.dirname(sde_workspace)

        del desc

        if os.path.splitext(sde_workspace)[1] != '.sde':
            logging.error('Could not find proper SDE workspace. Exiting.')
            sys.exit(1)

        arcpy.CreateVersion_management(sde_workspace, "sde.DEFAULT",
                                       version_name, "PRIVATE")
        arcpy.ChangeVersion_management("esri_service_output_fl",
                                       'TRANSACTIONAL', 'gfw.' + version_name,
                                       '')

        if input_where_clause:
            logging.debug(
                'Deleting features from esri_service_output feature layer based on input_where_clause. '
                'SQL statement: {0}'.format(input_where_clause))

            arcpy.SelectLayerByAttribute_management("esri_service_output_fl",
                                                    "NEW_SELECTION",
                                                    input_where_clause)

            # Delete the features selected by the input where_clause
            arcpy.DeleteRows_management("esri_service_output_fl")

        else:
            logging.debug(
                'No where clause for esri_service_output found; deleting all features before '
                'appending from source')

            sde_sql_conn = arcpy.ArcSDESQLExecute(sde_workspace)
            esri_fc_name = os.path.basename(esri_output_fc)
            print esri_fc_name

            # why this, exactly?
            # there's also a lbr_plantations_old feature class (for some reason)
            # and lbr_plantation_evw points to that.
            # I don't know why and I don't have time to fix it

            # WDPA test - leave off
            if esri_fc_name != 'gfw_countries.gfw.lbr_plantations':
                esri_fc_name += '_evw'

            # Find the min and max OID values
            to_delete_oid_field = [
                f.name for f in arcpy.ListFields(esri_output_fc)
                if f.type == 'OID'
            ][0]

            sql = 'SELECT min({0}), max({0}) from {1}'.format(
                to_delete_oid_field, esri_fc_name)
            to_delete_min_oid, to_delete_max_oid = sde_sql_conn.execute(sql)[0]

            # If there are features to delete, do it
            if to_delete_min_oid and to_delete_max_oid:

                for wc in util.generate_where_clause(to_delete_min_oid,
                                                     to_delete_max_oid,
                                                     to_delete_oid_field,
                                                     1000):

                    logging.debug('Deleting features with {0}'.format(wc))
                    arcpy.MakeFeatureLayer_management("esri_service_output_fl",
                                                      "fl_to_delete", wc)

                    arcpy.DeleteRows_management("fl_to_delete")
                    arcpy.Delete_management("fl_to_delete")

            else:
                pass

        esri_output_pre_append_count = int(
            arcpy.GetCount_management("esri_service_output_fl").getOutput(0))
        input_feature_count = int(
            arcpy.GetCount_management(fc_to_append).getOutput(0))

        logging.debug('Starting to append to esri_service_output')

        # don't need to batch append if it's coming from an SDE data source
        # these are used exclusively by country-vector layers
        # and the data is generally small, compared to things like WDPA
        if 'sde' in fc_to_append:
            logging.debug(
                "Appending all features from {}- no wc because it's an SDE input"
                .format(fc_to_append))

            arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append")
            arcpy.Append_management("fl_to_append", "esri_service_output_fl",
                                    "NO_TEST")

            arcpy.Delete_management("fl_to_append")

        else:
            # Find the min and max OID values
            to_append_oid_field = [
                f.name for f in arcpy.ListFields(fc_to_append)
                if f.type == 'OID'
            ][0]
            to_append_min_oid, to_append_max_oid = cartodb.ogrinfo_min_max(
                fc_to_append, to_append_oid_field)

            for wc in util.generate_where_clause(to_append_min_oid,
                                                 to_append_max_oid,
                                                 to_append_oid_field, 1000):

                logging.debug('Appending features with {0}'.format(wc))
                arcpy.MakeFeatureLayer_management(fc_to_append, "fl_to_append",
                                                  wc)

                arcpy.Append_management("fl_to_append",
                                        "esri_service_output_fl", "NO_TEST")
                arcpy.Delete_management("fl_to_append")

        logging.debug('Append finished, starting to reconcile versions')

        arcpy.ReconcileVersions_management(
            input_database=sde_workspace,
            reconcile_mode="ALL_VERSIONS",
            target_version="sde.DEFAULT",
            edit_versions='gfw.' + version_name,
            acquire_locks="LOCK_ACQUIRED",
            abort_if_conflicts="NO_ABORT",
            conflict_definition="BY_OBJECT",
            conflict_resolution="FAVOR_TARGET_VERSION",
            with_post="POST",
            with_delete="KEEP_VERSION",
            out_log="")

        logging.debug('Deleting temporary FL and temporary version')

        # For some reason need to run DeleteVersion_management here, will have errors if with_delete is used above
        arcpy.Delete_management("esri_service_output_fl")
        arcpy.DeleteVersion_management(sde_workspace, 'gfw.' + version_name)

        post_append_count = int(
            arcpy.GetCount_management(esri_output_fc).getOutput(0))

        if esri_output_pre_append_count + input_feature_count == post_append_count:
            logging.debug('Append successful based on sum of input features')
        else:
            logging.debug(
                'esri_output_pre_append_count: {0}\input_feature_count: {1}\npost_append_count{2}\n'
                'Append failed, sum of input features does not match. '
                'Exiting'.format(esri_output_pre_append_count,
                                 input_feature_count, post_append_count))
            sys.exit(1)

        # now that we've finished syncing everything locally, need to push it to the PROD replica
        local_sde = r'D:\scripts\connections\gfw (gfw@localhost).sde'
        prod_gdb = r'P:\data\gfw_database\gfw.gdb'
        arcpy.SynchronizeChanges_management(local_sde, "gfw.GFW", prod_gdb,
                                            "FROM_GEODATABASE1_TO_2")

        return