抽象统一定位方法,支持多种地理要素
This commit is contained in:
@@ -15,6 +15,7 @@ import VectorLayer from "ol/layer/Vector";
|
||||
import { Style, Stroke, Fill, Circle } from "ol/style";
|
||||
import Feature from "ol/Feature";
|
||||
import { GeoJSON } from "ol/format";
|
||||
import Point from "ol/geom/Point";
|
||||
import { bbox, featureCollection } from "@turf/turf";
|
||||
import StyleEditorPanel from "./StyleEditorPanel";
|
||||
import { LayerStyleState } from "./StyleEditorPanel";
|
||||
@@ -72,38 +73,52 @@ const Toolbar: React.FC<ToolbarProps> = ({
|
||||
useCallback(
|
||||
(action) => {
|
||||
const geojsonFormat = new GeoJSON();
|
||||
const zoomToFeatures = (features: Feature[]) => {
|
||||
const zoomToFeatures = (
|
||||
features: Feature[],
|
||||
geometryKind: "point" | "line",
|
||||
) => {
|
||||
if (features.length === 0) return;
|
||||
|
||||
if (geometryKind === "point" && features.length === 1) {
|
||||
const geometry = features[0].getGeometry();
|
||||
if (geometry instanceof Point) {
|
||||
map?.getView().animate({
|
||||
center: geometry.getCoordinates(),
|
||||
zoom: 18,
|
||||
duration: 1000,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const geojsonFeatures = features.map((f) =>
|
||||
geojsonFormat.writeFeatureObject(f),
|
||||
);
|
||||
const extent = bbox(featureCollection(geojsonFeatures as any));
|
||||
if (extent) {
|
||||
map?.getView().fit(extent, { maxZoom: 18, duration: 1000 });
|
||||
map?.getView().fit(extent, {
|
||||
maxZoom: 18,
|
||||
duration: 1000,
|
||||
padding: geometryKind === "line" ? [60, 60, 60, 60] : [40, 40, 40, 40],
|
||||
});
|
||||
}
|
||||
};
|
||||
const locateFeatures = (
|
||||
ids: string[],
|
||||
layer: string,
|
||||
geometryKind: "point" | "line",
|
||||
) => {
|
||||
queryFeaturesByIds(ids, layer).then((features) => {
|
||||
if (features.length > 0) {
|
||||
setHighlightFeatures(features);
|
||||
zoomToFeatures(features, geometryKind);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
switch (action.type) {
|
||||
case "locate_nodes": {
|
||||
queryFeaturesByIds(action.ids, "geo_junctions_mat").then(
|
||||
(features) => {
|
||||
if (features.length > 0) {
|
||||
setHighlightFeatures(features);
|
||||
zoomToFeatures(features);
|
||||
}
|
||||
},
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "locate_pipes": {
|
||||
queryFeaturesByIds(action.ids, "geo_pipes_mat").then(
|
||||
(features) => {
|
||||
if (features.length > 0) {
|
||||
setHighlightFeatures(features);
|
||||
zoomToFeatures(features);
|
||||
}
|
||||
},
|
||||
);
|
||||
case "locate_features": {
|
||||
locateFeatures(action.ids, action.layer, action.geometryKind);
|
||||
break;
|
||||
}
|
||||
case "view_history": {
|
||||
|
||||
Reference in New Issue
Block a user