3

I'm a new python user.

I wanted to make a tool box like fishnet in ArcGIS 10.2.

Everything, works fine. However, at output there is no .prj file, although spatial reference was defined at the tool.

How can I modify the script to get a .prj file in output according to defined spatial reference?

I used following function to define spatial reference and finally used fishnet to make the grid.

spatial_ref = arcpy.GetParameter(2)

arcpy.CreateFishnet_management(outFeatureClass, originCoordinate, yAxisCoordinate, cellSizeWidth, cellSizeHeight, numRows, numColumns, oppositeCoorner, labels, templateExtent, geometryType)
0

2 Answers 2

4

PRJ files define the projection of the shapefile. To create one define the projection in ArcCatalog either by using the tool Define Projection or right click on the layer and select properties (there's a whole dialog there).

It would also be sufficient to copy the prj file from your input and rename it to match the shape file.

Considering you're doing this as part of a tool I'd go with define projection...

# get spatial reference from an existing object
desc = arcpy.Describe(InputData)
SR = desc.spatialReference

# set the spatial reference of a feature class
arcpy.DefineProjection_management(outFeatureClass,SR)

I notice your script has spatial_ref = arcpy.GetParameter(2), there may be problems with that.. perhaps you should be using spatial_ref = arcpy.GetParameterAsText(2) or there may be problems with the definition. I generally use sys.argv[n] to access parameters I don't experience problems there. Check to see if your spatial reference is an object:

SR = arcpy.SpatialReference(28355) # one for example
print type(SR)
<class 'arcpy.arcobjects.arcobjects.SpatialReference'>
print len(SR)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: object of type 'SpatialReference' has no len()

Where defining as text:

SR = "PROJCS['GDA_1994_MGA_Zone_55',GEOGCS['GCS_GDA_1994',DATUM['D_GDA_1994',SPHEROID['GRS_1980',6378137,298.257222101]],PRIMEM['Greenwich',0],UNIT['Degree',0.017453292519943295]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000],PARAMETER['False_Northing',10000000],PARAMETER['Central_Meridian',147],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0],UNIT['Meter',1]]"
print type(SR)
<type 'str'>
print len(SR)
396

To ensure that your spatial reference as passed contains a value, if it's a string check the length of the string to ensure it's not ''

3
  • my spatial_ref is a object, as I used {GetParameter} in stead of {GetParameterAsText}. My understanding was, if i chose spatial_ref as a object, it should have a .prj file in output automatically. unfortunately, it is not. Commented Dec 8, 2014 at 19:21
  • I'm not sure when you could use GetParameter, I am fairly sure that the toolbox passes everything as text. Perhaps somebody that knows will elaborate on when to use GetParameter instead of GetParameterAsText. Commented Dec 8, 2014 at 21:46
  • 1
    GetParameter return an Object, where GetParameterAsText return String. I solved the issue with an extra line of code. {arcpy.env.outputCoordinateSystem = spatial_ref} Commented Dec 8, 2014 at 21:48
2

You haven't defined your spatial reference. Your tool has passed a spatial reference object to your script as a parameter, but you script does not do anything with it. Simply passing it to your script does not define the spatial reference.

2 options:

  • Don't pass a spatial reference. Instead set the Output Coordinate System in the Environments... -> Output Coordinates dialog when you run your tool.

environment.png

  • Pass a spatial reference as you do now, then use it in your code to set the Output Coordinate System environment:

    arcpy.env.outputCoordinateSystem = arcpy.GetParameter(2)

1
  • You are right. There's no need to pass the Spatial reference. your second option works better for me. Thanks. Commented Dec 8, 2014 at 22:00

Your Answer

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

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