Panda sdf.spatial.to_featureclass fails to write Featureclass but writes shapefile

3341
7
Jump to solution
06-11-2020 02:56 PM
BillChappell
Occasional Contributor II

Below is the abbreviated copy of my code. I take an Excel file, read into a panda dataframe, convert that to a spatial dataframe and write it out as a point featureclass. If I write a shapefile it works but when I write to a File GDB it writes nothing and doesn't even show an error.

My Code:

import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor

df = pd.read_excel (filename)


# convert Pandas dataframe to Spatial Data Frame
sdf= pd.DataFrame.spatial.from_xy(df=df,
x_column='Structure_Lng',
y_column='Structure Lat',
sr=4326)

# show first 5 records in SDF
print(sdf.head()) # looks good

#Convert SDF to Featureclass. ### This fails
sdf.spatial.to_featureclass(location="C:/temp/test.fdb/myPts", overwrite=True);

# this works
sdf.spatial.to_featureclass(location="C:/temp/myPts.shp", overwrite=True)

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BillChappell
Occasional Contributor II

Got it. While I was running my script thru Python 3.6.8 IDLE, I had never logged into Pro on this laptop. Once I logged in, it threw an error and I found some Lat/Lngs were actually null throwing a bad geometry error. By making a shapefile these were just ignored, to a featureclass these were an issue. So .fillna(0) fixed that.

My working script is attached as a text file.

Thanks for the help. 

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

"C:/temp/test.fdb

do you mean

"C:/temp/test.gdb


... sort of retired...
BillChappell
Occasional Contributor II

It was gdb on my code but I must have typed it wrong when posting. Either way, no featureclass. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

I can only find references to

arcgis.features module — arcgis 1.8.0 documentation 

to convert converts a SpatialDataFrame to a feature class


... sort of retired...
0 Kudos
BillChappell
Occasional Contributor II

I'm converting a panda DF to a spatial DF.:

# convert Pandas dataframe to Spatial Data Frame
sdf= pd.DataFrame.spatial.from_xy(df=df,
x_column='Structure_Lng',
y_column='Structure Lat',
sr=4326)

I got that from Esri Training, so it must be right....
https://www.esri.com/training/catalog/5ea8a5c359bcad254d2eb63b/arcgis-api-for-python%3A-getting-to-k...

Now to export it as a shapefile. :

# this works so my spatial dataframe should be ok?
sdf.spatial.to_featureclass(location="C:/temp/myPts.shp", overwrite=True)

Trying a FC in a File gdb 

#Convert SDF to Featureclass. ### This fails
sdf.spatial.to_featureclass(location="C:/temp/test.fdb/myPts", overwrite=True);

0 Kudos
DanPatterson
MVP Esteemed Contributor

Introduction to the Spatially Enabled DataFrame | ArcGIS for Developers 

I don't see where you imported "arcpy"

The ArcGIS API for Python installs on all macOS and Linux machines, as well as those Windows machines not using Python interpreters that have access to ArcPy will only be able to write out to shapefile format with the to_featureclass method. Writing to file geodatabases requires the ArcPy site-package.

The arcgis module performs checks to see whether arcpy is imported, If it is, then you can write to featureclasses, If it isn't then it uses the "shapely" module which can only write shapefiles.

You can examine all the revealed code in your installation path

C:\Path_to_ArcGISPro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis

Your particular method is described in

C:\Path_to_ArcGISPro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\_data\geodataset\geodataframe.py

The SpatialDataFrame class has the to_featureclass method which imports to_featureclass from the _io module

C:\...\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\_data\geodataset\io\fileops.py

The  to_featureclass method, checks to see if arcpy is installed (around line 400), if so, it can create a featureclass in a gdb.

So in short, just see if adding

import arcpy

to your script allows you to save to a featureclass


... sort of retired...
0 Kudos
BillChappell
Occasional Contributor II

Got it. While I was running my script thru Python 3.6.8 IDLE, I had never logged into Pro on this laptop. Once I logged in, it threw an error and I found some Lat/Lngs were actually null throwing a bad geometry error. By making a shapefile these were just ignored, to a featureclass these were an issue. So .fillna(0) fixed that.

My working script is attached as a text file.

Thanks for the help. 

SanjaykumarRajbhar
New Contributor II

Thanks ..!

How to transform data from SR 4326 to 27700(BNG)?

0 Kudos