Select to view content in your preferred language

cut & paste annotations

2749
28
09-06-2013 05:39 AM
MikeYoung5
Emerging Contributor
Can Python be used to cut & paste Annotations?  I have read through arcpy.CopyFeatures_management and arcpy.Copy_management ESRI help docs.  I have been unable to find anything that mentions cut & paste Annotations.
Tags (2)
0 Kudos
28 Replies
RichardFairhurst
MVP Honored Contributor
I am confused as to why the script does not work.  I believe something is wrong my CopyFeatures_management statement.

import arcpy

arcpy.env.workSpace = "C:\Working\MyData\Python.gdb"
arcpy.env.overwriteOutput = 'True'

mxd = arcpy.mapping.MapDocument ("CURRENT") 
df = arcpy.mapping.ListDataFrames (mxd)[0] 
pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0]
oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0]
arcpy.AddMessage(pa.name)
arcpy.AddMessage(oa.name)

arcpy.CopyFeatures_management("pa", "oa")


Yet, if I write a basic script the CopyFeatures_management works.  I only want to copy selected features & paste into an existing Feature Class, not create a new Feature Class.

import arcpy
try:
    arcpy.env.workspace = "C:\Working\MyData\Python.gdb"

    arcpy.CopyFeatures_management("HomeValue", "Test")
except:
    print arcpy.GetMessages()


It doesn't work because you are telling the CopyFeatures_management to use the literal strings "pa" and "oa", which are not the names of any layers.  Unquote those variables and possibly use the .name property as you did in the messages.  So try either:

arcpy.CopyFeatures_management(pa, oa)

or try

arcpy.CopyFeatures_management(pa.name, oa.name)
0 Kudos
MikeYoung5
Emerging Contributor
It doesn't work because you are telling the CopyFeatures_management to use the literal strings "pa" and "oa", which are not the names of any layers.  Unquote those variables and possibly use the .name property as you did in the messages.  So try either:

arcpy.CopyFeatures_management(pa, oa)

or try

arcpy.CopyFeatures_management(pa.name, oa.name)


Richard,


I have tested each of the suggestions multiple times and my results were unsuccessful.
arcpy.CopyFeatures_management(pa, oa) approach returns the Error 840: The value is not a Feature Class.
arcpy.CopyFeatures_management(pa.name, oa.name approach runs but does not work.

But, arcpy.env.workspace = "C:\Working\MyData\Python.gdb"
arcpy.CopyFeatures_management("PropertyAnno", "OwnerAnno") works.  An annotation is selected and using the Select Features tool, the script runs, and copies and pastes the annotation from PropertyAnno into OwnerAnno.

The issue is with:

pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0]
oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0]

They are defined as Layers, but the output requires a Feature Class.  Commenting those out, the script works outside of an edit session.  But, when the script is run multiple times, the previous copy does not save.  How can the copy be saved?


Mark, I am currently testing your suggestion.
0 Kudos
AdamCrateau1
Regular Contributor
Hello,

I am trying to do a similar task:

1. Make a selection_by_attributes on a .gdb Annotation layer.
2. Copy & Paste this selection into an Existing .gdb Annotation layer.  Further, I'd like to copy the selected annotation into a specific annotation class within the specified target layer.

I am able to do this in an editing session.  Is this possible using python?  Can you use cursors to copy and paste features from one FC to another?

Thanks.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Richard,


I have tested each of the suggestions multiple times and my results were unsuccessful.
arcpy.CopyFeatures_management(pa, oa) approach returns the Error 840: The value is not a Feature Class.
arcpy.CopyFeatures_management(pa.name, oa.name approach runs but does not work.

But, arcpy.env.workspace = "C:\Working\MyData\Python.gdb"
arcpy.CopyFeatures_management("PropertyAnno", "OwnerAnno") works.  An annotation is selected and using the Select Features tool, the script runs, and copies and pastes the annotation from PropertyAnno into OwnerAnno.

The issue is with:

pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0]
oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0]

They are defined as Layers, but the output requires a Feature Class.  Commenting those out, the script works outside of an edit session.  But, when the script is run multiple times, the previous copy does not save.  How can the copy be saved?


Mark, I am currently testing your suggestion.


Try:

