text stringlengths 1 93.6k |
|---|
QgsRasterLayer,
|
QgsVectorLayer,
|
QgsField,
|
QgsMemoryProviderUtils,
|
QgsProcessingFeatureSourceDefinition,
|
QgsPointXY,
|
QgsGeometry,
|
QgsFeature,
|
QgsWkbTypes,
|
edit
|
)
|
from qgis import processing
|
#============================USER PARAMETERS============================
|
# These two params below are in DEM pixel units. So choosing 6 for the
|
# max_hachure density means the script aims to make hachures 6 px apart
|
# when the slope is at its minimum
|
min_hachure_spacing = 2
|
max_hachure_spacing = 6
|
spacing_checks = 100
|
# this parameter is how many times we check the hachure spacing
|
# smaller number runs faster, but if lines are getting too close or too
|
# far, it's not checking often enough
|
min_slope = 15 #degrees
|
max_slope = 40
|
DEM = iface.activeLayer() #The layer of interest must be selected
|
#============================PREPATORY WORK=============================
|
#---------STEP 1: Get slope/aspect/contours using built in tools--------
|
stats = DEM.dataProvider().bandStatistics(1)
|
elevation_range = stats.maximumValue - stats.minimumValue
|
contour_interval = elevation_range / spacing_checks
|
parameters = {
|
'INPUT': DEM,
|
'OUTPUT': 'TEMPORARY_OUTPUT'
|
}
|
slope_layer = QgsRasterLayer(
|
processing.run('qgis:slope', parameters)['OUTPUT'],'Slope')
|
aspect_layer = QgsRasterLayer(
|
processing.run('qgis:aspect', parameters)['OUTPUT'],'Aspect')
|
parameters['INTERVAL'] = contour_interval
|
filled_contours = QgsVectorLayer(processing.run('gdal:contour_polygon',
|
parameters)['OUTPUT'], "Contour Layer", "ogr")
|
line_contours = QgsVectorLayer(processing.run('gdal:contour',
|
parameters)['OUTPUT'], "Contour Layer", "ogr")
|
#--------STEP 2: Set up variables & prepare rasters for reading---------
|
instance = QgsProject.instance()
|
crs = instance.crs()
|
provider = slope_layer.dataProvider()
|
extent = provider.extent()
|
rows = slope_layer.height()
|
cols = slope_layer.width()
|
slope_block = provider.block(1, extent, cols, rows)
|
aspect_block = aspect_layer.dataProvider().block(1, extent, cols, rows)
|
cell_width = extent.width() / cols
|
cell_height = extent.height() / rows
|
average_pixel_size = 0.5 * (slope_layer.rasterUnitsPerPixelX() +
|
slope_layer.rasterUnitsPerPixelY())
|
jump_distance = average_pixel_size * 3
|
min_spacing = average_pixel_size * min_hachure_spacing
|
max_spacing = average_pixel_size * max_hachure_spacing
|
spacing_range = max_spacing - min_spacing
|
slope_range = max_slope - min_slope
|
#===========================CLASS DEFINITIONS===========================
|
#------Contour lines are used to check the spacing of the hachures------
|
class Contour:
|
def __init__(self,contour_geometry,poly_geometry):
|
self.geometry = contour_geometry
|
self.polygon = poly_geometry
|
def ring_list(self):
|
# Returns a list of all rings that this contour is made from
|
if self.geometry.isMultipart():
|
all_rings = [QgsGeometry.fromPolylineXY(line)
|
for line in self.geometry.asMultiPolyline()]
|
else:
|
all_rings = [self.geometry]
|
return all_rings
|
def split_by_hachures(self):
|
# Split this contour according to our current list of hachures
|
all_segments = []
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.