I have a script that creates a polyline geometry from scratch using some math to generate coordinate pairs. At the same time, there is a user-defined polygon shapefile that is used to "erase" the polylines. Doing this using the low-level geometry methods (e.g. http://bit.ly/1vBMSjP under the "Methods" section) is much, much faster than using feature classes (e.g. arcpy.Erase_analysis). However I am trying to figure out the fastest way to assign a projection to the un-projected polyline geometry coordinates. Is there a way to set a default projection for all processing in arcpy?

I want to use this geometry operator to erase the polyline geometry:


However this fails if both geometry objects do not have the same projection. So my question is, is it possible to set a default projection for all geometry and arcpy operations, so that a new geometry created from scratch will be assumed to have that projection?

For reference, below are the three strategies that I have used so far to assign a projection to the geometry, since I have not been able to figure out how to assign a default projection.

  1. use the "Define Projection" function

    arcpy.DefineProjection_management(lineGeometry, SpatialReference)

where lineGeometry is a Polyline geometry object created from calculated coordinates, and SpatialReference is read from the polygon shapefile. This is very slow (possibly because the geometry is converted to a feature class internally, then back to a geometry).

  1. copy the line geometry into a pre-existing polyline feature class with a defined projection, then copy it back to a geometry list object. Oddly enough this is much faster than #1 above.

    g = arcpy.Geometry()
    lineProjected = arcpy.CopyFeatures_management(linesTemp, g)
    currentLine = linesTemp[0]
  2. Use a polyline feature class with a defined projection, with only one polyline feature in it. Then, copy the line geometry directly into the SHAPE field of the existing polyline feature, overwriting the existing geometry. Then copy it back to a geometry list object. This is the fastest method I have found so far.

    g = arcpy.Geometry()
    with arcpy.da.UpdateCursor(linesTemp,["SHAPE@"]) as cursor:
        for row in cursor:
            row[0] = lineGeometry
            #Update the current cursor with the changes
    #Copy the projected line feature back out into a geometry list
    lineProjected = arcpy.CopyFeatures_management(linesTemp, g)
    #Get the actual line geometry from the geometry list
    lineGeometry= lineProjected[0]

You can set the Output Coordinate System (Environment setting), but only certain tools will honor it. I don't think it will help for what you're doing.

Why don't you just use coordinate pairs that are in the projected coordinate system you want to use? And then set the spatial reference when you create the polyline?

arcpy.Polyline(inputs, {spatial_reference}, {has_z}, {has_m})
  • 1
    +1. If the lines need to be in a different projection from the polygons, create the line with the spatial_reference parameter set to that different projection, then use the projectAs() method to project the line into the polygon's projection. – nmpeterson Oct 2 '14 at 23:36
  • Thanks Ian, this looks very promising. I will try it and see if it works. – spindrift Oct 3 '14 at 0:13
  • This solved my problem. I was not aware that you could assign a projection directly using arcpy.Polyline(inputs, {spatial_reference}, {has_z}, {has_m}). That is the reason why my polyline geometry objects were being created without any projection. Very convenient, thank you. – spindrift Oct 3 '14 at 0:24
  • @spindrift Awesome, glad it worked! – ianbroad Oct 3 '14 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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