mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-30 10:13:04 +00:00
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'
|
import type { GeometryCollection, Topology } from 'topojson-specification'
|
||||||
|
|
||||||
/** Every committed mesh is polygonal; only the properties differ. */
|
/** 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 = {
|
type BahiaMunicipalityProperties = {
|
||||||
codarea: string
|
codarea: string
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,14 @@
|
||||||
import { feature } from 'topojson-client'
|
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'
|
import type { GeometryCollection, Topology } from 'topojson-specification'
|
||||||
|
|
||||||
type PolygonalFeature<Properties extends Record<string, unknown>> = Feature<
|
|
||||||
Polygon | MultiPolygon,
|
|
||||||
Properties
|
|
||||||
>
|
|
||||||
|
|
||||||
type GeometryModuleBase<
|
type GeometryModuleBase<
|
||||||
Properties extends Record<string, unknown>,
|
Properties extends Record<string, unknown>,
|
||||||
TObjectName extends string,
|
TObjectName extends string,
|
||||||
> = {
|
> = {
|
||||||
topology: Topology<{ [key in TObjectName]: GeometryCollection<Properties> }>
|
topology: Topology<{ [key in TObjectName]: GeometryCollection<Properties> }>
|
||||||
features: readonly PolygonalFeature<Properties>[]
|
features: readonly BahiaFeature<Properties>[]
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexedGeometryModule<
|
type IndexedGeometryModule<
|
||||||
|
|
@ -21,7 +16,7 @@ type IndexedGeometryModule<
|
||||||
TObjectName extends string,
|
TObjectName extends string,
|
||||||
KeyProperty extends keyof Properties & string,
|
KeyProperty extends keyof Properties & string,
|
||||||
> = GeometryModuleBase<Properties, TObjectName> & {
|
> = GeometryModuleBase<Properties, TObjectName> & {
|
||||||
getFeatureByKey: (key: Properties[KeyProperty]) => PolygonalFeature<Properties> | undefined
|
getFeatureByKey: (key: Properties[KeyProperty]) => BahiaFeature<Properties> | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
type BuildGeometryModuleOptions<
|
type BuildGeometryModuleOptions<
|
||||||
|
|
@ -63,7 +58,7 @@ export function buildGeometryModuleFromTopology<
|
||||||
| GeometryModuleBase<Properties, TObjectName>
|
| GeometryModuleBase<Properties, TObjectName>
|
||||||
| IndexedGeometryModule<Properties, TObjectName, KeyProperty> {
|
| IndexedGeometryModule<Properties, TObjectName, KeyProperty> {
|
||||||
const features = feature(topology, topology.objects[objectName])
|
const features = feature(topology, topology.objects[objectName])
|
||||||
.features as PolygonalFeature<Properties>[]
|
.features as BahiaFeature<Properties>[]
|
||||||
|
|
||||||
const base: GeometryModuleBase<Properties, TObjectName> = {
|
const base: GeometryModuleBase<Properties, TObjectName> = {
|
||||||
topology,
|
topology,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,11 @@ import type {
|
||||||
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
||||||
import municipalityTopologyJson from '@/lib/geometries/bahia-municipalities.topo.json'
|
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,
|
topology: municipalityTopologyJson as unknown as MunicipalityTopology,
|
||||||
objectName: 'municipalities',
|
objectName: 'municipalities',
|
||||||
keyProperty: 'codarea',
|
keyProperty: 'codarea',
|
||||||
|
|
@ -14,6 +18,6 @@ const { topology, features, getFeatureByKey } = buildGeometryModuleFromTopology(
|
||||||
|
|
||||||
export const municipalityGeometryModule: MunicipalityGeometryModule = {
|
export const municipalityGeometryModule: MunicipalityGeometryModule = {
|
||||||
topology,
|
topology,
|
||||||
features: features as readonly BahiaMunicipalityFeature[],
|
features,
|
||||||
getMunicipalityFeature: getFeatureByKey,
|
getMunicipalityFeature: getFeatureByKey,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,15 @@ import type {
|
||||||
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
import { buildGeometryModuleFromTopology } from '@/lib/bahiaGeometryModuleFactory'
|
||||||
import territoryTopologyJson from '@/lib/geometries/bahia-identity-territories.topo.json'
|
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,
|
topology: territoryTopologyJson as unknown as TerritoryTopology,
|
||||||
objectName: 'territories',
|
objectName: 'territories',
|
||||||
})
|
})
|
||||||
|
|
||||||
export const territoryGeometryModule: TerritoryGeometryModule = {
|
export const territoryGeometryModule: TerritoryGeometryModule = {
|
||||||
topology,
|
topology,
|
||||||
features: features as readonly BahiaTerritoryFeature[],
|
features,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue