Created
November 14, 2019 11:44
-
-
Save pkinglinz/d627c18d69b319191bc5eaabb479f4b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from qgis.core import Qgis, QgsMapLayer, QgsPointXY, QgsProject | |
from qgis.utils import iface | |
def create_joyplot(): | |
rlayer = iface.activeLayer() | |
if rlayer.type() != QgsMapLayer.RasterLayer: | |
iface.messageBar().pushMessage("Error", "Please select a DEM layer", level=Qgis.Critical) | |
return | |
crs = rlayer.crs() | |
# create layer | |
layer = QgsVectorLayer("Polygon?field=latitude:integer", "joy_plot", "memory") | |
pr = layer.dataProvider() | |
layer.setCrs(crs) | |
y_steps = 60 | |
x_steps = 300 | |
vert_exag = 7 | |
extent = iface.mapCanvas().extent() | |
height = extent.height() | |
width = extent.width() | |
x_min = extent.xMinimum() | |
y_max = extent.yMaximum() | |
y_step_size = height / y_steps | |
x_step_size = width / x_steps | |
for y_step in range(y_steps): | |
poly = [] | |
y = y_max - (y_step * y_step_size) | |
# create point lower so the line becomes a polygon with some depth | |
first_point = QgsPointXY(x_min, y - y_step_size) | |
poly.append(first_point) | |
# add joyplot line for each step in y_steps | |
for x_step in range(x_steps): | |
x = x_min + (x_step * x_step_size) | |
point = QgsPointXY(x, y) | |
val, res = rlayer.dataProvider().sample(point, 1) | |
if not res: | |
val = 0 | |
if val < 0: | |
val = 0 | |
poly.append(QgsPointXY(x, y + (val * vert_exag))) | |
# add final point below to close the polygon | |
last_point = poly[-1] | |
last_pointx = last_point.x() | |
last_pointy = y - y_step_size | |
poly.append(QgsPointXY(last_pointx, last_pointy)) | |
out_feat = QgsFeature() | |
out_feat.setGeometry(QgsGeometry.fromPolygonXY([poly])) | |
out_feat.setAttributes([y_step]) | |
pr.addFeatures([out_feat]) | |
QgsProject.instance().addMapLayer(layer) | |
layer.updateExtents() | |
create_joyplot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment