9

I am trying to perform the function join attributes by location as found on the QGIS menu Vector>Data Management Tools. I am seeking an open source python option for this. I know arcpy has a spatial join function but I'm trying to perform this outside of the ESRI environment.

2
  • 1
    I would suggest to look at the source code of the actual Join attributes by location command from the fTools plugin: doSpatialJoin.py, particularly the compute() method. Shouldn't be too hard to eliminate any UI code from that and strip it down to a plain python function. Commented Oct 25, 2013 at 22:32
  • Hi i have a little bit different problem , i want to check if there is a joint between two layer!I am seeking an open source python option for this. I want to test if the s.join function has been used and I'm trying to perform this outside of the ESRI environment.. Can anyone help me please ! Commented Nov 30, 2017 at 11:27

2 Answers 2

9

You may want to take a look at Shapely and Fiona. Fiona is a wrapper for gdal to make spatial file import and export easy. Shapely provides geometry functionality. Here is a very simple example to give you the idea. It joins polygon attributes to all points within that polygon.

The example data I have used are these polygons and these points.

import fiona from shapely.geometry import shape from copy import deepcopy with fiona.open("planning_neighborhoods.shp", "r") as n: with fiona.open("Schools_Private_Pt.shp", "r") as s: # create a schema for the attributes outSchema = deepcopy(s.schema) outSchema['properties'].update(n.schema['properties']) with fiona.open ("Schools_withNbhd.shp", "w", s.driver, outSchema, s.crs) as output: for school in s: for neighborhood in n: # check if point is in polygon and set attribute if shape(school['geometry']).within(shape(neighborhood['geometry'])): school['properties']['neighborho'] = neighborhood['properties']['neighborho'] # write out output.write({ 'properties': school['properties'], 'geometry': school['geometry'] }) 
8
  • Thanks @cengel. This looks like it will put me on the right track! I'm actually interested in the join with lines and polygons (specifically finding where rivers intersect model cells) and I think that will work following your example. Commented Oct 28, 2013 at 15:26
  • @cengel Do qgis stations using plugins employing these methods require installation of modules and gdal? Commented May 5, 2014 at 22:36
  • @user25976 sorry, not sure I quite understand your question. My code example is a standalone python script. Both fiona and shapely require gdal. Commented May 7, 2014 at 0:36
  • @cengel Excuse me, let me clarify (I'm new to programming). Regarding standalone python scripts: you mean that a plugin written with fiona and shapely imports can be used by QGIS users even if they don't have python or the modules installed on their computer? Commented May 7, 2014 at 0:47
  • @user25976 They do need the modules installed on their computer. See for example here Commented May 7, 2014 at 6:16
3

Although still a bit rough around the edges, especially when it comes to documentation and examples, but geopandas' future looks bright. It basically combines the power of pandas dataframes with geospatial capabilities of shapely.

the function you look for is called sjoin

Make sure your machine/instance has enough memory to perform the operation

import geopandas as gpd import pandas as pd import os gdfLeft = gpd.read_file(os.path.join(PATH,INPUT_FILE_NAME_1)) gdfRight = gpd.read_file(os.path.join(PATH,INPUT_FILE_NAME_2)) gdfJoined = gpd.sjoin(gdfLeft, gdfRight, how="left", op='intersects') 
1
  • This piece of code performs the spatial join but the attributes of the joined shapefile is empty. Any pointers? Commented Oct 16, 2019 at 11:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.