0

My function takes the points a polyline and removes the multiple points along any straight line segment.

The points fed in are as follows:

pts=[['639.625', '-180.719'], ['629.625', '-180.719'], ['619.625', '-180.719'], ['617.312', '-180.719'], ['610.867', '-182.001'], ['605.402', '-185.652'], ['601.751', '-191.117'], ['600.469', '-197.562'], ['600.469', '-207.562'], ['600.469', '-208.273']]

  pta=[None]*2
  ptb=[None]*2
  ptc=[None]*2
  simplepts=[]
  for pt in pts:
    if pta[0]==None:
      simplepts.append(pt)
      pta[:]=pt
      continue
    if ptb[0]==None:
      ptb[:]=pt
      continue
    if ptb==pta:
      ptb[:]=pt
      continue
    ptc[:]=pt
    print simplepts#<--[['639.625', '-180.719'], ['605.402', '-185.652']]
    # we check if a, b and c are on a straight line
    # if they are, then b becomes c and the next point is allocated to c.
    # if the are not, then a becomes b and the next point is allocate to c
    if testforStraightline(pta,ptb,ptc):
      ptb[:]=ptc # if it is straight
    else:
      simplepts.append(ptb)
      print simplepts#<--[['639.625', '-180.719'], ['617.312', '-180.719']]
      pta[:]=ptb # if it's not straight

If the section is not straight, then the ptb is appended to the simplepts array, which is now (correctly) [['639.625', '-180.719'], ['617.312', '-180.719']]

However, on the next pass the simplepts array has changed to [['639.625', '-180.719'], ['605.402', '-185.652']] which is baffling.

I presume that the points in my array are being held by reference only and changing other values updates the values in the array.

How do I make sure that my array values retain the values as they are assigned?

Thank you.

Dave Coventry
  • 129
  • 4
  • 11

1 Answers1

0

You are appending a list ptb in simplepts and then you are modifying it in place.Not sure if you can improve your design. But a quick solution with current design-

import copy
simplepts.append(copy.deepcopy(ptb))
AlokThakur
  • 3,599
  • 1
  • 19
  • 32