I want to change by a little the position of some points that are randomly generated inside a Shapely polygon and so far what I use is this:
point_on_polygon = False
while point_on_polygon == False:
y=uniform(0,1)
if y<0.5:
new_x_pos = old_x_pos+(upbound-old_x_pos)*uniform(0,1)*uniform(0,1)
new_y_pos = old_y_pos+(upbound-old_y_pos)*uniform(0,1)*uniform(0,1)
else:
new_x_pos = old_x_pos-(old_x_pos-lowbound)*uniform(0,1)*uniform(0,1)
new_y_pos = old_y_pos-(old_y_pos-lowbound)*uniform(0,1)*uniform(0,1)
point_on_polygon = field.contains(Point(new_x_pos,new_y_pos))
However, there are some points that are near the boundaries of my polygon, and in this case, the algorithm takes forever to complete. I have checked it out and it stucks for a lot of seconds in the while loop, until it produces a point inside the polygon. I was wandering if there is a better way to produce directly a point close to the other and inside the polygon without needing the while loop for points close to the boundaries. I can detect those using the "object.distance(other)" shapely function.