def export_layer_maps(helper,
                      title,
                      layer_key='Map:',
                      title_element='Title',
                      subtitle_element='Subtitle',
                      output='C:/Temp/',
                      file_suffix=''):
    """
    exports a set of maps by toggling a filtered set of layers
    By default, it searches for layers that start with 'Map:' and
    uses this list to generate a set of maps
        helper - A MapLayoutHelper
        title - The primary title of the map
        layer_key - (optional) the string to filter map layers on. The default is 'Map:'
        title_element - (optional) the string to search for a title text element. This item
            will be updated with the title text
        subtitle_element - (optional) the string to search for a subtitle text element.
            This item will be updated to the name of the layer, minus the layer_key text.
            If a layer's name is Map:Aerial, the subtitle will be set to "Aerial"
        output - (optional) The output folder path.
        file_suffix - (optional) The default filename will be the title_subtitle.pdf,
            adding this value will add an additional string to the end of the filepath.
    """

    # get a list of layers to use for "maps"
    # set them all to not visible for starters
    layers = [l for l in helper.map.map.listLayers() if layer_key in l.name]
    for l in layers:
        l.visible = False

    # get the text elements
    subtitle_element = helper.layout.listElements('TEXT_ELEMENT',
                                                  subtitle_element)[0]
    title_element = helper.layout.listElements('TEXT_ELEMENT',
                                               title_element)[0]
    title_element.text = title

    # Go through each layer, set it to visible, and export a pdf
    for l in layers:
        l.visible = True
        name = l.name.split(':')[1]
        subtitle_element.text = name
        filename = get_safe_string('{}_{}_{}'.format(title, name, file_suffix))
        helper.layout.exportToPDF(output + filename)
        l.visible = False
def export_layer_maps(helper, title, layer_key='Map:', title_element='Title', subtitle_element='Subtitle', output='C:/Temp/', file_suffix=''):
    """
    exports a set of maps by toggling a filtered set of layers
    By default, it searches for layers that start with 'Map:' and
    uses this list to generate a set of maps
        helper - A MapLayoutHelper
        title - The primary title of the map
        layer_key - (optional) the string to filter map layers on. The default is 'Map:'
        title_element - (optional) the string to search for a title text element. This item
            will be updated with the title text
        subtitle_element - (optional) the string to search for a subtitle text element.
            This item will be updated to the name of the layer, minus the layer_key text.
            If a layer's name is Map:Aerial, the subtitle will be set to "Aerial"
        output - (optional) The output folder path.
        file_suffix - (optional) The default filename will be the title_subtitle.pdf,
            adding this value will add an additional string to the end of the filepath.
    """

    # get a list of layers to use for "maps"
    # set them all to not visible for starters
    layers = [l for l in helper.map.map.listLayers() if layer_key in l.name]
    for l in layers:
        l.visible = False

    # get the text elements
    subtitle_element = helper.layout.listElements('TEXT_ELEMENT', subtitle_element)[0]
    title_element = helper.layout.listElements('TEXT_ELEMENT', title_element)[0]
    title_element.text = title

    # Go through each layer, set it to visible, and export a pdf
    for l in layers:
        l.visible = True
        name = l.name.split(':')[1]
        subtitle_element.text = name
        filename = get_safe_string('{}_{}_{}'.format(title, name, file_suffix))
        helper.layout.exportToPDF(output + filename)
        l.visible = False
