
import arcpy
import random
import math
import os
import shutil

newpath = r"C:\\geomask_data\\"
if not os.path.exists(newpath): os.makedirs(newpath)


arcpy.env.overwriteOutput = True




def setupMask():
    OriginalPoint = arcpy.GetParameterAsText(0)
    #xcoor = arcpy.GetParameterAsText(1)  # set to field
    #ycoor = arcpy.GetParameterAsText(2)  # set to field
    BasePolygon = arcpy.GetParameterAsText(1)
    LakePolygon = arcpy.GetParameterAsText(2)
    x_shifting = arcpy.GetParameter(3)
    y_shifting = arcpy.GetParameter(4)
    OutBoundAffineR = arcpy.GetParameter(5)
                 
    Output = arcpy.GetParameterAsText(6)
    
    #MaskingMethod = ARCPY.GetParameterAsText(2).upper().replace(" ", "_")


    
    Mask(OriginalPoint, BasePolygon,LakePolygon,  x_shifting, y_shifting,OutBoundAffineR, Output)



def Mask(OriginalPoint, BasePolygon,LakePolygon,  x_shifting, y_shifting,OutBoundAffineR, Output):
    InputPoint = OriginalPoint
    
    sc = arcpy.SearchCursor(InputPoint)
    row = sc.next()
    p_x = []
    p_y = []
    
    while row:
        p_x.append(row.POINT_X)
        p_y.append(row.POINT_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()

    x_mask = x_shifting
    y_mask = y_shifting
    
    while row:
        p2_x.append(row.POINT_X + x_mask)
        p2_y.append(row.POINT_Y + y_mask)
        row = sc.next()

        
    i =0
    num = len(p2_x)
    InsideIndex = []
    lakeindex = []
    

    
    while i < num:
        output = r"C:\\geomask_data\\" + 'test' + str(i) + '.shp'
        ptList = [ [ p2_x[i], p2_y[i] ] ]
        pt = arcpy.Point()
        ptGeoms = []

        pt.X = p2_x[i]
        pt.Y = p2_y[i]
        ptGeoms.append(arcpy.PointGeometry(pt) )
        arcpy.CopyFeatures_management(ptGeoms, output )
        
        

        join_features = output
        target_features = BasePolygon

        arcpy.MakeFeatureLayer_management(join_features,'outputlyr')


        arcpy.SelectLayerByLocation_management('outputlyr',"COMPLETELY_WITHIN",target_features)
        count1 = int( arcpy.GetCount_management( 'outputlyr' ).getOutput(0) )
        InsideIndex.append(count1)
        




        target_features2 = LakePolygon  ##之後要改成參數

        
        arcpy.SelectLayerByLocation_management('outputlyr',"COMPLETELY_WITHIN",target_features2)
        count2 = int( arcpy.GetCount_management( 'outputlyr' ).getOutput(0) )
        lakeindex.append(count2)


        arcpy.DeleteFeatures_management( 'outputlyr' )
        


        
            
        i += 1
    j = 0
    
    ptList2 = []
    ptList3 = []

    pt2 = arcpy.Point()
    ptGeoms2 = []
    OutputName = Output
    
    while j <  len(InsideIndex):
        if InsideIndex[j] == 1 and lakeindex[j] == 0:
            ptList2.append([ p2_x[j], p2_y[j] ])

            pt2.X = p2_x[j]
            pt2.Y = p2_y[j]
            ptGeoms2.append(arcpy.PointGeometry(pt2))


        if lakeindex[j] == 1: #random affine again
            newIndex = 0
            turns = 0
            while newIndex <1:
                theta = random.randrange(0,361)
                radius = OutBoundAffineR  # 之後要改成參數
                x_mask = radius* math.cos(theta*pi/180)
                y_mask = radius* 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 = []
                pt3.X = new_x
                pt3.Y = new_y
                ptGeoms3.append(arcpy.PointGeometry(pt3) )
                arcpy.CopyFeatures_management(ptGeoms3, r"C:\\geomask_data\\" + 'test' + str(j) + '.shp' )

                
                join_features = r"C:\\geomask_data\\" + 'test' + str(j) + '.shp'   # excute spatial join to check outside boundary

                arcpy.MakeFeatureLayer_management(join_features,'outputlyr2')
                
                target_features = BasePolygon
                arcpy.SelectLayerByLocation_management('outputlyr2',"COMPLETELY_WITHIN",target_features)
                count3 = int( arcpy.GetCount_management( 'outputlyr2' ).getOutput(0) )
                newIndex += count3
                

                target_features2 = LakePolygon
                arcpy.SelectLayerByLocation_management('outputlyr2',"COMPLETELY_WITHIN",target_features2)
                count4 = int( arcpy.GetCount_management( 'outputlyr2' ).getOutput(0) )
                newIndex -= count4

                arcpy.DeleteFeatures_management( 'outputlyr2' )
                



                turns += 1
                if turns == 10:
                    there_is_something_wrong_within_lakepolygon
            ptList2.append([ new_x, new_y ])
            pt2.X = new_x
            pt2.Y = new_y
            ptGeoms2.append(arcpy.PointGeometry(pt2))
                

              
            
        if InsideIndex[j] == 0: # random affine again

            newIndex = 0
            while newIndex <1:
                theta = random.randrange(0,361)
                radius = OutBoundAffineR  # 之後要改成參數
                x_mask = radius* math.cos(theta*pi/180)
                y_mask = radius* 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 = []

                pt3.X = new_x
                pt3.Y = new_y
                ptGeoms3.append(arcpy.PointGeometry(pt3) )
                arcpy.CopyFeatures_management(ptGeoms3, r"C:\\geomask_data\\" + 'test' + str(j) + '.shp' )

                join_features = r"C:\\geomask_data\\" + 'test' + str(j) + '.shp'   # excute spatial join to check outside boundary

                arcpy.MakeFeatureLayer_management(join_features,'outputlyr3')
                target_features = BasePolygon
                arcpy.SelectLayerByLocation_management('outputlyr3',"COMPLETELY_WITHIN",target_features)
                count5 = int( arcpy.GetCount_management( 'outputlyr3' ).getOutput(0) )
                newIndex += count5



                target_features2 = LakePolygon
                arcpy.SelectLayerByLocation_management('outputlyr3',"COMPLETELY_WITHIN",target_features2)
                count6 = int( arcpy.GetCount_management( 'outputlyr3' ).getOutput(0) )
                newIndex -= count6


                arcpy.DeleteFeatures_management( 'outputlyr3' )
    
            
            ptList2.append([ new_x, new_y ])
            
            pt2.X = new_x
            pt2.Y = new_y
            ptGeoms2.append(arcpy.PointGeometry(pt2))
            

        j += 1
    arcpy.CopyFeatures_management(ptGeoms2, OutputName)# OutputName = r"D:\\Data\\" + 'final' +  '.shp'



 


if __name__ == "__main__":
    setupMask()
    print 'Finish'
    #shutil.rmtree(newpath) ## can not delete with LOCKED files



#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"



    




