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, Shape@WKT. 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:
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\xyz'
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)