| 32 | const DEFAULT_NAME_PROPERTY = 'name' as const; |
| 33 | |
| 34 | export class GeoJSONResource implements GeoResource { |
| 35 | |
| 36 | readonly type = 'geoJSON'; |
| 37 | private _geoJSON: GeoJSON | GeoJSONCompressed; |
| 38 | private _specialAreas: GeoSpecialAreas; |
| 39 | private _mapName: string; |
| 40 | |
| 41 | private _parsedMap = createHashMap<{ |
| 42 | regions: GeoJSONRegion[]; |
| 43 | boundingRect: BoundingRect; |
| 44 | }, string>(); |
| 45 | |
| 46 | constructor( |
| 47 | mapName: string, |
| 48 | geoJSON: GeoJSONSourceInput, |
| 49 | specialAreas: GeoSpecialAreas |
| 50 | ) { |
| 51 | this._mapName = mapName; |
| 52 | this._specialAreas = specialAreas; |
| 53 | |
| 54 | // PENDING: delay the parse to the first usage to rapid up the FMP? |
| 55 | this._geoJSON = parseInput(geoJSON); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param nameMap can be null/undefined |
| 60 | * @param nameProperty can be null/undefined |
| 61 | */ |
| 62 | load(nameMap: NameMap, nameProperty: string) { |
| 63 | |
| 64 | nameProperty = nameProperty || DEFAULT_NAME_PROPERTY; |
| 65 | |
| 66 | let parsed = this._parsedMap.get(nameProperty); |
| 67 | if (!parsed) { |
| 68 | const rawRegions = this._parseToRegions(nameProperty); |
| 69 | parsed = this._parsedMap.set(nameProperty, { |
| 70 | regions: rawRegions, |
| 71 | boundingRect: calculateBoundingRect(rawRegions) |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | const regionsMap = createHashMap<GeoJSONRegion>(); |
| 76 | |
| 77 | const finalRegions: GeoJSONRegion[] = []; |
| 78 | each(parsed.regions, function (region) { |
| 79 | let regionName = region.name; |
| 80 | |
| 81 | // Try use the alias in geoNameMap |
| 82 | if (nameMap && hasOwn(nameMap, regionName)) { |
| 83 | region = region.cloneShallow(regionName = nameMap[regionName]); |
| 84 | } |
| 85 | |
| 86 | finalRegions.push(region); |
| 87 | regionsMap.set(regionName, region); |
| 88 | }); |
| 89 | |
| 90 | return { |
| 91 | regions: finalRegions, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…