import React, { useMemo, useState, useEffect, ReactElement, ComponentType, } from 'react'; import {ExpressionField, FilterExpression} from '../utils/VietmapStyles'; import CircleLayer from './CircleLayer'; import RasterLayer from './RasterLayer'; import SymbolLayer from './SymbolLayer'; import LineLayer from './LineLayer'; import FillLayer from './FillLayer'; import FillExtrusionLayer from './FillExtrusionLayer'; import BackgroundLayer from './BackgroundLayer'; import HeatmapLayer from './HeatmapLayer'; import VectorSource from './VectorSource'; import RasterSource from './RasterSource'; import ImageSource from './ImageSource'; import ShapeSource from './ShapeSource'; import {BaseLayerProps} from './AbstractLayer'; function toCamelCase(s: string): string { return s.replace(/([-_][a-z])/gi, $1 => { return $1.toUpperCase().replace('-', '').replace('_', ''); }); } // Patches the Mapbox Style Specification keys into the style props attributes: // icon-allow-overlap -> iconAllowOverlap function toCamelCaseKeys( oldObj: Record, ): Record { if (!oldObj) { return {}; } const newObj: Record = {}; Object.keys(oldObj).forEach(key => { const value = oldObj[key]; if (key.includes('-')) { newObj[toCamelCase(key)] = value; } else { newObj[key] = value; } }); return newObj; } function getLayerComponentType( layer: VietmapJSONLayer, ): ComponentType | null { const {type} = layer; switch (type) { case 'circle': return CircleLayer; case 'symbol': return SymbolLayer; case 'raster': return RasterLayer; case 'line': return LineLayer; case 'fill': return FillLayer; case 'fill-extrusion': return FillExtrusionLayer; case 'background': return BackgroundLayer; case 'heatmap': return HeatmapLayer; } console.warn(`Mapbox layer type '${type}' is not supported/`); return null; } interface VietmapJSONLayer { type: string; paint: {[k: string]: unknown}; layout: {[k: string]: unknown}; source?: string; 'source-layer'?: string; minzoom?: number; maxzoom?: number; filter?: FilterExpression; id: string; } function asLayerComponent( layer: VietmapJSONLayer, ): ReactElement | null { const LayerComponent = getLayerComponentType(layer); if (!LayerComponent) { return null; } const style = { ...toCamelCaseKeys(layer.paint), ...toCamelCaseKeys(layer.layout), }; const layerProps: Partial = {}; if (layer.source) { layerProps.sourceID = layer.source; } if (layer['source-layer']) { layerProps.sourceLayerID = layer['source-layer']; } if (layer.minzoom) { layerProps.minZoomLevel = layer.minzoom; } if (layer.maxzoom) { layerProps.maxZoomLevel = layer.maxzoom; } if (layer.filter) { layerProps.filter = layer.filter; } if (Object.keys(style).length) { layerProps.style = style; } return ; } interface VietmapJSONSource { type: string; url?: string; tiles?: string[]; minzoom?: number; maxzoom?: number; attribution?: string; scheme?: 'xyz' | 'tms'; bounds?: number[]; buffer?: number; tileSize?: number; coordinates?: [ [number, number], [number, number], [number, number], [number, number], ]; cluster?: boolean; clusterMaxZoom?: number; clusterMinPoints?: number; clusterRadius?: number; clusterProperties?: {[propertyName: string]: ExpressionField}; data?: string | object; filter?: FilterExpression; generateId?: boolean; lineMetrics?: boolean; tolerance?: number; } type SourceProps = { url?: string; tileUrlTemplates?: string[]; minZoomLevel?: number; maxZoomLevel?: number; attribution?: string; tms?: boolean; }; function getTileSourceProps(source: VietmapJSONSource): SourceProps { const sourceProps: Partial = {}; if (source.url) { sourceProps.url = source.url; } if (source.tiles) { sourceProps.tileUrlTemplates = source.tiles; } if (source.minzoom !== undefined) { sourceProps.minZoomLevel = source.minzoom; } if (source.maxzoom !== undefined) { sourceProps.maxZoomLevel = source.maxzoom; } if (source.attribution) { sourceProps.attribution = source.attribution; } if (source.scheme && source.scheme === 'tms') { sourceProps.tms = true; } return sourceProps; } function getVectorSource(id: string, source: VietmapJSONSource): ReactElement { const sourceProps = {...getTileSourceProps(source)}; return ; } function getRasterSource(id: string, source: VietmapJSONSource): ReactElement { const sourceProps: SourceProps & {tileSize?: number} = { ...getTileSourceProps(source), }; if (source.tileSize) { sourceProps.tileSize = source.tileSize; } return ; } function getImageSource(id: string, source: VietmapJSONSource): ReactElement { const sourceProps = { url: source.url, coordinates: source.coordinates, }; return ; } type ShapeSourceShape = (typeof ShapeSource.prototype.props)['shape']; function getShapeSource(id: string, source: VietmapJSONSource): ReactElement { const sourceProps: SourceProps & { shape?: ShapeSourceShape; cluster?: boolean; clusterRadius?: number; clusterMaxZoomLevel?: number; clusterProperties?: {[propertyName: string]: ExpressionField}; buffer?: number; tolerance?: number; lineMetrics?: boolean; } = {}; if (source.data && typeof source.data === 'string') { sourceProps.url = source.data; } else if (source.data && typeof source.data === 'object') { sourceProps.shape = source.data as ShapeSourceShape; } if (source.cluster !== undefined) { sourceProps.cluster = source.cluster; } if (source.clusterRadius !== undefined) { sourceProps.clusterRadius = source.clusterRadius; } if (source.maxzoom !== undefined) { sourceProps.maxZoomLevel = source.maxzoom; } if (source.clusterMaxZoom !== undefined) { sourceProps.clusterMaxZoomLevel = source.clusterMaxZoom; } if (source.clusterProperties !== undefined) { sourceProps.clusterProperties = source.clusterProperties; } if (source.buffer !== undefined) { sourceProps.buffer = source.buffer; } if (source.tolerance !== undefined) { sourceProps.tolerance = source.tolerance; } if (source.lineMetrics !== undefined) { sourceProps.lineMetrics = source.lineMetrics; } return ; } function asSourceComponent( id: string, source: VietmapJSONSource, ): ReactElement | null { switch (source.type) { case 'vector': return getVectorSource(id, source); case 'raster': return getRasterSource(id, source); case 'image': return getImageSource(id, source); case 'geojson': return getShapeSource(id, source); } console.warn(`Vietmap source type '${source.type}' is not supported`); return null; } interface VietmapJSON { layers?: VietmapJSONLayer[]; sources?: {[key: string]: VietmapJSONSource}; } interface StyleProps { /** * A JSON object conforming to the schema described in the Vietmap Style Specification, or a URL to such JSON. */ json?: VietmapJSON | URL; } /** * Style is a component that automatically adds sources / layers to the map using Vietmap Style Spec. * Other fields such as `sprites`, `glyphs` etc. will be ignored. Not all layer / source attributes from the style spec are supported, in general the supported attributes will be mentioned under https://github.com/vietmap-company/vietmap-gl-react-native/tree/main/docs. * * TODO: Maintainer forking this project does not understand the above comment regarding what is supported. */ const Style = (props: StyleProps): ReactElement => { const [fetchedJson, setFetchedJson] = useState({}); const json: VietmapJSON = typeof props.json === 'object' ? props.json : fetchedJson; // Fetch style when props.json is a URL useEffect(() => { const abortController = new AbortController(); const fetchStyleJson = async (url: string): Promise => { try { const response = await fetch(url, { signal: abortController.signal, }); const responseJson = await response.json(); setFetchedJson(responseJson); } catch (error: unknown) { const e = error as {name?: string}; if (e.name === 'AbortError') { return; } throw e; } }; if (typeof props.json === 'string') { fetchStyleJson(props.json); } return function cleanup(): void { abortController.abort(); }; }, [props.json]); // Extract layer components from json const layerComponents = useMemo(() => { if (!json.layers) { return []; } return json.layers .map(asLayerComponent) .filter((x: ReactElement | null) => !!x); }, [json.layers]); // Extract source components from json const sourceComponents = useMemo(() => { const {sources} = json; if (!sources || !Object.keys(sources)) { return []; } return Object.keys(sources) .map(id => asSourceComponent(id, sources[id])) .filter(x => !!x); }, [json]); return ( <> {sourceComponents} {layerComponents} ); }; export default Style;