Files
Territory-Analysis---Mapper/category_analysis.py

79 lines
2.9 KiB
Python

import csv
import json
import argparse
parser = argparse.ArgumentParser(description='Analyze territory data by category.')
parser.add_argument('filename', help='The CSV file to analyze.')
args = parser.parse_args()
data = []
with open(args.filename, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
data.append(row)
category_address_counts = {}
for row in data:
category = row['CategoryCode']
if 'Address Count' in row and row['Address Count']:
address_count = int(row['Address Count'])
if category in category_address_counts:
category_address_counts[category] += address_count
else:
category_address_counts[category] = address_count
# --- New code for category colors ---
unique_categories = sorted(list(category_address_counts.keys()))
# A list of 12 distinct colors
colors = [
'#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4',
'#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff'
]
category_colors = {}
for i, category in enumerate(unique_categories):
category_colors[category] = colors[i % len(colors)]
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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}}).addTo(map);
var territories = {json.dumps(data, 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) {{
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);
}}
}}
</script>
</body>
</html>
''')