def main(argv):
  args = argv[1:]
  global VERBOSE
  i = 0
  while i < (len(args) - 1):
    if args[i] == "-v":
      VERBOSE=True
      del(args[i])
    else:
      i = i + 1
  if len(args) == 0 or len(args) > 2 or any([arg.startswith("-") for arg in args]):
      command_line_error("wrong argument", HELP_MESSAGE)
  else:
    input_filename = args[0]
    if len(args) > 1:
      output_filename = args[1]
    else:
      name,ext = os.path.splitext(input_filename)
      output_filename = name + "-prediction_segmente" + ext

    if VERBOSE: print "load " + input_filename + " ..."

    osm = OsmParser().parse(input_filename)
    #simplify(osm, 0.2, 0.2, 0.1)

    if VERBOSE: print "transform..."
    inputTransform, outputTransform = get_centered_metric_equirectangular_transformation_from_osm(osm)
    compute_transformed_position_and_annotate(osm, inputTransform)

    if VERBOSE: print "detect..."
    classifier, scaler = load_classifier_and_scaler()
    buildings = get_predicted_segmented_buildings(classifier, scaler, osm)
    if VERBOSE: print " -> ", len(buildings), "cas"

    output_osm = filter_buildings_junction(osm, buildings)
    
    if len(output_osm.nodes) > 0:
        if VERBOSE: print "save ", output_filename 
        OsmWriter(output_osm).write_to_file(output_filename)
    else:
        print "Nothing detected"

    return 0
def main(argv):
    osm_args = [f for f in argv[1:] if os.path.splitext(f)[1] in (".zip", ".osm")]
    other_args = [f for f in argv[1:] if os.path.splitext(f)[1] not in (".zip", ".osm")]
    if len(other_args) != 0:
        command_line_error("invalid argument: " + str(other_args[0]), HELP_MESSAGE)
    if len(osm_args) == 0:
        command_line_error(u"not enough file.osm args", HELP_MESSAGE)

    all_data = []
    all_result = []

    for name, stream in open_zip_and_files_with_extension(osm_args, ".osm"):
        print("load " + name)
        osm = OsmParser().parse_stream(stream)
        inputTransform, outputTransform = get_centered_metric_equirectangular_transformation_from_osm(osm)
        compute_transformed_position_and_annotate(osm, inputTransform)

        data, result = get_segmented_buildings_data(osm)

        print(" -> {0} cas, dont {1} positifs".format(len(result), result.count(1)))
        all_data.extend(data)
        all_result.extend(result)

    scaler, classifier = train(all_data, all_result)
    ext = pickle_extension()
    with open("classifier" + ext, "wb") as f:
        pickle.dump(classifier, f)
    with open("scaler" + ext, "wb") as f:
        pickle.dump(scaler, f)

    #for positive_weight in 1,2,5:
    #    print "-------------------------------------------------------------"
    #    print "Train with scoring for positive segemented building weighted %d over non segmented ones" % positive_weight
    #    scaler, classifier = train(all_data, all_result, weighted_scoring([1,positive_weight]))
    #    with open("classifier_%d.txt" % positive_weight, "w") as f:
    #        f.write(str(classifier))
    #        f.write("\n")
    #    with open("classifier_%d.pickle" % positive_weight, "w") as f:
    #        pickle.dump(classifier, f)
    #    with open("scaler_%d.pickle" % positive_weight, "w") as f:
    #        pickle.dump(scaler, f)

    return 0
def main(argv):
    global VERBOSE
    VERBOSE = True
    osm_args = [f for f in argv[1:] if os.path.splitext(f)[1] in (".zip", ".osm")]
    other_args = [f for f in argv[1:] if os.path.splitext(f)[1] not in (".zip", ".osm")]
    if len(other_args) != 0:
        command_line_error("invalid argument: " + other_args[0], HELP_MESSAGE)
    if len(osm_args) == 0:
        command_line_error("not enough file.osm args", HELP_MESSAGE)

    classifier, scaler = load_classifier_and_scaler()

    score = 0

    for name, stream in open_zip_and_files_with_extension(osm_args, ".osm"):
        if VERBOSE: print "load " + name
        input_osm = OsmParser().parse_stream(stream)
        inputTransform, outputTransform = get_centered_metric_equirectangular_transformation_from_osm(input_osm)
        compute_transformed_position_and_annotate(input_osm, inputTransform)

        nb_ok, nb_missed, nb_false, missed_osm, false_osm = test_classifier(classifier, scaler, input_osm)

        if VERBOSE: print nb_ok, "correctly found"

        if len(missed_osm.ways) or nb_missed:
            missed_name = os.path.splitext(name)[0] + "-missed.osm"
            if VERBOSE: print nb_missed, " missed detections, write file", missed_name
            OsmWriter(missed_osm).write_to_file(missed_name)
        if len(false_osm.ways) or nb_false:
            false_name = os.path.splitext(name)[0] + "-false.osm"
            if VERBOSE: print nb_false, " false positives, write file", false_name
            OsmWriter(false_osm).write_to_file(false_name)
        score  += nb_ok * 2 - nb_missed - nb_false*10

    if VERBOSE: print "TOTAL SCORE (ok*3 - missed - false*10):"
    print score
    return 0