-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add tile_json_parser * feat: update lint --------- Co-authored-by: kip.song <kip.song@nio.com>
- Loading branch information
Showing
4 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
getData, | ||
getURLFromTemplate, | ||
RequestParameters, | ||
SourceTile, | ||
TileLoadParams, | ||
} from '@antv/l7-utils'; | ||
import { Feature } from '@turf/helpers'; | ||
import { IParserData, ITileSource } from '../interface'; | ||
import VtSource from '../source/geojsonvt'; | ||
|
||
import { ITileParserCFG } from '@antv/l7-core'; | ||
|
||
export type MapboxVectorTile = { | ||
layers: { [key: string]: { features: Feature[] } }; | ||
}; | ||
|
||
const getVectorTile = async ( | ||
url: string, | ||
tile: SourceTile, | ||
requestParameters?: Partial<RequestParameters>, | ||
getCustomData?: ITileParserCFG['getCustomData'], | ||
): Promise<ITileSource> => { | ||
const params = { x: tile.x, y: tile.y, z: tile.z }; | ||
const tileUrl = getURLFromTemplate(url, params); | ||
return new Promise((resolve) => { | ||
if (getCustomData) { | ||
getCustomData(params, (err, data) => { | ||
if (err || !data) { | ||
const vectorTile: MapboxVectorTile = { | ||
layers: { defaultLayer: { features: [] } }, | ||
}; | ||
const vectorSource = new VtSource(vectorTile, tile.x, tile.y, tile.z); | ||
resolve(vectorSource); | ||
} else { | ||
const vectorTile: MapboxVectorTile = { | ||
layers: { defaultLayer: { features: data.features } }, | ||
}; | ||
const vectorSource = new VtSource(vectorTile, tile.x, tile.y, tile.z); | ||
resolve(vectorSource); | ||
} | ||
}); | ||
} else { | ||
getData( | ||
{ | ||
...requestParameters, | ||
url: tileUrl, | ||
}, | ||
(err, data) => { | ||
if (err || !data) { | ||
const vectorTile: MapboxVectorTile = { | ||
layers: { | ||
defaultLayer: { | ||
features: [], | ||
}, | ||
}, | ||
}; | ||
const vectorSource = new VtSource( | ||
vectorTile, | ||
tile.x, | ||
tile.y, | ||
tile.z, | ||
); | ||
resolve(vectorSource); | ||
} else { | ||
const json = JSON.parse(data); | ||
const vectorTile: MapboxVectorTile = { | ||
layers: { | ||
defaultLayer: { | ||
features: json, | ||
}, | ||
}, | ||
}; | ||
const vectorSource = new VtSource( | ||
vectorTile, | ||
tile.x, | ||
tile.y, | ||
tile.z, | ||
); | ||
resolve(vectorSource); | ||
} | ||
}, | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
export default function jsonTile( | ||
url: string, | ||
cfg: ITileParserCFG, | ||
): IParserData { | ||
const getTileData = (_: TileLoadParams, tile: SourceTile) => { | ||
return getVectorTile(url, tile, cfg?.requestParameters, cfg.getCustomData); | ||
}; | ||
|
||
const tilesetOptions = { | ||
...cfg, | ||
getTileData, | ||
}; | ||
|
||
return { | ||
dataArray: [], | ||
tilesetOptions, | ||
isTile: true, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters