diff --git a/mapactionpy_arcmap/arcmap_runner.py b/mapactionpy_arcmap/arcmap_runner.py index f99d0eb..8bdaaf4 100644 --- a/mapactionpy_arcmap/arcmap_runner.py +++ b/mapactionpy_arcmap/arcmap_runner.py @@ -1,27 +1,73 @@ +import arcpy +import argparse +import json +from shutil import copyfile +from layer_builder import LayerBuilder +from mapframe_builder import MapFrameBuilder -def main(args): - pass + +def update_map(self, mxd, datasources_dict): + lb = LayerBuilder(arcpy.mapping.ListLayers(mxd)) + lb.set_datasource() + + # mfb = MapFrameBuilder() + # mfb.do_stuff() + # etc... + # + # end with this + mxd.save() + + +def main(self, args): + print (args) + try: + # open mxd + copyfile(args.input_mxd, args.output_mxd) + mxd = arcpy.mapping.MapDocument(args.output_mxd) + + # depending on opions open datasource dictionary + if args.datasource_dictionary == '-': + # standard in + datasources_dict = json.load(sys.stdin) + elif args.datasource_dictionary[0] == '@': + # open file (removing the `@`` symbol from the path) + with open(args.datasource_dictionary[1:]) as f: + datasources_dict = json.load(f) + else: + # assume that input is the json as a string + datasources_dict = json.loads + + update_map(mxd, datasources_dict) + + except IOError: + print "error" + finally: + print "tidying up" if __name__ == '__main__': parser = argparse.ArgumentParser( description='This component accepts a template MXD file, a list of the' 'relevant datasets along with other information required to create an' - 'event specific instance of a map.', - formatter_class=SmartFormatter + 'event specific instance of a map.' ) parser.add_argument( - 'output_mxd_path' + 'output_mxd', help='The absolute path where the output mxd file should be written' ) # positional, rather than option. parser.add_argument( - '-i', '--input-mxd-path', + '-i', '--input-mxd', help='The absolute path to the template MXD to be used as the basis' 'for new products' ) parser.add_argument( - '-d', '--dataset-dictionary', - help="A dictionary of key value pairs in json format" + '-d', '--datasource-dictionary', + help="A dictionary of key value pairs in json format\n" + " can be passed as a string directly\n" + " If prefixed with an `@` symbol a fully qualified path of a file" + " containing the datasource dictionary should be provided.\n" + " If a '-' character is passed, then the datasource-dictionary is" + " read from stdin, assuming a json structure" ) args = parser.parse_args() main(args) diff --git a/mapactionpy_arcmap/layer_builder.py b/mapactionpy_arcmap/layer_builder.py index 75b63e7..e424bb6 100644 --- a/mapactionpy_arcmap/layer_builder.py +++ b/mapactionpy_arcmap/layer_builder.py @@ -1,9 +1,11 @@ class LayerBuilder(): + def __init__(self, layers): + self.layers = layers def set_layer_title(): pass - def set_datasource(): + def set_datasource(datasource_dict): pass def set_symbology(): diff --git a/mapactionpy_arcmap/tests/fixture_datasource_dictionary.json b/mapactionpy_arcmap/tests/fixture_datasource_dictionary.json new file mode 100644 index 0000000..14c4569 --- /dev/null +++ b/mapactionpy_arcmap/tests/fixture_datasource_dictionary.json @@ -0,0 +1,4 @@ +{ + "settlement_points": "D:\\MapAction\\2019-06-12-GBR\\GIS\\2_Active_Data\\gbr_stle_stle_pt_s0_naturalearth_pp.shp", + "airports_points": "D:\\MapAction\\2019-06-12-GBR\\GIS\\2_Active_Data\\232_tran\\scr_tran_air_pt_s1_ourairports_pp.shp" +} \ No newline at end of file diff --git a/mapactionpy_arcmap/tests/fixtures.py b/mapactionpy_arcmap/tests/fixtures.py index 58f152d..d341317 100644 --- a/mapactionpy_arcmap/tests/fixtures.py +++ b/mapactionpy_arcmap/tests/fixtures.py @@ -1,5 +1,5 @@ -fixture_datasource_dictionary_ma001 = """ +fixture_datasource_dictionary_ma001 = r""" { "settlement_points": "D:\MapAction\2019-06-12-GBR\GIS\2_Active_Data\gbr_stle_stle_pt_s0_naturalearth_pp.shp" "airports_points": "D:\MapAction\2019-06-12-GBR\GIS\2_Active_Data\232_tran\scr_tran_air_pt_s1_ourairports_pp.shp" @@ -7,13 +7,17 @@ """ -fixture_layer_description_ma001 = """ +# "D:\code\github\mapactionpy_arcmap\mapactionpy_arcmap\tests\test_data\slv_stle_ste_pt_s0_osm_pp_places.shp" + + + +fixture_layer_description_ma001 = r""" - m: Main Map layer-group: None layer-name: Settlement - Places - pt source-folder: 229_stle - regex: ^ XXX_stle_stl_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_stle_stl_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: "SettleType" IN('national_capital', 'city') visable: Yes - @@ -21,7 +25,7 @@ layer-group: Transport - Points layer-name: Transport - Airports - pt source-folder: 232_tran - regex: ^ XXX_trans_air_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_trans_air_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -29,7 +33,7 @@ layer-group: Transport - Points layer-name: Transport - Seaports - pt source-folder: 232_tran - regex: ^ XXX_trans_por_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_trans_por_pt_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -37,7 +41,7 @@ layer-group: Admin - Lines layer-name: Elevation - Coastline - ln source-folder: 211_elev - regex: ^ XXX_elev_cst_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_elev_cst_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: none visable: Yes - @@ -45,7 +49,7 @@ layer-group: Admin - Lines layer-name: Borders - Admin1 - ln source-folder: 202_admn - regex: ^ XXX_admn_ad1_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_admn_ad1_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -53,7 +57,7 @@ layer-group: Admin - Lines layer-name: Borders - Admin2 - ln source-folder: 202_admn - regex: ^ XXX_admn_ad2_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_admn_ad2_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -61,7 +65,7 @@ layer-group: Transport - Lines layer-name: Transport - Rail - ln source-folder: 232_tran - regex: ^ XXX_tran_rrd_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_tran_rrd_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -69,7 +73,7 @@ layer-group: Transport - Lines layer-name: Transport - Road - ln source-folder: 232_tran - regex: ^ XXX_tran_rds_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_tran_rds_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -77,7 +81,7 @@ layer-group: None layer-name: Cartography - Feather - pt source-folder: 207_carto - regex: ^ (?!(XXX))_carto_fea_py_s0_mapaction_pp(_(.+) + regex: ^(?!(XXX))_carto_fea_py_s0_mapaction_pp(_(.+) query-definition: None visable: Yes - @@ -93,7 +97,7 @@ layer-group: Physical layer-name: Physical - River - ln source-folder: 221_phys - regex: ^ XXX_phys_riv_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_phys_riv_ln_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -101,7 +105,7 @@ layer-group: Admin - Polygons layer-name: Admin - Admin2 - py source-folder: 202_admn - regex: ^ XXX_admn_ad2_py_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_admn_ad2_py_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -109,7 +113,7 @@ layer-group: Admin - Polygons layer-name: Admin - Admin1 - py source-folder: 202_admn - regex: ^ XXX_admn_ad1_py_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_admn_ad1_py_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -117,7 +121,7 @@ layer-group: Admin - Polygons layer-name: Admin - AffectedCountry - py source-folder: 202_admn - regex: ^ XXX_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^XXX_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: None visable: Yes - @@ -125,7 +129,7 @@ layer-group: Admin - Polygons layer-name: Admin - SurroundingCountry - py source-folder: 202_admn - regex: ^ (?!(XXX))_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(_(.+)) + regex: ^(?!(XXX))_admn_ad0_py_(.*?)_(.*?)_([phm][phm])(_(.+)) query-definition: ADM0_NAME <> '[reference country]' visable: Yes -