2

I have a cube stored as geometry in a PostGIS DB. I get the coordinates using the following SQL:

select ST_AsEWKT(geom) from polyhedral.vol_1 where id = 1

This presents the following result.

POLYHEDRALSURFACE(
  ((0 1 0,0 0 0,1 0 0,1 1 0,0 1 0)),
  ((1 -0 1,0 0 1,0 1 1,1 1 1,1 -0 1)),
  ((1 0 0,1 -0 1,1 1 1,1 1 0,1 0 0)),
  ((0 0 0,0 0 1,1 -0 1,1 0 0,0 0 0)),
  ((0 1 0,0 1 1,0 0 1,0 0 0,0 1 0)),
  ((0 1 1,0 1 0,1 1 0,1 1 1,0 1 1))
)

would like to use this within the Open3D library. For .ply files it is simple mesh = read_triangle_mesh(filename.ply)

Any ideas about how I would get the PostGIS output to be compatible with Open3D?


Using the PostGIS python library I can get the result above into python like this.

conn = <connection details>
cur = conn.cursor()
print("connected")
cur.execute('SELECT ST_AsEWKT(geom) FROM polyhedral.vol_1 LIMIT 1')
geom = cur.fetchone()[0]
geom

This parses the query result into a pandas dataframe

s = geom.replace('POLYHEDRALSURFACE', '')
s = s.replace("((","|")
s = s.replace("))","|")
s = s.replace(")","")
s = s.replace("|,|",",")
s = s.replace("|","",1)
s = s.replace("(","")
s = s.replace(s[len(s)-1], '')
s = s.replace(",",";")
s = s.replace(" ",",")

test = s.split(";")

df = pd.DataFrame([sub.split(",") for sub in test], columns=['x','y','z'])
5
  • Psycopg2 is a python library to connect to a PostgreSQL database. Many python interfaces can read from file objects or strings (it is also possible through the io modul, to read a string like a file), but i don't know about open3D. Commented Nov 3, 2019 at 10:32
  • Yes, I've been using psycopg2 a lot in other projects, I've added some example code, but the result doesn't come through in a python way. Even getting it into numpy would be a start. Commented Nov 3, 2019 at 23:23
  • 1
    Your cursor returns a string, you'll have to parse it. This may be easier with a json format (ST_AsGeoJSON), which can be used with the json-Module to make a python dict or list. Commented Nov 4, 2019 at 16:01
  • That would be logical, however ERROR: lwgeom_to_geojson: 'PolyhedralSurface' geometry type not supported Commented Nov 4, 2019 at 18:22
  • @SpatialDigger you are right. ST_AsGeoJSON() will only work if you cast your Polyhedral to a TIN using ST_Tesselate(). Which basically breaks your polyhedral into simple triangles. Then you will be able to build a GeoJSON. Commented Apr 24, 2022 at 10:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.