[GIS] How to copy selected features from one sde feature class to another with different attributes using Arcpy

arcgis-10.2arcgis-desktoparcpyenterprise-geodatabaseoracle-spatial

I am new to ArcPy scripting, I need help on copying the features to SDE feature class.

My Scenario: We have two Geodatabases running on Oracle 11g ArcSDE 10.2. I have been asked to write the Python script for query and copy the features from SDE connection1: Feature class1 to SDE connection2: Feature class2.

The feature classes are not identical. I need to copy Shape and only two attribute values feature class1 to feature class2 and also populate the remaining attribute values of feature class2 with constant values.

Please guide me in right direction to achieve this.

My Code till now:

# Import system modules
import arcpy
import os
import sys
try:
        ParcelSAMNo = arcpy.GetParameterAsText(0)
        ModeofQuery = arcpy.GetParameterAsText(1)
        TransactionNo= arcpy.GetParameterAsText(2)
        arcpy.env.workspace = r"C:\Users\chenna.kishore\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\LIC.sde\LIC.LandPlan"
        arcpy.env.overwriteOutput = True 
        fc = "LIC.LPLN_CADASTREPLOT"
        where_clause = "PIN in (" + ParcelSAMNo + ")"
        s_cursor = arcpy.da.SearchCursor(fc, ("SHAPE@", "PIN", "PDAREA"), where_clause)
        arcpy.env.workspace = r"C:\Users\chenna.kishore\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\SDE@REGEO.sde\SDE.Parcels"
        outFc="SDE.RPAR_PD_Parcels"
        with arcpy.da.InsertCursor(outFc,["PIN", "PAR_AREA", "SAM_NO", "TRANS_NO", "SHAPE@"]) as rowInserter:
            for s_row in s_cursor:
                    # Insert the new row into the Parcel Layer
                    geom = s_row[0]
                    row_values = [str(s_row[1]), s_row[2], 0, int(TransactionNo), geom]
                    print row_values
                    rowInserter.insertRow(row_values)
                    #rowInserter.insertRow([str(s_row[1]), s_row[2], 0, int(TransactionNo), geom])
        # Clean up the cursor
        del rowInserter
        del s_cursor
        del s_row
except Exception as e:
    print e.message
print "Success Full..!"

I cannot view the data that has been copied. But there is no errors from the script. Any Suggestions?

Best Answer

Since the two table schemas are different, you cannot use the Append tool. I would suggest to use the arcpy.da cursors. The general code flow would go something like this:

  1. Do a search cursor against the source layer, setup conditional logic or use a where clause to isolate the features/field attributes you would like to pull out of (e.g. geometry x/y info, and additoinal attributes), and assign them to variables.
  2. Next, use an insert cursor to create a new feature in the destination layer referencing the variables from step 1.