画像出力(縮尺指定)


レイヤの最大/最小出力がおかしい。1くらいなら誤差だろうが1どころではない。
さすが世界シェアNo.1縮尺計算できなくともなんともないぜ
C#でとりあえずは現象再発しなさそう。GDI+?縮尺はGISでしか管理されてないだろうし、描画ライブラリ関係ないだろ

Pythonのコード (レイヤ縮尺制御がおかしくなる)

# -*- coding: cp932 -*-

import arcpy
import os.path

# 縮尺設定
# 1:500,000,000

exportDPI = 96

mxdDirPath = r"C:\Program Files (x86)\ArcGIS\DeveloperKit10.1\Samples\data\World"

mxdPath = os.path.join(mxdDirPath, "World.mxd")

mxd = arcpy.mapping.MapDocument(mxdPath)
df =  arcpy.mapping.ListDataFrames(mxd)[0]

mxd.activeView='PAGE_LAYOUT'
df.zoomToSelectedFeatures();
df.scale = 500000000.0

arcpy.mapping.ExportToJPEG(map_document=mxd,
                           out_jpeg = os.path.join(mxdDirPath, "DPI" + str(exportDPI) + "_" + str(df.scale) + ".jpg"),
                           resolution=exportDPI)

mxd.activeView='PAGE_LAYOUT'
df.zoomToSelectedFeatures();
df.scale = 500000000.0 + 1
                           
arcpy.mapping.ExportToJPEG(map_document=mxd,
                           out_jpeg = os.path.join(mxdDirPath, "DPI" + str(exportDPI) + "_" + str(df.scale) + ".jpg"),
                           resolution=exportDPI)

mxd.activeView='PAGE_LAYOUT'
df.zoomToSelectedFeatures();
#          499999999
df.scale = 500000000 - 1

arcpy.mapping.ExportToJPEG(map_document=mxd,
                           out_jpeg = os.path.join(mxdDirPath, "DPI" + str(exportDPI) + "_" + str(df.scale) + ".jpg"),
                           resolution=exportDPI)

mxd.activeView='PAGE_LAYOUT'
df.zoomToSelectedFeatures();
df.scale = 500000000 - 10000

arcpy.mapping.ExportToJPEG(map_document=mxd,
                           out_jpeg = os.path.join(mxdDirPath, "DPI" + str(exportDPI) + "_" + str(df.scale) + ".jpg"),
                           resolution=exportDPI)

C#のコード (正常にでる)


var mxdPathDir = @"C:\Program Files (x86)\ArcGIS\DeveloperKit10.1\Samples\data\World";
var mxdPath = System.IO.Path.Combine(mxdPathDir, "World.mxd");
var exportDpi = 300;//96;
var resample = 1;
var exportType = "JPEG";

using (ComReleaser com = new ComReleaser())
{
  var exporter = new ExportActiveViewCS_Net_Addin.ExportActiveViewCS_Net();

  IMapDocument mxd = new MapDocumentClass();
  com.ManageLifetime(mxd);
  mxd.Open(mxdPath);

  var map = mxd.get_Map(0);
  com.ManageLifetime(map);
  var actView = map as IActiveView;


  //縮尺設定
  actView.Activate(0);
  map.MapScale = 500000000;
  actView.Deactivate();

  var outName = string.Format("DPI_{0}_{1}", exportDpi, map.MapScale);
  outName = mxdPathDir + "\\" + outName;
  exporter.ExportActiveViewParameterized(actView, exportDpi, resample, exportType, outName, false);

  //縮尺設定
  actView.Activate(0);
  map.MapScale = 500000000 + 1;
  actView.Deactivate();

  outName = string.Format("DPI_{0}_{1}", exportDpi, map.MapScale);
  outName = mxdPathDir + "\\" + outName;
  exporter.ExportActiveViewParameterized(actView, exportDpi, resample, exportType, outName, false);

  //縮尺設定
  actView.Activate(0);
  map.MapScale = 500000000 - 1;
  actView.Deactivate();

  outName = string.Format("DPI_{0}_{1}", exportDpi, map.MapScale);
  outName = mxdPathDir + "\\" + outName;
  exporter.ExportActiveViewParameterized(actView, exportDpi, resample, exportType, outName, false);

  mxd.Close();
}

C#のコード (同名のサンプルをちょろっと改変)

// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Display;
//using System.Windows.Forms;

namespace ExportActiveViewCS_Net_Addin
{

  /// <summary>
  /// This class creates a command that will export the active view to any supported format.
  /// </summary>
  public class ExportActiveViewCS_Net
  {

    /* GDI delegate to SystemParametersInfo function */
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);

    /* constants used for user32 calls */
    const uint SPI_GETFONTSMOOTHING = 74;
    const uint SPI_SETFONTSMOOTHING = 75;
    const uint SPIF_UPDATEINIFILE = 0x1;

