2

Is it somehow possible to make several spatial joins with GeoPandas without always saving the outcome and loading it again?

I have to join like six different Esri Shapefiles and it takes really long to save them every time.

My code:

import geopandas


#first join
grid = geopandas.GeoDataFrame.from_file("inputpath")
dem = geopandas.GeoDataFrame.from_file("inputpath2")
sjoin = geopandas.sjoin(grid, dem, how = "left")
sjoin.to_file("outputpath", driver='ESRI Shapefile')

#second join with outcome of first join
griddem = geopandas.GeoDataFrame.from_file("outputpath")
slope = geopandas.GeoDataFrame.from_file("inputpath3")
sjoin2 = geopandas.sjoin(griddem, slope, how = "left")  
sjoin2.to_file("outputpath2", driver='ESRI Shapefile')

I already tried to directly use the outcome of the first join for the second join like I did in the following code:

import geopandas


#first join
grid = geopandas.GeoDataFrame.from_file("inputpath")
dem = geopandas.GeoDataFrame.from_file("inputpath2")
sjoin = geopandas.sjoin(grid, dem, how = "left")

#second join with outcome of first join
slope = geopandas.GeoDataFrame.from_file("inputpath3")
sjoin2 = geopandas.sjoin(sjoin, slope, how = "left")  

But I got following error code:

File "/anaconda2/envs/IP3/lib/python2.7/site-packages/geopandas/tools/sjoin.py", line 51, in sjoin " joined".format(index_left, index_right)) ValueError: 'index_left' and 'index_right' cannot be names in the frames being joined

Is there any solution to make this work efficient?

| improve this question | | | | |
1

Geopandas won't let you create duplicate columns for index_left and index_right. Unless there's a particular reason that you need the original indexes it's simple enough to drop the duplicates between each step, e.g.

sjoin.drop('index_left', axis=1, inplace=True)
sjoin.drop('index_right', axis=1, inplace=True)

Given you have several datasets to join you can even put this in a loop and use the python reduce (note, this in Python 2.7 this you don't need the functools import) method to put the joins and subsequent column removals in a loop:

from functools import reduce  # python 3 only

import geopandas as gpd

def join_reducer(left, right):
    """
    Take two geodataframes, do a spatial join, and return without the
    index_left and index_right columns.
    """
    sjoin = gpd.sjoin(left, right, how='left')
    for column in ['index_left', 'index_right']:
        try:
            sjoin.drop(column, axis=1, inplace=True)
        except ValueError:
            # ignore if there are no index columns
            pass
    return sjoin

input_frames = [
    gpd.read_file(path) for 
    for path in ['inputpath1', 'inputpath2', 'inputpath3', ...]
]
sjoin = reduce(join_reducer, input_frames)
sjoin.to_file('outputpath')
| improve this answer | | | | |
  • Worked Perfect! Do you also know how it is possible that the output shp file is about 350mb and the dbf file about 800mb in size, seems too huge? The 7 join input shp files are just between 1 and 20 mb big? – Ceppy Dec 22 '17 at 13:46

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.