Improve my Python Code - Mobile Map Package

1009
8
02-01-2021 06:54 AM
higgy7
by
New Contributor III

Hi,

I am relatively new to Python. I have created a script to create 8 Mobile Map Packages, 1 for each area required. I was just wondering if there is a better or more efficient way to write the code? Possibly looping through each area.Each one uses the same Map File but clips each based off a different layer file. I have the script scheduled to run weekly yo update each with the latest data. Any help, tips or advice would be greatly appeciated.

 

Thanks 

import arcpy
arcpy.env.overwriteOutput = True

# Local Input Variables:
inputmapfile = r"'\\filelocation\Water Map Files\Water Network.mapx'"
inputn1layerfile = r"\\filelocation\Water Layer Files\North_1.lyrx"
inputn2layerfile = r"\\filelocation\Water Layer Files\North_2.lyrx"
inputs1layerfile = r"\\filelocation\Water Layer Files\South_1.lyrx"
inputs2layerfile = r"\\filelocation\Water Layer Files\South_2.lyrx"
inpute1layerfile = r"\\filelocation\Water Layer Files\East_1.lyrx"
inpute2layerfile = r"\\filelocation\Water Layer Files\East_2.lyrx"
inputw1layerfile = r"\\filelocation\Water Layer Files\West_1.lyrx"
inputw2layerfile = r"\\filelocation\Water Layer Files\West_2.lyrx"

# Local Output Variables
north1outputfile =r"\\filelocation\Mobile Map Packages\North 1 Water Network.mmpk"
north2outputfile =r"\\filelocation\Mobile Map Packages\North 2 Water Network.mmpk"
south1outputfile =r"\\filelocation\\Mobile Map Packages\South 1 Water Network.mmpk"
south2outputfile =r"\\filelocation\\Mobile Map Packages\South 2 Water Network.mmpk"
east1outputfile =r"\\filelocation\\Mobile Map Packages\East 1 Water Network.mmpk"
east2outputfile =r"\\filelocation\\Mobile Map Packages\East 2 Water Network.mmpk"
west1outputfile =r"\\filelocation\\Mobile Map Packages\West 1 Water Network.mmpk"
west2outputfile =r"\\filelocation\\Mobile Map Packages\West 2 Water Network.mmpk"

