feat: Remove redundant GeoJSON formats and return only mapped result for vector_with_kato
This commit is contained in:
@@ -637,20 +637,16 @@ func (s *DatasetService) ListObservations(ctx context.Context, id uuid.UUID, kat
|
||||
// with empty properties (or an empty collection when the dataset has no
|
||||
// geometry).
|
||||
//
|
||||
// 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 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.
|
||||
// A vector_with_kato dataset always ignores any geometry it carries and instead
|
||||
// joins the districts table on KATO code: 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.
|
||||
func (s *DatasetService) GeoJSON(ctx context.Context, id uuid.UUID) (domain.FeatureCollection, error) {
|
||||
dataset, err := s.loadGeoJSONDataset(ctx, id, true)
|
||||
dataset, err := s.loadGeoJSONDataset(ctx, id)
|
||||
if err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
@@ -670,89 +666,32 @@ func (s *DatasetService) GeoJSON(ctx context.Context, id uuid.UUID) (domain.Feat
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
// vector_with_kato: always ignore the dataset's own geometry and build one
|
||||
// Feature per KATO from the districts table.
|
||||
obs, err := s.repo.ListAllObservations(ctx, id)
|
||||
if err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
grouped, order := groupObservationsByKato(obs)
|
||||
|
||||
// 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 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) {
|
||||
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 {
|
||||
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: map[string]any{"data": data},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// No geometry: build one Feature per KATO from the districts table.
|
||||
features, err := s.districtFeatures(ctx, grouped, order, true)
|
||||
features, err := s.districtFeatures(ctx, grouped, order)
|
||||
if err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
return domain.FeatureCollection{Type: domain.GeoJSONFeatureCollection, Features: features}, nil
|
||||
}
|
||||
|
||||
// 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 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) {
|
||||
if _, err := s.loadGeoJSONDataset(ctx, id, false); err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
obs, err := s.repo.ListAllObservations(ctx, id)
|
||||
if err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
grouped, order := groupObservationsByKato(obs)
|
||||
features, err := s.districtFeatures(ctx, grouped, order, true)
|
||||
if err != nil {
|
||||
return domain.FeatureCollection{}, err
|
||||
}
|
||||
return domain.FeatureCollection{Type: domain.GeoJSONFeatureCollection, Features: features}, nil
|
||||
}
|
||||
|
||||
// loadGeoJSONDataset fetches a dataset for a GeoJSON endpoint and validates that
|
||||
// it is ready and of a supported file type. vector_with_kato is always
|
||||
// accepted; plain vector is accepted only when allowVector is true (the
|
||||
// .kato.geojson endpoint requires KATO observations, which plain vector lacks).
|
||||
func (s *DatasetService) loadGeoJSONDataset(ctx context.Context, id uuid.UUID, allowVector bool) (domain.Dataset, error) {
|
||||
// loadGeoJSONDataset fetches a dataset for the GeoJSON endpoint and validates
|
||||
// that it is ready and of a supported file type (vector or vector_with_kato).
|
||||
func (s *DatasetService) loadGeoJSONDataset(ctx context.Context, id uuid.UUID) (domain.Dataset, error) {
|
||||
dataset, err := s.repo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return domain.Dataset{}, err
|
||||
}
|
||||
|
||||
supported := dataset.FileType == domain.FileTypeVectorWithKato ||
|
||||
(allowVector && dataset.FileType == domain.FileTypeVector)
|
||||
dataset.FileType == domain.FileTypeVector
|
||||
if !supported {
|
||||
allowed := "vector_with_kato"
|
||||
if allowVector {
|
||||
allowed = "vector and vector_with_kato"
|
||||
}
|
||||
return domain.Dataset{}, fmt.Errorf("%w: geojson is only available for %s datasets", domain.ErrValidation, allowed)
|
||||
return domain.Dataset{}, fmt.Errorf("%w: geojson is only available for vector and vector_with_kato datasets", domain.ErrValidation)
|
||||
}
|
||||
if dataset.Status != domain.DatasetStatusReady {
|
||||
return domain.Dataset{}, fmt.Errorf("%w: dataset is not ready (status %q)", domain.ErrConflict, dataset.Status)
|
||||
@@ -760,13 +699,11 @@ func (s *DatasetService) loadGeoJSONDataset(ctx context.Context, id uuid.UUID, a
|
||||
return dataset, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// districtFeatures builds one Feature per KATO from the districts table, with
|
||||
// the grouped observation values (keyed by date) nested under a `data` object
|
||||
// alongside `kato` and `name` in each Feature's 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) ([]domain.Feature, error) {
|
||||
districts, err := s.repo.DistrictGeometriesByKato(ctx, order)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -777,18 +714,10 @@ func (s *DatasetService) districtFeatures(ctx context.Context, grouped map[strin
|
||||
if !ok {
|
||||
continue // skip KATO codes with no district boundary
|
||||
}
|
||||
props := map[string]any{"kato": kato, "name": dist.Name}
|
||||
if nestData {
|
||||
props["data"] = grouped[kato]
|
||||
} else {
|
||||
for date, value := range grouped[kato] {
|
||||
props[date] = value
|
||||
}
|
||||
}
|
||||
features = append(features, domain.Feature{
|
||||
Type: domain.GeoJSONFeature,
|
||||
Geometry: dist.Geometry,
|
||||
Properties: props,
|
||||
Properties: map[string]any{"kato": kato, "name": dist.Name, "data": grouped[kato]},
|
||||
})
|
||||
}
|
||||
return features, nil
|
||||
|
||||
Reference in New Issue
Block a user