Open TheMapSmith opened 1 year ago
Sure, I have added processing messages and status reporting to the separate_rasters_by_color and simplify_and_generalize functions.
separate_rasters.py:
import numpy as np
from osgeo import gdal
def separate_rasters_by_color(input_file, output_folder, color_values):
print("Separating rasters by color...")
# Open the input raster file
src_ds = gdal.Open(input_file)
src_band = src_ds.GetRasterBand(1)
src_array = src_band.ReadAsArray()
# Loop through each color value and create a new raster for that value
for color_value in color_values:
print(f"Processing color value: {color_value}")
# Create a mask for the current color value
mask = np.where(src_array == color_value, 1, 0).astype(np.uint8)
# Create the output raster file
output_file = os.path.join(output_folder, f"{color_value}_global.tif")
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.Create(output_file, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte)
# Copy the geotransform and projection from the source raster
dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
dst_ds.SetProjection(src_ds.GetProjection())
# Write the mask data to the output raster
dst_band = dst_ds.GetRasterBand(1)
dst_band.WriteArray(mask)
# Clean up
dst_band, dst_ds = None, None
# Clean up
src_band, src_ds = None, None
print("Separation of rasters by color complete.")
merge_rasters.py:
import os
from osgeo import gdal, ogr, osr
def simplify_and_generalize(input_file, output_file):
print(f"Simplifying and generalizing raster: {input_file}")
# Open the global file
src_ds = gdal.Open(input_file)
# Create an output shapefile
shp_output_file = os.path.splitext(output_file)[0] + ".shp"
drv = ogr.GetDriverByName("ESRI Shapefile")
dst_ds = drv.CreateDataSource(shp_output_file)
dst_layer = dst_ds.CreateLayer("polygonized", srs=osr.SpatialReference().ImportFromWkt(src_ds.GetProjection()))
dst_layer.CreateField(ogr.FieldDefn("DN", ogr.OFTInteger))
# Polygonize and simplify
gdal.Polygonize(src_ds.GetRasterBand(1), None, dst_layer, 0, [], callback=None)
dst_layer.SimplifyPreserveTopology(1.0) # Adjust the simplification factor to control the degree of simplification
# Clean up
src_ds, dst_ds = None, None
print(f"Simplification and generalization of raster {input_file} complete.")
These updates include print statements to report the processing status and progress for each color value in the separate_rasters_by_color function, as well as print statements to report the processing status for the simplify_and_generalize function.
can you also write a note for each file that get's exported?
Certainly! I have added code to write a note for each file that gets exported in the separate_rasters_by_color and simplify_and_generalize functions.
separate_rasters.py:
import numpy as np
from osgeo import gdal
def separate_rasters_by_color(input_file, output_folder, color_values):
print("Separating rasters by color...")
# Open the input raster file
src_ds = gdal.Open(input_file)
src_band = src_ds.GetRasterBand(1)
src_array = src_band.ReadAsArray()
# Loop through each color value and create a new raster for that value
for color_value in color_values:
print(f"Processing color value: {color_value}")
# Create a mask for the current color value
mask = np.where(src_array == color_value, 1, 0).astype(np.uint8)
# Create the output raster file
output_file = os.path.join(output_folder, f"{color_value}_global.tif")
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.Create(output_file, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte)
# Copy the geotransform and projection from the source raster
dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
dst_ds.SetProjection(src_ds.GetProjection())
# Write the mask data to the output raster
dst_band = dst_ds.GetRasterBand(1)
dst_band.WriteArray(mask)
# Write a note for the exported file
with open(output_file + ".txt", "w") as note:
note.write(f"Raster file for color value: {color_value}\n")
# Clean up
dst_band, dst_ds = None, None
# Clean up
src_band, src_ds = None, None
print("Separation of rasters by color complete.")
merge_rasters.py:
import os
from osgeo import gdal, ogr, osr
def simplify_and_generalize(input_file, output_file):
print(f"Simplifying and generalizing raster: {input_file}")
# Open the global file
src_ds = gdal.Open(input_file)
# Create an output shapefile
shp_output_file = os.path.splitext(output_file)[0] + ".shp"
drv = ogr.GetDriverByName("ESRI Shapefile")
dst_ds = drv.CreateDataSource(shp_output_file)
dst_layer = dst_ds.CreateLayer("polygonized", srs=osr.SpatialReference().ImportFromWkt(src_ds.GetProjection()))
dst_layer.CreateField(ogr.FieldDefn("DN", ogr.OFTInteger))
# Polygonize and simplify
gdal.Polygonize(src_ds.GetRasterBand(1), None, dst_layer, 0, [], callback=None)
dst_layer.SimplifyPreserveTopology(1.0) # Adjust the simplification factor to control the degree of simplification
# Write a note for the exported file
with open(shp_output_file + ".txt", "w") as note:
note.write(f"Simplified and generalized shapefile for raster: {input_file}\n")
# Clean up
src_ds, dst_ds = None, None
print(f"Simplification and generalization of raster {input_file} complete.")
These updates include writing a text file with a note for each exported file in the separate_rasters_by_color and simplify_and_generalize functions. The note describes the
can you please add some processing messages and status reporting to the separate_rasters_by_color function and simplify_and_generalize functions?