    public void ExportActiveViewParameterized(IActiveView docActiveView, long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, Boolean bClipToGraphicsExtent)
    {

      /* EXPORT PARAMETER: (iOutputResolution) the resolution requested.
       * EXPORT PARAMETER: (lResampleRatio) Output Image Quality of the export.  The value here will only be used if the export
       * object is a format that allows setting of Output Image Quality, i.e. a vector exporter.
       * The value assigned to ResampleRatio should be in the range 1 to 5.
       * 1 corresponds to "Best", 5 corresponds to "Fast"
       * EXPORT PARAMETER: (ExportType) a string which contains the export type to create.
       * EXPORT PARAMETER: (sOutputDir) a string which contains the directory to output to.
       * EXPORT PARAMETER: (bClipToGraphicsExtent) Assign True or False to determine if export image will be clipped to the graphic 
       * extent of layout elements.  This value is ignored for data view exports
       */

      /* Exports the Active View of the document to selected output format. */

      // using predefined static member
      //IActiveView docActiveView = ArcMap.Document.ActiveView;
      IExport docExport;
      IPrintAndExport docPrintExport;
      IOutputRasterSettings RasterSettings;
      string sNameRoot;
      bool bReenable = false;

      if (GetFontSmoothing())
      {
        /* font smoothing is on, disable it and set the flag to reenable it later. */
        bReenable = true;
        DisableFontSmoothing();
        if (GetFontSmoothing())
        {
          //font smoothing is NOT successfully disabled, error out.
          return;
        }
        //else font smoothing was successfully disabled.
      }

      // The Export*Class() type initializes a new export class of the desired type.
      if (ExportType == "PDF")
      {
        docExport = new ExportPDFClass();
      }
      else if (ExportType == "EPS")
      {
        docExport = new ExportPSClass();
      }
      else if (ExportType == "AI")
      {
        docExport = new ExportAIClass();
      }
      else if (ExportType == "BMP")
      {

        docExport = new ExportBMPClass();
      }
      else if (ExportType == "TIFF")
      {
        docExport = new ExportTIFFClass();
      }
      else if (ExportType == "SVG")
      {
        docExport = new ExportSVGClass();
      }
      else if (ExportType == "PNG")
      {
        docExport = new ExportPNGClass();
      }
      else if (ExportType == "GIF")
      {
        docExport = new ExportGIFClass();
      }
      else if (ExportType == "EMF")
      {
        docExport = new ExportEMFClass();
      }
      else if (ExportType == "JPEG")
      {
        docExport = new ExportJPEGClass();
      }
      else
      {
        Console.WriteLine("Unsupported export type " + ExportType + ", defaulting to EMF.");
        //MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF.");
        ExportType = "EMF";
        docExport = new ExportEMFClass();
      }

      docPrintExport = new PrintAndExportClass();

      //set the name root for the export
      sNameRoot = "";

      //set the export filename (which is the nameroot + the appropriate file extension)
      docExport.ExportFileName = sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];

      //Output Image Quality of the export.  The value here will only be used if the export
      // object is a format that allows setting of Output Image Quality, i.e. a vector exporter.
      // The value assigned to ResampleRatio should be in the range 1 to 5.
      // 1 corresponds to "Best", 5 corresponds to "Fast"

      // check if export is vector or raster
      if (docExport is IOutputRasterSettings)
      {
        // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time   
        RasterSettings = (IOutputRasterSettings)docExport;
        RasterSettings.ResampleRatio = (int)lResampleRatio;

        // NOTE: for raster formats output quality of the DISPLAY is set to 1 for image export 
        // formats by default which is what should be used
      }

      docPrintExport.Export(docActiveView, docExport, iOutputResolution, bClipToGraphicsExtent, null);

      //MessageBox.Show("Finished exporting " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "Export Active View Sample");
      Console.WriteLine("Finished exporting " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "Export Active View Sample");
      if (bReenable)
      {
        /* reenable font smoothing if we disabled it before */
        EnableFontSmoothing();
        bReenable = false;
        if (!GetFontSmoothing())
        {
          //error: cannot reenable font smoothing.
          //MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error");
          Console.WriteLine("Unable to reenable Font Smoothing", "Font Smoothing error");
        }
      }
    }

    private void DisableFontSmoothing()
    {
      bool iResult;
      int pv = 0;

      /* call to systemparametersinfo to set the font smoothing value */
      iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 0, ref pv, SPIF_UPDATEINIFILE);
    }

    private void EnableFontSmoothing()
    {
      bool iResult;
      int pv = 0;

      /* call to systemparametersinfo to set the font smoothing value */
      iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ref pv, SPIF_UPDATEINIFILE);

    }

    private Boolean GetFontSmoothing()
    {
      bool iResult;
      int pv = 0;

      /* call to systemparametersinfo to get the font smoothing value */
      iResult = SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref pv, 0);

      if (pv > 0)
      {
        //pv > 0 means font smoothing is ON.
        return true;
      }
      else
      {
        //pv == 0 means font smoothing is OFF.
        return false;
      }

    }
  }

}
カテゴリー: 開発 タグ: , パーマリンク