Checkpoint all changes
This commit is contained in:
parent
65877acf95
commit
66c2a25457
28 changed files with 3035 additions and 621 deletions
|
|
@ -8,9 +8,13 @@ use rustc_hash::FxHashMap;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::consts::{DEFAULT_PROPERTIES_LIMIT, ENUM_NULL, MAX_PROPERTIES_LIMIT};
|
||||
use crate::data::EnumFeatureData;
|
||||
use crate::filter::{parse_filters, row_passes_filters};
|
||||
use crate::state::AppState;
|
||||
|
||||
use super::parse::h3_cell_bounds;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct HexagonPropertiesParams {
|
||||
pub h3: String,
|
||||
|
|
@ -35,6 +39,8 @@ pub struct Property {
|
|||
pub lat: f64,
|
||||
pub lon: f64,
|
||||
|
||||
pub is_construction_date_approximate: Option<bool>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub features: FxHashMap<String, f64>,
|
||||
}
|
||||
|
|
@ -48,20 +54,51 @@ pub struct HexagonPropertiesResponse {
|
|||
pub truncated: bool,
|
||||
}
|
||||
|
||||
fn non_empty_string(text: &str) -> Option<String> {
|
||||
let trimmed = text.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn lookup_enum_value(
|
||||
enum_features: &[EnumFeatureData],
|
||||
enum_idx: &FxHashMap<String, usize>,
|
||||
row: usize,
|
||||
names: &[&str],
|
||||
) -> Option<String> {
|
||||
for name in names {
|
||||
if let Some(&feature_index) = enum_idx.get(*name) {
|
||||
let enum_feature = &enum_features[feature_index];
|
||||
let data_index = enum_feature.data[row];
|
||||
if data_index != ENUM_NULL {
|
||||
if let Some(value) = enum_feature.values.get(data_index as usize) {
|
||||
return Some(value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn get_hexagon_properties(
|
||||
state: Arc<AppState>,
|
||||
Query(params): Query<HexagonPropertiesParams>,
|
||||
) -> Result<Json<HexagonPropertiesResponse>, (StatusCode, String)> {
|
||||
let cell = h3o::CellIndex::from_str(¶ms.h3)
|
||||
.map_err(|e| {
|
||||
warn!(h3 = %params.h3, error = %e, "Invalid H3 cell index");
|
||||
(StatusCode::BAD_REQUEST, format!("Invalid H3 cell: {}", e))
|
||||
})?;
|
||||
let cell = h3o::CellIndex::from_str(¶ms.h3).map_err(|error| {
|
||||
warn!(h3 = %params.h3, error = %error, "Invalid H3 cell index");
|
||||
(StatusCode::BAD_REQUEST, format!("Invalid H3 cell: {}", error))
|
||||
})?;
|
||||
let cell_u64: u64 = cell.into();
|
||||
|
||||
let resolution = params.resolution as usize;
|
||||
if resolution >= state.h3_cells.len() || state.h3_cells[resolution].is_empty() {
|
||||
warn!(resolution, "Invalid or non-precomputed resolution for hexagon-properties");
|
||||
warn!(
|
||||
resolution,
|
||||
"Invalid or non-precomputed resolution for hexagon-properties"
|
||||
);
|
||||
return Err((
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Invalid or non-precomputed resolution".to_string(),
|
||||
|
|
@ -84,31 +121,29 @@ pub async fn get_hexagon_properties(
|
|||
let feature_data = &state.data.feature_data;
|
||||
let enum_features = &state.data.enum_features;
|
||||
|
||||
let matching_rows: Vec<usize> = h3_data
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(idx, &h3_cell)| {
|
||||
if h3_cell == cell_u64 {
|
||||
if row_passes_filters(
|
||||
idx,
|
||||
let (min_lat, min_lon, max_lat, max_lon) = h3_cell_bounds(cell, 0.001);
|
||||
|
||||
let mut matching_rows: Vec<usize> = Vec::new();
|
||||
state
|
||||
.grid
|
||||
.for_each_in_bounds(min_lat, min_lon, max_lat, max_lon, |row_idx| {
|
||||
let row = row_idx as usize;
|
||||
if h3_data[row] == cell_u64
|
||||
&& row_passes_filters(
|
||||
row,
|
||||
&parsed_filters,
|
||||
&parsed_enum_filters,
|
||||
feature_data,
|
||||
num_features,
|
||||
enum_features,
|
||||
) {
|
||||
Some(idx)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
)
|
||||
{
|
||||
matching_rows.push(row);
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
});
|
||||
|
||||
let total = matching_rows.len();
|
||||
let limit = params.limit.unwrap_or(100).min(500);
|
||||
let limit = params.limit.unwrap_or(DEFAULT_PROPERTIES_LIMIT).min(MAX_PROPERTIES_LIMIT);
|
||||
let offset = params.offset.unwrap_or(0);
|
||||
let truncated = total > offset + limit;
|
||||
|
||||
|
|
@ -120,49 +155,46 @@ pub async fn get_hexagon_properties(
|
|||
let mut features = FxHashMap::default();
|
||||
let base = row * num_features;
|
||||
for (feat_idx, feat_name) in state.data.feature_names.iter().enumerate() {
|
||||
let v = feature_data[base + feat_idx];
|
||||
if v.is_finite() {
|
||||
features.insert(feat_name.clone(), v);
|
||||
let value = feature_data[base + feat_idx];
|
||||
if value.is_finite() {
|
||||
features.insert(feat_name.clone(), value);
|
||||
}
|
||||
}
|
||||
|
||||
let get_string = |s: &str| -> Option<String> {
|
||||
let trimmed = s.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let get_enum_value = |names: &[&str]| -> Option<String> {
|
||||
for name in names {
|
||||
if let Some(val) = enum_features.iter().find_map(|ef| {
|
||||
if ef.name == *name {
|
||||
let idx = ef.data[row];
|
||||
if idx == 255 {
|
||||
None
|
||||
} else {
|
||||
ef.values.get(idx as usize).cloned()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
None
|
||||
};
|
||||
|
||||
Property {
|
||||
address: get_string(&state.data.address[row]),
|
||||
postcode: get_string(&state.data.postcode[row]),
|
||||
property_type: get_enum_value(&["Property type", "epc_property_type", "pp_property_type"]),
|
||||
built_form: get_enum_value(&["Property type/built form", "built_form"]),
|
||||
duration: get_enum_value(&["Leashold/Freehold", "duration"]),
|
||||
current_energy_rating: get_enum_value(&["Current energy rating", "current_energy_rating"]),
|
||||
potential_energy_rating: get_enum_value(&["Potential energy rating", "potential_energy_rating"]),
|
||||
address: non_empty_string(&state.data.address[row]),
|
||||
postcode: non_empty_string(&state.data.postcode[row]),
|
||||
is_construction_date_approximate: Some(state.data.is_approx_build_date[row]),
|
||||
property_type: lookup_enum_value(
|
||||
enum_features,
|
||||
&state.enum_name_to_idx,
|
||||
row,
|
||||
&["Property type", "epc_property_type", "pp_property_type"],
|
||||
),
|
||||
built_form: lookup_enum_value(
|
||||
enum_features,
|
||||
&state.enum_name_to_idx,
|
||||
row,
|
||||
&["Property type/built form", "built_form"],
|
||||
),
|
||||
duration: lookup_enum_value(
|
||||
enum_features,
|
||||
&state.enum_name_to_idx,
|
||||
row,
|
||||
&["Leashold/Freehold", "duration"],
|
||||
),
|
||||
current_energy_rating: lookup_enum_value(
|
||||
enum_features,
|
||||
&state.enum_name_to_idx,
|
||||
row,
|
||||
&["Current energy rating", "current_energy_rating"],
|
||||
),
|
||||
potential_energy_rating: lookup_enum_value(
|
||||
enum_features,
|
||||
&state.enum_name_to_idx,
|
||||
row,
|
||||
&["Potential energy rating", "potential_energy_rating"],
|
||||
),
|
||||
lat: state.data.lat[row],
|
||||
lon: state.data.lon[row],
|
||||
features,
|
||||
|
|
@ -192,7 +224,7 @@ pub async fn get_hexagon_properties(
|
|||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
.map_err(|error| (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()))?;
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue