refactor(B5): reuse BahiaFeature type and drop wrapper casts
This commit is contained in:
parent
f4b41c2b9d
commit
8188c9a386
4 changed files with 19 additions and 14 deletions
|
|
@ -2,7 +2,10 @@ import type { Feature, MultiPolygon, Polygon } from 'geojson'
|
|||
import type { GeometryCollection, Topology } from 'topojson-specification'
|
||||
|
||||
/** Every committed mesh is polygonal; only the properties differ. */
|
||||
type BahiaFeature<Properties> = Feature<Polygon | MultiPolygon, Properties>
|
||||
export type BahiaFeature<Properties extends Record<string, unknown>> = Feature<
|
||||
Polygon | MultiPolygon,
|
||||
Properties
|
||||
>
|
||||
|
||||
type BahiaMunicipalityProperties = {
|
||||
codarea: string
|
||||
|
|
|
|||
|
|
@ -1,19 +1,14 @@
|
|||
import { feature } from 'topojson-client'
|
||||
|
||||
import type { Feature, MultiPolygon, Polygon } from 'geojson'
|
||||
import type { BahiaFeature } from '@/lib/bahiaGeometriesTypes'
|
||||
import type { GeometryCollection, Topology } from 'topojson-specification'
|
||||
|
||||
type PolygonalFeature<Properties extends Record<string, unknown>> = Feature<
|
||||
Polygon | MultiPolygon,
|
||||
Properties
|
||||
>
|
||||
|
||||
type GeometryModuleBase<
|
||||
Properties extends Record<string, unknown>,
|
||||
TObjectName extends string,
|
||||
> = {
|
||||
topology: Topology<{ [key in TObjectName]: GeometryCollection<Properties> }>
|
||||
features: readonly PolygonalFeature<Properties>[]
|
||||
features: readonly BahiaFeature<Properties>[]
|
||||
}
|
||||
|
||||
type IndexedGeometryModule<
|
||||
|
|
@ -21,7 +16,7 @@ type IndexedGeometryModule<
|
|||
TObjectName extends string,
|
||||
KeyProperty extends keyof Properties & string,
|
||||
> = GeometryModuleBase<Properties, TObjectName> & {
|
||||
getFeatureByKey: (key: Properties[KeyProperty]) => PolygonalFeature<Properties> | undefined
|
||||
getFeatureByKey: (key: Properties[KeyProperty]) => BahiaFeature<Properties> | undefined
|
||||
}
|
||||
|
||||
type BuildGeometryModuleOptions<
|
||||
|
|
@ -63,7 +58,7 @@ export function buildGeometryModuleFromTopology<
|
|||
| GeometryModuleBase<Properties, TObjectName>
|
||||
| IndexedGeometryModule<Properties, TObjectName, KeyProperty> {
|
||||
const features = feature(topology, topology.objects[objectName])
|
||||
.features as PolygonalFeature<Properties>[]
|
||||
.features as BahiaFeature<Properties>[]
|
||||
|
||||
const base: GeometryModuleBase<Properties, TObjectName> = {
|
||||
topology,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@ import type {
|
|||
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
||||
import municipalityTopologyJson from '@/lib/geometries/bahia-municipalities.topo.json'
|
||||
|
||||
const { topology, features, getFeatureByKey } = buildGeometryModuleFromTopology({
|
||||
const { topology, features, getFeatureByKey } = buildGeometryModuleFromTopology<
|
||||
BahiaMunicipalityFeature['properties'],
|
||||
'municipalities',
|
||||
'codarea'
|
||||
>({
|
||||
topology: municipalityTopologyJson as unknown as MunicipalityTopology,
|
||||
objectName: 'municipalities',
|
||||
keyProperty: 'codarea',
|
||||
|
|
@ -14,6 +18,6 @@ const { topology, features, getFeatureByKey } = buildGeometryModuleFromTopology(
|
|||
|
||||
export const municipalityGeometryModule: MunicipalityGeometryModule = {
|
||||
topology,
|
||||
features: features as readonly BahiaMunicipalityFeature[],
|
||||
features,
|
||||
getMunicipalityFeature: getFeatureByKey,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@ import type {
|
|||
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
||||
import territoryTopologyJson from '@/lib/geometries/bahia-identity-territories.topo.json'
|
||||
|
||||
const { topology, features } = buildGeometryModuleFromTopology({
|
||||
const { topology, features } = buildGeometryModuleFromTopology<
|
||||
BahiaTerritoryFeature['properties'],
|
||||
'territories'
|
||||
>({
|
||||
topology: territoryTopologyJson as unknown as TerritoryTopology,
|
||||
objectName: 'territories',
|
||||
})
|
||||
|
||||
export const territoryGeometryModule: TerritoryGeometryModule = {
|
||||
topology,
|
||||
features: features as readonly BahiaTerritoryFeature[],
|
||||
features,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue