<?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: How to create feature class from SQL Server preserving data types? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-create-feature-class-from-sql-server/m-p/1294470#M67757</link>
    <description>&lt;P&gt;Take a look at sqlalchemy and pandas to convert the sql result to a dataframe that you can then use to create the point featureclass.&amp;nbsp; There are several methods that you can use with the dataframe like the insert cursor or using arcgis spatially enabled dataframe and its to_featureclass method- for example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sqlalchemy
from sqlalchemy import text
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor

engine = sqlalchemy.create_engine(f"use the sqlalchemy docs for the sql string here")

with engine.begin() as conn:
    tble_data = pd.read_sql_query(sql=text(query), con=conn)

# Convert a dataframe (df) to a spatial dataframe (sdf) with point geometrie
sdf = pd.DataFrame.spatial.from_xy(df=tble_data, x_column='X', y_column='Y', sr=4364)

# Save your feature class in a gdb (or in .shp,...)
sdf.spatial.to_featureclass(location=r'C:\MyDB.gdb\MyFeature', overwrite=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2023 13:36:19 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2023-05-31T13:36:19Z</dc:date>
    <item>
      <title>How to create feature class from SQL Server preserving data types?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-feature-class-from-sql-server/m-p/1294271#M67751</link>
      <description>&lt;P&gt;An explanation of the background:&lt;BR /&gt;We have ambulance data in a sql server relational database. It has longitude and latitude data for emergency incidents and the goal is to bring this data into ArcGIS Pro 3.x as a point feature class. In other words, I want to programmatically query data out of our relational database, the data would flow into a file geodatabase preserving the date time fields, then map the XY fields into a point feature class. The code I wrote serves me well, but I want to create a geoprocessing tool for other users.&lt;/P&gt;&lt;P&gt;The ideal pseudocode would look like:&lt;BR /&gt;1) query SQL Server and select longitude, latitude and date/time fields and other data.&lt;BR /&gt;2) flow the results of this query to a file geodatabase preserving the date/time fields. In other words, string fields remain as strings, floats remain as floats and date/times remain as date/times. The data is now in a geodatase. Now I can do calculations on the date time stamps such as response intervals.&lt;BR /&gt;3) Create a point feature class using this data. The data is now points that can be mapped out. Now I can do spatial analysis on the points such as areas of growth, or clustering analysis.&lt;/P&gt;&lt;P&gt;My current solution is to use PyODBC to query the database, export the results to a comma separated values file, then use arcpy.management.XYTableToPoint() to create the points feature class.&lt;BR /&gt;My question is:&lt;BR /&gt;1.) First, is PyODBC the best way to do this? (In my research I have found ArcSDE but reading the ESRI documentation makes it seem like SDE is all about geodatabases, and my SQL Server database is not spatially enabled.)&lt;BR /&gt;2.) Second, exporting to a .csv file just seems very inefficient, Can I skip this intermediate step? How can the data flow from SQL Server to the file geodatabase and preserve the original date time formatting?&lt;/P&gt;&lt;P&gt;The code I am using is shown below. This gives you an idea of what I am trying to accomplish.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-
# ArcGIS Pro 3.1.1
# Python 3.7

import pandas as pd
import pyodbc
import arcpy

cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=my_server; DATABASE=my_database; Trusted_Connection=yes')
cursor = cnxn.cursor()
query = """
SELECT
  Incident_Primary_Key
, Longitude
, Latitude
, Date_of_Emergency
FROM Emergency_Incidents
WHERE Date_of_Emergency &amp;gt; 'january 1, 2023'
"""
df = pd.read_sql(query, cnxn) # execute the query and put results into a Pandas dataframe.
df.to_csv('c:\\emergency_incidents.csv', sep=',', encoding='utf-8') # export the results of the query to a csv file. How to skip this step?

print(df.head(n=10)) # check the data

in_table = r"C:\emergency_incidents.csv" # open the csv file and use it as input data
out_feature_class = r"C:\Scratch_v2.gdb\emergency_incidents" # my file geodatabase for the project
x_coords = "Longitude"
y_coords = "Latitude"
coordinate_system = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision'

# make points out of the data
arcpy.management.XYTableToPoint(in_table = in_table, out_feature_class = out_feature_class, x_field = x_coords, y_field = y_coords, z_field = None, coordinate_system = coordinate_system)

# close the cursor and connection&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 20:47:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-feature-class-from-sql-server/m-p/1294271#M67751</guid>
      <dc:creator>KulpanowskiDavid</dc:creator>
      <dc:date>2023-05-30T20:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to create feature class from SQL Server preserving data types?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-feature-class-from-sql-server/m-p/1294470#M67757</link>
      <description>&lt;P&gt;Take a look at sqlalchemy and pandas to convert the sql result to a dataframe that you can then use to create the point featureclass.&amp;nbsp; There are several methods that you can use with the dataframe like the insert cursor or using arcgis spatially enabled dataframe and its to_featureclass method- for example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sqlalchemy
from sqlalchemy import text
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor

engine = sqlalchemy.create_engine(f"use the sqlalchemy docs for the sql string here")

with engine.begin() as conn:
    tble_data = pd.read_sql_query(sql=text(query), con=conn)

# Convert a dataframe (df) to a spatial dataframe (sdf) with point geometrie
sdf = pd.DataFrame.spatial.from_xy(df=tble_data, x_column='X', y_column='Y', sr=4364)

# Save your feature class in a gdb (or in .shp,...)
sdf.spatial.to_featureclass(location=r'C:\MyDB.gdb\MyFeature', overwrite=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 13:36:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-feature-class-from-sql-server/m-p/1294470#M67757</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-05-31T13:36:19Z</dc:date>
    </item>
  </channel>
</rss>

