2

I have a layer containing thousands of points of X,Y,Z data (sample below).

fid field_1 field_2 field_3 1 525146 675224 -342.1 2 525148 675224 -342.2 3 525150 675224 -342.1 4 525152 675224 -341.9 5 525154 675224 -341.8 6 525156 675226 -342.1 

I want to manually select a subset (eg Select Features by Polygon) and then pass the selection into a script to produce a 3D point cloud. The 3D plot part works as a standalone for reading the original txt file. What I am having trouble with is converting my QGIS selected points into a dataframe.

import pandas as pd layer = qgis.utils.iface.activeLayer() sel=layer.selectedFeatures()#[:,0] #x = layer.selectedFeatures()[:,1] #y = layer.selectedFeatures()[:,2] #z = layer.selectedFeatures()[:,3] df=pd.DataFrame({'X':sel[:,1],'Y':sel[:,2],'Z':sel[:,3]},index=sel[:,0]) 

The various #out sections are my attempts to get the correct combination of rows/columns but I either get errors or only a single point plotting. The code below is the standalone

##3D graph plotting standalone code. import plotly.express as px ##These first two lines is what I am trying to bypass by using the code above## #df= pd.read_csv("Some_Path/input_data.txt") #df.columns=['X','Y','Z'] fig = px.scatter_3d(df, x='X', y='Y', z='Z',color='Z',color_continuous_scale=[(0,'violet'),(0.18,'blue'),(0.35,'lightblue'),(0.50,'green'),(0.65,'yellow'),(0.83,'orange'),(1,'red')]) fig.update_yaxes( scaleanchor="x", scaleratio=1, exponentformat='none') fig.update_xaxes( scaleanchor="x", scaleratio=1, exponentformat='none') fig.update_layout(template="plotly_dark",title="3D Point model.") fig.update_scenes(aspectratio=dict(x=2,y=2,z=0.05)) fig.update_traces(marker=dict(size=2)) fig.write_html("Some_Path/Point_Cloud.html") 

I can just save the selection and then run the standalone script, but it would be neater to do it within QGIS.

1 Answer 1

2

The selected data, although looking more complicated when viewed, was ready to directly go into a dataframe.

import pandas as pd import plotly.express as px layer = qgis.utils.iface.activeLayer() sel=layer.selectedFeatures() df=pd.DataFrame(sel) df.columns=['X','Y','Z'] 
1
  • 1
    This is thanks to QGIS and many other tools supporting the Geo Interface (for existing QGIS features at least, I haven't figured out if one can easily create features from other software through it): gist.github.com/sgillies/2217756 Commented Jun 11, 2022 at 16:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.