<?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: Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor() in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551453#M43072</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You cannot write directly from Desktop to enterprise tables that are not register in the Geodatabase.&lt;/P&gt;&lt;P&gt;You cannot start edit in ArcMap and you cannot do it in arcpy too.&lt;/P&gt;&lt;P&gt;You must register the table or use external software (pyodbc)&lt;/P&gt;&lt;P&gt;Have fun&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 23 Sep 2020 05:47:38 GMT</pubDate>
    <dc:creator>ModyBuchbinder</dc:creator>
    <dc:date>2020-09-23T05:47:38Z</dc:date>
    <item>
      <title>Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor()</title>
      <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551452#M43071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to do what seems to be a simple operation:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;1. Iterate through a feature class&lt;BR /&gt;&amp;nbsp;2. Insert found rows to an MSSQL spatial table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However I keep getting the following error &lt;STRONG&gt;No support for this geometry type&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Strangely I get this error even if I only try to insert and int or string with no shapes involved. It seems that Arc does not understand the table which is strange because I can run select operations and Describe() on the table without issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does Arc support inserting spatial data to MSSQL in this manner?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the code snipped below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;selected_grid_fc&lt;/STRONG&gt;: File Geodatabase Feature Class, Geometry Type: Polygon&lt;BR /&gt;&lt;STRONG&gt;selected_grids_mssql&lt;/STRONG&gt;: MSSQL table with a geometry column. The geometry column stores Polygon grid squares from a 100m Fishnet in EPSG:2157&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;try&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# Start an edit session. Must provide the workspace.&lt;/SPAN&gt;
    edit_session &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Editor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# Edit session is started without an undo/redo stack for versioned data (for second argument, use False for unversioned data)&lt;/SPAN&gt;
    edit_session&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;startEditing&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token boolean"&gt;False&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token boolean"&gt;False&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# Start an edit operation&lt;/SPAN&gt;
    edit_session&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;startOperation&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

    search_cols &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'OBJECTID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'GridId'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'SHAPE@'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
    insert_cols &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'ObjectId'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'GridId'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'Shape'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# If I user 'Shape@', the error is the same.&lt;/SPAN&gt;
    selected_grids_mssql &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;path&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;self&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;sde_path&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'dbo.SelectedGrids'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;selected_grid_fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; search_cols&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; search_cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        &lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;InsertCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;selected_grids_mssql&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; insert_cols&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; insert_cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
            &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; search_cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
                insert_cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;insertRow&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; Exception &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; ex&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddError&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Something went wrong."&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;raise&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# Stop the edit operation.&lt;/SPAN&gt;
    edit_session&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;stopOperation&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# Stop the edit session and save the changes&lt;/SPAN&gt;
    edit_session&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;stopEditing&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token boolean"&gt;True&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddMessage&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Import successful."&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For reference, the following works fine via pyodbc, but I wanted to see if it could be done without the need for an external library.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;selected_grids &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
selected_grid_cols &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'OBJECTID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'GridId'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'SHAPE@WKT'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;selected_grid_fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; selected_grid_cols&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        selected_grids&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; Helpers&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;db_connect&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;cursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"INSERT INTO [dbo].[SelectedGrids](ObjectId, GridId, Shape) VALUES (?,?,geometry::STGeomFromText(?, 2157));"&lt;/SPAN&gt;
    cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;executemany&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;query&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; selected_grids&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:51:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551452#M43071</guid>
      <dc:creator>BarryO__Neill</dc:creator>
      <dc:date>2021-12-11T23:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor()</title>
      <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551453#M43072</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You cannot write directly from Desktop to enterprise tables that are not register in the Geodatabase.&lt;/P&gt;&lt;P&gt;You cannot start edit in ArcMap and you cannot do it in arcpy too.&lt;/P&gt;&lt;P&gt;You must register the table or use external software (pyodbc)&lt;/P&gt;&lt;P&gt;Have fun&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2020 05:47:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551453#M43072</guid>
      <dc:creator>ModyBuchbinder</dc:creator>
      <dc:date>2020-09-23T05:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor()</title>
      <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551454#M43073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks very much for letting me know. I suspected as much but the error message was unclear and I had wondered if I was making a mistake in some manner. Not to worry, I will continute with pyODBC.&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:15:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551454#M43073</guid>
      <dc:creator>BarryO__Neill</dc:creator>
      <dc:date>2020-09-23T14:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor()</title>
      <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551455#M43074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you intend to only create read-only feature classes that do not need any ESRI Geodatabase functionality, then I second &lt;A href="https://community.esri.com/migrated-users/3417"&gt;Mody Buchbinder&lt;/A&gt;‌ suggestion of using &lt;A href="https://github.com/mkleehammer/pyodbc"&gt;pyodbc&lt;/A&gt; to insert the records into the MSSQL table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This means you need to replace the nested &lt;STRONG&gt;arcpy.InsertCursor&lt;/STRONG&gt; with an appropriate pyodbc Cursor object.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have written a Python multi-threaded application doing just that, and successfully used it to insert &amp;gt; 100M(!) records from a File Geodatabase into a PostgreSQL table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that pyodbc is available in the ArcGIS Pro Conda environment. Just select it from there and install it to start working with it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By the way, I recommend inserting as WKT (Well Know Text) geometries using the "SHAPE@WKT" token, and not use pyodbc "parameter binding" but just concatenate all fields to transfer into a long INSERT string statement. I have seen some instabilities with WKB and using parameter binding on ultra large datasets, that maybe related to some known unresolved issues with pyodbc and parameter binding (but this is speculation from my side at this point in time... you can try WKB and parameter binding on smaller datasets and see if it works for you).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:31:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/551455#M43074</guid>
      <dc:creator>MarcoBoeringa</dc:creator>
      <dc:date>2020-09-23T14:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Insert geometry from ESRI feature class to MSSQL spatial table using ArcPy InsertCursor()</title>
      <link>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/1060278#M61183</link>
      <description>&lt;P&gt;I know this is an older post, but I just wanted to reply that the suggestion above has serious security implications.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Writing SQL statements in Python (or any language) without binding input parameters opens your database and tool to SQL injection vulnerabilities.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Parameter binding is included in libraries for a reason. I would always recommend to use them.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 05:03:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-geometry-from-esri-feature-class-to-mssql/m-p/1060278#M61183</guid>
      <dc:creator>nzjs</dc:creator>
      <dc:date>2021-05-21T05:03:03Z</dc:date>
    </item>
  </channel>
</rss>

