From 5a6e767a11a1d6b98e70f7034aca933bb645763a Mon Sep 17 00:00:00 2001 From: Andy Smith Date: Tue, 2 Jul 2019 19:01:48 +0100 Subject: [PATCH 1/2] basic aurgument handling --- mapactionpy_arcmap/arcmap_runner.py | 62 ++++++++++++++++--- mapactionpy_arcmap/layer_builder.py | 4 +- .../tests/fixture_datasource_dictionary.json | 4 ++ mapactionpy_arcmap/tests/fixtures.py | 4 ++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 mapactionpy_arcmap/tests/fixture_datasource_dictionary.json 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..07dd665 100644 --- a/mapactionpy_arcmap/tests/fixtures.py +++ b/mapactionpy_arcmap/tests/fixtures.py @@ -7,6 +7,10 @@ """ +# "D:\code\github\mapactionpy_arcmap\mapactionpy_arcmap\tests\test_data\slv_stle_ste_pt_s0_osm_pp_places.shp" + + + fixture_layer_description_ma001 = """ - m: Main Map From 24dc11bd0b29896a5a6779ad82a87667dbe23f02 Mon Sep 17 00:00:00 2001 From: Andy Smith Date: Fri, 5 Jul 2019 09:50:37 +0100 Subject: [PATCH 2/2] raw strings in fixtures --- mapactionpy_arcmap/tests/fixtures.py | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mapactionpy_arcmap/tests/fixtures.py b/mapactionpy_arcmap/tests/fixtures.py index 07dd665..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" @@ -11,13 +11,13 @@ -fixture_layer_description_ma001 = """ +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 - @@ -25,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 - @@ -33,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 - @@ -41,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 - @@ -49,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 - @@ -57,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 - @@ -65,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 - @@ -73,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 - @@ -81,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 - @@ -97,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 - @@ -105,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 - @@ -113,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 - @@ -121,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 - @@ -129,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 -