Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data sources in MXD #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions mapactionpy_arcmap/arcmap_runner.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion mapactionpy_arcmap/layer_builder.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
4 changes: 4 additions & 0 deletions mapactionpy_arcmap/tests/fixture_datasource_dictionary.json
Original file line number Diff line number Diff line change
@@ -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"
}
36 changes: 20 additions & 16 deletions mapactionpy_arcmap/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,87 @@

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"
}
"""


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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
Expand All @@ -93,39 +97,39 @@
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
m: Main Map
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
-
Expand Down