forked from mapbox/togeojson
-
Notifications
You must be signed in to change notification settings - Fork 67
/
ground_overlay.ts
156 lines (136 loc) · 3.75 KB
/
ground_overlay.ts
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
import { Feature, Polygon } from "geojson";
import { StyleMap, get1, num1, getMulti } from "../shared";
import {
extractCascadedStyle,
extractExtendedData,
extractTimeSpan,
extractTimeStamp,
getMaybeHTMLDescription,
Schema,
} from "./shared";
import { extractIconHref, extractStyle } from "./extractStyle";
import { coord, fixRing, getCoordinates } from "./geometry";
import { KMLOptions } from "lib/kml";
interface BoxGeometry {
bbox?: BBox;
geometry: Polygon;
}
function getGroundOverlayBox(node: Element): BoxGeometry | null {
const latLonQuad = get1(node, "gx:LatLonQuad");
if (latLonQuad) {
const ring = fixRing(coord(getCoordinates(node)));
return {
geometry: {
type: "Polygon",
coordinates: [ring],
},
};
}
return getLatLonBox(node);
}
type BBox = [number, number, number, number];
const DEGREES_TO_RADIANS = Math.PI / 180;
function rotateBox(
bbox: BBox,
coordinates: Polygon["coordinates"],
rotation: number
): Polygon["coordinates"] {
const center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
return [
coordinates[0].map((coordinate) => {
const dy = coordinate[1] - center[1];
const dx = coordinate[0] - center[0];
const distance = Math.sqrt(Math.pow(dy, 2) + Math.pow(dx, 2));
const angle = Math.atan2(dy, dx) - rotation * DEGREES_TO_RADIANS;
return [
center[0] + Math.cos(angle) * distance,
center[1] + Math.sin(angle) * distance,
];
}),
];
}
function getLatLonBox(node: Element): BoxGeometry | null {
const latLonBox = get1(node, "LatLonBox");
if (latLonBox) {
const north = num1(latLonBox, "north");
const west = num1(latLonBox, "west");
const east = num1(latLonBox, "east");
const south = num1(latLonBox, "south");
const rotation = num1(latLonBox, "rotation");
if (
typeof north === "number" &&
typeof south === "number" &&
typeof west === "number" &&
typeof east === "number"
) {
const bbox: BBox = [west, south, east, north];
let coordinates = [
[
[west, north], // top left
[east, north], // top right
[east, south], // top right
[west, south], // bottom left
[west, north], // top left (again)
],
];
if (typeof rotation === "number") {
coordinates = rotateBox(bbox, coordinates, rotation);
}
return {
bbox,
geometry: {
type: "Polygon",
coordinates,
},
};
}
}
return null;
}
export function getGroundOverlay(
node: Element,
styleMap: StyleMap,
schema: Schema,
options: KMLOptions
): Feature<Polygon | null> | null {
const box = getGroundOverlayBox(node);
const geometry = box?.geometry || null;
if (!geometry && options.skipNullGeometry) {
return null;
}
const feature: Feature<Polygon | null> = {
type: "Feature",
geometry,
properties: Object.assign(
/**
* Related to
* https://gist.github.com/tmcw/037a1cb6660d74a392e9da7446540f46
*/
{ "@geometry-type": "groundoverlay" },
getMulti(node, [
"name",
"address",
"visibility",
"open",
"phoneNumber",
"description",
]),
getMaybeHTMLDescription(node),
extractCascadedStyle(node, styleMap),
extractStyle(node),
extractIconHref(node),
extractExtendedData(node, schema),
extractTimeSpan(node),
extractTimeStamp(node)
),
};
if (box?.bbox) {
feature.bbox = box.bbox;
}
if (feature.properties?.visibility !== undefined) {
feature.properties.visibility = feature.properties.visibility !== "0";
}
const id = node.getAttribute("id");
if (id !== null && id !== "") feature.id = id;
return feature;
}