-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathageswrtcompanies.py
More file actions
76 lines (65 loc) · 3.83 KB
/
Copy pathageswrtcompanies.py
File metadata and controls
76 lines (65 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from fastapi import FastAPI, Query, Body
from fastapi.responses import JSONResponse
import numpy as np
app = FastAPI()
# Function to create population density around each company and return individual densities
def create_company_population_density(company_x, company_y, width, height, max_radius=50):
"""
Creates individual population densities for each company.
:param company_x: List of x-coordinates for companies
:param company_y: List of y-coordinates for companies
:param width: Width of the grid
:param height: Height of the grid
:param max_radius: Radius of influence for each company point
:return: List of density arrays for each company
"""
# List to hold density arrays for each company
densities = []
# Loop through each company and create density map
for x, y in zip(company_x, company_y):
if 0 <= x < width and 0 <= y < height: # Ensure each company is within bounds
Y, X = np.mgrid[0:height, 0:width]
distance = np.sqrt((X - x)**2 + (Y - y)**2)
density = np.clip(1 - (distance / max_radius), 0, 1) # Radial decay
densities.append(density) # Store individual company density
return densities # List of individual density arrays
# Endpoint to generate the heatmap based on company coordinates and grid size
@app.post("/get_company_heatmap")
async def get_company_heatmap(companies: list = Body(...), width: int = Query(...), height: int = Query(...), radius: int = Query(50)):
"""
Generate and store density maps for each company within the specified grid.
:param companies: List of company coordinates as [{"x": int, "y": int}, ...]
:param width: Width of the heatmap grid
:param height: Height of the heatmap grid
:param radius: Radius of influence for each company point
:return: JSON list of population densities for each company
"""
# Extract company coordinates
company_x = [company["x"] for company in companies]
company_y = [company["y"] for company in companies]
# Generate density map for each company
densities = create_company_population_density(company_x, company_y, width, height, max_radius=radius)
# Return the list of density arrays for each company
return JSONResponse(content=[density.tolist() for density in densities])
# Endpoint to compute population density at a specific point, considering all company densities
@app.get("/get_company_population_density")
async def get_company_population_density(x: int = Query(...), y: int = Query(...), radius: int = Query(10), width: int = Query(...), height: int = Query(...)):
"""
Calculate the combined population density at a specific point from all companies.
:param x, y: Coordinates of the target point
:param radius: Radius within which to measure density impact
:param width, height: Dimensions of the grid for consistent scaling
:return: Population density percentage at the specified point
"""
# Placeholder density grids to simulate a response (use actual densities in practice)
densities = create_company_population_density([10, 20, 30], [40, 50, 60], width, height) # Example company locations
# Calculate density impact from each company within the specified radius
def calculate_density_at_point(density_grid, x, y, radius):
Y, X = np.mgrid[0:height, 0:width]
distance = np.sqrt((X - x)**2 + (Y - y)**2)
mask = distance <= radius
return density_grid[mask].mean() if mask.any() else 0
# Aggregate densities from all companies at (x, y)
total_density = sum(calculate_density_at_point(density, x, y, radius) for density in densities)
density_percentage = (total_density / len(densities)) * 100 if densities else 0 # Averaged over all companies
return JSONResponse(content={"x": x, "y": y, "population_density": density_percentage})