MCPcopy
hub / github.com/opengeos/segment-geospatial / save_masks

Method save_masks

qgis-samgeo-plugin/samgeo_plugin.py:1382–1461  ·  view source on GitHub ↗

Save the segmentation masks.

(self)

Source from the content-addressed store, hash-verified

1380 self.progress_bar.setVisible(False)
1381
1382 def save_masks(self):
1383 """Save the segmentation masks."""
1384 if self.sam is None or self.sam.masks is None or len(self.sam.masks) == 0:
1385 self.show_error("No masks to save. Please run segmentation first.")
1386 return
1387
1388 import tempfile
1389
1390 output_path = self.output_path_edit.text().strip()
1391 format_text = self.output_format_combo.currentText()
1392
1393 # Generate temp file path if not specified
1394 use_temp_file = False
1395 if not output_path:
1396 use_temp_file = True
1397 if "Raster" in format_text:
1398 temp_file = tempfile.NamedTemporaryFile(suffix=".tif", delete=False)
1399 output_path = temp_file.name
1400 temp_file.close()
1401 elif "GeoPackage" in format_text:
1402 temp_file = tempfile.NamedTemporaryFile(suffix=".gpkg", delete=False)
1403 output_path = temp_file.name
1404 temp_file.close()
1405 else: # Shapefile
1406 temp_dir = tempfile.mkdtemp()
1407 output_path = os.path.join(temp_dir, "masks.shp")
1408
1409 try:
1410 self.progress_bar.setVisible(True)
1411 self.progress_bar.setRange(0, 0)
1412 QCoreApplication.processEvents()
1413
1414 unique = self.unique_check.isChecked()
1415
1416 if "Raster" in format_text:
1417 # Save as raster
1418 self.sam.save_masks(output=output_path, unique=unique)
1419
1420 if self.add_to_map_check.isChecked():
1421 layer_name = (
1422 "samgeo_masks"
1423 if use_temp_file
1424 else os.path.basename(output_path)
1425 )
1426 layer = QgsRasterLayer(output_path, layer_name)
1427 if layer.isValid():
1428 QgsProject.instance().addMapLayer(layer)
1429 else:
1430 # Save as vector - first save as raster, then convert
1431 temp_raster = tempfile.NamedTemporaryFile(
1432 suffix=".tif", delete=False
1433 ).name
1434 try:
1435 self.sam.save_masks(output=temp_raster, unique=unique)
1436
1437 # Convert raster to vector
1438 from samgeo import common
1439

Callers

nothing calls this directly

Calls 4

show_errorMethod · 0.95
log_messageMethod · 0.95
closeMethod · 0.80
raster_to_vectorMethod · 0.45

Tested by

no test coverage detected