Skip to main content
5 of 5
deleted 950 characters in body

Read file form txt and perform operation by name in geopandas

I have a txt that contains in each line a sentence like this:

INT_ASTIK_EAS
ER_ASTOM_ASTIK
and so on.

I want to ask for a directory of the main folder that contains the subfolders that each contain the ASTIK.shp, then the EAS.shp, then the ASTOM.shp, etc

INT means to perform an intersection with this code where in this case should involve ASTIK and EAS shapefiles.

inte_s=gpd.overlay(ASTIK,EAS,how='intersection')

and check if it has no info in the attribute table. Then go the to the other line and catch on the meaning of the line and perform erase between the two named shapefiles. Find them and perform it.How do i do that without making a lot of conditions and line by line: like

directory=input('Insert dir of the main folder')

    If 'INT' in firstline of txt in directory and ASTOM and EAS:
    perform interesection between those.

i don't think the only way is to create hundreads of if conditions that will probably never be valid. How should i do it?

THE LAST VERSION

import os
import fiona
rootdir = r'C:\Users\user\Desktop\a' # path to your 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]] = fiona.open(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('_')  # here line is ER_ASTOM_ASTIK or whatever line in your txt file
    if shape1 in sfiles and shape2 in sfiles:
        gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])

it doesn't probably perform the intersection or difference. how can i store its results in a variable because i have to do further process with the outcomes of every operation of every line.