Select to view content in your preferred language

Export from Portal to Shapefile and adding X & Y

400
3
Jump to solution
08-16-2023 12:10 PM
KristenJones1
New Contributor III

I have a working Export tool that grabs a published Rest service of several layers and saves them locally on my computer, the only thing I need to add to it is on the point feature classes there is no X and Y coordinates. So I have to calculate them and add them to the exported shape file.  The following code WORKS PERFECT  (I just left out the variables and such) but I am working on the "If it is a Point FC then add the X&Y to that exported FC.  The CODE I have that is remarked out is what I am wanting to do logically but it is not correct.  Can someone help with the correct syntax and calls?

with arcpy.EnvManager(preserveGlobalIds=True):
    for x in data_content.layers:
        if x.properties.type == "Feature Layer":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
 
#if layer_url.geometry.dtype == "Point":
   #Calulate/get the  X & Y values
   #XVALUE = arcpy.management.CalculateGeometryAttributes(arcpy.env.workspace, POINT_X)
   #YVALUE = arcpy.management.CalculateGeometryAttributes(arcpy.env.workspace, POINT_Y)
   #XField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "X")
   #YField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "Y")
   #expression = XField + " = XVALUE, YField + " = YVALUE,"
   #arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name, expression)    
            arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)         
            
    for x in data_content.tables:
        if x.properties.type == "Table":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;                 
            arcpy.conversion.TableToTable(layer_url, workspace_gdb, layer_name)  

 

0 Kudos
1 Solution

Accepted Solutions
KristenJones1
New Contributor III

to anyone else following this or would like my solution..  it was a LOT easier than I thought 

 

 I just opened the workspace and ran the ADD XY using this simple command  arcpy.management.AddXY(layer_name)

 

with arcpy.EnvManager(preserveGlobalIds=True):
    for x in data_content.layers:
        if x.properties.type == "Feature Layer":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            
            if x.properties.geometryType == "esriGeometryPoint": 
      		arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)  
                arcpy.management.AddXY(layer_name)

            else:
                
                arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)    
                      
            
    for x in data_content.tables:
        if x.properties.type == "Table":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            
            arcpy.conversion.TableToTable(layer_url, workspace_gdb, layer_name) 

View solution in original post

0 Kudos
3 Replies
EarlMedina
Esri Regular Contributor

Hi, I think for the if statement you should change to something like

if x.properties.geometryType == "esriGeometryPoint":

 

You could also approach this differently if this is the only thing you need to do. Here's a working snippet that uses just the ArcGIS API for Python:

from arcgis import GIS
from arcgis.features import FeatureLayer

gis = GIS()
sample_url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Earthquakes_Since1970/FeatureServer/0"
fl = FeatureLayer(sample_url, gis)
sdf = fl.query(as_df=True)

if fl.properties.geometryType == "esriGeometryPoint":
    sdf["x"] = sdf.SHAPE.apply(lambda x: x.x)
    sdf["y"] = sdf.SHAPE.apply(lambda x: x.y)

sdf.spatial.to_featureclass(location=r"c:\temp\eq.shp")

 

 

0 Kudos
KristenJones1
New Contributor III

Thanks but I can't seem to "plug in your code to work.  The esriGeometryPoint works but not the adding X & Y coordinates. Here is my exact working code with the lines REMED out that I am trying to edit.  

 

import arcpy, getpass, logging, matplotlib, json
import datetime
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import pandas as pd
from pandas import json_normalize
now = datetime.datetime.now()

# Create a unique name
namedate = now.strftime("%Y%m%d%H%M%S")

# Print the name
print(namedate)

name = "DEV"
gdb_name = namedate + "-" + name
portal_url = 'https://gisdev.testing.com/portal' 
rest_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
user = 'test'
pwd = 'test' 
Company_Code = 'X'
folder_input = 'C:\test\\' 

gis = GIS(portal_url, user, pwd)

data_content = gis.content.get(rest_id)
workspace_gdb = arcpy.CreateFileGDB_management(folder_input, f"{gdb_name}.gdb")

arcpy.env.workspace = fr"{workspace_gdb}"

with arcpy.EnvManager(preserveGlobalIds=True):
    for x in data_content.layers:
        if x.properties.type == "Feature Layer":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            
            if x.properties.geometryType == "esriGeometryPoint":
                    # BOMBS sdf = x.properties.query(as_df=True)
                    # BOMBS sdf["x"] = sdf.SHAPE.apply(lambda x: x.x)
                    # BOMBS sdf["y"] = sdf.SHAPE.apply(lambda x: x.y)
                    arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)  
            Else       
                    arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)

    for x in data_content.tables:
        if x.properties.type == "Table":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            arcpy.conversion.TableToTable(layer_url, workspace_gdb, layer_name) 

   

0 Kudos
KristenJones1
New Contributor III

to anyone else following this or would like my solution..  it was a LOT easier than I thought 

 

 I just opened the workspace and ran the ADD XY using this simple command  arcpy.management.AddXY(layer_name)

 

with arcpy.EnvManager(preserveGlobalIds=True):
    for x in data_content.layers:
        if x.properties.type == "Feature Layer":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            
            if x.properties.geometryType == "esriGeometryPoint": 
      		arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)  
                arcpy.management.AddXY(layer_name)

            else:
                
                arcpy.conversion.FeatureClassToFeatureClass(layer_url, workspace_gdb, layer_name)    
                      
            
    for x in data_content.tables:
        if x.properties.type == "Table":
            layer_name = x.properties.name.replace(" ","")
            layer_url = x.url;
            
            arcpy.conversion.TableToTable(layer_url, workspace_gdb, layer_name) 
0 Kudos