Select to view content in your preferred language

Trying to project a layer in a Python script

7225
10
02-10-2011 06:14 AM
ChristinaHerrick1
New Contributor
I am trying to write a python script to do a batch extraction of pixel values.  I have a feature class in a file geodatabase that has about 6500 records in it, and I also have a list of rasters.  I am trying to write code that will select one point at a time based on an attribute called 'cellnum', scroll through the list of rasters and see if the point overlaps with any of the rasters, and if it does, it will extract the pixel value.  However, the points are in WGS84 and my rasters are all in different UTM zones.  I need to reproject the selected point to match the raster in question (the script doesn't do on-the-fly projections).

However, when I try to reproject the selected point, I get errors.

import arcpy
from arcpy import env

env.overwriteOutput = True
env.workspace = "C:/Data/Project.gdb"
env.scratchWorkspace = env.workspace
sort = "points"
inlyr = "in_memory\\sort"
newlyr = "in_memory\\sort2"
hypdir = arcpy.ListRasters("")

arcpy.MakeFeatureLayer_management(sort, inlyr)

rows = arcpy.SearchCursor(inlyr)
for row in rows:
    center = row.getValue("cellnum")
    Expression = "\"cellnum\" = " + str(center)
    
    if center == 0:
        arcpy.SelectLayerByAttribute_management(inlyr, "NEW_SELECTION", Expression)
        
        point = arcpy.SearchCursor(inlyr)
        for i in point:
            for scene in hypdir:
                des = arcpy.Describe(scene)
                descr = arcpy.Describe(inlyr)
                sr1 = des.spatialReference
                sr2 = descr.spatialReference
                            
                if sr1 != sr2:
                    print "Matching projections..."
                    arcpy.Project_management(inlyr, newlyr, str(sr1.exportToString()))


I get the error message:

Failed to execute. Parameters are not valid.
ERROR 000944: Output feature class cannot be in the in_memory workspace.
Failed to execute (Project).

I understand that.  So I change 'newlyr' to be "C:/Data/temp_sort.lyr", and I get this error:

ERROR 999999: Error executing function.
Create output feature class failed
Failed to execute (Project).

I've tried this also, newlyr = "C:/Data/Project.gdb/temp_sort", but still get an error:

Runtime error <type 'exceptions.IOError'>: C:/Data/Project.gdb/temp_sort does not exist

I'd rather not reproject the entire point file each time if I don't have to.  This script will be run in the future with other datasets that could be much larger. 

I'm at my wit's end.  Can anyone shed some light?

Thanks a million in advance, this has been giving me headaches for over a week now.
0 Kudos
10 Replies
DarrinChristy
New Contributor
This seems like related problem, I'm updating an arcgisscripting 9.3 python script to arcpy.  The In_Memory layer errors out on the Project geoprocess.

ERROR 000944: Output feature class cannot be in the in_memory workspace.

Here's the test code, minus the bogus projection info string.

import sys, string, os, shutil, arcpy

# Create the Geoprocessor object and set variables
arcpy.env.overwriteOutput = 1

Ticket_Envelope = r"C:\Temp\Matt.shp"
Ticket_Layer = r"In_Memory\Ticket_Layer"
arcpy.Project_management(Ticket_Envelope, Ticket_Layer, "blah blah blah", "", "")

So "In_Memory" feature classes seem to work like normal, with other geoprocesses in ArcPy.  However, Projection appears to be an exception And\Or a bug.  Any insight?


Hi Matthew,

This is actually a known limitation in the software.  The error message description can be found here:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00vp0000000m000944.htm

Also, the help section for the Project tool now mentions that "Although in-memory feature classes are valid input to the tool, output cannot be in the in_memory workspace."  http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000007m000000.htm

Hope this helps!
0 Kudos