1

I want to convert my Pandas DataFrame with a WKT geometry column to a Spatially Enabled DataFrame (SEDF) and then into a featureclass in a geodatabase; however, sometimes the geometry column in the resulting SEDF is blank. Here is an example:

This DF to SEDF conversion works just fine.

from arcgis.features import GeoAccessor
df = pd.DataFrame(['POLYGON ((-122.4879908428375 37.8580879209098, -122.48752841647929 37.8578624638935, -122.48727791959379 37.85818273662985, -122.487740345952 37.858408193646149, -122.4879908428375 37.8580879209098))'], columns = ['SHAPE'])
    sedf = GeoAccessor.from_df(
        df=df,
        sr=4326,
        geometry_column= 'SHAPE', )

This DF to SEDF conversion appears to delete the geometries in the SHAPE column:

from arcgis.features import GeoAccessor
df = pd.DataFrame(['POLYGON ((-79.841542412627334 40.511379203842651, -79.841545978234777 40.511381914693381, -79.84155175157241 40.511385469109257, -79.841557981970013 40.511388545242923, -79.841564601166084 40.5113911093916, -79.841571536639307 40.511393133461922, -79.841578712403177 40.511394595277778, -79.84158604983854 40.5113954788232, -79.8415934685549 40.511395774417863, -79.841600887271269 40.5113954788232, -79.841608224706633 40.511394595277778, -79.8416154004705 40.511393133461922, -79.841622335943725 40.5113911093916, -79.8416289551398 40.511388545242923, -79.8416351855374 40.511385469109257, -79.841640958875033 40.511381914693381, -79.841646211898762 40.5113779209382, -79.8416508870554 40.511373531600142, -79.841654933122911 40.511368794769687, -79.841658305771759 40.511363762344573, -79.841660968050491 40.511358489461088, -79.84166289079063 40.511353033890053, -79.84166405292622 40.511347455403843, -79.841664441724674 40.511341815121511, -79.84166405292622 40.511336174839187, -79.84166289079063 40.51133059635297, -79.841660968050491 40.511325140781928, -79.841658305771759 40.51131986789845, -79.841654933122911 40.511314835473343, -79.8416508870554 40.511310098642888, -79.841646211898762 40.511305709304821, -79.841640958875033 40.511301715549642, -79.8416351855374 40.511298161133759, -79.8416289551398 40.5112950850001, -79.841622335943725 40.511292520851427, -79.8416154004705 40.5112904967811, -79.841608224706633 40.511289034965237, -79.841600887271269 40.511288151419834, -79.8415934685549 40.511287855825159, -79.84158604983854 40.511288151419834, -79.841578712403177 40.511289034965237, -79.841571536639307 40.5112904967811, -79.841564601166084 40.511292520851427, -79.841557981970013 40.5112950850001, -79.84155175157241 40.511298161133759, -79.841545978234777 40.511301715549642, -79.841540725211047 40.511305709304821, -79.841536050054415 40.511310098642888, -79.8415320039869 40.511314835473343, -79.841528631338051 40.51131986789845, -79.841525969059319 40.511325140781928, -79.841525845748663 40.511325490662813, -79.841078013264578 40.51121738182654, -79.84106219300962 40.511264926460342, -79.84105393100468 40.511313564959153, -79.8410533447901 40.511362605362, -79.841060442705725 40.5114113499902, -79.841075123772256 40.511459105372943, -79.841097179127914 40.511505192113013, -79.841126294999739 40.51154895455231, -79.841162057167566 40.511589770099711, -79.841203956856987 40.511627058088308, -79.841542412627334 40.511379203842651))'], columns = ['SHAPE'])
sedf = GeoAccessor.from_df(
    df=df,
    sr=4326,
    geometry_column= 'SHAPE', )

Is there a way to get a valid SEDF in this scenario? I've checked the WKT geometry with Shapely's .is_valid method and it's valid. Alternatively, is there a way to get from WKT to a geodatabase without the SEDF intermediary?

I'm using ArcGIS version 1.9.1 and ArcPy 2.9. I recently updated my ArcGIS Pro to v2.9. I did not have this issue with 2.8 and its associated packages.

1 Answer 1

0

