|
| 1 | +import os |
| 2 | +import arcpy |
| 3 | +from arcpy import mapping |
| 4 | +from arcpy import env |
| 5 | + |
| 6 | +# load settings from settings.py |
| 7 | +try: |
| 8 | + import settings |
| 9 | +except: |
| 10 | + raise Exception( |
| 11 | + "Please copy the settings_template.py file to " + |
| 12 | + "a file named settings.py and edit the values as required." |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +ACTIVE_QUERY = "enddate = timestamp '2100-01-01 00:00:00'" |
| 17 | +INACTIVE_QUERY = "enddate <> timestamp '2100-01-01 00:00:00'" |
| 18 | + |
| 19 | +ALL = "ALL" |
| 20 | +ACTIVE = "ACTIVE" |
| 21 | +INACTIVE = "INACTIVE" |
| 22 | + |
| 23 | +CONNECTION_FILE_NAME = "awdb.ags" |
| 24 | + |
| 25 | +mxd_path = os.path.join(settings.repo, "blank_map.mxd") |
| 26 | +map_dir = settings.MAP_DIR |
| 27 | + |
| 28 | + |
| 29 | +# create map dir if not present |
| 30 | +if not os.path.isdir(map_dir): |
| 31 | + os.makedirs(map_dir) |
| 32 | + |
| 33 | +########################################################### |
| 34 | +### This us useful if one can figure out how to properly |
| 35 | +### update the SD draft that can be generated from an mxd. |
| 36 | +### Otherwise it is useless and will remain commented out. |
| 37 | +## create server connection file |
| 38 | +#mapping.CreateGISServerConnectionFile( |
| 39 | +# "PUBLISH_GIS_SERVICES", |
| 40 | +# map_dir, |
| 41 | +# CONNECTION_FILE_NAME, |
| 42 | +# "http://{}:{}/arcgis/admin".format( |
| 43 | +# settings.SERVER_ADDRESS, |
| 44 | +# settings.SERVER_PORT |
| 45 | +# ), |
| 46 | +# "ARCGIS_SERVER", |
| 47 | +# username=settings.SERVER_USER, |
| 48 | +# password=settings.SERVER_PASS, |
| 49 | +#) |
| 50 | +########################################################### |
| 51 | + |
| 52 | +connection_file = os.path.join(map_dir, CONNECTION_FILE_NAME) |
| 53 | + |
| 54 | +# get the AWDB fcs in the SDE database |
| 55 | +env.workspace = os.path.join( |
| 56 | + # this should use a read-only database connection file |
| 57 | + # to prevent any possibility of unintended data manipulation |
| 58 | + settings.SDE_READONLY, |
| 59 | + settings.FDS_SDE_PREFIX + settings.FDS_NAME |
| 60 | +) |
| 61 | + |
| 62 | +fcs = arcpy.ListFeatureClasses() |
| 63 | + |
| 64 | +# iterate through the fcs and create map docs, publishing each as a WFS |
| 65 | +for fc in fcs: |
| 66 | + # fc name is the whole SDE path, including DB, schema, and FDS names |
| 67 | + # splitting on . and taking last piece gives the actual FC name |
| 68 | + fc_name = fc.split(".")[-1] |
| 69 | + |
| 70 | + # we want to make three layers for each FC: |
| 71 | + # one with all stations, one with only active stations, |
| 72 | + # and one with only inactive stations |
| 73 | + lyrs = { |
| 74 | + # all stations |
| 75 | + ALL: arcpy.MakeFeatureLayer_management( |
| 76 | + fc, |
| 77 | + "{}_{}".format(fc_name, ALL) |
| 78 | + ), |
| 79 | + # only active stations |
| 80 | + ACTIVE: arcpy.MakeFeatureLayer_management( |
| 81 | + fc, |
| 82 | + "{}_{}".format(fc_name, ACTIVE), |
| 83 | + ACTIVE_QUERY |
| 84 | + ), |
| 85 | + # only inactive stations |
| 86 | + INACTIVE: arcpy.MakeFeatureLayer_management( |
| 87 | + fc, |
| 88 | + "{}_{}".format(fc_name, INACTIVE), |
| 89 | + INACTIVE_QUERY |
| 90 | + ), |
| 91 | + } |
| 92 | + |
| 93 | + # now we want to make a map for each |
| 94 | + for lyr_name, lyr in lyrs.iteritems(): |
| 95 | + # the result stored in lyr is a result object, not a layer object |
| 96 | + # getOutput(0) gets the first (and only) output from the |
| 97 | + # MakeFeatureLayer commands, which happens to be a layer object |
| 98 | + lyr = lyr.getOutput(0) |
| 99 | + |
| 100 | + # open the blank mxd because we can't create them reasonably |
| 101 | + mxd = mapping.MapDocument(mxd_path) |
| 102 | + |
| 103 | + # have to get the first (and only) dataframe |
| 104 | + df = mapping.ListDataFrames(mxd)[0] |
| 105 | + |
| 106 | + # now we can add the layer object we pulled out above |
| 107 | + mapping.AddLayer(df, lyr) |
| 108 | + |
| 109 | + # let's save the modified mxd to a new file |
| 110 | + outmxd = os.path.join(map_dir, "{}_{}.mxd".format(fc_name, lyr_name)) |
| 111 | + print "Writing map doc {}".format(outmxd) |
| 112 | + mxd.saveACopy(outmxd) |
| 113 | + |
| 114 | + # clean up the mxd object |
| 115 | + del mxd |
| 116 | + |
| 117 | +######################################################################### |
| 118 | +### The below would be useful if it made any sense whatsoever as to |
| 119 | +### what one needs to do with a sddraft file to setup the sd as |
| 120 | +### required for the AWDB services. Without knowing how to do that, |
| 121 | +### I've decided it is not worth automating at this time. |
| 122 | +### If something changes and someone understands this, just uncomment and |
| 123 | +### fix as needed. |
| 124 | +# # output file for sddraft |
| 125 | +# outsddraft = os.path.join( |
| 126 | +# map_dir, |
| 127 | +# "{}_{}.sddraft".format(fc_name, lyr_name) |
| 128 | +# ) |
| 129 | +# |
| 130 | +# outsd = os.path.join( |
| 131 | +# map_dir, |
| 132 | +# "{}_{}.sd".format(fc_name, lyr_name) |
| 133 | +# ) |
| 134 | +# |
| 135 | +# # create the sddraft from the mxd previously created |
| 136 | +# sddraft = mapping.CreateMapSDDraft( |
| 137 | +# outmxd, |
| 138 | +# outsddraft, |
| 139 | +# "{}_{}".format(fc_name, lyr_name), |
| 140 | +# "ARCGIS_SERVER", |
| 141 | +# connection_file, |
| 142 | +# folder_name="AWDB_{}".format(lyr_name), |
| 143 | +# ) |
| 144 | +# |
| 145 | +# # stage and upload the service if the |
| 146 | +# # sddraft analysis did not contain errors |
| 147 | +# if sddraft['errors'] == {}: |
| 148 | +# # Execute StageService |
| 149 | +# #arcpy.StageService_server(outsddraft, outsd) |
| 150 | +# # Execute UploadServiceDefinition |
| 151 | +# #arcpy.UploadServiceDefinition_server(outsd, connection_file) |
| 152 | +# print "no errors" |
| 153 | +# else: |
| 154 | +# # if the sddraft analysis contained errors, display them |
| 155 | +# print sddraft['errors'] |
0 commit comments