将ply转换为postgis,脚本有效但卷无效

问题描述 投票:1回答:1

我正在尝试将一个层转换为存储在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接受它作为多面体表面,但我如何确保立方体构造正确?附:和postgis 3D观众在那里,我这样做是盲目的。

postgis data-conversion polyhedra
1个回答
0
投票

我解决了它,我的订单错了,这是正确的订购。

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
    coords1 = verts[x1][0]
    coords2 = verts[x1][1]
    coords3 = verts[x1][2]

    coords4 = verts[y1][0]
    coords5 = verts[y1][1]
    coords6 = verts[y1][2]

    coords7 = verts[z1][0]
    coords8 = verts[z1][1]
    coords9 = verts[z1][2]

    data += "((%s %s %s, %s %s %s, %s %s %s, %s %s %s))," % \
        (
        coords1, coords2, coords3,
        coords4, coords5, coords6,
        coords7, coords8, coords9,
        coords1, coords2, coords3
        )
data = data[:-1]
© www.soinside.com 2019 - 2024. All rights reserved.