0

Is there a way to input a GeoPandas DataFrame as FeatureClass into a GDB without using the arcgis module?

I managed to do it in Python (3.7.11) with:

import arcgis # need to eliminate!!!
import arcpy
import os
import pandas as pd
import geopandas as gpd

gdb=r'...\Default.gdb' my_df = gpd.GeoDataFrame(r.my_df) my_df.geometry = gpd.points_from_xy(x=pd.DataFrame(my_df.geometry.to_list())[0], y=pd.DataFrame(my_df.geometry.to_list())[1], crs=4326)

my_feature = my_df.spatial.from_geodataframe(my_df[0]) # .spatial.from_geodataframe relies on the arcgis module

...

arcpy.FeatureClassToFeatureClass_conversion(in_features=my_feature, out_path=gdb, out_name="feature_name")

This relies on the arcgis package which I need to avoid though.

As arcpy should be able to handle most everything arcgis did, I hoped to replicate the results from the code above but was unsuccessful.

Using ArcGISBridge in R it is also possible to solve this but as I am confined, using R 4.1.3, this is also not an optimal solution.

Here is a dput() sample of the R data table (it is moved to python using reticulate version 1.22 on a windows 10 OS)

my_df = structure(list(events = c(8L, 7L, 6L, 5L, 5L), geometry = structure(list(
    structure(c(16.3378451408185, 48.1906416102795), class = c("XY", 
    "POINT", "sfg")), structure(c(16.3600388676805, 48.2066526410091
    ), class = c("XY", "POINT", "sfg")), structure(c(16.3634777044012, 
    48.2040462780181), class = c("XY", "POINT", "sfg")), structure(c(16.3421793940926, 
    48.2171604659865), class = c("XY", "POINT", "sfg")), structure(c(16.3389856762508, 
    48.1969142650631), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
"sfc"), precision = 0, bbox = structure(c(xmin = 16.3378451408185, 
ymin = 48.1906416102795, xmax = 16.3634777044012, ymax = 48.2171604659865
), class = "bbox"), crs = structure(list(input = "EPSG:4326", 
    wkt = "GEOGCRS[\"WGS 84\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(NA, 
5L), class = "data.frame")
D.J
  • 101
  • 2
  • 1
    If you only need a point feature class, NumPyArrayToFeatureClass might work for you. https://pro.arcgis.com/en/pro-app/2.8/arcpy/data-access/numpyarraytofeatureclass.htm – Brennan Jul 11 '22 at 13:36
  • @Brennan interesting idea! will check. not really a fully usable solution for my problem as i do need to keep the information/structure of multiple columns (excluded from the sample) to display in the map(s) – D.J Jul 11 '22 at 14:11
  • 1
    In that case, you could also export NumPyArrayToTable to get your fields as a table, and then use Join Field to add them back to the feature class. I've used this before to help manage dtypes when using NumPy to table. https://gis.stackexchange.com/a/428284/170001 – Brennan Jul 11 '22 at 14:34
  • you could write out my_df to a shapefile and the use subprocess.call() along with OGR as is shown in the answer here: https://gis.stackexchange.com/questions/121051/can-ogr2ogr-write-data-to-a-file-geodatabase-feature-dataset – rickD Jul 15 '22 at 02:35
  • subprocess.call('ogr2ogr', '-f', '"FileGDB"', 'mygdb.gdb' '~/PathTo/my_df.shp', '-lco', 'FEATURE_DATASET="esri_features"') – rickD Jul 15 '22 at 02:41
  • @rickD, yes if i wanted to create shapefiles as an intermediary step this would be a solution. the whole point of my task is to avoid that though. else i could just make may arcgis pro layout point to a shapefie and overwrite it - that way i wouldn't need to do anything programmatically in python in the first place. the problem is, that anything which relies on a folder structure is (imo) too easy to break (especially on a system i do not have admin access to and where i can't gurantee only "gis/R/python-savy" users will use my programm). – D.J Jul 15 '22 at 04:11

0 Answers0