2

I've been struggling with 3D ploting some coordinates since a long ago and now I'm really frustrated, so your help will be really appreciated.

I'd like to plot the facade of a building from a CityGML file (which is originally simply an XML file). I have no problem with parsing the CityGML file using XML.etree and extracting the coordinates. But after extracting the coordinates, I cann't find a way to 3D plot them.

from xml.etree import ElementTree as ET

tree = ET.parse('3860_5819__.gml')
root = tree.getroot()

namespaces = {
    'ns0': "http://www.opengis.net/citygml/1.0",
    'ns1': "http://www.opengis.net/gml",
    'ns2': "http://www.opengis.net/citygml/building/1.0"
}

c = 0
wallString = []
for wallSurface in root.findall('.//ns2:WallSurface', namespaces):

    for posList in wallSurface.findall('.//ns1:posList', namespaces):
        c += 1
        wallCoordinates = posList.text
        wallCoordinates = wallCoordinates.split()
        wallString.append(wallCoordinates)


verts = []
for string in wallString:
    X, Y, Z = [], [], []

    c = 0
    for value in string:
        value = float(value)

        if c % 3 == 0:
            X.append(value)
        elif c % 3 == 1:
            Y.append(value)
        else:
            Z.append(value)

        c += 1
        if c > len(string) - 3:
            break

    vert = [list(zip(X, Y, Z))]
    verts.append(vert)


from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

for vert in verts:
    ax.add_collection3d(Poly3DCollection(vert))

ax.autoscale_view(tight=True, scalex=True, scaley=True, scalez=True)
plt.show()
plt.close()

Could the problem be that I can't make my plot "tight"? And if not, is there something I'm doing fundamentally wrong?

If relevant, the CityGML file in this case is related to TU Berlin center of entrepreneurship which can be taken from here.

sasan
  • 41
  • 5

1 Answers1

2

Just realized that nothing was wrong with the main part of the code. The only issue was that the axis were not set. I change the plot part like this:

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as mpl3

fig = plt.figure()
ax = mpl3.Axes3D(fig)

for vert in verts:
    poly = mpl3.art3d.Poly3DCollection(vert)
    ax.add_collection3d(poly)
ax.set_xlim3d(left=386284-50,right=386284+50)
ax.set_ylim3d(bottom=5819224-50, top=5819224+50)
ax.set_zlim3d(bottom=32-10,top=32+20)

plt.show()
plt.close()

Now it works perfectly fine.

sasan
  • 41
  • 5