1

Here is the table:

Reproducible example

import pandas as pd

d = {'id': [1, 2, 3], 'table_na': ['ER_ASTEN_ASTOT', 'INT_ASTEN_ASTOT','ER_ASTEN_ASTTOM']}
df = pd.DataFrame(data=d)
df

which gives:

   id   table_na
0   1   ER_ASTENOT_ASTOTA
1   2   INT_ASTENOT_ASTOTA
2   3   ER_ASTENOT_ASTTOM

I would like to read a table that looks like the above that I have in a gdb using Arcpy.

For every row in the table_na field, when it sees the ER or INT it should get the Erase or Intersection function in Arcpy respectively and use the relevant shapefiles (ASTENOT,ASTOTA,etc) for the calculation in a loop sequence.


I have already done this using geopandas but the process was very slow. In similar situations using ArcPy was very efficient. I do not have much experience in ArcPy which is why I am asking this question.

Here is the code in Geopandas I used to do the process and to help you help me:

#this reads the guide ex.ER_ASTENOT_ASTOTA from a txt.

import os
import fiona
import geopandas as gpd
rootdir = r'C:\Users\user\Desktop\a' # path to root directory you walk
sfiles = {} # a dictionary with all the .shp files
for entry in os.listdir(rootdir):
    dirpath = os.path.join(rootdir, entry)
    if os.path.isdir(dirpath): 
        for file in os.listdir(dirpath): # Get all files in the subdirectories
            if file.endswith('.shp'): # If it's an .shp.
                filepath = os.path.join(dirpath, file)
                sfiles[filepath[:-4]] = gpd.read_file(filepath)


action_dict = {'INT': 'intersection', 'ER': 'difference'}
directory=input('Insert dir of the main folder')
with open(input()) as txtfile: #insert directory of txt
    x = txtfile.readlines()
for line in x:
    action, shape1, shape2 = line.split('_')
    print( action, shape1, shape2) # here line is ER_ASTOM_ASTIK or whatever line in your txt file
    if shape1 in sfiles and shape2 in sfiles:
        a=gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])
  • Portion of answer (how to read from GDB) already provided in separate question: gis.stackexchange.com/questions/299386/…. OP still needs assistance with performing specific arcpy functions based on the table. – smiller Oct 18 '18 at 14:32
  • What kind of naming convention are you using for your outputs? – smiller Oct 18 '18 at 14:43
  • the same with the rows. Example: The output of ER_ASTENOT_ASTOTA is called ER_ASTENOT_ASTOTA. – user10522417 Oct 18 '18 at 14:54
1

The below assumes you're at the stage of for row in cursor: action, shape1, shape2 = row[0].split('_') from your other question How to read line by line a column from a table in a gdb using ArcPy? -- see there for code snippet to read from the GDB table.

outname = row[0] # update naming convention / path here (default is saved in workspace, e.g. gdb). 
if shape1 in sfiles and shape2 in sfiles and action == "ER":
    # Code sample and documentation for arcpy Erase: http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/erase.htm
    in_features = shape1
    erase_features = shape2
    arcpy.Erase_analysis(in_features, erase_features, outname)
elif shape1 in sfiles and shape2 in sfiles and action == "INT":
    # Code sample and documentation for for arcpy intersect: http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/intersect.htm
    infeatures = [sfiles[shape1], sfiles[shape2]]
    arcpy.Intersect_analysis(inFeatures, outname)
else: # different action code or one of the files doesn't exist?
    print("Error processing {}".format(outname))
  • Looks promising, how the shape1 was created?Like in the question?Does it need it anything differently since it is in Arcpy? I am upvoting and will check it out extensively after i finish with another part I am working on. – user10522417 Oct 18 '18 at 14:57
  • Sorry I can't upvote yet. Low on points. I am accepting and will read it later.Thanks for the help. – user10522417 Oct 18 '18 at 15:01
  • shape1 etc. are coming from the row[0].split('_'). I assumed that your sfiles dictionary is working fine from the geopandas version of your code. Make sure you set your environment workspace (arcpy.env.workspace = pathtogdbfd). – smiller Oct 18 '18 at 15:03
  • Can you tell me how to convert this line: ` sfiles[filepath[:-4]] = gpd.read_file(filepath)` ? It's in the middle of the code? – user10522417 Oct 19 '18 at 6:33
  • Please add as a new question.I'm not familiar with geopandas. It looks like that's trying to open the shapefile as a geopandas data frame. geopandas.org/io.html. You don't need to explicitly open the file for most of the arcgis tasks. But what needs to be stored in the dictionary is likely the filename or path in the workspace. – smiller Oct 19 '18 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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