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))
2
  • Do you want just what is in the current extent, or all layers' data in the data frame? Commented Jun 29, 2015 at 15:11
  • @EmilBrundage: all layers
    – Ibe
    Commented Jun 29, 2015 at 15:19

2 Answers 2

3

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)
4
  • 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
    Commented Jun 29, 2015 at 16:02
  • That's common with projections. They aren't perfect. When you export, you're not projecting, Commented Jun 29, 2015 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
    Commented Jun 29, 2015 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
    Commented Jun 29, 2015 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 and acknowledge you have read our privacy policy.

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