2

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\[email protected]\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?

5
  • 1
    Please update the question to show what you have done so far.
    – Vince
    Jun 25, 2014 at 11:05
  • Are you running this code while in an edit session?
    – GISHuman
    Jun 25, 2014 at 13:34
  • I believe you are not inserting the geometry correctly, this link gives examples of this for both insert cursor versions, e-education.psu.edu/geog485/node/137
    – artwork21
    Jun 25, 2014 at 14:07
  • You may also want to use the SHAPE@XY token for your search cursor.
    – artwork21
    Jun 25, 2014 at 14:09
  • You are correct "artwork21". but shape@xy is also not working. any suggestions..!
    – Chenna
    Jun 26, 2014 at 6:52

2 Answers 2

1

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.
1
  • Updated the question as suggested by you. Please check the error...
    – Chenna
    Jun 25, 2014 at 13:00
1

It seems you didn't define your FieldMappings. Since you are using the Append with "NO_TEST" option, you need to dictate which fields go where. You can do it programmaticaly like the provided examples in the link. A shortcut might be to define the field mapping for the Append tool in ModelBuilder and export the model to python script (i.e. Model > Export > To Python Script...). You can then copy&paste the field_mapping argument into your script.

For the constant values, I would simply use the CalculateField tool, paired with SelectLayerByAttribute if needed.

Alternatively, there are ArcGIS extensions that could help you with you task (if you happen to have them) including: Data Interoperability, which is a water-down version of FME Desktop; and the Data Loader tool in the Production Mapping extension.

2
  • @Chenna It seems that, while I was drafting my answer for you, you changed the code logic; which make my answer now "out of context". I would argue that using Append is still a valid option. As such, I will leave my answer. Jun 25, 2014 at 13:27
  • I take it back. Looking at you new code closely, it seem schema difference is more than simple name change but data type as well. In this case Append won't work. Jun 26, 2014 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.