Select Page

Spatial task

Could you please generate a raster map with resolution of 100 meter, showing the distances from San Francisco census tracts to the nearest hospitals?

Dataset

San Francisco Hospitals 
San Francisco census tracts

Result

Generated Python Code

# The code is designed to rasterize hospital locations and then generate a proximity raster showing distances from these hospitals.
# There are several main sections:
# 1. Rasterizing the hospital vector data.
# 2. Generating a proximity map from the rasterized hospitals.
# 3. Correcting the code to meet documentation guidelines and ensuring correct functionality.

import os
from qgis.core import QgsVectorLayer, QgsRasterLayer, QgsProject
import processing
from time import sleep

def generate_distance_raster():
# Define the input paths
hospital_shapefile = ‘C:/Users/17036/Downloads/DEMO/SanFranciscoHospitals/SanFranciscoHospitals.shp’
census_tracts_shapefile = ‘C:/Users/17036/Downloads/DEMO/SanFrancisco_tracts_central/SanFrancisco_tracts_central.shp’

# Define the output directory
base_output_dir = ‘C:/Users/17036/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/SpatialAnalysisAgent-master/Default_workspace/’

# Output file paths for rasterized hospitals and proximity map
rasterized_hospitals_path = os.path.join(base_output_dir, ‘rasterized_hospitals.tif’)
proximity_output_path = os.path.join(base_output_dir, ‘hospital_distance.tif’)

# Check if output file already exists and create a unique name if necessary
if os.path.exists(rasterized_hospitals_path):
base_name, ext = os.path.splitext(rasterized_hospitals_path)
rasterized_hospitals_path = f“{base_name}_1{ext}”

if os.path.exists(proximity_output_path):
base_name, ext = os.path.splitext(proximity_output_path)
proximity_output_path = f“{base_name}_1{ext}”

# The input vector layers for hospitals and census tracts
hospitals_layer = QgsVectorLayer(hospital_shapefile, ‘San Francisco Hospitals’, ‘ogr’)
census_tracts_layer = QgsVectorLayer(census_tracts_shapefile, ‘San Francisco Census Tracts’, ‘ogr’)

# Get the extent of the census tracts to use as the raster extent
census_extent = census_tracts_layer.extent()

# Step 1: Rasterize the hospital locations
rasterize_params = {
‘INPUT’: hospitals_layer,
‘FIELD’: ,
‘BURN’: 1.0,  # Burn a value of 1.0 where hospitals are located
‘USE_Z’: False,
‘UNITS’: 1,  # Georeferenced units
‘WIDTH’: 100,  # 100 meter resolution
‘HEIGHT’: 100,  # 100 meter resolution
‘EXTENT’: f“{census_extent.xMinimum()},{census_extent.xMaximum()},{census_extent.yMinimum()},{census_extent.yMaximum()}”,
‘NODATA’: 0.0,
‘OUTPUT’: rasterized_hospitals_path,
‘OPTIONS’: ,
‘DATA_TYPE’: 5  # Float32
}
processing.run(“gdal:rasterize”, rasterize_params)

# Wait for a second to ensure the rasterized layer is created before using it in further processing
sleep(1)

# Load the rasterized layer for the hospitals into QGIS
rasterized_layer = QgsRasterLayer(rasterized_hospitals_path, ‘Rasterized Hospitals’)
QgsProject.instance().addMapLayer(rasterized_layer)

# Step 2: Generate a proximity raster from the rasterized hospital locations
proximity_params = {
‘INPUT’: rasterized_hospitals_path,
‘BAND’: 1,
‘VALUES’: ,  # All non-zero pixels are considered target pixels
‘UNITS’: 0,  # Georeferenced coordinates
‘MAX_DISTANCE’: 0.0,
‘REPLACE’: 0.0,
‘NODATA’: 0.0,
‘OUTPUT’: proximity_output_path,
‘OPTIONS’: ,
‘EXTRA’: None,
‘DATA_TYPE’: 5  # Float32
}

# Run the proximity algorithm
processing.run(“gdal:proximity”, proximity_params)

# Load the resulting proximity map layer into QGIS
proximity_layer = QgsRasterLayer(proximity_output_path, ‘Proximity Map’)
QgsProject.instance().addMapLayer(proximity_layer)

# Execute the function to generate the distance raster
generate_distance_raster()