<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Connect to .SDE via Python and read table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264925#M66993</link>
    <description>&lt;P&gt;You might want to check out the ArcGIS Python API, too. You can query an SDE table straight into a dataframe, then use the &lt;STRONG&gt;GeoAccessor&lt;/STRONG&gt; class to convert that to a spatial dataframe, and continue working with it from there.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.features import GeoAccessor
import pandas as pd

df = pd.read_sql(
    'some_table',
    'database_connection_string'
)

sdf = GeoAccessor.from_xy(
    df,
    'lon',
    'lat',
    'spatial_reference'
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Mar 2023 14:37:18 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2023-03-07T14:37:18Z</dc:date>
    <item>
      <title>Connect to .SDE via Python and read table</title>
      <link>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264855#M66990</link>
      <description>&lt;P&gt;Well hello here,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to know how to make a python script that reads the attribute table from .sde feature class? In my case that feature class is inside feature dataset. At the moment I know how to make it happen with excel, but wanted to reconstruct script a little bit to miss that part where you need to export excel file out.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, here is a code snippet of how I'm reading latitude and&amp;nbsp;longitude from a excel file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;file_path = r"D:\coordinates\coordinates_excel.xls"
dataframe = pd.read_excel(file_path)

for i in range(0,len(dataframe)):
    lat2 = dataframe['lat'][i]
    lon2 = dataframe['lon'][i]
    distance = haversine(lat, lon, lat2, lon2)
    distance_nxt.append(distance)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The goal is to read attribute table (lat, lon) from a shapefile which is in .SDE, and to do further tasks. Right now, this script is reading LAT and LON from excel file. Maybe someone have an idea of how to shoot it right away from .sde database?&amp;nbsp; How to read shapefile attribute table like excel table?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 09:47:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264855#M66990</guid>
      <dc:creator>GIS_Rookie</dc:creator>
      <dc:date>2023-03-07T09:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to .SDE via Python and read table</title>
      <link>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264889#M66992</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/561267"&gt;@GIS_Rookie&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Here is an example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Variables
fc = r"C:\Projects\GIS.sde\Crimes"      # Path to SDE feature class
latField = 'latitude'                   # Latitude field name
longField = 'longitude'                 # Longitude field name

# Create search cursor
with arcpy.da.SearchCursor(fc, [latField, longField]) as cursor:
    for row in cursor:
        latitude = row[0]
        longitude = row[1]
        print(latitude, longitude)
del cursor&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 07 Mar 2023 12:45:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264889#M66992</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2023-03-07T12:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to .SDE via Python and read table</title>
      <link>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264925#M66993</link>
      <description>&lt;P&gt;You might want to check out the ArcGIS Python API, too. You can query an SDE table straight into a dataframe, then use the &lt;STRONG&gt;GeoAccessor&lt;/STRONG&gt; class to convert that to a spatial dataframe, and continue working with it from there.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.features import GeoAccessor
import pandas as pd

df = pd.read_sql(
    'some_table',
    'database_connection_string'
)

sdf = GeoAccessor.from_xy(
    df,
    'lon',
    'lat',
    'spatial_reference'
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 14:37:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1264925#M66993</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-03-07T14:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to .SDE via Python and read table</title>
      <link>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1265391#M67030</link>
      <description>&lt;P&gt;Okay, thank you both! Managed this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next problem is that X and Y coordinates are into, I guess, nested list?&lt;/P&gt;&lt;P&gt;How to make those XY coordinates as variables again?&amp;nbsp;&lt;/P&gt;&lt;P&gt;For excel table that would be easy:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;path = r"G:\FOLDER\TEST_excel.xls"
df = pd.read_excel(path)

for i in range(0,len(df)):
    lat3 = df['lat'][i]
    lon3 = df['lon'][i]
    distance = haversine(lat, lon, lat2, lon2)
    dist.append(distance)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In list those XY values look's something like this [(11111111.1111, 22222222.2222) , (44444.4242, 818182388.1239), (etc...)]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;pole_xy = []
dist = []

def haversine(lon1, lat1, lon2, lat2):
    etc...

shp = os.path.join(path,r"C:\Users\name\Documents\GIS\PRJ\TEST\DB.sde\DB.NetworkData\DB.FeatureClass")

with arcpy.da.SearchCursor(shp, "SHAPE@XY") as rows:
    for row in rows:
        pole_xy.append(row[0])

for i in range(0,len(pole_xy)):
    lat3 = pole_xy['SHAPE@X']
    lon3 = pole_xy['SHAPE@Y']
    distance = haversine(lat, lon, lat3, lon3)
    dist.append(distance)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how to get lat3 = all X values and into lon3 = all Y values?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 12:37:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/connect-to-sde-via-python-and-read-table/m-p/1265391#M67030</guid>
      <dc:creator>GIS_Rookie</dc:creator>
      <dc:date>2023-03-08T12:37:30Z</dc:date>
    </item>
  </channel>
</rss>

