# -*- coding: utf-8 -*-
import os
import sys
import log
import uuid
import arcpy
import arcpy.mapping as mp


class OutMap:
    def __init__(self, data):
        self.root = os.path.dirname(os.path.abspath(__file__))
        guid = ''.join(str(uuid.uuid1()).split('-'))
        self.jpg = os.path.join(self.root, r"mxds\fzxz\jpgs", guid);
        self.mxd = os.path.join(self.root, r"mxds\fzxz\FzxzMap.mxd");
        self.shp = os.path.join(self.root, r"mxds\fzxz\fzxz.mxd");
        os.makedirs(self.jpg)
        arcpy.RefreshActiveView()

    def run(self):
        mxd = mp.MapDocument(self.mxd)
        df = mp.ListDataFrames(mxd)[0]
        layF = mp.ListLayers(mxd, 'fzxzF')[0]
        layM = mp.ListLayers(mxd, 'fzxzM')[0]
        layExtent = layF.getExtent()

        cursor = arcpy.SearchCursor(layF, ['BSM'])
        for row in cursor:
            fid = row.getValue("BSM")
            where = "BSM='{0}'".format(str(fid))
            # 全局
            layF.definitionQuery = where
            layM.definitionQuery = ''
            df.extent = layExtent
            fileF = os.path.join(self.jpg, '{0}F.jpg'.format(fid))
            arcpy.RefreshActiveView()
            arcpy.mapping.ExportToJPEG(mxd, fileF, resolution=90)
            print(fileF)
            # 局部
            layF.definitionQuery = '-1'
            layM.definitionQuery = where
            fileM = os.path.join(self.jpg, '{0}M.jpg'.format(fid))
            df.extent = layM.getExtent()
            arcpy.RefreshActiveView()
            arcpy.mapping.ExportToJPEG(mxd, fileM, resolution=90)
            print(fileM)
