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))
asked Jun 29, 2015 at 15:00
-
Do you want just what is in the current extent, or all layers' data in the data frame?Emil Brundage– Emil Brundage2015年06月29日 15:11:22 +00:00Commented Jun 29, 2015 at 15:11
-
@EmilBrundage: all layersIbe– Ibe2015年06月29日 15:19:34 +00:00Commented Jun 29, 2015 at 15:19
2 Answers 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)
answered Jun 29, 2015 at 15:38
-
When I use
Data > Export Data
by right clicking on layer then it overlaps perfectly with layer in the frame. But witharcpy.Project_management
, it shifts shapefile to southIbe– Ibe2015年06月29日 16:02:39 +00:00Commented Jun 29, 2015 at 16:02 -
That's common with projections. They aren't perfect. When you export, you're not projecting,Emil Brundage– Emil Brundage2015年06月29日 16:04:08 +00:00Commented 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 outputsIbe– Ibe2015年06月29日 16:23:37 +00:00Commented 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– mkennedy2015年06月29日 16:33:00 +00:00Commented Jun 29, 2015 at 16:33
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
tinlyx
11.3k18 gold badges78 silver badges130 bronze badges
lang-py