86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
import csv
|
|
import json
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(description='Analyze territory data.')
|
|
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)
|
|
|
|
total_territories = len(data)
|
|
address_counts = [int(row['Address Count']) for row in data if row['Address Count']]
|
|
total_addresses = sum(address_counts)
|
|
average_addresses = total_addresses / total_territories if total_territories > 0 else 0
|
|
|
|
min_addresses = min(address_counts) if address_counts else 0
|
|
max_addresses = max(address_counts) if address_counts else 0
|
|
|
|
category_counts = {}
|
|
for row in data:
|
|
category = row['CategoryCode']
|
|
if category in category_counts:
|
|
category_counts[category] += 1
|
|
else:
|
|
category_counts[category] = 1
|
|
|
|
with open('analysis.md', 'w') as f:
|
|
f.write('# Territory Analysis\n')
|
|
f.write(f'Total Territories: {total_territories}\n')
|
|
f.write(f'Total Addresses: {total_addresses}\n')
|
|
f.write(f'Average Addresses per Territory: {average_addresses:.2f}\n')
|
|
f.write('## Territories by Category\n')
|
|
for category, count in category_counts.items():
|
|
f.write(f'- {category}: {count}\n')
|
|
|
|
with open('map.html', 'w') as f:
|
|
f.write(f'''
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Territory 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, indent=4)};
|
|
var minAddresses = {min_addresses};
|
|
var maxAddresses = {max_addresses};
|
|
|
|
function getColor(d) {{
|
|
var ratio = (d - minAddresses) / (maxAddresses - minAddresses);
|
|
var hue = (1 - ratio) * 120;
|
|
return 'hsl(' + hue + ', 100%, 50%)';
|
|
}}
|
|
|
|
for (var i = 0; i < territories.length; i++) {{
|
|
var territory = territories[i];
|
|
if (territory.Boundary) {{
|
|
var boundary = JSON.parse('[' + territory.Boundary + ']');
|
|
var color = getColor(territory['Address Count']);
|
|
var polygon = L.polygon(boundary.map(p => [p[1], p[0]]), {{
|
|
fillColor: color,
|
|
color: "#000",
|
|
weight: 1,
|
|
fillOpacity: 0.7
|
|
}}).addTo(map);
|
|
polygon.bindPopup('<b>Territory ID:</b> ' + territory.TerritoryID + '<br><b>Territory Number:</b> ' + territory.CategoryCode + '-' + territory.Number + '<br><b>Address Count:</b> ' + territory['Address Count']);
|
|
}}
|
|
}}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
''') |