# Process: Create Water Packages
arcpy.CreateMobileMapPackage_management(inputmapfile, north1outputfile, in_locator="", area_of_interest= inputn1layerfile, extent="DEFAULT", clip_features="CLIP", title="North 1 Water Network", summary="Mobile Map Package of the North 1 Water Network", description="", tags="Water Network, North 1", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, north2outputfile, in_locator="", area_of_interest= inputn2layerfile, extent="DEFAULT", clip_features="CLIP", title="North 2 Water Network", summary="Mobile Map Package of the North 2 Water Network", description="", tags="Water Network, North 2", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, south1outputfile, in_locator="", area_of_interest= inputs1layerfile, extent="DEFAULT", clip_features="CLIP", title="South 1 Water Network", summary="Mobile Map Package of the South 1 Water Network", description="", tags="Water Network, South 1", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, south2outputfile, in_locator="", area_of_interest= inputs2layerfile, extent="DEFAULT", clip_features="CLIP", title="South 2 Water Network", summary="Mobile Map Package of the South 2 Water Network", description="", tags="Water Network, South 2", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, east1outputfile, in_locator="", area_of_interest= inpute1layerfile, extent="DEFAULT", clip_features="CLIP", title="East 1 Water Network", summary="Mobile Map Package of the East 1 Water Network", description="", tags="Water Network, East 1", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, east2outputfile, in_locator="", area_of_interest= inpute2layerfile, extent="DEFAULT", clip_features="CLIP", title="East 2 Water Network", summary="Mobile Map Package of the East 2 Water Network", description="", tags="Water Network, East 2", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, west1outputfile, in_locator="", area_of_interest= inputw1layerfile, extent="DEFAULT", clip_features="CLIP", title="West 1 Water Network", summary="Mobile Map Package of the West 1 Water Network", description="", tags="Water Network, West 1", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

arcpy.CreateMobileMapPackage_management(inputmapfile, west2outputfile, in_locator="", area_of_interest= inputw2layerfile, extent="DEFAULT", clip_features="CLIP", title="West 2 Water Network", summary="Mobile Map Package of the West 2 Water Network", description="", tags="Water Network, West 2", credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION", map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

 

0 Kudos
8 Replies
by Anonymous User
Not applicable

Take a look at the book called Clean Code.  Not to be mean, but you will learn more if you go through your code yourself, and apply the principles of refractoring.  As a quick code review, here are some tips to clean up your code:

import os
import arcpy

arcpy.env.overwriteOutput = True

# set the base path to a variable so you only have to change it once if it changes
filelocwater = r'filelocation\Water Map Files'

# Local Input Variables:
inputmapfile = os.path.join(filelocwater, 'Water Network.mapx')
# do this for each lryx you have

# Local Output Variables
filelocMMPK = r'filelocation\Mobile Map Packages'
north1outputfile = os.path.join(filelocwater, r'North 1 Water Network.mmpk')
# do this for each mmpk

# Process: Create Water Packages
# create tuples of in and respective out...
ExportList = [(inputn1layerfile, north1outputfile), (inputn2layerfile, north2outputfile) ...] # do this for each pair

# iterate over list of tuples you just created using their index for the parameters in the function.

for i in ExportList:
    title = i[1].split('\\')[-1].split('.')[0]  
    # gets North 2 Water Network  !! can probably use arcpy.Describe(i[1]) as well
    tag = '{} {}, {} {}'.format(title.split(' ')[2], title.split(' ')[3], title.split(' ')[0], title.split(' ')[1])

    arcpy.CreateMobileMapPackage_management(i[0], i[1], in_locator="", area_of_interest=i[0], extent="DEFAULT", clip_features="CLIP", title=title, summary="Mobile Map Package of the {}".format(title), description="", tags=tag, credits="", use_limitations="", anonymous_use="STANDARD", enable_map_expiration="DISABLE_MAP_EXPIRATION",
map_expiration_type="ALLOW_TO_OPEN", expiration_date="", expiration_message="This map is expired.  Contact the map publisher for an updated map.")

 

higgy7
by
New Contributor III

Hi Jeff much appreciated. My problem with python is that in my job I only get to use it now and again, I pick things up and then forget it as there are long breaks in between. I will have to check out the book you suggested. I did find your use of tuples more complicated in my head but that's just because i have only heard of them and never used them. Thanks again

0 Kudos
by Anonymous User
Not applicable

No problem!  I didn't go as deep into the paths as @BlakeTerhune  did, and for your script I think his refractoring is pretty elegant.  We have a lot of fcins and fcouts that have unique names that can't be dynamically created so we use the tuple method (fcin, fcout) to set the input and output for use within the loop.  

for i in ExportList:

by doing

for infc, outfc in ExportList:

 

then in the loop code instead of indexing the i with i[0], i[1], use infc and outfc.  But yours is essentially the same names in and and out; Blake's is a better solution to iterate over the names like he did.

BlakeTerhune
MVP Regular Contributor

We're in the same boat; lots of fcin, fcout that can't be dynamically renamed. To add to your menu of options, I'll mention that we use dictionaries instead of tuple pairs.

dataSources = {
    infc1: outfc1,
    infc2: outfc2,
    infc3: outfc3,
    # etc...
}

for infc, outfc in dataSources.items():
0 Kudos
BlakeTerhune
MVP Regular Contributor

Here's what I would do.

 

import arcpy
import os

inDir = r"\\filelocation"
inputmapfile = os.path.join(inDir, "Water Map Files", "Water Network.mapx")
outDir = r"\\filelocation\Mobile Map Packages"

waterLayer = [
    "North_1",
    "North_2",
    "South_1",
    "South_2",
    "East_1",
    "East_2",
    "West_1",
    "West_2"
]

for lyr in waterLayer:
    inLyrx = os.path.join(inDir, "Water Map Files", "{}.lyrx".format(lyr))
    outName = lyr.replace("_", " ")
    outMmpk = os.path.join(outDir, "{} Water Network.mmpk".format(outName))
    arcpy.CreateMobileMapPackage_management(
        in_map=inputmapfile,
        output_file=outMmpk,
        in_locator="",
        area_of_interest=inLyrx,
        extent="DEFAULT",
        clip_features="CLIP",
        title="{} Water Network".format(outName),
        summary="Mobile Map Package of the {} Water Network".format(outName),
        description="",
        tags="Water Network, {}".format(outName),
        credits="",
        use_limitations="",
        anonymous_use="STANDARD",
        enable_map_expiration="DISABLE_MAP_EXPIRATION",
        map_expiration_type="ALLOW_TO_OPEN",
        expiration_date="",
        expiration_message="This map is expired.  Contact the map publisher for an updated map."
    )

 

higgy7
by
New Contributor III

Thanks Blake I know there are more than one way to do this but i can actually follow what is going on in this method. My problem with python is that in my job I only get to use it now and again, I pick things up and then forget it as there are long breaks in between.

0 Kudos
BlakeTerhune
MVP Regular Contributor

I'm in a similar situation. Python is not a constant thing at the front of my mind. I find it helpful to browse GeoNet to keep current on the technology and learn new coding patterns. And of course, trying to answer questions. The best way to learn something is to teach it!

0 Kudos
higgy7
by
New Contributor III

All thanks very much this will be of great help!!

0 Kudos