-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d177d70
commit faf9da1
Showing
18 changed files
with
548 additions
and
309 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
import time | ||
|
||
data_dir = os.path.join(os.path.expanduser('~'), 'Downloads', 'pyqgis_masterclass') | ||
|
||
layer = iface.activeLayer() | ||
|
||
# Check if a layer is selected | ||
if not layer: | ||
iface.messageBar().pushMessage('Please select a layer', level=Qgis.Critical) | ||
# Check if the selected layer is a vector layer | ||
if layer.type() != QgsMapLayer.VectorLayer: | ||
iface.messageBar().pushMessage('Please select a vector layer', level=Qgis.Critical) | ||
|
||
# Define parameters for QgsVectorFileWriter | ||
output_name = 'output.csv' | ||
output_path = os.path.join(data_dir, output_name) | ||
|
||
# We will use QgsVectorFileWriter.create() method | ||
# It takes the following parameters | ||
# fileName: Path to the file | ||
# fields: Fields to write | ||
# geometryType: geometry type of output file | ||
# srs: CRS of the output file | ||
# transformContext: Datum transformation settings | ||
# options: Save Options such as format, encoding etc. | ||
|
||
# Define the options for saving the layer | ||
save_options = QgsVectorFileWriter.SaveVectorOptions() | ||
save_options.driverName = 'CSV' | ||
save_options.fileEncoding = 'UTF-8' | ||
# We can also add some format-specific layer options | ||
# These come from GDAL/OGR | ||
# https://gdal.org/drivers/vector/csv.html | ||
save_options.layerOptions = ['SEPARATOR=COMMA'] | ||
|
||
# Write the file | ||
writer = QgsVectorFileWriter.create( | ||
fileName=output_path, | ||
fields=layer.fields(), | ||
geometryType=QgsWkbTypes.NoGeometry, | ||
srs=layer.crs(), | ||
transformContext=transform_context, | ||
options=save_options) | ||
|
||
# Check if we were able to create the writer | ||
if writer.hasError() != QgsVectorFileWriter.NoError: | ||
iface.messageBar().pushMessage( | ||
'Error:', writer.errorMessage, level=Qgis.Critical) | ||
|
||
selected_count = layer.selectedFeatureCount() | ||
if selected_count == 0: | ||
iface.messageBar().pushMessage( | ||
'Error:', 'No selected features', level=Qgis.Critical) | ||
else: | ||
for f in layer.selectedFeatures(): | ||
writer.addFeature(f) | ||
|
||
# delete the writer to flush features to disk | ||
del writer | ||
iface.messageBar().pushMessage( | ||
'Success:', | ||
f'Output file with {selected_count} features written at {output_path}', | ||
level=Qgis.Success) |
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
layer_id = '[%@layer_id%]' | ||
fid = [% $id %] | ||
|
||
layer = QgsProject.instance().mapLayer(layer_id) | ||
|
||
def get_neighbors(fid): | ||
f = layer.getFeature(fid) | ||
# Use list comprehension to get all intersecting features | ||
# You may also use touches() if your data is topologically correct | ||
# Supply the bounding box to getFeatures() to use Spatial Index | ||
neighbors = [ | ||
c.id() | ||
for c in layer.getFeatures(f.geometry().boundingBox()) | ||
if c.geometry().intersects(f.geometry()) and c.id() != f.id() | ||
] | ||
return neighbors | ||
|
||
|
||
first_degree_neighbors = get_neighbors(fid) | ||
|
||
second_degree_neighbors = set() | ||
|
||
for n in first_degree_neighbors: | ||
neighbors = get_neighbors(n) | ||
second_degree_neighbors.update(neighbors) | ||
|
||
# Remove all first-degree neighbors from the set | ||
second_degree_neighbors = second_degree_neighbors.difference( | ||
set(first_degree_neighbors)) | ||
|
||
# Remove the feature itself from the set if it exists | ||
second_degree_neighbors.discard(fid) | ||
|
||
# Apply the selection | ||
layer.selectByIds(list(second_degree_neighbors), QgsVectorLayer.AddToSelection) |
Oops, something went wrong.