1

I want to export data in current data frame and use the same coordiante system as data frame using ArcPy as I have multiple files. Any suggestion on how to "Export with data frame's coordinate system"? Following is what I have:

import arcpy
from arcpy.sa import *

mxd = arcpy.mapping.MapDocument(r'C:/test.mxd') 
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
for layers in xrange(1,12,1):
    inLU = r'C:/rl_' + str(layers) + '.lyr'
    lyrFile = arcpy.mapping.Layer(inLU)
    arcpy.mapping.AddLayer(df, lyrFile)
    arcpy.CopyFeatures_management(r'rl_' + str(layers), r'C:/shape_' + str(layers))
  • Do you want just what is in the current extent, or all layers' data in the data frame? – Emil Brundage Jun 29 '15 at 15:11
  • @EmilBrundage: all layers – Ibe Jun 29 '15 at 15:19
2

See comments below:

import arcpy

#Get data frame object
mxd = arcpy.mapping.MapDocument(r'C:/test.mxd') 
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]

#Get spatial reference object from data frame
SR = df.spatialReference

#List all layers in data frame
layers = arcpy.mapping.ListLayers (mxd, "", df)

#Iterator for naming files
i = 0
#Iterate all layers
for layer in layers:
    i += 1
    #Export/project
    arcpy.Project_management (layer, r'C:/shape_' + str(i) + '.shp', SR)
  • When I use Data > Export Data by right clicking on layer then it overlaps perfectly with layer in the frame. But with arcpy.Project_management, it shifts shapefile to south – Ibe Jun 29 '15 at 16:02
  • That's common with projections. They aren't perfect. When you export, you're not projecting, – Emil Brundage Jun 29 '15 at 16:04
  • This is the reason why I wanted to save layers in first place. because several attempts of reprojection led to shift in outputs – Ibe Jun 29 '15 at 16:23
  • @EmilBrundage When you export, you are calling exactly the same process as the Project tool. A difference might be caused by no or a different geographic/datum transformation. Are any applied in the data frame? If so, you probably need to set it in the gp env so the project tool will pick it up. Or specify it in the call. – mkennedy Jun 29 '15 at 16:33
0
import arcpy
import os,sys

saveFeatureFold = 'F:\\data\\data.gdb\\myData\\'#you must create the gdb first and define the project you like
featureFold = 'F:\\dealWithData\\
for file in os.listdir(featureFold ):
    if file.endswith('.shp'):
        fileName = featureFold +file
        saveName = saveFeatureFold +file
        arcpy.CopyFeatures_management(fileName,saveName )#so the project is you want to be

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.