原文链接及内容

运行界面

演示了在客户端OpenStreetMap重新投影至NAD83 Indiana East,包括使用美国单位的比例尺控件。
我特意去瞅了下源码,这里截取了部分代码,来理解下图上mi这个单位:

1
2
3
4
5
6
7
8
9
10
11
12
else if (units == 'us') {
if (nominalCount < 0.9144) {
suffix = 'in';
pointResolution *= 39.37;
} else if (nominalCount < 1609.344) {
suffix = 'ft';
pointResolution /= 0.30480061;
} else {
suffix = 'mi';
pointResolution /= 1609.3472;
}
}

而默认单位即公制单位(metric)源码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
else if (units == 'metric') {
if (nominalCount < 0.001) {
suffix = 'μm';
pointResolution *= 1000000;
} else if (nominalCount < 1) {
suffix = 'mm';
pointResolution *= 1000;
} else if (nominalCount < 1000) {
suffix = 'm';
} else {
suffix = 'km';
pointResolution /= 1000;
}
}

main.js代码如下:

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
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import proj4 from 'proj4';
import {ScaleLine} from 'ol/control.js';
import {fromLonLat, transformExtent} from 'ol/proj.js';
import {register} from 'ol/proj/proj4.js';

proj4.defs(
'Indiana-East',
'PROJCS["IN83-EF",GEOGCS["LL83",DATUM["NAD83",' +
'SPHEROID["GRS1980",6378137.000,298.25722210]],PRIMEM["Greenwich",0],' +
'UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],' +
'PARAMETER["false_easting",328083.333],' +
'PARAMETER["false_northing",820208.333],' +
'PARAMETER["scale_factor",0.999966666667],' +
'PARAMETER["central_meridian",-85.66666666666670],' +
'PARAMETER["latitude_of_origin",37.50000000000000],' +
'UNIT["Foot_US",0.30480060960122]]'
);
register(proj4);

const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
],
target: 'map',
view: new View({
projection: 'Indiana-East',
center: fromLonLat([-85.685, 39.891], 'Indiana-East'),
zoom: 7,
extent: transformExtent(
[-172.54, 23.81, -47.74, 86.46],
'EPSG:4326',
'Indiana-East'
),
minZoom: 6,
}),
});

map.addControl(new ScaleLine({units: 'us'}));

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OpenStreetMap Reprojection with ScaleLine Control</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>