Spatial task
Could you help me analyze the fast accessibility in Pennsylvania by performing the following analysis:1) Count the fast food restaurants in each county and store the result in a new field named “Count”. 2) Calculate the fast food accessibility score for each county as (Count /Population) * 1,000 and store the result in a new field named “Score”. 3) Create a thematic map showing the fast food accessibility score for each county.
Dataset
Pennsylvania Fastfoods shapefile
Pennsylvania counties shapefile
Result
Geoprocessing workflow

Generated Python Code
# Purpose of the script:
# This script processes spatial data to assess fast food accessibility across counties in Pennsylvania.
# The task consists of counting fast food restaurants per county, calculating an accessibility score,
# and creating a thematic map representing these scores.
import os
from qgis.core import QgsProject, QgsVectorLayer, QgsGraduatedSymbolRenderer, QgsSymbol
import processing
from qgis.PyQt.QtGui import QColor
def analyze_fast_food_accessibility():
# Setting up the paths for the shapefiles and output directory
fast_foods_path = “C:/Users/17036/Downloads/DEMO/PA_Fastfoods_XY/PA_Fastfoods_XY.shp”
counties_path = “C:/Users/17036/Downloads/DEMO/PennsylvaniaCounties/PennsylvaniaCounties.shp”
output_dir = ‘C:/Users/17036/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/SpatialAnalysisAgent-master/Default_workspace’
# Load the input layers
fast_foods_layer = QgsVectorLayer(fast_foods_path, ‘FastFoods’, ‘ogr’)
counties_layer = QgsVectorLayer(counties_path, ‘Counties’, ‘ogr’)
# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
# Step 1: Count fast food restaurants in each county
count_output_path = os.path.join(output_dir, “CountiesFastFoodCount.shp”)
result = processing.run(“native:countpointsinpolygon”, {
‘POLYGONS’: counties_layer,
‘POINTS’: fast_foods_layer,
‘WEIGHT’: None,
‘CLASSFIELD’: None,
‘FIELD’: ‘Count’,
‘OUTPUT’: count_output_path
})
# Load the Layer with Count field
counties_with_count = QgsVectorLayer(result[‘OUTPUT’], ‘CountiesWithCount’, ‘ogr’)
QgsProject.instance().addMapLayer(counties_with_count)
# Step 2: Calculate the Fast Food Accessibility Score
score_output_path = os.path.join(output_dir, “CountiesFastFoodScore.shp”)
processing.run(“native:fieldcalculator”, {
‘INPUT’: counties_with_count,
‘FIELD_NAME’: ‘Score’,
‘FIELD_TYPE’: 0,
‘FIELD_LENGTH’: 10,
‘FIELD_PRECISION’: 3,
‘FORMULA’: ‘(“Count” / “Population”) * 1000’,
‘OUTPUT’: score_output_path
})
# Load the Layer with Score field
counties_with_score = QgsVectorLayer(score_output_path, ‘CountiesWithScore’, ‘ogr’)
QgsProject.instance().addMapLayer(counties_with_score)
# Step 3: Create a Thematic Map
renderer = QgsGraduatedSymbolRenderer(”, [])
renderer.setClassAttribute(‘Score’)
renderer.setMode(QgsGraduatedSymbolRenderer.Quantile)
renderer.updateClasses(counties_with_score, 5)
# Set the color ramp
from qgis.core import QgsGradientColorRamp
color1 = QColor(255, 255, 204)
color2 = QColor(0, 104, 55)
color_ramp = QgsGradientColorRamp(color1, color2)
renderer.updateColorRamp(color_ramp)
counties_with_score.setRenderer(renderer)
counties_with_score.triggerRepaint()
# Execute the function
analyze_fast_food_accessibility()