68 lines
2.8 KiB
Python
68 lines
2.8 KiB
Python
import json
|
|
import pandas as pd
|
|
|
|
def generate_category_map(df):
|
|
"""
|
|
Takes a DataFrame, performs category-based analysis, and generates category_map.html.
|
|
"""
|
|
# Calculate total address count for each CategoryCode
|
|
category_address_counts = df.groupby('CategoryCode')['Address Count'].sum().to_dict()
|
|
|
|
# Assign a distinct color to each category for the map
|
|
unique_categories = sorted(list(df['CategoryCode'].unique()))
|
|
colors = [
|
|
'#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4',
|
|
'#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff'
|
|
]
|
|
category_colors = {category: colors[i % len(colors)] for i, category in enumerate(unique_categories)}
|
|
|
|
# Prepare data for embedding in HTML's JavaScript
|
|
data_for_json = df.to_dict(orient='records')
|
|
|
|
with open('category_map.html', 'w') as f:
|
|
f.write(f'''
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Category Map</title>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
|
|
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="map" style="width: 100vw; height: 100vh"></div>
|
|
<script>
|
|
var map = L.map('map').setView([26.3, 127.8], 10);
|
|
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
|
}}).addTo(map);
|
|
|
|
var territories = {json.dumps(data_for_json, indent=4)};
|
|
var categoryColors = {json.dumps(category_colors, indent=4)};
|
|
var categoryAddressCounts = {json.dumps(category_address_counts, indent=4)};
|
|
|
|
for (var i = 0; i < territories.length; i++) {{
|
|
var territory = territories[i];
|
|
if (territory.Boundary) {{
|
|
try {{
|
|
var boundary = JSON.parse('[' + territory.Boundary + ']');
|
|
var color = categoryColors[territory.CategoryCode];
|
|
var polygon = L.polygon(boundary.map(p => [p[1], p[0]]), {{
|
|
fillColor: color,
|
|
color: "#000",
|
|
weight: 1,
|
|
fillOpacity: 0.7
|
|
}}).addTo(map);
|
|
var categoryAddressCount = categoryAddressCounts[territory.CategoryCode]
|
|
polygon.bindPopup('<b>Territory ID:</b> ' + territory.TerritoryID + '<br><b>Category:</b> ' + territory.CategoryCode + '<br><b>Category Address Count:</b> ' + categoryAddressCount);
|
|
}} catch(e) {{
|
|
console.error("Could not parse boundary for territory: " + territory.TerritoryID, e);
|
|
}}
|
|
}}
|
|
}}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
''')
|