#!/usr/bin/env python
# Filename: polygons_cd 
"""
introduction: compare two polygons in to shape file

authors: Huang Lingcao
email:huanglingcao@gmail.com
add time: 26 February, 2020
"""

import sys,os
from optparse import OptionParser

# added path of DeeplabforRS
sys.path.insert(0, os.path.expanduser('~/codes/PycharmProjects/DeeplabforRS'))
import basic_src.io_function as io_function
import basic_src.basic as basic
import basic_src.map_projection as map_projection
import parameters

import polygons_cd_multi
import polygons_cd

def main(options, args):

    old_shp_path = args[0]
    new_shp_path = args[1]

    # check files do exist
    assert io_function.is_file_exist(new_shp_path)
    assert io_function.is_file_exist(old_shp_path)

    # check projection of the shape file, should be the same
    old_shp_proj4 = map_projection.get_raster_or_vector_srs_info_proj4(old_shp_path)
    new_shp_proj4 = map_projection.get_raster_or_vector_srs_info_proj4(new_shp_path)

    if old_shp_proj4 != new_shp_proj4:
        raise ValueError('error, projection insistence between %s and %s' % (old_shp_proj4, new_shp_proj4))

    main_shp_name = polygons_cd_multi.get_main_shp_name(old_shp_path,new_shp_path)

    # conduct change detection
    if options.output is not None:
        main_shp_name = options.output

    # get expanding and shrinking parts
    output_path_expand = 'expand_' + main_shp_name
    output_path_shrink = 'shrink_' + main_shp_name
    polygons_cd.polygons_change_detection(old_shp_path, new_shp_path, output_path_expand,output_path_shrink)



if __name__ == "__main__":
    usage = "usage: %prog [options] old_shape_file new_shape_file "
    parser = OptionParser(usage=usage, version="1.0 2020-02-26")
    parser.description = 'Introduction: compare two groups of polygons '

    parser.add_option("-p", "--para",
                      action="store", dest="para_file",
                      help="the parameters file")

    parser.add_option('-o', '--output',
                      action="store", dest = 'output',
                      help='the path to save the change detection results')

    (options, args) = parser.parse_args()
    if len(sys.argv) < 2:
        parser.print_help()
        sys.exit(2)

    # # set parameters files
    # if options.para_file is None:
    #     print('error, no parameters file')
    #     parser.print_help()
    #     sys.exit(2)
    # else:
    #     parameters.set_saved_parafile_path(options.para_file)

    basic.setlogfile('polygons_changeDetection.log')

    main(options, args)




[((15, 19, 15, 77), 'os.path.expanduser', 'os.path.expanduser', ({(15, 38, 15, 76): '"""~/codes/PycharmProjects/DeeplabforRS"""'}, {}), "('~/codes/PycharmProjects/DeeplabforRS')", False, 'import sys, os\n'), ((30, 11, 30, 50), 'basic_src.io_function.is_file_exist', 'io_function.is_file_exist', ({(30, 37, 30, 49): 'new_shp_path'}, {}), '(new_shp_path)', True, 'import basic_src.io_function as io_function\n'), ((31, 11, 31, 50), 'basic_src.io_function.is_file_exist', 'io_function.is_file_exist', ({(31, 37, 31, 49): 'old_shp_path'}, {}), '(old_shp_path)', True, 'import basic_src.io_function as io_function\n'), ((34, 20, 34, 84), 'basic_src.map_projection.get_raster_or_vector_srs_info_proj4', 'map_projection.get_raster_or_vector_srs_info_proj4', ({(34, 71, 34, 83): 'old_shp_path'}, {}), '(old_shp_path)', True, 'import basic_src.map_projection as map_projection\n'), ((35, 20, 35, 84), 'basic_src.map_projection.get_raster_or_vector_srs_info_proj4', 'map_projection.get_raster_or_vector_srs_info_proj4', ({(35, 71, 35, 83): 'new_shp_path'}, {}), '(new_shp_path)', True, 'import basic_src.map_projection as map_projection\n'), ((40, 20, 40, 82), 'polygons_cd_multi.get_main_shp_name', 'polygons_cd_multi.get_main_shp_name', ({(40, 56, 40, 68): 'old_shp_path', (40, 69, 40, 81): 'new_shp_path'}, {}), '(old_shp_path, new_shp_path)', False, 'import polygons_cd_multi\n'), ((49, 4, 49, 108), 'polygons_cd.polygons_change_detection', 'polygons_cd.polygons_change_detection', ({(49, 42, 49, 54): 'old_shp_path', (49, 56, 49, 68): 'new_shp_path', (49, 70, 49, 88): 'output_path_expand', (49, 89, 49, 107): 'output_path_shrink'}, {}), '(old_shp_path, new_shp_path,\n    output_path_expand, output_path_shrink)', False, 'import polygons_cd\n'), ((55, 13, 55, 64), 'optparse.OptionParser', 'OptionParser', (), '', False, 'from optparse import OptionParser\n'), ((79, 4, 79, 52), 'basic_src.basic.setlogfile', 'basic.setlogfile', ({(79, 21, 79, 51): '"""polygons_changeDetection.log"""'}, {}), "('polygons_changeDetection.log')", True, 'import basic_src.basic as basic\n'), ((69, 8, 69, 19), 'sys.exit', 'sys.exit', ({(69, 17, 69, 18): '(2)'}, {}), '(2)', False, 'import sys, os\n')]