-
Notifications
You must be signed in to change notification settings - Fork 58
/
ScaleCombo.tsx
165 lines (141 loc) · 4.63 KB
/
ScaleCombo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import './ScaleCombo.less';
import MapUtil from '@terrestris/ol-util/dist/MapUtil/MapUtil';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import { Select } from 'antd';
import { SelectProps } from 'antd/lib/select';
import _clone from 'lodash/clone';
import _isEmpty from 'lodash/isEmpty';
import _isEqual from 'lodash/isEqual';
import _isFunction from 'lodash/isFunction';
import _isInteger from 'lodash/isInteger';
import _isNil from 'lodash/isNil';
import _isNumber from 'lodash/isNumber';
import _reverse from 'lodash/reverse';
import { ObjectEvent as OlObjectEvent } from 'ol/Object';
import OlView from 'ol/View';
import React, {
useCallback,
useEffect,
useMemo,
useState
} from 'react';
import { CSS_PREFIX } from '../../constants';
type OwnProps = {
resolutionsFilter?: (item: any, index?: number, resolutions?: number[]) => boolean;
syncWithMap?: boolean;
scales?: number[];
onZoomLevelSelect?: (zoomLevel: string) => void;
resolutions?: number[];
};
export type ScaleComboProps = SelectProps & OwnProps;
const defaultClassName = `${CSS_PREFIX}scalecombo`;
const ScaleCombo: React.FC<ScaleComboProps> = ({
resolutionsFilter = () => true,
syncWithMap = true,
scales = [],
className,
onZoomLevelSelect,
resolutions,
...passThroughProps
}) => {
const [internalZoomLevel, setInternalZoomLevel] = useState<number>();
const map = useMap();
const getOptionsFromMap = useCallback(() => {
if (!map) {
return [];
}
const optionScales: number[] = [];
const view = map.getView();
// use existing resolutions array if exists
const viewResolutions = view.getResolutions();
const pushScale = (s: number[], r: number, v: OlView) => {
const scale = MapUtil.getScaleForResolution(r, v.getProjection().getUnits());
if (!scale) {
return;
}
const roundScale = MapUtil.roundScale(scale);
if (optionScales.includes(roundScale) ) {
return;
}
optionScales.push(roundScale);
};
if (_isEmpty(viewResolutions) || _isNil(viewResolutions)) {
for (let currentZoomLevel = view.getMaxZoom(); currentZoomLevel >= view.getMinZoom(); currentZoomLevel--) {
const resolution = view.getResolutionForZoom(currentZoomLevel);
if (resolutionsFilter(resolution)) {
pushScale(optionScales, resolution, view);
}
}
} else {
const reversedResolutions = _reverse(_clone(viewResolutions));
reversedResolutions
.filter(resolutionsFilter)
.forEach(resolution => pushScale(scales, resolution, view));
}
return optionScales;
}, [map, resolutionsFilter, scales]);
const zoomListener = useCallback((evt: OlObjectEvent) => {
const zoom = (evt.target as OlView).getZoom();
let roundZoom = 0;
if (_isNumber(zoom)) {
roundZoom = Math.round(zoom);
}
setInternalZoomLevel(roundZoom);
}, []);
const internalScales = useMemo(() => {
return scales.length > 0 ? scales : getOptionsFromMap();
}, [scales, getOptionsFromMap]);
useEffect(() => {
if (!map) {
return;
}
if (syncWithMap) {
map.getView().on('change:resolution', zoomListener);
} else {
map.getView().un('change:resolution', zoomListener);
}
}, [map, syncWithMap, zoomListener]);
useEffect(() => {
setInternalZoomLevel(map?.getView().getZoom());
}, [map]);
const onZoomLevelSelectInternal = (selectedScale: string) => {
if (!map) {
return;
}
if (onZoomLevelSelect) {
onZoomLevelSelect(selectedScale);
} else {
// The default.
const mapView = map.getView();
const calculatedResolution = MapUtil.getResolutionForScale(
parseInt(selectedScale, 10), mapView.getProjection().getUnits()
);
mapView.setResolution(calculatedResolution);
}
};
const determineOptionKeyForZoomLevel = (zoom: number): string | undefined => {
if (!_isInteger(zoom) || (internalScales.length - 1 - zoom) < 0) {
return undefined;
}
return internalScales[internalScales.length - 1 - zoom].toString();
};
const finalClassName = className
? `${className} ${defaultClassName}`
: defaultClassName;
return (
<Select
showSearch
onChange={onZoomLevelSelectInternal}
filterOption={(input, option) => option?.key?.toString().startsWith(input) ?? false}
value={determineOptionKeyForZoomLevel(internalZoomLevel ?? 0)}
size="small"
className={finalClassName}
options={internalScales.map(roundScale => ({
value: roundScale.toString(),
label: `1:${roundScale.toLocaleString()}`
}))}
{...passThroughProps}
/>
);
};
export default ScaleCombo;