Пример #3
0
    def execute(self, parameters, messages):
        """generates the site map pdf and csv files"""

        # set up local vars for easy access
        layer = parameters[p_layer].valueAsText
        export_location = parameters[p_export_location].valueAsText
        buffer_dist = parameters[p_buffer_dist].valueAsText
        title_field = parameters[p_title_field].valueAsText
        individual = parameters[p_individual].valueAsText
        document_title = parameters[p_title].valueAsText
        current_document = arcpy.mapping.MapDocument("CURRENT")
        data_frame = arcpy.mapping.ListDataFrames(current_document)[0]

        arcpy.AddMessage(
            """layer={}; export_location={}; buffer_dist={};
                title_field={}; individual={}; document_title={};""".format(
                layer, export_location, buffer_dist, title_field, individual, document_title
            )
        )

        # generate the output workspace
        if document_title and document_title != "":
            directory = document_title
            file_name = get_safe_string(document_title)
        else:
            directory = time.strftime("%m-%d-%y-(%H.%M)")
            file_name = time.strftime("%m-%d-%y-(%H.%M)")

        export_location = os.path.join(export_location, directory)
        verify_path_exists(export_location)
        arcpy.AddMessage("Creating temp workspace: {}/{}_data.gdb".format(export_location, file_name))
        arcpy.CreateFileGDB_management(export_location, "{}_data.gdb".format(file_name))
        file_gdb = "{}/{}_data.gdb".format(export_location, file_name)

        # activate export view
        current_document.activeView = "PAGE_LAYOUT"

        # export the selected features
        arcpy.AddMessage("Exporting: layer={}, {}/selected".format(layer, file_gdb))
        arcpy.FeatureClassToFeatureClass_conversion(layer, file_gdb, "selected")

        # perform buffer if necessary
        if buffer_dist:
            # buffer and select features
            arcpy.AddMessage("Buffering and Selecting: layer={}, {}/buffer".format(layer, file_gdb))
            arcpy.Buffer_analysis(layer, "{}/buffer".format(file_gdb), buffer_dist, "FULL", "ROUND", "ALL")
            arcpy.SelectLayerByLocation_management(layer, "INTERSECT", "{}/buffer".format(file_gdb), 0, "NEW_SELECTION")
            buffer = MapDocument.add_map_layer(
                "{}/buffer".format(file_gdb), symbols["orange"], "{} Buffer".format(buffer_dist)
            )

        # export the selected features
        arcpy.AddMessage("Exporting: layer={}, {}/buffer_selected".format(layer, file_gdb))
        arcpy.FeatureClassToFeatureClass_conversion(layer, file_gdb, "buffer_selected")
        buffer_selected = MapDocument.add_map_layer(
            "{}/buffer_selected".format(file_gdb), symbols["gray"], "Buffered Features"
        )

        # add original selection
        selected = MapDocument.add_map_layer("{}/selected".format(file_gdb), symbols["red"], "Selected Features")

        # prep output pdf
        final_pdf = arcpy.mapping.PDFDocumentCreate(os.path.join(export_location, "{}_Final.pdf".format(file_name)))
        arcpy.AddMessage("exporting pdf file {}_Final.pdf to {}".format(file_name, export_location))

        # activate export view
        current_document.activeView = "PAGE_LAYOUT"

        # zoom to the extent of the buffer
        data_frame.extent = Extent.expand(buffer_selected.getExtent(), 10)

        # set the title
        current_document.title = document_title

        # clear selection in layers with selected rows
        for l in arcpy.mapping.ListLayers(current_document):
            try:
                if arcpy.Describe(l).fidSet != "":
                    # throws errors on mosaic layers
                    arcpy.SelectLayerByAttribute_management(l, "CLEAR_SELECTION")
            except:
                pass

        # export
        MapDocument.export_and_append(export_location, final_pdf)

        rows = []
        cursor = arcpy.da.SearchCursor("{}/buffer_selected".format(file_gdb, file_name), ["SHAPE@", "*"])
        for row in cursor:
            rows.append(row)
            if individual == "false":
                continue
            # generate the pdf
            if title_field in cursor.fields:
                current_document.title = row[cursor.fields.index(title_field)]
            data_frame.extent = Extent.expand(row[0].extent, 10)
            MapDocument.export_and_append(export_location, final_pdf)

        # write out csv rows
        arcpy.AddMessage("Exporting csv: {}_output.csv to {}".format(file_name, export_location))
        # open csv file as binary so it avoids windows empty lines
        csv_output = open(os.path.join(export_location, "{}_output.csv".format(file_name)), "wb")
        csv_writer = csv.writer(csv_output, delimiter=",", quoting=csv.QUOTE_ALL, dialect="excel")
        csv_writer.writerow(cursor.fields)
        csv_writer.writerows(rows)

        # save the outputs
        csv_output.close()
        final_pdf.saveAndClose()

        # open explorer
        Popen('explorer "{}"'.format(export_location.replace("/", "\\")))

        # clean up
        del cursor
