import arcpy
arcpy.env.overwriteOutput = True

def setupReport():
    OriginalPoints = arcpy.GetParameterAsText(0)
    OriX = arcpy.GetParameterAsText(1)
    OriY = arcpy.GetParameterAsText(2)
    NewPoints = arcpy.GetParameterAsText(3)
    NewX = arcpy.GetParameterAsText(4)
    NewY = arcpy.GetParameterAsText(5)
    BasePolygon = arcpy.GetParameterAsText(6) # pop density polygon
    density = arcpy.GetParameterAsText(7)
    #Output = arcpy.GetParameterAsText(8)

    Report(OriginalPoints,OriX,OriY,NewPoints,NewX,NewY,BasePolygon,density)

def Report(OriginalPoints,OriX,OriY,NewPoints,NewX,NewY,BasePolygon,density):

    den_field = density
    target_features = BasePolygon
    #out_feature = Output

    sc = arcpy.SearchCursor(OriginalPoints)
    row = sc.next()
    p_x = []
    p_y = []
    orixcoor = OriX
    oriycoor = OriY
    while row:
        p_x.append(row.getValue(orixcoor))
        p_y.append(row.getValue(oriycoor))
        row = sc.next()







        
        
   

    # ¤wÅª¨ú original xy coor
    sc2 = arcpy.SearchCursor(NewPoints)
    row2 = sc2.next()
    p2_x = []
    p2_y = []
    newxcoor = NewX
    newycoor = NewY
    while row2:
        p2_x.append(row2.getValue(newxcoor))
        p2_y.append(row2.getValue(newycoor))
        row2 = sc2.next()

    # ¥HÅª¨ú new xy coor


    # create single point of Original and New


    i =0
    num = len(p_x)

    density = []
    density2 = []
    
    
    while i < num:
        output = r"C:\\geomask_data\\" + 'original' + str(i) + '.shp'
        output2 = r"C:\\geomask_data\\" + 'new' + str(i) + '.shp'
        
        ptList = [ [ p_x[i], p_y[i] ] ]
        ptList2 = [ [ p2_x[i], p2_y[i] ] ]
        
        pt = arcpy.Point()
        pt2 = arcpy.Point()
        
        ptGeoms = []
        ptGeoms2 = []
        

        pt.X = p_x[i]
        pt.Y = p_y[i]

        pt2.X = p2_x[i]
        pt2.Y = p2_y[i]
        
        ptGeoms.append(arcpy.PointGeometry(pt) )
        ptGeoms2.append(arcpy.PointGeometry(pt2) )

        
        arcpy.CopyFeatures_management(ptGeoms, output )
        arcpy.CopyFeatures_management(ptGeoms2, output2 )


        # # use select layer by location to search point - density match
        arcpy.MakeFeatureLayer_management(output,'outputlyr')
        arcpy.SelectLayerByLocation_management(target_features,"CONTAINS",'outputlyr')
        sc = arcpy.SearchCursor(target_features)
        row = sc.next()
        den = row.getValue(den_field)
        density.append(den)


        arcpy.MakeFeatureLayer_management(output2,'outputlyr2')
        arcpy.SelectLayerByLocation_management(target_features,"CONTAINS",'outputlyr2')
        sc2 = arcpy.SearchCursor(target_features)
        row2 = sc2.next()
        den2 = row2.getValue(den_field)
        density2.append(den2)
        

        

        i += 1

    # now we have p_x, p_y, p2_x, p2_y, density, density2
    # store them in a shp file

    
    arcpy.AddField_management( NewPoints, "distance","DOUBLE" )
    arcpy.AddField_management( NewPoints, "ori_den","DOUBLE" )
    arcpy.AddField_management( NewPoints, "new_den","DOUBLE" )
    uc = arcpy.UpdateCursor(NewPoints)
    row = uc.next()
    a = 0
    while a < len(p_x):
        row.distance = ( ( p_x[a] - p2_x[a] )**2  +  ( p_y[a] - p2_y[a] )**2 )**0.5
        row.ori_den = density[a]
        row.new_den = density2[a]
        uc.updateRow(row)
        row = uc.next()
        a += 1

    #arcpy.AverageNearestNeighbor_stats( NewPoints,"EUCLIDEAN_DISTANCE","GENERATE_REPORT" )

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

    

    
    
