Why firstPoint and lastPoint does not match?

401
7
09-19-2018 05:52 AM
ValerioLuzzi1
New Contributor II
SR = arcpy.Describe(linepathname).spatialReference
polyline = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686221.91579999961,4929363.8212000001],[686224.60580000002,4929363.6297999993],[686227.87069999985,4929364.5913999993],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
#segment = arcpy.Polyline(arcpy.Array([polyline.firstPoint,polyline.lastPoint]),SR)
segment = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
 
I have tried to create a simple line that join the first point and the last point of the polyline but the render on the map is wrong (see the Image).
I have an offset and I cant figure the problem. 
(The spatial reference it's the same)
Anyone can give me some suggestion?
0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

Using this code:

>>> polyline = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686221.91579999961,4929363.8212000001],[686224.60580000002,4929363.6297999993],[686227.87069999985,4929364.5913999993],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
>>> arcpy.CopyFeatures_management(polyline,"in_memory\polyline")
<Result 'in_memory\\polyline'>
>>> 
>>> segment = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
>>> arcpy.CopyFeatures_management(segment,"in_memory\segment")
<Result 'in_memory\\segment'>
>>> 

I get:

segment connecting 2 ends of polyline

What version of ArcGIS are you using?  I am using ArcMap 10.6.1.

ValerioLuzzi1
New Contributor II
My version is 10.4 but I think this cannot be the problem. I tried your code and all seems ok also for me,
so the problem is not here.
This code should duplicate a record and change the shape with the segment but here you should see the trouble.
outpathname = "in_memory\polyline"
polyline = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686221.91579999961,4929363.8212000001],[686224.60580000002,4929363.6297999993],[686227.87069999985,4929364.5913999993],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
arcpy.CopyFeatures_management(polyline,outpathname)
SR = arcpy.Describe(outpathname).spatialReference
with arcpy.da.SearchCursor(outpathname, ['SHAPE@','*'] ) as cursor:
 with arcpy.da.InsertCursor(outpathname, ['SHAPE@','*'] ) as icur:
  for row in cursor:
   polyline = row[0]
   #print polyline
   segment = arcpy.Polyline(arcpy.Array([polyline.firstPoint,polyline.lastPoint]),SR)
   row = list(row)
   row[0] = segment
   icur.insertRow(row)
Please give me a feedbak. Thank you!
0 Kudos
DarrenWiens2
MVP Honored Contributor

When you print `polyline`, it shows the correct coordinates? And, CRS is defined in the output feature class (just verify that it actually gets set in the feature class)?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Nesting an insert cursor within a search cursor on the same feature class or table seems ripe for problems.  That said, it isn't the cause of the issue you are seeing.  Try the following code:

outpathname = "in_memory\polyline"
polyline = arcpy.AsShape("""{"paths":[[[686187.19259999972,4929374.2093000002],[686221.91579999961,4929363.8212000001],[686224.60580000002,4929363.6297999993],[686227.87069999985,4929364.5913999993],[686230.85759999976,4929365.9749999996]]],"spatialReference":{"wkid":25832,"latestWkid":25832}}""", True)
arcpy.CopyFeatures_management(polyline,outpathname)
SR = arcpy.Describe(outpathname).spatialReference
with arcpy.da.SearchCursor(outpathname, ['SHAPE@','*'] ) as cursor:
 with arcpy.da.InsertCursor(outpathname, ['SHAPE@'] ) as icur:
  for row in cursor:
   polyline = row[0]
   #print polyline
   segment = arcpy.Polyline(arcpy.Array([polyline.firstPoint,polyline.lastPoint]),SR)
   icur.insertRow([segment])
ValerioLuzzi1
New Contributor II

Yes!, it works! Thank you.

The problem is probably hidden in nested cursors. I also solved with a workaround. First duplicating records and after with a (not nested ) UpdateCursor updating the geometry, but your solution is more compact and I think I will use it.

Thank you again.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I think the issue here is that you were passing conflicting geometry data to the insert cursor.  Since you used ["SHAPE@", "*"] for the fields, you were returning both a geometry object as well as a tuple of the shape's centroid x,y coordinates.  When you went to insert the record, you passed a new geometry object but with the same tuple containing the previous geometry centroid.

0 Kudos
ValerioLuzzi1
New Contributor II

Ok, I thinked something similar … but I always thinked that all spatial information was inside the SHAPE field and replacing it with a new geometry all should be fine.

Anyway Tahnk you again. I should be more  carefull  in using cursors.

0 Kudos