Skip to content

Commit

Permalink
Update count_overlaps.py: Add code to create a heatmap based on the o…
Browse files Browse the repository at this point in the history
…verlap counts
  • Loading branch information
mhhd2020 committed Jul 12, 2023
1 parent 7d62ba2 commit 52440d9
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion sketch_map_tool/upload_processing/count_overlaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from tempfile import NamedTemporaryFile
from qgis.core import (QgsProject, QgsVectorLayer, QgsCoordinateReferenceSystem, QgsApplication,
QgsProcessingFeatureSourceDefinition, QgsProcessingFeedback)
from qgis.analysis import QgsNativeAlgorithms
from zipfile import ZipFile
import processing
from processing.core.Processing import Processing
from processing.script import ScriptUtils
import os
import shutil
import pathlib
import geopandas as gpd
import matplotlib.pyplot as plt


def create_qgis_project(markings: BytesIO):
Expand Down Expand Up @@ -56,3 +57,41 @@ def create_qgis_project(markings: BytesIO):
zip_file.writestr(f"project.qgs", f_qgis.read())
buffer.seek(0)
return buffer


def generate_heatmap(geojson_path, lon_min, lat_min, lon_max, lat_max, bg_img_path) -> List[Tuple[str, BytesIO]]:
"""
Create a heatmap covering the extent given in 'lon_min', ..., 'lat_max' arguments, based on the
'COUNT' data in the GeoJSON referred to with 'geojson_path'.
:param geojson_path: Path to a GeoJSON containing a 'COUNT' property for all features.
:param lon_min: Longitude in web mercator of the lower left corner of the extent.
:param lat_min: Latitude in web mercator of the lower left corner of the extent.
:param lon_max: Longitude in web mercator of the upper right corner of the extent.
:param lat_max: Latitude in web mercator of the upper right corner of the extent.
:param bg_img_path: Path to the map image shown as background to the heatmap.
:return: JPGs of the heatmaps for each colour of detected markings and the names of the
corresponding colours.
"""
df_geojson = gpd.read_file(geojson_path)
results = []
for colour in df_geojson["color"].unique():
df_col = df_geojson[df_geojson["color"] == colour]
fig = plt.figure()
ax = fig.subplots()

xlim = ([lon_min, lon_max])
ylim = ([lat_min, lat_max])
ax.set_xlim(xlim)
ax.set_ylim(ylim)

img = plt.imread(bg_img_path)
ax.imshow(img, extent=[lon_min, lon_max, lat_min, lat_max])

df_col.plot(column="COUNT", cmap="cividis", ax=ax)
plt.colorbar(ax.get_children()[1], ax=ax)
result_buffer = BytesIO()
fig.savefig(result_buffer, format="jpg")
result_buffer.seek(0)
results.append((colour, result_buffer))
return results

0 comments on commit 52440d9

Please sign in to comment.