Пример #4
0
    def execute(self, parameters, messages):
        """generates the site map pdf and csv files"""

        # set up local vars for easy access
        layer = parameters[p_layer].valueAsText
        export_location = parameters[p_export_location].valueAsText
        buffer_dist = parameters[p_buffer_dist].valueAsText
        title_field = parameters[p_title_field].valueAsText
        individual = parameters[p_individual].valueAsText
        document_title = parameters[p_title].valueAsText
        current_document = arcpy.mapping.MapDocument("CURRENT")
        data_frame = arcpy.mapping.ListDataFrames(current_document)[0]

        arcpy.AddMessage("""layer={}; export_location={}; buffer_dist={};
                title_field={}; individual={}; document_title={};""".format(
            layer, export_location, buffer_dist, title_field, individual,
            document_title))

        # generate the output workspace
        if document_title and document_title != '':
            directory = document_title
            file_name = get_safe_string(document_title)
        else:
            directory = time.strftime('%m-%d-%y-(%H.%M)')
            file_name = time.strftime('%m-%d-%y-(%H.%M)')

        export_location = os.path.join(export_location, directory)
        verify_path_exists(export_location)
        arcpy.AddMessage('Creating temp workspace: {}/{}_data.gdb'.format(
            export_location, file_name))
        arcpy.CreateFileGDB_management(export_location,
                                       '{}_data.gdb'.format(file_name))
        file_gdb = '{}/{}_data.gdb'.format(export_location, file_name)

        # activate export view
        current_document.activeView = 'PAGE_LAYOUT'

        # export the selected features
        arcpy.AddMessage('Exporting: layer={}, {}/selected'.format(
            layer, file_gdb))
        arcpy.FeatureClassToFeatureClass_conversion(layer, file_gdb,
                                                    'selected')

        # perform buffer if necessary
        if buffer_dist:
            # buffer and select features
            arcpy.AddMessage(
                'Buffering and Selecting: layer={}, {}/buffer'.format(
                    layer, file_gdb))
            arcpy.Buffer_analysis(layer, '{}/buffer'.format(file_gdb),
                                  buffer_dist, 'FULL', 'ROUND', 'ALL')
            arcpy.SelectLayerByLocation_management(
                layer, 'INTERSECT', '{}/buffer'.format(file_gdb), 0,
                'NEW_SELECTION')
            buffer = MapDocument.add_map_layer('{}/buffer'.format(file_gdb),
                                               symbols['orange'],
                                               '{} Buffer'.format(buffer_dist))

        # export the selected features
        arcpy.AddMessage('Exporting: layer={}, {}/buffer_selected'.format(
            layer, file_gdb))
        arcpy.FeatureClassToFeatureClass_conversion(layer, file_gdb,
                                                    'buffer_selected')
        buffer_selected = MapDocument.add_map_layer(
            '{}/buffer_selected'.format(file_gdb), symbols['gray'],
            'Buffered Features')

        # add original selection
        selected = MapDocument.add_map_layer('{}/selected'.format(file_gdb),
                                             symbols['red'],
                                             'Selected Features')

        # prep output pdf
        final_pdf = arcpy.mapping.PDFDocumentCreate(
            os.path.join(export_location, '{}_Final.pdf'.format(file_name)))
        arcpy.AddMessage('exporting pdf file {}_Final.pdf to {}'.format(
            file_name, export_location))

        # activate export view
        current_document.activeView = 'PAGE_LAYOUT'

        #zoom to the extent of the buffer
        data_frame.extent = Extent.expand(buffer_selected.getExtent(), 10)

        #set the title
        current_document.title = document_title

        # clear selection in layers with selected rows
        for l in arcpy.mapping.ListLayers(current_document):
            try:
                if arcpy.Describe(l).fidSet != '':
                    # throws errors on mosaic layers
                    arcpy.SelectLayerByAttribute_management(
                        l, "CLEAR_SELECTION")
            except:
                pass

        # export
        MapDocument.export_and_append(export_location, final_pdf)

        rows = []
        cursor = arcpy.da.SearchCursor(
            '{}/buffer_selected'.format(file_gdb, file_name), ['SHAPE@', '*'])
        for row in cursor:
            rows.append(row)
            if individual == 'false':
                continue
            # generate the pdf
            if title_field in cursor.fields:
                current_document.title = row[cursor.fields.index(title_field)]
            data_frame.extent = Extent.expand(row[0].extent, 10)
            MapDocument.export_and_append(export_location, final_pdf)

        # write out csv rows
        arcpy.AddMessage('Exporting csv: {}_output.csv to {}'.format(
            file_name, export_location))
        # open csv file as binary so it avoids windows empty lines
        csv_output = open(
            os.path.join(export_location, '{}_output.csv'.format(file_name)),
            'wb')
        csv_writer = csv.writer(csv_output,
                                delimiter=',',
                                quoting=csv.QUOTE_ALL,
                                dialect='excel')
        csv_writer.writerow(cursor.fields)
        csv_writer.writerows(rows)

        # save the outputs
        csv_output.close()
        final_pdf.saveAndClose()

        # open explorer
        Popen('explorer "{}"'.format(export_location.replace('/', '\\')))

        # clean up
        del cursor