import { buffer, FeatureCollection, LineString, point } from "@turf/turf"; import * as wkt from "wkt"; import createAxiosInstance from "./axios_wfs"; // 路网 const segmentsCommonParams = { service: "wfs", version: "2.0.0", request: "GetFeature", outputFormat: "application/json", typeName: "hpaas:segments", // srsName: "urn:x-ogc:def:crs:EPSG:4326", 无需指定,geojson 不受 srs 影响 exceptions: "application/json", }; const areaCommonParams = { service: "wfs", version: "2.0.0", request: "GetFeature", outputFormat: "application/json", typeName: "hpaas:areas", // srsName: "urn:x-ogc:def:crs:EPSG:4326", 无需指定,geojson 不受 srs 影响 exceptions: "application/json", }; export type SegmentFeatureCollectionType = FeatureCollection< LineString, { name: string; out_id: ""; road_name: string; road_type: number; road_uid: string; segment_type: number; uid: string; } >; export type AreaFeatureCollectionType = FeatureCollection< LineString, { name: string; uid: string; political_type: number; postal_code: string; parent_uid: string; } >; class WFSApi { private get: (url: string, params?: any, ifNeedErrorMsg?: boolean) => Promise; private post: (url: string, params: any, ifNeedErrorMsg?: boolean) => Promise; baseUrl = "http://10.0.10.174:9999/geoserver"; timeout: number; constructor(baseUrl: string, options?: { timeout?: number }) { this.baseUrl = baseUrl; this.timeout = options?.timeout || 60000; const request = createAxiosInstance({ baseUrl: this.baseUrl, timeout: this.timeout, }); this.get = request.get; this.post = request.post; } querySegmentFeaturesAtPoint(lng: number, lat: number) { // 点击缓冲区 const _point = point([lng, lat]); const buffered = buffer(_point, 10, { units: "meters" }); return this.get( "/wfs?cql_filter=" + `CROSSES(geometry, SRID=4326;${wkt.stringify(buffered)})`, segmentsCommonParams ); } querySegmentFeaturesAtBounds(bounds: [number, number, number, number]) { // 点击缓冲区 return this.get( "/wfs?bbox=" + `${bounds.join(",")}, EPSG:4326`, segmentsCommonParams ); } querySegmentFeaturesByRoadUid(roadUid: string) { return this.get( "/wfs?cql_filter=" + encodeURIComponent(`road_uid='${roadUid}'`), segmentsCommonParams ); } queryAreaByUid(areaUid: string) { return this.get( "/wfs?cql_filter=" + encodeURIComponent(`uid='${areaUid}'`), areaCommonParams ); } } export default WFSApi;