Arcpy 10.1 Export Map BUG

1021
0
09-26-2016 01:20 PM
DEVAPP
by
New Contributor III

Hi at all i have write a script that starting by jpg2 image and 2 shape file, create a ndvi, resample and after a click zoom on each layer created into map and export it to PNG. This script work very fine into arcmap 10.2 and 10.3, but when i run on arcmap 10.1 the export png have a shift respect the layer into map. I have try to export manualy and there isn't a shift instead if a try to execute Export by arcpy into python window the result have a shift.

I think that this is a bug.....please some esri people can say me if is a bug, if there is a fix or workaround?

Thanks

This is my code script

import os
import sys
import datetime
import arcpy
from arcpy import env
from arcpy.sa import *

def main():
    print "Beginning of the script"
    arcpy.env.overwriteOutput=True
    env.workspace = arcpy.GetParameterAsText(0)
    path = env.workspace = arcpy.GetParameterAsText(0)
    date = arcpy.GetParameterAsText(1)
    excludedValues = arcpy.GetParameterAsText(2)
    mxd = arcpy.mapping.MapDocument(path + "\\Map.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    Temp_Symbology = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ndviLayer.lyr')
    
    

    strNdvi = "ndvi.tif"
    strNdviLayer = "ndviLayer.lyr"
    #Calculate NDVI
    strRedImage = "InputImages\\B04.jp2"
    strIVImage = "InputImages\\B08.jp2"
    sr = arcpy.Describe(strRedImage).spatialReference
    df.spatialReference = sr

    strPolygons_big = arcpy.GetParameterAsText(3)
    strPolygons = "Costumers\\teat.shp"
    strPolygon = "polygon"

    arcpy.AddMessage("Start Project Raster 1 ...")
    arcpy.ProjectRaster_management(strRedImage, "B04_reproject.tif", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "BILINEAR", "9,86715266279924E-05 9,86715266279924E-05", "", "", "PROJCS['WGS_1984_UTM_zone_36N',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['false_easting',500000.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',33.0],PARAMETER['scale_factor',0.9996],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]")
    arcpy.AddMessage("Finish Project Raster 1 ...")
    arcpy.AddMessage("Start Project Raster 2 ...")
    arcpy.ProjectRaster_management(strIVImage, "B08_reproject.tif", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "BILINEAR", "9,86715266279924E-05 9,86715266279924E-05", "", "", "PROJCS['WGS_1984_UTM_zone_36N',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['false_easting',500000.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',33.0],PARAMETER['scale_factor',0.9996],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]")
    arcpy.AddMessage("Finish Project Raster 2 ...")

    strRedImage_r = "B04_reproject.tif"
    strIVImage_r = "B08_reproject.tif"
    
    arcpy.AddMessage("Creating NDVI ...")
    if not arcpy.Exists(strNdvi) and arcpy.Exists(strRedImage_r) and arcpy.Exists(strIVImage_r):
        rasNdvi = Divide(Minus(Float(strRedImage_r),Float(strIVImage_r)),Plus(Float(strRedImage_r),Float(strIVImage_r)))
        rasNdvi.save("ndvi.tif")
        print "NDVI created."
        arcpy.AddMessage("NDVI Creating Correctly!")
    arcpy.AddMessage("Starting First Clip ...")
    arcpy.Clip_management("ndvi.tif","#", "costumers_big_clip.tif",strPolygons_big, "0", "ClippingGeometry")
    arcpy.AddMessage("Finish First Clip Correctly!")
    arcpy.AddMessage("Starting Resample ...")
    arcpy.Resample_management("costumers_big_clip.tif", "customers_big_resample.tif", "0.00000495 0.00000495", resampling_type = "BILINEAR")
    arcpy.AddMessage("Finish Resample!")
    arcpy.AddMessage("Starting ListRaw ...")
    
    
    inField = "Number"

    if inField in [f.name for f in arcpy.ListFields(strPolygons)]:
        pass
    else:
        arcpy.AddField_management(strPolygons, inField, "SHORT", "", "", "")


    listRow = []
    #Unique values VERY USEFUL
    with arcpy.da.SearchCursor(strPolygons, ["ID", "Number"]) as searchCursor:

        for row in searchCursor:
            if row[0] not in listRow:
                listRow.append(row[0])

    with arcpy.da.UpdateCursor(strPolygons, ["ID", "Number"]) as updCursor:
        k = 0
        for row in updCursor:
            for k in range(0,len(listRow)):
                if row[0] == listRow:
                    row[1] = k

            updCursor.updateRow(row)
    arcpy.AddMessage("Starting ListRaw ...")

    #lyr.visible = False
    for j in range(0,len(listRow)):
        arcpy.AddMessage("Starting process for each customer...")


        arcpy.AddMessage("Create Make Feature Layer ...")
        arcpy.MakeFeatureLayer_management (strPolygons, "customerlyr"+str(j),' "Number" = ' + str(j))
        arcpy.AddMessage("Finish Make Feature Layer ...")
        #arcpy.SelectLayerByAttribute_management("customerlyr", "NEW_SELECTION", ' "Number" = ' + str(j))
        arcpy.AddMessage("Starting Second Clip by customer ...")
        arcpy.Clip_management("customers_big_resample.tif","#", "costumers_" +str(j)+".tif","customerlyr"+str(j), "0", "ClippingGeometry")
        arcpy.AddMessage("Finish Second Clip by customer!")
        
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        add_layer = arcpy.mapping.Layer("costumers_" +str(j)+".tif")
        arcpy.mapping.AddLayer(df, add_layer, "TOP")
        reclass_raster_lyr = arcpy.mapping.ListLayers(mxd)[0]

        arcpy.ApplySymbologyFromLayer_management(reclass_raster_lyr, Temp_Symbology)
        if reclass_raster_lyr.symbologyType == "RASTER_CLASSIFIED":
            reclass_raster_lyr.symbology.excludedValues = excludedValues
            reclass_raster_lyr.symbology.reclassify()

        lyrPolygonj = arcpy.mapping.Layer("customerlyr"+str(j))
        df.extent = lyrPolygonj.getSelectedExtent()
        lyrPolygonj.visible = False

        strAux = listRow.split(" ")[1::]
        strOutput = date + " " + " ".join(x for x in strAux) + ".tif"
        world_file = True
        arcpy.AddMessage("Starting Export Tiff")
        arcpy.mapping.ExportToPNG(mxd, strOutput,df, 10199, 7688,820,True)
        #arcpy.mapping.ExportToPNG(mxd, strOutput,df, 10199, 7688,820, world_file = world_file, background_color = "255,255,255",transparent_color = "255,255,255")
         
        arcpy.AddMessage("Finish Export Tiff")

        #arcpy.Delete_management("costumers" + str(j) + ".tif")
        #arcpy.Delete_management("aux" + str(j) + ".tif")
        
        arcpy.RefreshActiveView()

        

if __name__ == '__main__':
    main()

Tags (2)
0 Kudos
0 Replies