1

I've got a python script that is connecting a sequence of points to make lines (script is based on answers to my previous question). To attempt to make subsequent processing more efficient, I would like to do analysis/processing to each polyline as the points are connected, rather than storing them cumulatively in a shapefile (or feature class) and then reading them back into the script later. So the revised code (just the functional looping part, for the sake of brevity) is basically this:

for start in start_cursor:
    startFeature = start.getValue(shapefieldname)
    pnt1 = startFeature.getPart()
    for end in end_cursor:
        endFeature = end.getValue(shapefieldname)
        pnt2 = endFeature.getPart()
        point.X = pnt1.X
        point.Y = pnt1.Y
        array.add(point)
        point.X = pnt2.X
        point.Y = pnt2.Y
        array.add(point)
        polyline = arcpy.Polyline(array)
        arcpy.MakeFeatureLayer_management(polyline, "sightline_lyr")

However, MakeFeatureLayer_management doesn't seem to exactly be working. Any subsequent functions I want to apply (e.g., FeatureToRaster_conversion) to that new feature layer that was supposed to be created from that polyline has no data. If I add the new feature layer to the data frame while the script is running, it shows up in the table of contents in the appropriate data frame, but with the little red exclamation mark that indicates ArcMap can't find the data for that feature layer.

Is an arcpy.Polyline able to be made into a (temporary) feature layer? If not, how can I make a feature layer such that I can do more spatial analysis in further steps? (Short of "make a shapefile or feature class, then read it back in," preferably...)

  • I think you may be encountering another aspect of an ArcPy limitation that there is an existing ArcGIS Idea to have lifted. – PolyGeo May 22 '13 at 20:05
  • 1
    Hmmm... well at least I can blame ArcPy instead of my coding :) – Erica May 22 '13 at 20:25
  • 1
    You don't need to run MakeFeatureLayer for Geoprocessing tools such as Clip, Buffer,etc. Just pass in the pure polyline geometry. – klewis May 22 '13 at 21:29
2

I think Hornbydd is right; instead of MakeFeatureLayer_management I think what you want to be using is CreateFeatureclass_management instead with an in_memory location. I'm not sure what further processing you want to do, but here's a quick replacement for your MakeFeatureLayer:

linefc = CreateFeatureclass_management("in_memory", templines, "POLYLINE")
line_cursor = arcpy.InsertCursor(linefc)
for start in start_cursor:
    startFeature = start.getValue(shapefieldname)
    pnt1 = startFeature.getPart()
    for end in end_cursor:
        endFeature = end.getValue(shapefieldname)
        pnt2 = endFeature.getPart()
        point.X = pnt1.X
        point.Y = pnt1.Y
        array.add(point)
        point.X = pnt2.X
        point.Y = pnt2.Y
        array.add(point)
        polyline = arcpy.Polyline(array)
        line_feat = line_cursor.newRow()
        line_feat.shape = polyline
        line_cursor.insertRow(line_feat)

edit: Also, using arcpy.da and the with statement is a more efficient way of looping through cursors.

  • Question about the arcpy.da -- is that compatible with Arc 10.0? – Erica May 23 '13 at 0:20
  • No, I think the data access cursors were introduced in 10.1: proceedings.esri.com/library/userconf/devsummit12/papers/… – Mintx May 23 '13 at 19:21
  • That's what I thought -- and we need the script to be backward compatible for now, so I'm avoiding the arcpy.da functions :) It is an excellent point, though. – Erica May 24 '13 at 13:09
0

Erica,

I think what you are asking does not make sense, how can you make a layer from just a single polyline? You know a FeatureClass has got to have at least an OBJECTID field and shape field. So your call to the Make Featurelayer tool will not work.

I believe if you want to have a "temporary" layer to do stuff with you should have a look at using a FeatureSet, look at this FeatureSet Example.

You could also try creating an in_memory FeatureClass as a temporary layer?

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.