import localforage from "localforage"; import { Bounds } from "@antv/l7"; import api from "../../../http/api_road_network"; import wfsApi from "../api/wfs"; import { getVersionCode } from "../../../utils/user"; import { AreaStructure, SegmentStructure, ControlPoint, IRoadServer } from "../types"; import { FeatureCollection, LineString } from "@turf/turf"; export const IndexDBstore = localforage.createInstance({ name: "segments-v2", }); export const IndexDBArea = localforage.createInstance({ name: "areas", }); /** * 根据区域uid查询区域详情 * @param uid 区域uid */ export const getAreaByUid = async (uid: string, disableCache = true) => { if (!disableCache) { const cache = await IndexDBArea.getItem(uid); console.log("get area from cache"); if (cache) { return cache; } } const area = (await api.area.queryAreaByUid(uid))[0]; IndexDBArea.setItem(uid, area); return area; }; export const getAreaByParentUid = async (parentUid: string) => { return await api.area.selectRelationByParentAreaUid(parentUid); }; export const queryPoiByWKT = async (wkt: string) => { return await api.poi.queryByWKT(wkt); }; /** * 根据区域wkt查询区域内路段信息 * @param areaUid 区域uid * @param wkt 区域范围格式化string */ export const getSegmentsData = async (areaUid: string, wkt: string) => { const versionCode = getVersionCode(); let roadTypeList: number[] = []; if (versionCode === "city") { roadTypeList = [101, 102, 201, 202, 203, 204, 205]; } else { roadTypeList = [101, 102, 204, 202, 201]; } const params = { wkt: wkt, noNeedSegmentUidList: [], roadTypeList, }; const segments = await api.segment.querySegmentListByWKT(params); IndexDBstore.setItem(areaUid, JSON.stringify(segments)); return segments; }; export enum RoadType { 高速国道 = 101, 高速省道 = 102, 城市国道 = 201, 城市省道 = 202, 城市县道 = 203, 城市快速路 = 204, 城市主干道 = 205, 小路 = 207, } /** * 根据bounds查询路段 * @param bounds 区域范围 */ export const getSegmentsByBounds = async (bounds: Bounds, areaUid?: string) => { const params = { startPoint: { latitude: bounds[0][1], longitude: bounds[0][0], }, endPoint: { latitude: bounds[1][1], longitude: bounds[1][0], }, noNeedSegmentUidList: [], roadTypeList: [RoadType.高速国道, RoadType.高速省道], }; const segments = await api.segment.querySegmentListByBounds(params); areaUid && IndexDBstore.setItem(areaUid + "-bounds", JSON.stringify(segments)); return segments; }; /** * 根据boudns查询控制点 * @param bounds 区域范围 */ export const getControlPointsByBounds = async (bounds: Bounds) => { const params = { startPoint: { latitude: bounds[0][1], longitude: bounds[0][0], }, endPoint: { latitude: bounds[1][1], longitude: bounds[1][0], }, typeList: [27, 28], }; const points = await api.point.queryControlPointsByBounds(params); return points; }; export const querySegment = async (segmentUidList: string[]) => { const segments = await api.segment.querySegment(segmentUidList); return segments; }; export const queryRoad = async (roadUid: string) => { const road = await api.road.queryRoad(roadUid); return road; };