Saturday, June 25, 2022

Useful code snippets to Clip polygon with another polygon and Convert Vector file in Geopackage to Shapefile in QGIS

 Clip polygon with another polygon using processing algorithm in QGIS

# Clip polygon with another polygon using processing algorithm in QGIS

ovly = 'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Name_Bouganville Lower - N.gpkg'

outfile = ovly.split('\\')[-1].replace('Name_', '').replace('.gpkg', '')
print('Processing...', outfile)

parameters = {'INPUT':'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Results\\SHP\\Woody_02.shp',
'OVERLAY':ovly,
'OUTPUT':f'C:/Users/Yusuf_08039508010/Desktop/Working_Files/.../Results/SHP/Study Location Individual Files/SHP with Holes/{outfile}.shp'}

processing.run("native:clip", parameters)



# -----------------------------------------------
# Bulk Clip polygon with another polygon using processing algorithm in QGIS

import glob

overlay_file = glob.glob('C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\Fiverr\\2022\\06-June\\Masking None Vegetation Ground Covers\\Results\\SHP\\Study Location Individual Files\\*.gpkg')

failed = []
for ovly in overlay_file:
    try:
        outfile = ovly.split('\\')[-1].replace('Name_', '').replace('.gpkg', '')
        print('Processing...', outfile)

        parameters = {'INPUT':'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\Fiverr\\2022\\06-June\\Masking None Vegetation Ground Covers\\Results\\SHP\\Woody_02.shp',
        'OVERLAY':ovly,
        'OUTPUT':f'C:/Users/Yusuf_08039508010/Desktop/Working_Files/Fiverr/2022/06-June/Masking None Vegetation Ground Covers/Results/SHP/Study Location Individual Files/SHP with Holes/{outfile}.shp'}

        processing.run("native:clip", parameters)
    except Exception:
        failed.append(ovly)

print('Done...')



Convert Vector file in Geopackage to Shapefile

# Convert Vector file in Geopackage to Shapefile


gpkg = 'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Name_Bouganville Lower - N.gpkg'

# Read vector layer and test it is valid....
input_file = QgsVectorLayer(gpkg, "polygon", "ogr")
input_file.isValid()

# Construct output file name...
out_filename = gpkg.split('\\')[-1].replace('.gpkg', '')

# Write the file to disc...
out_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\...\Results\SHP\Study Location Individual Files\Failed files"
QgsVectorFileWriter.writeAsVectorFormat(input_file, f"{out_folder}\\{out_filename}.shp", "UTF-8", input_file.crs(), "ESRI Shapefile")
print('Done...') # ----------------------------------------------------------- # Bulk Convert Vector file in Geopackage to Shapefile import glob gpkg_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\Fiverr\2022\06-June\Masking None Vegetation Ground Covers\Results\Deliverables\Shp\gpk" gpkg_files = glob.glob(f'{gpkg_folder}\\*.gpkg') for gpkg in gpkg_files: # Read vector layer and test it is valid.... input_file = QgsVectorLayer(gpkg, "polygon", "ogr") input_file.isValid() # Construct output file name... out_filename = gpkg.split('\\')[-1].split('_')[-1].replace('.gpkg', '') # Write the file to disc... out_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\Fiverr\2022\06-June\Masking None Vegetation Ground Covers\Results\Deliverables\Shp" QgsVectorFileWriter.writeAsVectorFormat(input_file, f"{out_folder}\\{out_filename}.shp", "UTF-8", input_file.crs(), "ESRI Shapefile") print('Done...')



Different ways to read map layers into pyqgis

#1 - Read active layer from the layer panel
layer = iface.activeLayer() # or
layer = qgis.utils.iface.activeLayer()

  
#2 - Read all layers listed on the layers panel...
layers_on_panel = QgsProject.instance().mapLayers()


#3 - Read layer by name from the layer panel...
layer = QgsProject.instance().mapLayersByName('Churches')


#4 - Read from file...
vector_file = r"C:\Users\Yusuf_08039508010\Desktop\...\NGA_adm2.shp"
layer = QgsVectorLayer(vector_file, 'DISPLAYNAME', 'ogr')



Save all shapefile layer listed on the layer panel into a new directory/folder

# Save layers on layer panel to new folder...
import shutil

newFolder = r'C:\Users\Yusuf_08039508010\Desktop\Map of StudyArea\BMC SHP'

# Read all layers listed on the layers panel...
layers_on_panel = QgsProject.instance().mapLayers()

# Loop over each file and copy it to new folder location...
for k, v in layers_on_panel.items():
    # Construct file name...
    fn = v.name() + '.shp'
    
    # Get file path...
    fp1 = v.source()
    fp2 = v.source().replace('.shp', '') + '.dbf'
    fp3 = v.source().replace('.shp', '') + '.shx'
    fp4 = v.source().replace('.shp', '') + '.prj'
    fp5 = v.source().replace('.shp', '') + '.prj'
    
    print('Copying....', fn)
    print('')
    
    # Copy the shapefiles to new folder location...
    shutil.copy( fp1, newFolder ) # shutil.copy( src, dst )
    shutil.copy( fp2, newFolder )
    shutil.copy( fp3, newFolder )
    shutil.copy( fp4, newFolder )
    shutil.copy( fp5, newFolder )
    
    # break

print('Done...')


No comments:

Post a Comment