fix: Change geojson properties structure

This commit is contained in:
2026-07-01 00:48:02 +05:00
parent 8d9e11db0c
commit f74253140f
4 changed files with 87 additions and 45 deletions

View File

@@ -628,11 +628,13 @@ func (s *DatasetService) ListObservations(ctx context.Context, id uuid.UUID, kat
//
// A vector_with_kato dataset is built from its observations: when it carries its
// own (dissolved) geometry the observations are taken to describe that whole
// geometry, yielding a single Feature whose properties hold only the
// observations keyed by KATO code (each KATO mapping to its date->value pairs);
// otherwise one Feature is emitted per KATO, its boundary taken from the
// districts table and its observation values placed into the Feature's
// properties keyed by date. KATO codes with no matching district are skipped.
// geometry, yielding a single Feature whose properties nest the observations
// under `data`, keyed by KATO code (each KATO mapping to its district `name` and
// its own `data` map of date->value pairs); otherwise one Feature is emitted per
// KATO, its boundary taken from the
// districts table and its observation values nested under a `data` object (keyed
// by date) alongside `kato` and `name`. KATO codes with no matching district are
// skipped.
//
// Only ready datasets are served; a dataset still being processed yields a
// conflict.
@@ -665,24 +667,33 @@ func (s *DatasetService) GeoJSON(ctx context.Context, id uuid.UUID) (domain.Feat
// The dataset has its own geometry (the dissolved union of all features): the
// observations describe that whole geometry, so emit a single Feature wrapping
// it whose properties hold only the observations, keyed by KATO code.
// it whose properties nest the observations under `data`, keyed by KATO code.
// Each KATO entry carries the district `name` alongside its own `data` map of
// date->value pairs.
if hasGeometry(dataset.Geometry) {
props := make(map[string]any, len(order))
districts, err := s.repo.DistrictGeometriesByKato(ctx, order)
if err != nil {
return domain.FeatureCollection{}, err
}
data := make(map[string]any, len(order))
for _, kato := range order {
props[kato] = grouped[kato]
data[kato] = map[string]any{
"name": districts[kato].Name,
"data": grouped[kato],
}
}
return domain.FeatureCollection{
Type: domain.GeoJSONFeatureCollection,
Features: []domain.Feature{{
Type: domain.GeoJSONFeature,
Geometry: dataset.Geometry,
Properties: props,
Properties: map[string]any{"data": data},
}},
}, nil
}
// No geometry: build one Feature per KATO from the districts table.
features, err := s.districtFeatures(ctx, grouped, order)
features, err := s.districtFeatures(ctx, grouped, order, true)
if err != nil {
return domain.FeatureCollection{}, err
}
@@ -692,9 +703,9 @@ func (s *DatasetService) GeoJSON(ctx context.Context, id uuid.UUID) (domain.Feat
// KatoGeoJSON assembles a GeoJSON FeatureCollection (RFC 7946) for a
// vector_with_kato dataset by always joining the districts table on KATO code,
// ignoring any geometry the dataset carries. One Feature is emitted per KATO,
// its boundary taken from the districts table and its observation values placed
// into the Feature's properties keyed by date (alongside `kato` and `name`).
// KATO codes with no matching district are skipped. Plain vector datasets are
// its boundary taken from the districts table and its observation values nested
// under a `data` object (keyed by date) in the Feature's properties, alongside
// `kato` and `name`. KATO codes with no matching district are skipped. Plain vector datasets are
// not supported (they have no KATO observations). Only ready datasets are
// served; a dataset still being processed yields a conflict.
func (s *DatasetService) KatoGeoJSON(ctx context.Context, id uuid.UUID) (domain.FeatureCollection, error) {
@@ -706,7 +717,7 @@ func (s *DatasetService) KatoGeoJSON(ctx context.Context, id uuid.UUID) (domain.
return domain.FeatureCollection{}, err
}
grouped, order := groupObservationsByKato(obs)
features, err := s.districtFeatures(ctx, grouped, order)
features, err := s.districtFeatures(ctx, grouped, order, true)
if err != nil {
return domain.FeatureCollection{}, err
}
@@ -738,11 +749,13 @@ func (s *DatasetService) loadGeoJSONDataset(ctx context.Context, id uuid.UUID, a
return dataset, nil
}
// districtFeatures builds one Feature per KATO from the districts table, placing
// the grouped observation values into each Feature's properties keyed by date
// (alongside `kato` and `name`). KATO codes with no matching district are
// skipped. order drives the deterministic feature order.
func (s *DatasetService) districtFeatures(ctx context.Context, grouped map[string]map[string]any, order []string) ([]domain.Feature, error) {
// districtFeatures builds one Feature per KATO from the districts table,
// alongside `kato` and `name` in each Feature's properties. When nestData is
// true the grouped observation values (keyed by date) are placed under a nested
// `data` object; otherwise they are spread as flat date-keyed properties. KATO
// codes with no matching district are skipped. order drives the deterministic
// feature order.
func (s *DatasetService) districtFeatures(ctx context.Context, grouped map[string]map[string]any, order []string, nestData bool) ([]domain.Feature, error) {
districts, err := s.repo.DistrictGeometriesByKato(ctx, order)
if err != nil {
return nil, err
@@ -754,8 +767,12 @@ func (s *DatasetService) districtFeatures(ctx context.Context, grouped map[strin
continue // skip KATO codes with no district boundary
}
props := map[string]any{"kato": kato, "name": dist.Name}
for date, value := range grouped[kato] {
props[date] = value
if nestData {
props["data"] = grouped[kato]
} else {
for date, value := range grouped[kato] {
props[date] = value
}
}
features = append(features, domain.Feature{
Type: domain.GeoJSONFeature,