Search code examples
pythonpostgresqlpipgisplpython

plpygis can't find shapely dependency


I have a problem with the plpygis library for python. Whenever I'm trying to convert a plpygis Point type into its shapely counterpart, I get the following error:

ERROR: plpygis.exceptions.DependencyError: Dependency 'Shapely' is not available.

The code is executed within a PL/Python function in Postgres. The code looks like this:

CREATE or REPLACE FUNCTION test(n int)
  RETURNS text
AS $$
from plpygis import Geometry, Point
coordinates = []
for i in range(n):
 srows = plpy.execute("...")
 for i in srows:
  point = Geometry(i['geom'])
  if point.type != "Point":
   pass
  coordinates.append(point.shapely)
return coordinates
$$ LANGUAGE plpython3u;

It fails on the line coordinates.append(point.shapely). The shapely library is installed in the same pip env as all other packages, and everything is up-to-date. Importing shapely by itself works perfectly fine.

I took a look into the plpygis source code and it determines the error by checking if this import is working:

 from shapely.geos import lgeos, WKBWriter

I checked it manually and the import didn't work, which is then the reason for the DependencyError.

Does anyone have an idea on why this is happening?


Solution

  • plpygis author here. Can you try a more recent version of plpygis? I suspect the issue is due to missing support for Shapely 2.x, but that has now been added.