
import arcpy
import random
import math

arcpy.env.overwriteOutput = True




def setupMask():
    OriginalPoint = arcpy.GetParameterAsText(0)
    BasePolygon = arcpy.GetParameterAsText(1)
    Method = arcpy.GetParameterAsText(2)
    Output = arcpy.GetParameterAsText(3)
    #MaskingMethod = ARCPY.GetParameterAsText(2).upper().replace(" ", "_")


    
    Mask(OriginalPoint, BasePolygon, Method, Output)



def Mask(OriginalPoint, BasePolygon, Method, Output):
    InputPoint = OriginalPoint
    
    sc = arcpy.SearchCursor(InputPoint)
    row = sc.next()
    p_x = []
    p_y = []
    while row:
        p_x.append(row.x)
        p_y.append(row.y)
        row = sc.next()

        
    mask = (max(p_x) - min(p_x)) / 20  # 10 E11
    MaskMethod = Method
    pi = math.pi
 
    
    p2_x = []
    p2_y = []
    sc = arcpy.SearchCursor(InputPoint)
    row = sc.next()
    while row:
        
        

        if MaskMethod == 'first':   # 1 == straight replace 
            p2_x.append(row.x + mask)
            p2_y.append(row.y + mask)
        if MaskMethod == 'second':   # 2 == donuts
            theta = random.randrange(0,361) 
            x_mask = mask* math.cos(theta*pi/180)
            y_mask = mask* math.sin(theta*pi/180)
            
            p2_x.append(row.x + x_mask)
            p2_y.append(row.y + y_mask)
            
        
        row = sc.next()

        
    i =0
    num = len(p2_x)
    InsideIndex = []
    while i < num:
        output = r"D:\\Data\\" + 'test' + str(i) + '.shp'
        ptList = [ [ p2_x[i], p2_y[i] ] ]
        pt = arcpy.Point()
        ptGeoms = []
        for p in ptList:
            pt.X = p[0]
            pt.Y = p[1]
            ptGeoms.append(arcpy.PointGeometry(pt) )
            arcpy.CopyFeatures_management(ptGeoms, output )
        join_features = output
        target_features = BasePolygon
        out_feature_class = "D:\\Data\\AreaJoinPoints" + str(i) + '.shp'
        arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
        sc2 = arcpy.SearchCursor(out_feature_class)
        row2 = sc2.next()
        while row2:
            InsideIndex.append(row2.Join_Count) # 判斷是否在POLYGON裡面 有就是1
            row2 = sc2.next()
            
        i += 1
    j = 0
    
    ptList2 = []
    ptList3 = []
    while j <  len(InsideIndex):
        if InsideIndex[j] == 1:
            ptList2.append([ p2_x[j], p2_y[j] ])
        if InsideIndex[j] == 0: # random again
            if MaskMethod == 'first':
                ptList2.append([ p_x[j], p_y[j] ])


            
            if MaskMethod == 'second':
                             
                newIndex = 0
                while newIndex <1:
                    theta = random.randrange(0,361)
                    x_mask = mask* math.cos(theta*pi/180)
                    y_mask = mask* math.sin(theta*pi/180)
                    new_x = p_x[j] + x_mask
                    new_y = p_y[j] + y_mask
                    ptList3 = [[ new_x, new_y ]]   # create point shp
                    pt3 = arcpy.Point()
                    ptGeoms3 = []
                    for p in ptList3:
                        pt3.X = p[0]
                        pt3.Y = p[1]
                        ptGeoms3.append(arcpy.PointGeometry(pt3) )
                        arcpy.CopyFeatures_management(ptGeoms3, r"D:\\Data\\" + 'test' + str(j) + '.shp' )

                    join_features = r"D:\\Data\\" + 'test' + str(j) + '.shp'   # excute spatial join to check outside boundary
                    target_features = BasePolygon
                    out_feature_class = "D:\\Data\\AreaJoinPoints" + str(j) + '.shp'
                    arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
                    sc3 = arcpy.SearchCursor(out_feature_class)
                    row3 = sc3.next()
                    while row3:
                        newIndex += row3.Join_Count
                        row3 = sc3.next()


            
                ptList2.append([ new_x, new_y ])

        j += 1


    # ptList2 is ready
    pt2 = arcpy.Point()
    ptGeoms2 = []
    OutputName = Output
    for p in ptList2:
        pt2.X = p[0]
        pt2.Y = p[1]
        ptGeoms2.append(arcpy.PointGeometry(pt2))
    arcpy.CopyFeatures_management(ptGeoms2, OutputName)# OutputName = r"D:\\Data\\" + 'final' +  '.shp'
    


if __name__ == "__main__":
    setupMask()
    print 'Finish'



#InputPoint = "C:\Users\ww410490\Desktop\Python\GeogMask\TestPoint.shp"
# check point outside area
# area location
#target_features = "C:\Users\ww410490\Desktop\Python\GeogMask\TestArea.shp"



    




