<?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 a WKT Field in Panda DataFrame or GeoDataFrame? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285264#M67578</link>
    <description>&lt;P&gt;Thanks Jeff! I recieved the following error:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the script that worked for me, but it would mean hard-coding the fields, which is not preferred:&lt;/P&gt;&lt;P&gt;#Converting feature class to panda dataframe&lt;BR /&gt;#df = self.PandasDF(FC1,"")&lt;BR /&gt;fieldList = ['ID_1','SiteNo','SiteName','Area_sqmi','Join_Flow','Shape@WKT']&lt;BR /&gt;data = [row for row in arcpy.da.SearchCursor(FC1,fieldList)]&lt;BR /&gt;guage_df = pd.DataFrame(data, columns = fieldList)&lt;/P&gt;&lt;P&gt;Ideally, I want to import all the fields into the panda dataframe with an additional field, "Shape@WKT." Any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 May 2023 16:40:55 GMT</pubDate>
    <dc:creator>tzz_12</dc:creator>
    <dc:date>2023-05-03T16:40:55Z</dc:date>
    <item>
      <title>How to Create a WKT Field in Panda DataFrame or GeoDataFrame?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1284559#M67561</link>
      <description>&lt;P&gt;I'm trying to add a WKT or object geometry field to my feature classes or panda dataframe or geodataframe. It works when I explicitly list the fields that I want in my panda dataframe using the token, &lt;A href="mailto:Shape@WKT" target="_blank"&gt;Shape@WKT&lt;/A&gt;. However, when I want to import all my fields in the feature class and also add a WKT or object geometry field, I keep getting an error problems. I know how to get WKT from a 'Shape' field, but I am not sure how to create a new column in the dataframe with that multipoint WKT values. I appreciate any feedbacks or tips to get me moving in the right direction. Thanks for your help! Here is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
from arcpy.sa import *
import pandas as pd
import shapely
import fiona
import geopandas as gp
import shapely

#convert spatial data to panda dataframe 
def PandasDF(in_table,fields):
        try:
            if fields=="":
                fieldList = [field.name for field in arcpy.ListFields(in_table)]
            else:
                fieldList = fields
                
            data = [row for row in arcpy.da.SearchCursor(in_table,fieldList)]
            fc_df = pd.DataFrame(data, columns = fieldList)
        except:
            arcpy.Message('Error: Unable to convert data to panda dataframe')
        return fc_df

#Declaring feature classes variables
output_folder = r'C:\Users\xxx'
FC_1 = os.path.join(output_folder,'Temp.gdb\FC_1')
FC_2 = os.path.join(output_folder,'Temp.gdb\\FC_2')
FC_3= os.path.join(output_folder,'Temp.gdb\\FC_3')

#Convert spatial to panda dataframe
FC1_df = PandasDF(FC_1,"")
FC2_df = PandasDF(FC_2,"")
FC3_df = PandasDF(FC_3,"")

#Obtain the wkt values
query= arcpy.SearchCursor(FC_1)
for row in query:
  the_geom=row.getValue('Shape') # Get Geometry field
  wkt = the_geom.WKT  #Not sure how to write out this wkt to df or gdf 
                      

#check if geometry field exist
geom_col = 'Geometry'
if geom_col not in df:
    #create geometry field
    geometry = FC1_df.wkt.map(shapely.wkt.loads)
    gdf = gp.GeoDataFrame(FC1_df, crs="EPSG:5070", geometry=geometry)  
else: 
    geometry = FC1_df[geom_col]
    gdf = gp.GeoDataFrame(FC1_df, crs="EPSG:5070", geometry=geometry)  

#Converting the geodataframe to GIS
FC1 = gdf.to_file(FC)
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 00:02:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1284559#M67561</guid>
      <dc:creator>tzz_12</dc:creator>
      <dc:date>2023-05-02T00:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a WKT Field in Panda DataFrame or GeoDataFrame?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285130#M67575</link>
      <description>&lt;P&gt;You should use the .da.SearchCursor, and I think if you did a loc based on matching objectid's, you can add the wkt.&amp;nbsp; This isn't tested and is a workflow idea:&lt;/P&gt;&lt;P&gt;Edit to fix the df indexing in loc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# add wkt column to df
FC1_df['wkt_column'] = 0
 
#Obtain the wkt values
with arcpy.da.SearchCursor(FC_1, ['objectid', "SHAPE@WKT"]) as sCur:
    for row in sCur:
        FC1_df.loc[FC1_df['objectid'] == row[0], 'wkt_column'] = row[1]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 14:15:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285130#M67575</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-05-03T14:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a WKT Field in Panda DataFrame or GeoDataFrame?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285264#M67578</link>
      <description>&lt;P&gt;Thanks Jeff! I recieved the following error:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the script that worked for me, but it would mean hard-coding the fields, which is not preferred:&lt;/P&gt;&lt;P&gt;#Converting feature class to panda dataframe&lt;BR /&gt;#df = self.PandasDF(FC1,"")&lt;BR /&gt;fieldList = ['ID_1','SiteNo','SiteName','Area_sqmi','Join_Flow','Shape@WKT']&lt;BR /&gt;data = [row for row in arcpy.da.SearchCursor(FC1,fieldList)]&lt;BR /&gt;guage_df = pd.DataFrame(data, columns = fieldList)&lt;/P&gt;&lt;P&gt;Ideally, I want to import all the fields into the panda dataframe with an additional field, "Shape@WKT." Any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 16:40:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285264#M67578</guid>
      <dc:creator>tzz_12</dc:creator>
      <dc:date>2023-05-03T16:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a WKT Field in Panda DataFrame or GeoDataFrame?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285331#M67580</link>
      <description>&lt;P&gt;I suspect that you'll need to swap out the 'objectid' field with whatever you have as the unique id field...&lt;/P&gt;&lt;PRE&gt;FC1_df.loc[FC1_df['objectid'] == row[0], 'wkt_column'] = row[1]&lt;/PRE&gt;&lt;P&gt;adds the 'wkt_column to the df and sets it to the @WKT value.&amp;nbsp; In my testing:&lt;/P&gt;&lt;P&gt;before:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JeffK_0-1683135838299.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/69777iA85548D54F3C8024/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JeffK_0-1683135838299.png" alt="JeffK_0-1683135838299.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JeffK_1-1683135857130.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/69778i266FB6FB214531DF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JeffK_1-1683135857130.png" alt="JeffK_1-1683135857130.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;but you can also create the column through other ways.&lt;/P&gt;&lt;P&gt;one is having a list of ['Shape@WKT'] and then add the fields from the ListFields:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;wkt = ['SHAPE@WKT']
flds = wkt + [f.name for f in arcpy.ListFields(in_table)]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;But I think you'd have to map the Shape@ to &lt;A href="mailto:Shape@WKT" target="_blank" rel="noopener"&gt;Shape@WKT&lt;/A&gt; in your PandasDF method.&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;Edit:&lt;/P&gt;&lt;P&gt;maybe do this in the function since the cursor can pull the wkt...&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 18:04:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-create-a-wkt-field-in-panda-dataframe-or/m-p/1285331#M67580</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-05-03T18:04:31Z</dc:date>
    </item>
  </channel>
</rss>

