Skip to main content
deleted 950 characters in body
Source Link

i have done this:

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)

then you make an action dict like this:

action_dict = {'INT': 'intersection', 'ER': 'difference'}

and later

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])

THE LAST VERSION

i have done this:

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)

then you make an action dict like this:

action_dict = {'INT': 'intersection', 'ER': 'difference'}

and later

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])

THE LAST VERSION

THE LAST VERSION

added 1233 characters in body
Source Link

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 you make needed correctionsi store its results in a variable because i have to make it work?do further process with the outcomes of every operation of every line.

can you make needed corrections to make it work?

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.

edited title
Link
Bera
  • 77.8k
  • 14
  • 78
  • 188

Read file form txt and perform operation by name in Pythongeopandas

added 1013 characters in body
Source Link
Loading
Source Link
Loading