I found a messy workaround using Shapely to export to a shapefile then using ArcPy to move the shapefile into my desired geodatabase. If anyone has an ArcGIS solution I'd love to see it.

from osgeo import ogr, osr
from shapely.geometry import Polygon
import shapely.wkt
import arcpy

wkt_poly = 'POLYGON ((-79.841542412627334 40.511379203842651, -79.841545978234777 40.511381914693381, -79.84155175157241 40.511385469109257, -79.841557981970013 40.511388545242923, -79.841564601166084 40.5113911093916, -79.841571536639307 40.511393133461922, -79.841578712403177 40.511394595277778, -79.84158604983854 40.5113954788232, -79.8415934685549 40.511395774417863, -79.841600887271269 40.5113954788232, -79.841608224706633 40.511394595277778, -79.8416154004705 40.511393133461922, -79.841622335943725 40.5113911093916, -79.8416289551398 40.511388545242923, -79.8416351855374 40.511385469109257, -79.841640958875033 40.511381914693381, -79.841646211898762 40.5113779209382, -79.8416508870554 40.511373531600142, -79.841654933122911 40.511368794769687, -79.841658305771759 40.511363762344573, -79.841660968050491 40.511358489461088, -79.84166289079063 40.511353033890053, -79.84166405292622 40.511347455403843, -79.841664441724674 40.511341815121511, -79.84166405292622 40.511336174839187, -79.84166289079063 40.51133059635297, -79.841660968050491 40.511325140781928, -79.841658305771759 40.51131986789845, -79.841654933122911 40.511314835473343, -79.8416508870554 40.511310098642888, -79.841646211898762 40.511305709304821, -79.841640958875033 40.511301715549642, -79.8416351855374 40.511298161133759, -79.8416289551398 40.5112950850001, -79.841622335943725 40.511292520851427, -79.8416154004705 40.5112904967811, -79.841608224706633 40.511289034965237, -79.841600887271269 40.511288151419834, -79.8415934685549 40.511287855825159, -79.84158604983854 40.511288151419834, -79.841578712403177 40.511289034965237, -79.841571536639307 40.5112904967811, -79.841564601166084 40.511292520851427, -79.841557981970013 40.5112950850001, -79.84155175157241 40.511298161133759, -79.841545978234777 40.511301715549642, -79.841540725211047 40.511305709304821, -79.841536050054415 40.511310098642888, -79.8415320039869 40.511314835473343, -79.841528631338051 40.51131986789845, -79.841525969059319 40.511325140781928, -79.841525845748663 40.511325490662813, -79.841078013264578 40.51121738182654, -79.84106219300962 40.511264926460342, -79.84105393100468 40.511313564959153, -79.8410533447901 40.511362605362, -79.841060442705725 40.5114113499902, -79.841075123772256 40.511459105372943, -79.841097179127914 40.511505192113013, -79.841126294999739 40.51154895455231, -79.841162057167566 40.511589770099711, -79.841203956856987 40.511627058088308, -79.841542412627334 40.511379203842651))'

# Here's an example Shapely geometry
poly = shapely.wkt.loads(wkt_poly)
sr = osr.SpatialReference()
sr.ImportFromEPSG(4326)

# Now convert it to a shapefile with OGR    
driver = ogr.GetDriverByName('Esri Shapefile')
ds = driver.CreateDataSource('temp_data\\my.shp')
layer = ds.CreateLayer('', sr, ogr.wkbPolygon)
# Add one attribute
layer.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
defn = layer.GetLayerDefn()

# Create a new feature (attribute and geometry)
feat = ogr.Feature(defn)
feat.SetField('id', 123)

# Make a geometry, from Shapely object
geom = ogr.CreateGeometryFromWkb(poly.wkb)
feat.SetGeometry(geom)

layer.CreateFeature(feat)
feat = geom = None  # destroy these

# Save and close everything
ds = layer = feat = geom = None
 
# Set environment settings
arcpy.env.workspace = '<workspace_dir>'
 
# Set local variables
in_features = ['my.shp']
out_location = '<gdb_dir>'
 
# Execute FeatureClassToGeodatabase
arcpy.FeatureClassToGeodatabase_conversion(in_features, out_location)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.