

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

#featureList = []
#polygon = arcpy.Polygon()
#featureList.append(polygon)
#arcpy.CopyFeatures_management(featureList, "C:\\geomask_data\\polygons.shp")



def setupMask():
    OriginalPoint = arcpy.GetParameterAsText(0)
    xcoor = arcpy.GetParameterAsText(1)
    ycoor = arcpy.GetParameterAsText(2)
    
    BasePolygon = arcpy.GetParameterAsText(3)
    #LakePolygon = arcpy.GetParameterAsText(4)
    inner_ring = arcpy.GetParameter(4)  # integer
    outer_ring = arcpy.GetParameter(5)  # integer
    
    Output = arcpy.GetParameterAsText(6)
    #MaskingMethod = ARCPY.GetParameterAsText(2).upper().replace(" ", "_")


    
    Mask(OriginalPoint, xcoor, ycoor, BasePolygon, inner_ring,outer_ring, Output)



def Mask(OriginalPoint, xcoor, ycoor, BasePolygon, inner_ring,outer_ring, Output):
    InputPoint = OriginalPoint
    
    sc = arcpy.SearchCursor(InputPoint)
    row = sc.next()
    p_x = []
    p_y = []

    x_field = xcoor
    y_field = ycoor
    while row:
        p_x.append(row.getValue(x_field))
        p_y.append(row.getValue(y_field))
        row = sc.next()

        
    
    
    pi = math.pi
 
    
    p2_x = []
    p2_y = []
    sc = arcpy.SearchCursor(InputPoint)
    row = sc.next()

    inner = inner_ring
    outer = outer_ring + 1
    
    while row:
        

        theta = random.randrange(0,361)
        radius = random.randrange(inner, outer)
        x_mask = radius* math.cos(theta*pi/180)
        y_mask = radius* math.sin(theta*pi/180)
            
        p2_x.append(row.getValue(x_field) + x_mask)
        p2_y.append(row.getValue(y_field) + y_mask)
            
        
        row = sc.next()

        
    i =0
    num = len(p2_x)
    InsideIndex = []
    lakeindex = []
    MergeInput = []
    while i < num:
        output = r"C:\\geomask_data\\" + 'test' + str(i) + '.shp'
        MergeInput.append(output)
        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)

        

        arcpy.DeleteFeatures_management( 'outputlyr' )
        

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

    pt2 = arcpy.Point()
    ptGeoms2 = []
    OutputName = Output

    
    while j <  len(InsideIndex):
        if InsideIndex[j] == 1 :
            ptList2.append([ p2_x[j], p2_y[j] ])

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

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

            

            newIndex = 0
            while newIndex <1:
                theta = random.randrange(0,361)
                radius = random.randrange(inner, outer)
                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

                

                
                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'
    
    #arcpy.Merge_management( MergeInput, OutputName )





if __name__ == "__main__":
    setupMask()
    print 'Finish'
    #shutil.rmtree(newpath)



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



    




