def process_layer(self, recipe_lyr, recipe_frame): """ Updates or Adds a layer of data. Maintains the Map Report. """ mapResult = MapResult(recipe_lyr.name) lyt = self.aprx.listLayouts("*")[0] arc_data_frame = lyt.listElements("MAPFRAME_ELEMENT", (recipe_frame.name + "*"))[0] try: # BUG # The layer name in the TOC is not necessarily == recipe_lyr.name # arc_lyr_to_update = arcpy.mapping.ListLayers(self.aprx, recipe_lyr.name, self.dataFrame)[0] # Try this instead # lyr_index = recipe_frame.layers.index(recipe_lyr) # arc_lyr_to_update = None # for m in self.aprx.listMaps("*"): # for lyr in m.listLayers(): # print(lyr.name) # if (lyr.name == recipe_lyr.name): # arc_lyr_to_update = lyr # arc_lyr_to_update = arcpy.mapping.ListLayers(self.aprx, None, arc_data_frame)[lyr_index] mapResult = self.addLayer(recipe_lyr, arc_data_frame) # Replace existing layer # mapResult = self.updateLayer(arc_lyr_to_update, recipe_lyr, recipe_frame) except IndexError: # Layer doesn't exist, add new layer mapResult = self.addLayer(recipe_lyr, arc_data_frame) self.mapReport.add(mapResult)
def process_layer(self, recipe_lyr, arc_data_frame): """ Updates or Adds a layer of data. Maintains the Map Report. """ mapResult = MapResult(recipe_lyr.name) # arc_data_frame = arcpy.mapping.ListDataFrames(self.mxd, recipe_frame.name)[0] # Try just using add Layer (no update layer option) mapResult = self.addLayer(recipe_lyr, arc_data_frame) self.mapReport.add(mapResult)
def addLayerWithGdb(self, layerProperties, layerToAdd, cookBookLayer, recipe_frame): mapResult = MapResult(layerProperties.name) # It's a File Geodatabase parts = layerProperties.reg_exp.split("/") gdbPath = parts[0] geoDatabases = self.find(self.crashMoveFolder.active_data, gdbPath, True) for geoDatabase in geoDatabases: arcpy.env.workspace = geoDatabase rasters = arcpy.ListRasters("*") for raster in rasters: if re.match(parts[1], raster): arc_data_frame = arcpy.mapping.ListDataFrames( self.aprx, recipe_frame.name)[0] mapResult.added = self.addDataToLayer( arc_data_frame, geoDatabase, layerToAdd, layerProperties.definition_query, raster, layerProperties.label_classes, layerProperties.add_to_legend) dataFile = geoDatabase + os.sep + raster ds = DataSource(dataFile) mapResult.dataSource = dataFile.replace("\\", "/").replace( self.crashMoveFolder.path.replace("\\", "/"), "") # noqa mapResult.hash = ds.calculate_checksum() break featureClasses = arcpy.ListFeatureClasses() for featureClass in featureClasses: if re.match(parts[1], featureClass): # Found Geodatabase. Stop iterating. arc_data_frame = arcpy.mapping.ListDataFrames( self.aprx, recipe_frame.name)[0] mapResult.added = self.addDataToLayer( arc_data_frame, geoDatabase, layerToAdd, layerProperties.definition_query, featureClass, layerProperties.label_classes, layerProperties.add_to_legend) dataFile = geoDatabase + os.sep + featureClass ds = DataSource(dataFile) mapResult.dataSource = dataFile.replace("\\", "/").replace( self.crashMoveFolder.path.replace("\\", "/"), "") # noqa mapResult.hash = ds.calculate_checksum() break return mapResult
def addLayer(self, recipe_lyr, recipe_frame): mapResult = MapResult(recipe_lyr.name) logging.debug('Attempting to add layer; {}'.format( recipe_lyr.layer_file_path)) lyrFile = arcpy.mp.LayerFile(recipe_lyr.layer_file_path) for arc_lyr_to_add in lyrFile.listLayers(): if (".gdb/" not in recipe_lyr.reg_exp): mapResult = self.addLayerWithFile(recipe_lyr, arc_lyr_to_add, recipe_lyr.name, recipe_frame) else: mapResult = self.addLayerWithGdb(recipe_lyr, arc_lyr_to_add, recipe_lyr.name, recipe_frame) return mapResult
def updateLayerWithFile(self, layerProperties, updateLayer, layerFilePath, recipe_frame): mapResult = MapResult(layerProperties.name) dataFiles = self.find(self.crashMoveFolder.active_data, layerProperties.reg_exp) for dataFile in (dataFiles): base = os.path.basename(dataFile) datasetName = os.path.splitext(base)[0] dataDirectory = os.path.dirname(os.path.realpath(dataFile)) sourceLayer = arcpy.mapping.Layer(layerFilePath) arc_data_frame = arcpy.mapping.ListDataFrames( self.aprx, recipe_frame.name)[0] arcpy.mapping.UpdateLayer(arc_data_frame, updateLayer, sourceLayer, False) # BUG # The layer name in the TOC is not necessarily == recipe_lyr.name # newLayer = arcpy.mapping.ListLayers(self.aprx, updateLayer.name, self.dataFrame)[0] # Try this instead lyr_index = recipe_frame.layers.index(updateLayer) newLayer = arcpy.mapping.ListLayers(self.aprx, None, arc_data_frame)[lyr_index] if newLayer.supports("DATASOURCE"): for datasetType in self.datasetTypes: try: if (newLayer.supports("DEFINITIONQUERY") and (layerProperties.definition_query)): newLayer.definition_query = layerProperties.definition_query.replace( '{COUNTRY_NAME}', self.eventConfiguration.country_name) newLayer.replaceDataSource(dataDirectory, datasetType, datasetName) mapResult.message = "Layer updated successfully" mapResult.added = True ds = DataSource(dataFile) mapResult.dataSource = dataFile.replace( "\\", "/").replace( self.crashMoveFolder.path.replace("\\", "/"), "") # noqa mapResult.hash = ds.calculate_checksum() break except Exception: pass if (mapResult.added is True): self.aprx.save() break return mapResult
def addLayerWithFile(self, recipe_lyr, arc_lyr_to_add, recipe_frame): mapResult = MapResult(recipe_lyr.name) # Skip past any layer which didn't already have a source file located try: recipe_lyr.data_source_path except AttributeError: return mapResult r_path = os.path.realpath(recipe_lyr.data_source_path) data_src_dir = os.path.dirname(r_path) dataset_type = self.get_dataset_type_from_path(r_path) # Apply Data Source if arc_lyr_to_add.supports("DATASOURCE"): try: arc_lyr_to_add.replaceDataSource(data_src_dir, dataset_type, recipe_lyr.data_name) mapResult.message = "Layer added successfully" mapResult.added = True ds = DataSource(recipe_lyr.data_name) mapResult.dataSource = recipe_lyr.data_source_path mapResult.hash = ds.calculate_checksum() except Exception as exp: raise exp if mapResult.added: arc_data_frame = arcpy.mapping.ListDataFrames( self.mxd, recipe_frame.name)[0] # TODO add proper fix for applyZoom in line with these two cards # https: // trello.com/c/Bs70ru1s/145-design-criteria-for-selecting-zoom-extent # https://trello.com/c/piE3tKRp/146-implenment-rules-for-selection-zoom-extent # self.applyZoom(self.dataFrame, arc_lyr_to_add, cookBookLayer.get('zoomMultiplier', 0)) # Is this even required after adding each layer? # self.apply_frame_crs_and_extent(arc_data_frame, recipe_frame) if recipe_lyr.add_to_legend is False: self.legendEntriesToRemove.append(arc_lyr_to_add.name) arcpy.mapping.AddLayer(arc_data_frame, arc_lyr_to_add, "BOTTOM") self.mxd.save() # break return mapResult
def addLayer(self, recipe_lyr, recipe_frame): # addLayer(recipe_lyr, recipe_lyr.layer_file_path, recipe_lyr.name) mapResult = MapResult(recipe_lyr.name) logging.debug('Attempting to add layer; {}'.format( recipe_lyr.layer_file_path)) arc_lyr_to_add = arcpy.mapping.Layer(recipe_lyr.layer_file_path) # if (".gdb/" not in recipe_lyr.reg_exp): # mapResult = self.addLayerWithFile(recipe_lyr, arc_lyr_to_add, recipe_frame) # else: # mapResult = self.addLayerWithGdb(recipe_lyr, arc_lyr_to_add, recipe_frame) # Apply Label Classes try: self.apply_layer_visiblity(arc_lyr_to_add, recipe_lyr) self.apply_label_classes(arc_lyr_to_add, recipe_lyr) mapResult = self.addLayerWithFile(recipe_lyr, arc_lyr_to_add, recipe_frame) # Apply Definition Query self.apply_definition_query(arc_lyr_to_add, recipe_lyr) recipe_lyr.success = True except Exception: recipe_lyr.success = False return mapResult
def addLayerWithFile(self, layerProperties, layerToAdd, cookBookLayer, recipe_frame): mapResult = MapResult(layerProperties.name) dataFiles = self.find(self.crashMoveFolder.active_data, layerProperties.reg_exp) for dataFile in (dataFiles): base = os.path.basename(dataFile) datasetName = os.path.splitext(base)[0] dataDirectory = os.path.dirname(os.path.realpath(dataFile)) if layerToAdd.supports("labelclasses"): for labelClass in layerProperties.label_classes: for lblClass in layerToAdd.labelClasses: if (lblClass.className == labelClass.class_name): lblClass.SQLQuery = labelClass.sql_query.replace( '{COUNTRY_NAME}', self.eventConfiguration.country_name) lblClass.expression = labelClass.expression lblClass.showClassLabels = labelClass.show_class_labels if layerToAdd.supports("datasource"): for datasetType in self.datasetTypes: try: cp = layerToAdd.connectionProperties cp['connection_info']['database'] = dataDirectory cp['dataset'] = datasetName + ".shp" # Use dictionary for suffixes layerToAdd.updateConnectionProperties( layerToAdd.connectionProperties, cp) # layerToAdd.replaceDataSource(dataDirectory, datasetType, datasetName) mapResult.message = "Layer added successfully" mapResult.added = True ds = DataSource(dataFile) mapResult.dataSource = dataFile.replace( "\\", "/").replace( self.crashMoveFolder.path.replace("\\", "/"), "") # noqa mapResult.hash = ds.calculate_checksum() break except Exception: pass if ((mapResult.added is True) and (layerProperties.definition_query)): definitionQuery = layerProperties.definition_query.replace( '{COUNTRY_NAME}', self.eventConfiguration.country_name) # NOQA layerToAdd.definition_query = definitionQuery try: arcpy.SelectLayerByAttribute_management( layerToAdd, "SUBSET_SELECTION", layerProperties.definition_query) except Exception: mapResult.added = False mapResult.message = "Selection query failed: " + layerProperties.definition_query self.aprx.save() if (mapResult.added is True): # TODO add proper fix for applyZoom in line with these two cards # https: // trello.com/c/Bs70ru1s/145-design-criteria-for-selecting-zoom-extent # https://trello.com/c/piE3tKRp/146-implenment-rules-for-selection-zoom-extent # self.applyZoom(self.dataFrame, layerToAdd, cookBookLayer.get('zoomMultiplier', 0)) # SAH self.applyZoom(arc_data_frame, layerToAdd, 0) m = self.aprx.listMaps( (recipe_frame.name.replace(" Map Frame", "") + "*")) m[0].addLayer(layerToAdd, "BOTTOM") self.aprx.save() break return mapResult
def updateLayerWithGdb(self, layerProperties, recipe_frame): mapResult = MapResult(layerProperties.name) mapResult.message = "Update layer for a GeoDatabase not yet implemented" return mapResult