arcpy.CopyFeatures_management(pa, oa.dataSource)

pa should be the layer, because it has a selection, but dataSource should get the feature class and path for the output.
0 Kudos
MikeYoung5
Emerging Contributor
Try:

arcpy.CopyFeatures_management(pa, oa.dataSource)

pa should be the layer, because it has a selection, but dataSource should get the feature class and path for the output.


Richard,

The result is the same.  The script works, the selected annotation is copied from pa to oa.  But, when the script is run the second time, the first copy is nullified, and the annotation(s) revert back from oa to pa.  It works like editing and not saving edits.  The tricky part seems to be copying & pasting into an existing Feature Class and having the Annotations saved into the Feature Class.  I have also tried the Copy_management(input, output), but the statement does not like the existing oa Feature Class.  Any further suggestions on how to get the copy & paste to "save"?

Mark,

Your suggestion of copying & pasting to a new Feature Class works.  But, I want to use the two existing Annotation Feature Classes within ArcMap.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Richard,

The result is the same.  The script works, the selected annotation is copied from pa to oa.  But, when the script is run the second time, the first copy is nullified, and the annotation(s) revert back from oa to pa.  It works like editing and not saving edits.  The tricky part seems to be copying & pasting into an existing Feature Class and having the Annotations saved into the Feature Class.  I have also tried the Copy_management(input, output), but the statement does not like the existing oa Feature Class.  Any further suggestions on how to get the copy & paste to "save"?

Mark,

Your suggestion of copying & pasting to a new Feature Class works.  But, I want to use the two existing Annotation Feature Classes within ArcMap.


It sounds to me like you are expecting Copy Features to do something it won't do.  It won't append features to an existing feature class, it creates a new feature class.  To append to an existing feature class and keep any previously existing features you have to use Append.

If you actually do want to overwrite an existing feature class with a new feature class that uses the same name you cannot use the environment overwrite setting in a Python script to do that.  That setting does not work in a Python script, it only works in Model Builder.  You have to explicitly use the Delete tool to delete an existing feature class and then you can write to the same name the second time.  This is how it always works.  I have to run a script once without the Delete tool to just generate the outputs during the first run.  Then before I can run it any more times I first have to add the Delete tool to the code and then I can run it again.
0 Kudos
MikeYoung5
Emerging Contributor
It sounds to me like you are expecting Copy Features to do something it won't do.  It won't append features to an existing feature class, it creates a new feature class.  To append to an existing feature class and keep any previously existing features you have to use Append.

If you actually do want to overwrite an existing feature class with a new feature class that uses the same name you cannot use the environment overwrite setting in a Python script to do that.  That setting does not work in a Python script, it only works in Model Builder.  You have to explicitly use the Delete tool to delete an existing feature class and then you can write to the same name the second time.  This is how it always works.  I have to run a script once without the Delete tool to just generate the outputs during the first run.  Then before I can run it any more times I first have to add the Delete tool to the code and then I can run it again.


Based on your second paragraph, I was able to modify the script.  The script now runs as intended.  It copies from pa, pastes into oa, and deletes the original annotation from pa.
0 Kudos
JacobDrvar
Emerging Contributor
All,

I am writing a similiar script.  I need the output to have parentheses added around an integer. For example, instead of 31, I need the output to be (31).  Where can I locate information on how to add parentheses?
0 Kudos
RichardFairhurst
MVP Honored Contributor
All,

I am writing a similiar script.  I need the output to have parentheses added around an integer. For example, instead of 31, I need the output to be (31).  Where can I locate information on how to add parentheses?


In python to make an integer variable into a string with parentheses added around it you would do something like:

parenInt = "(" + str(myInt) + ")"

where myInt is an integer value variable and parenInt is a string of the interger surrounded by parantheses.
0 Kudos
JacobDrvar
Emerging Contributor
In python to make an integer variable into a string with parentheses added around it you would do something like:

parenInt = "(" + str(myInt) + ")"

where myInt is an integer value variable and parenInt is a string of the interger surrounded by parantheses.


The string is not working.  I suspect that is because I asked the wrong question.  I have an Annotation Feature Class that is displaying the information as integers within ArcMap, but the field type is a string.
0 Kudos