我正在尝试将层转换为存储在 postgis 中的多面体。在这个例子中,我有一个从 0,0,0 到 1,1,1 的立方体,下面的脚本为 postgis 引入了一个几何图形,它就在那里,没问题,但是我去计算体积,它给出了以下错误:
询问:
SELECT ST_Volume(st_geomfromtext) FROM public.test3
错误:
ERROR: PolyhedralSurface is invalid : inconsistent orientation of PolyhedralSurface detected at edge 1 (4-3) of polygon 11 : POLYHEDRALSURFACE(((1/1 1/1 0/1,1/1 0/1 0/1,0/1 0/1 0/1,1/1 1/1 0/1)),((1/1 0/1 0/1,1/1 0/1 1/1,0/1 0/1 0/1,1/1 0/1 0/1)),((1/1 0/1 0/1,1
SQL state: XX000
对象是一个 0,0,0 到 1,1,1 的立方体,这里它是一个顶点和三角形的数组。
顶点:
array([[ 1., 1., -0.],
[ 1., 0., -0.],
[ 0., 0., -0.],
[ 0., 1., -0.],
[ 1., 1., 1.],
[ 0., 1., 1.],
[ 0., 0., 1.],
[ 1., 0., 1.],
[ 1., 1., -0.],
[ 1., 1., 1.],
[ 1., 0., 1.],
[ 1., 0., -0.],
[ 1., 0., -0.],
[ 1., 0., 1.],
[ 0., 0., 1.],
[ 0., 0., -0.],
[ 0., 0., -0.],
[ 0., 0., 1.],
[ 0., 1., 1.],
[ 0., 1., -0.],
[ 1., 1., 1.],
[ 1., 1., -0.],
[ 0., 1., -0.],
[ 0., 1., 1.]])
三角形定义为:
array([[ 0, 1, 2],
[ 0, 2, 3],
[ 4, 5, 6],
[ 4, 6, 7],
[ 8, 9, 10],
[ 8, 10, 11],
[12, 13, 14],
[12, 14, 15],
[16, 17, 18],
[16, 18, 19],
[20, 21, 22],
[20, 22, 23]], dtype=int32)
我把这个转换脚本放在一起,把它添加到 postgis 中:
import numpy as np
from open3d import *
import psycopg2
import dbconfig
def connect_db():
global connection
connection = psycopg2.connect(host=dbconfig.DATABASE_CONFIG['host'],
user=dbconfig.DATABASE_CONFIG['user'],
password=dbconfig.DATABASE_CONFIG['password'],
dbname=dbconfig.DATABASE_CONFIG['dbname'])
return connection
# mesh = read_triangle_mesh('C:/Users/garyn/PycharmProjects/pointcloudprocessor/tmp/contexts/99.526/396.ply')
mesh = read_triangle_mesh('C:/Users/garyn/PycharmProjects/pointcloudprocessor/tmp/contexts/cube3.ply')
verts = mesh.vertices
verts = np.asarray(verts)
tri = mesh.triangles
tri = np.asarray(tri)
data = ''
header = ("'POLYHEDRALSURFACE(")
for i in range(len(tri)):
# for i in range(0,2):
x1 = (tri[i][0]) # 3
y1 = (tri[i][1]) # 44
z1 = (tri[i][2]) # 1
x_coords1 = verts[x1][0]
y_coords1 = verts[y1][0]
z_coords1 = verts[z1][0]
x_coords2 = verts[x1][1]
y_coords2 = verts[y1][1]
z_coords2 = verts[z1][1]
x_coords3 = verts[x1][2]
y_coords3 = verts[y1][2]
z_coords3 = verts[z1][2]
data += "((%s %s %s, %s %s %s, %s %s %s, %s %s %s))," % \
(x_coords1, y_coords1, z_coords1, \
x_coords2, y_coords2, z_coords2, \
x_coords3, y_coords3, z_coords3, \
x_coords1, y_coords1, z_coords1)
data = data[:-1]
projection = ")',32635)"
create_stmt = "CREATE TABLE test3 AS "
select_stmt = "SELECT ST_GeomFromText("
polyhedron = header + data + projection
query = create_stmt + select_stmt + polyhedron
conn = connect_db()
cur = conn.cursor()
cur.execute(query)
conn.commit()
cur.close()
conn.close()
结果是:
CREATE TABLE test3 AS
SELECT ST_GeomFromText('POLYHEDRALSURFACE(((1.0 1.0 0.0, 1.0 0.0 0.0, -0.0 -0.0 -0.0, 1.0 1.0 0.0)),((1.0 0.0 0.0, 1.0 0.0 1.0, -0.0 -0.0 -0.0, 1.0 0.0 0.0)),((1.0 0.0 0.0, 1.0 1.0 0.0, 1.0 1.0 1.0, 1.0 0.0 0.0)),((1.0 0.0 1.0, 1.0 0.0 0.0, 1.0 1.0 1.0, 1.0 0.0 1.0)),((1.0 1.0 1.0, 1.0 1.0 0.0, -0.0 1.0 1.0, 1.0 1.0 1.0)),((1.0 1.0 1.0, 1.0 0.0 0.0, -0.0 1.0 -0.0, 1.0 1.0 1.0)),((1.0 1.0 0.0, 0.0 0.0 0.0, -0.0 1.0 1.0, 1.0 1.0 0.0)),((1.0 0.0 0.0, 0.0 0.0 0.0, -0.0 1.0 -0.0, 1.0 0.0 0.0)),((0.0 0.0 0.0, 0.0 0.0 1.0, -0.0 1.0 1.0, 0.0 0.0 0.0)),((0.0 0.0 0.0, 0.0 1.0 1.0, -0.0 1.0 -0.0, 0.0 0.0 0.0)),((1.0 1.0 0.0, 1.0 1.0 1.0, 1.0 -0.0 -0.0, 1.0 1.0 0.0)),((1.0 0.0 0.0, 1.0 1.0 1.0, 1.0 -0.0 1.0, 1.0 0.0 0.0)))',32635)
它看起来不错,postgis 接受它作为多面体表面,但我如何确保立方体构造正确?PS和postgis 3D查看器在那里,我在做这个盲人。