Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.87k stars 1.1k forks source link

empty geometry error when applying spatially enabled dataframe's to_featureclass #679

Closed phill0304 closed 4 years ago

phill0304 commented 4 years ago

Pro 2.5. When I try to execute to_featureclass on my spatially enabled dataframe I get the following run time error: The operation was attempted on an empty geometry.

I have run a script which checks for empty geometries in my dataframe and I don't detect any.

To Reproduce Steps to reproduce the behavior:

db = r'C:/sandbox/testout.gdb'
fc_name = os.path.join(db,'test_usda2')
arcpy.env.overwriteOutput=True
df_join = pd.merge(state_df, df_csv, how='right', left_on='NAME', right_on='State')
#check for null and empty geometries
for index, row in df_join.iterrows():
    row = row.copy()
    if not pd.isnull(row["SHAPE"]):
        if row["SHAPE"].is_empty:
            print('empty geometry')
            df_join.loc[index, "SHAPE"] = -999999
    else:
         df_join.loc[index, "SHAPE"] = -999999
df_join.drop(df_join[df_join["SHAPE"] == -999999].index, inplace=True)
df_join.spatial.to_featureclass(location=fc_name)

error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-333-4608aea7f523> in <module>
     13          df_join.loc[index, "SHAPE"] = -999999
     14 df_join.drop(df_join[df_join["SHAPE"] == -999999].index, inplace=True)
---> 15 df_join.spatial.to_featureclass(location=fc_name)

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py in to_featureclass(self, location, overwrite)
   1923         return to_featureclass(self,
   1924                                location=location,
-> 1925                                overwrite=overwrite)
   1926     #----------------------------------------------------------------------
   1927     def to_table(self, location, overwrite=True):

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_io\fileops.py in to_featureclass(geo, location, overwrite, validate)
    610 
    611         array = np.array([], np.dtype(dtypes))
--> 612         arcpy.da.ExtendTable(fc, oidfld, array, join_dummy, append_only=False)
    613 
    614         # 3. Insert the Data

RuntimeError: The operation was attempted on an empty geometry.

Screenshots If applicable, add screenshots to help explain your problem.

Expected behavior Exports to feature class successfully

Platform (please complete the following information):

Additional context Add any other context about the problem here, attachments etc.

achapkowski commented 4 years ago

@phill0304 can you provide the FGDB for this issue?

achapkowski commented 4 years ago

Also don't set SHAPE columns values to -999999

phill0304 commented 4 years ago

for_andrew.zip

@achapkowski attached gdb with states and the csv I'm working with. The csv needs some cleanup before it can be joined to the states. Also attached the workbook file with code that does all this plus creating the join and trying to export the file to fc. There are a couple paths that need to be reset to run...

Thanks -Patrick

phill0304 commented 4 years ago

image @achapkowski just a little more info: screenshot shows the result of is_empty run on the spatially enabled dataframe GeoSeriesAccessor -- all False -- so wondering if something else is wrong with data.

phill0304 commented 4 years ago

@achapkowski I resolved this by fixing a field name that had illegal characters. Still the empty geometry error is a bit worrisome since I had no issues with my geometry.

ATL2001 commented 4 years ago

I ran into this issue yesterday as well, this issue discussion helped point me in the right direction to resolving it, thanks! My issue turned out to be that I had a few column names that started with a number which isn't allowed in a gdb. Once I renamed the columns it went in with no issues :) . -aTL

achapkowski commented 4 years ago

At the latest release, we added an optional sanitize column parameter when exporting data.

nzjs commented 3 years ago

Can I request that improved error handling around this be added to the API? I recently ran into the above along with a separate issue #606, both errors returned were unrelated to the underlying problem.

For this issue, some of my dataframe column names were using characters unsupported in a GDB. For issue #606, some of my dataframe columns were unsupported dtypes.

I know it isn't critical, but it might be helpful for future users of the library.

robhaley commented 2 years ago

Can I request that improved error handling around this be added to the API? I recently ran into the above along with a separate issue #606, both errors returned were unrelated to the underlying problem.

For this issue, some of my dataframe column names were using characters unsupported in a GDB. For issue #606, some of my dataframe columns were unsupported dtypes.

I know it isn't critical, but it might be helpful for future users of the library.

I echo this. I fixed the problem with the above, but the error provided is pretty misleading.

achapkowski commented 2 years ago

@robhaley the problem, especially with pyshapefile is that it's the one passing these errors, and we just pass it along.