[Qgis-developer] visualizing 3D data (Z values) or data with z attribute: a solution
gene
martin.laloux at gmail.com
Sat Sep 29 15:17:37 PDT 2012
After the post of Etienne Tourigny about "Using/visualizing 3D data (Z
values)", I realized that it was relatively easy from the Python console
with matplotlib or visvis (new pure Python library for visualization of 1D
to 4D data, http://code.google.com/p/visvis/
<http://code.google.com/p/visvis/> ).
*1) 3D shapefile*
*a) with matplotlib.meshgrid*
With a 3d point shapefile, the *shapely* module makes it very easy to
extract the z coordinate:
from mpl_toolkits.mplot3d.axes3d import *
import pylab as plt
from matplotlib import cm
from matplotlib.mlab import griddata
import numpy as np
from shapely.wkb import loads
x=[]
y=[]
z=[]
for elem in layer.selectedFeatures():
geom= elem.geometry()
wkb = geom.asWkb()
x.append(loads(wkb).x)
y.append(loads(wkb).y)
z.append(loads(wkb).z)
now, with the x,y,z coordinates, we can build a grid/surface with the
matplotlib.meshgrid function
xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))
X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi)
and draw it in a 3D matplotlib window
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=cm.jet,linewidth=1,
antialiased=True)
plt.show()
<http://osgeo-org.1560.n6.nabble.com/file/n5005360/splinematplotlib.jpg>
It is also possible to plot the points or the contours in 3D
<http://osgeo-org.1560.n6.nabble.com/file/n5005360/contourpts.jpg>
*with visvis*
import visvis
visvis.plot(x,y,z, lc='k', ls='', mc='g', mw=2, lw=2, ms='.')
visvis.surf(xi, yi, Z)
<http://osgeo-org.1560.n6.nabble.com/file/n5005360/splinevisvis.jpg>
*b) with splines algorithms from scipy*
It is also possible to use other interpolation algorithms like the spline
interpolation class BivariateSpline
<http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.BivariateSpline.html#scipy.interpolate.BivariateSpline>
of scipy
*with matplotlib*
import scipy as sp
from scipy.interpolate import SmoothBivariateSpline
spl = SmoothBivariateSpline(x,y,z)
# construction of the grid/surface
x = np.array(x)
y = np.array(y)
xn, yn = sp.mgrid[x.min():x.max():50j,y.min():y.max():50j]
zspline = spl( xn[:,0], yn[0,:])
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xn, yn, zspline)
plt.show()
*with visvis*
import visvis
visvis.surf(xn,yn,zspline)
*2) shapefile with z attribute*
This is the same thing, but we must use the values of the attribute
*Conclusions and a plugin ?*
Build a plugin seems a priori to me very complex and time consuming
- plot 3 d points is easy.
- but in other cases, all the scenarios (3D shapefile points, lines or
polygons, shapefile with z attribute, interpolation algorithm, ...) should
be considered before
- you need modules like shapely or matplotlib
I do not have much time to write a plugin and personally I'm very happy with
the Python console, sorry.
<http://osgeo-org.1560.n6.nabble.com/file/n5005360/pythonconsole3.jpg>
Then, If somebody else wants to go,I can help
--
View this message in context: http://osgeo-org.1560.n6.nabble.com/visualizing-3D-data-Z-values-or-data-with-z-attribute-a-solution-tp5005360.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
More information about the Qgis-developer
mailing list