Select to view content in your preferred language

Spatial Reference grab not working...

3045
11
02-27-2013 11:42 AM
by Anonymous User
Not applicable
Original User: SStrand

I have a large metadata generating script that is working great, except for some reason the spatial reference of the data being grabbed and wrote to a Word file is not working.

First, the portion to write text to word (this works fine for all other instances in the script):

def search_replace_all(newword, find_str, replace_str):
    wdFindContinue = 1
    wdReplaceAll = 2
    app = win32com.client.Dispatch("Word.Application")
    app.Visible = 0
    app.DisplayAlerts = 0

    app.Documents.Open(newword)
    app.Selection.Find.Execute(find_str, False, False, False, False, False, \
        True, wdFindContinue, False, replace_str, wdReplaceAll)
    app.ActiveDocument.Close(SaveChanges=True)

    app.Quit()

search_replace_all(newword, olddate, newdate)


Here is the code that isn't working:

spatialdesc = arcpy.Describe(newlayer)
sr = spatialdesc.SpatialReference
newspatial = sr.Name

search_replace_all(newword, oldspatial, newspatial)


I've also seen this as/tried unsuccesfully in my code:


sr = arcpy.Describe(newlayer).spatialReference
newspatial = sr.name

search_replace_all(newword, oldspatial, newspatial)


All I get is a generic script error failure response when it gets to this point. Any ideas as to why it won't complete this part correctly?

I am using ArcGIS 10.0 SP3, Windows 7, Python 2.7
0 Kudos
11 Replies
MathewCoyle
Honored Contributor
How are you generating the oldspatial variable? Can you print out all the names you are passing to your word writing function in the interpreter before you attempt to call the function?
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

So the way it works is I have a template word file that is copied and then edited with metadata information from a variety of sources, including the layer I am working with. 'Oldspatial' is the filler text in the template word file that is replaced by the actual spatial reference 'newspatial'.

newlayer = arcpy.GetParameterAsText(0)
newlayername = arcpy.GetParameterAsText(1)
newdate = arcpy.GetParameterAsText(4)
tempword = "K:\\SDE_System\\metadata\\AGS10_Metadata\\_gis_metadata_template.doc"
tempdir = os.path.dirname(tempword)
newword = os.path.join(tempdir, newlayername + ".doc")
olddate = "06/09/2000"
oldspatial = "ie: State Plane California 6 Feet"


These are all related to what I have posted code wise.
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Just ran the script with

arcpy.AddMessage("test")


before the

search_replace_all(newword, oldspatial, newspatial)


and it failed without displaying the name. There is already an arcpy.AddMessage("The document is X% complete") right before

spatialdesc = arcpy.Describe(newlayer)
sr = spatialdesc.SpatialReference
newspatial = sr.Name


so something is going wrong in there...
0 Kudos
MathewCoyle
Honored Contributor
How are you passing this parameter to your tool? Object, string, something else?
newlayer = arcpy.GetParameterAsText(0)
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

In the script properties it is a Layer data type, to allow for a drop down of available layers in the ArcMap document. In the ArcGIS help under describe it made it sound like that wasn't an issue.
0 Kudos
DarrenWiens2
MVP Alum
Does it work with the capitalization shown below?
sr = spatialdesc.spatialReference
newspatial = sr.name

edit: I should add, this works for me
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Does it work with the capitalization shown below?
sr = spatialdesc.spatialReference
newspatial = sr.name

edit: I should add, this works for me


Nope, that is actually how I originally had it until, while researching possible issues, I came across this page which had it the current way.

Edit: I don't believe the code is wrong...but I have no clue why it is failing.
0 Kudos
MathewCoyle
Honored Contributor
This code works fine for me.
import arcpy


def main(layer):
    spatialdesc = arcpy.Describe(layer)
    sr = spatialdesc.SpatialReference
    newspatial = sr.Name
    arcpy.AddMessage(newspatial)

if __name__ == '__main__':
    layer = arcpy.GetParameterAsText(0)
    main(layer)

The only other issue I can think of it being is something wonky with your 2.7 install and arcpy not playing nicely.
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand


The only other issue I can think of it being is something wonky with your 2.7 install and arcpy not playing nicely.


I'll try it out on another computer here and see how it goes.
0 Kudos