Skip to content

Commit

Permalink
feat: Add geometry-type property
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Oct 28, 2022
1 parent 4a5b220 commit 59df17e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
80 changes: 78 additions & 2 deletions lib/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12797,6 +12797,7 @@ Object {
"type": "Polygon",
},
"properties": Object {
"@geometry-type": "groundoverlay",
"description": "Overlay shows Mount Etna erupting
on July 13th, 2001.",
"icon": "https://developers.google.com/kml/documentation/images/etna.jpg",
Expand All @@ -12813,7 +12814,45 @@ exports[`toGeoJSON ground_overlay.kml 2`] = `
Object {
"children": Array [
Object {
"children": Array [],
"children": Array [
Object {
"geometry": Object {
"coordinates": Array [
Array [
Array [
14.60128369746704,
37.46543388598137,
],
Array [
14.60128369746704,
37.91904192681665,
],
Array [
15.35832653742206,
37.91904192681665,
],
Array [
14.60128369746704,
37.91904192681665,
],
Array [
14.60128369746704,
37.46543388598137,
],
],
],
"type": "Polygon",
},
"properties": Object {
"@geometry-type": "groundoverlay",
"description": "Overlay shows Mount Etna erupting
on July 13th, 2001.",
"icon": "https://developers.google.com/kml/documentation/images/etna.jpg",
"name": "Large-scale overlay on terrain",
},
"type": "Feature",
},
],
"meta": Object {
"description": "Examples of ground overlays",
"name": "Ground Overlays",
Expand Down Expand Up @@ -12857,6 +12896,7 @@ Object {
"type": "Polygon",
},
"properties": Object {
"@geometry-type": "groundoverlay",
"icon": "http://developers.google.com/kml/documentation/images/rectangle.gif",
"name": "gx:LatLonQuad Example",
},
Expand All @@ -12869,7 +12909,43 @@ Object {

exports[`toGeoJSON ground_overlay_quad.kml 2`] = `
Object {
"children": Array [],
"children": Array [
Object {
"geometry": Object {
"coordinates": Array [
Array [
Array [
81.601884,
44.160723,
],
Array [
83.529902,
43.665148,
],
Array [
82.947737,
44.248831,
],
Array [
81.509322,
44.321015,
],
Array [
81.601884,
44.160723,
],
],
],
"type": "Polygon",
},
"properties": Object {
"@geometry-type": "groundoverlay",
"icon": "http://developers.google.com/kml/documentation/images/rectangle.gif",
"name": "gx:LatLonQuad Example",
},
"type": "Feature",
},
],
"type": "root",
}
`;
Expand Down
25 changes: 25 additions & 0 deletions lib/kml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ function getFolder(node: Element): Folder {
* ]
* }
* ```
*
* ### GroundOverlay
*
* GroundOverlay elements are converted into
* `Feature` objects with `Polygon` geometries,
* a property like:
*
* ```json
* {
* "@geometry-type": "groundoverlay"
* }
* ```
*
* And the ground overlay's image URL in the `href`
* property. Ground overlays will need to be displayed
* with a separate method to other features, depending
* on which map framework you're using.
*/
export function kmlWithFolders(node: Document): Root {
const styleMap = buildStyleMap(node);
Expand All @@ -142,6 +159,14 @@ export function kmlWithFolders(node: Document): Root {
) {
if (isElement(node)) {
switch (node.tagName) {
case "GroundOverlay": {
placemarks.push(node);
const placemark = getGroundOverlay(node, styleMap);
if (placemark) {
pointer.children.push(placemark);
}
break;
}
case "Placemark": {
placemarks.push(node);
const placemark = getPlacemark(node, styleMap);
Expand Down
20 changes: 13 additions & 7 deletions lib/kml/ground_overlay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature, Geometry } from "geojson";
import { Feature, Polygon } from "geojson";
import { StyleMap, get1, num1, getMulti } from "../shared";
import {
extractCascadedStyle,
Expand All @@ -10,7 +10,7 @@ import {
import { extractIconHref, extractStyle } from "./extractStyle";
import { coord, fixRing, getCoordinates } from "./geometry";

function getGroundOverlayBox(node: Element): Geometry | null {
function getGroundOverlayBox(node: Element): Polygon | null {
const latLonQuad = get1(node, "gx:LatLonQuad");

if (latLonQuad) {
Expand All @@ -21,6 +21,10 @@ function getGroundOverlayBox(node: Element): Geometry | null {
};
}

return getLatLonBox(node);
}

function getLatLonBox(node: Element): Polygon | null {
const latLonBox = get1(node, "LatLonBox");

if (latLonBox) {
Expand All @@ -29,9 +33,6 @@ function getGroundOverlayBox(node: Element): Geometry | null {
const east = num1(latLonBox, "east");
const south = num1(latLonBox, "south");

// FIXME: rotation support
// const rotation = num1(latLonBox, "rotation");

if (
typeof north === "number" &&
typeof south === "number" &&
Expand Down Expand Up @@ -59,13 +60,18 @@ function getGroundOverlayBox(node: Element): Geometry | null {
export function getGroundOverlay(
node: Element,
styleMap: StyleMap
): Feature<Geometry | null> {
): Feature<Polygon | null> {
const geometry = getGroundOverlayBox(node);

const feature: Feature<Geometry | 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",
Expand Down

0 comments on commit 59df17e

Please sign in to comment.