Refactor: Integrate scripts into a single application (v1.2.0)
This commit is contained in:
@@ -1,41 +1,26 @@
|
||||
import csv
|
||||
import json
|
||||
import argparse
|
||||
import pandas as pd
|
||||
|
||||
parser = argparse.ArgumentParser(description='Analyze territory data by category.')
|
||||
parser.add_argument('filename', help='The CSV file to analyze.')
|
||||
args = parser.parse_args()
|
||||
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()
|
||||
|
||||
data = []
|
||||
with open(args.filename, 'r') as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
data.append(row)
|
||||
# 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)}
|
||||
|
||||
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
|
||||
# Prepare data for embedding in HTML's JavaScript
|
||||
data_for_json = df.to_dict(orient='records')
|
||||
|
||||
# --- 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'''
|
||||
with open('category_map.html', 'w') as f:
|
||||
f.write(f'''
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -53,23 +38,27 @@ with open('category_map.html', 'w') as f:
|
||||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||
}}).addTo(map);
|
||||
|
||||
var territories = {json.dumps(data, indent=4)};
|
||||
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) {{
|
||||
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);
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user