-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #945 from ZeLonewolf/zlw-better-clip-script
Improved render sample config
- Loading branch information
Showing
7 changed files
with
172 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,6 @@ | |
import config from "./config.js"; | ||
|
||
import * as Label from "./constants/label.js"; | ||
import * as Style from "./js/style.js"; | ||
|
||
import * as Poi from "./js/poi.js"; | ||
|
||
import * as languageLabel from "./js/language_label.js"; | ||
|
||
|
@@ -17,26 +14,8 @@ import LegendControl from "./js/legend_control.js"; | |
import * as LegendConfig from "./js/legend_config.js"; | ||
import SampleControl from "openmapsamples-maplibre/OpenMapSamplesControl.js"; | ||
import { default as OpenMapTilesSamples } from "openmapsamples/samples/OpenMapTiles/index.js"; | ||
import { URLShieldRenderer } from "@americana/maplibre-shield-generator"; | ||
import { | ||
shieldPredicate, | ||
networkPredicate, | ||
routeParser, | ||
} from "./js/shield_format.js"; | ||
|
||
export function buildStyle() { | ||
var getUrl = window.location; | ||
var baseUrl = (getUrl.protocol + "//" + getUrl.host + getUrl.pathname) | ||
//Trim trailing slashes from URL | ||
.replace(/\/+$/, ""); | ||
return Style.build( | ||
config.OPENMAPTILES_URL, | ||
`${baseUrl}/sprites/sprite`, | ||
config.FONT_URL ?? | ||
"https://osm-americana.github.io/fontstack66/{fontstack}/{range}.pbf", | ||
Label.getLocales() | ||
); | ||
} | ||
|
||
import { createMap, loadRTLPlugin, buildStyle } from "./js/map_builder.js"; | ||
|
||
function upgradeLegacyHash() { | ||
let hash = window.location.hash.substr(1); | ||
|
@@ -47,22 +26,17 @@ function upgradeLegacyHash() { | |
} | ||
upgradeLegacyHash(); | ||
|
||
maplibregl.setRTLTextPlugin( | ||
"https://unpkg.com/@mapbox/[email protected]/mapbox-gl-rtl-text.min.js", | ||
null, | ||
true | ||
); | ||
loadRTLPlugin(); | ||
|
||
window.maplibregl = maplibregl; | ||
export const map = (window.map = new maplibregl.Map({ | ||
export const map = createMap(window, (shields) => shieldDefLoad(shields), { | ||
container: "map", // container id | ||
hash: "map", | ||
antialias: true, | ||
style: buildStyle(), | ||
center: [-94, 40.5], // starting position [lng, lat] | ||
zoom: 4, // starting zoom | ||
center: [-94, 40.5], | ||
zoom: 4, | ||
attributionControl: false, | ||
})); | ||
}); | ||
|
||
let options = {}; | ||
|
||
|
@@ -79,12 +53,6 @@ OpenMapTilesSamples.forEach((sample, i) => { | |
|
||
let legendControl; | ||
|
||
const shieldRenderer = new URLShieldRenderer("shields.json", routeParser) | ||
.filterImageID(shieldPredicate) | ||
.filterNetwork(networkPredicate) | ||
.renderOnMaplibreGL(map) | ||
.onShieldDefLoad((shields) => shieldDefLoad(shields)); | ||
|
||
function shieldDefLoad(shields) { | ||
legendControl = new LegendControl(shields); | ||
legendControl.sections = LegendConfig.sections; | ||
|
@@ -128,19 +96,6 @@ function shieldDefLoad(shields) { | |
} | ||
} | ||
|
||
map.on("styleimagemissing", function (e) { | ||
switch (e.id.split("\n")[0]) { | ||
case "shield": | ||
break; | ||
case "poi": | ||
Poi.missingIconHandler(shieldRenderer, map, e); | ||
break; | ||
default: | ||
console.warn("Image id not recognized:", JSON.stringify(e.id)); | ||
break; | ||
} | ||
}); | ||
|
||
function hotReloadMap() { | ||
map.setStyle(buildStyle()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
import "maplibre-gl/dist/maplibre-gl.css"; | ||
|
||
import { createMap, loadRTLPlugin, buildStyle } from "./js/map_builder.js"; | ||
|
||
loadRTLPlugin(); | ||
|
||
export const map = createMap(window, (shields) => shieldDefLoad(), { | ||
container: "map", // container id | ||
hash: "map", | ||
antialias: true, | ||
style: buildStyle(), | ||
center: [-94, 40.5], | ||
zoom: 4, | ||
fadeDuration: 0, | ||
attributionControl: false, | ||
}); | ||
|
||
function shieldDefLoad() { | ||
if (window.top === window.self) { | ||
// if not embedded in an iframe, autofocus canvas to enable keyboard shortcuts | ||
map.getCanvas().focus(); | ||
} | ||
|
||
if (window.LIVE_RELOAD) { | ||
new EventSource("/esbuild").addEventListener("change", () => | ||
location.reload() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>OpenStreetMap Americana</title> | ||
<meta | ||
name="viewport" | ||
content="initial-scale=1,maximum-scale=1,user-scalable=no" | ||
/> | ||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
#map { | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
width: 100%; | ||
} | ||
</style> | ||
<script type="module" src="bare_americana.js"></script> | ||
<link rel="stylesheet" href="americana.css" /> | ||
</head> | ||
|
||
<body> | ||
<div id="map"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Functions for assembling user-facing map components | ||
*/ | ||
import { | ||
ShieldDefinitions, | ||
URLShieldRenderer, | ||
} from "@americana/maplibre-shield-generator"; | ||
import config from "../config.js"; | ||
|
||
import { | ||
shieldPredicate, | ||
networkPredicate, | ||
routeParser, | ||
} from "../js/shield_format.js"; | ||
|
||
import * as Poi from "../js/poi.js"; | ||
import * as Label from "../constants/label.js"; | ||
import * as Style from "../js/style.js"; | ||
import maplibregl, { Map, MapOptions, StyleSpecification } from "maplibre-gl"; | ||
|
||
export function buildStyle(): StyleSpecification { | ||
var getUrl = window.location; | ||
var baseUrl = ( | ||
getUrl.protocol + | ||
"//" + | ||
getUrl.host + | ||
removeAfterLastSlash(getUrl.pathname) | ||
) | ||
//Trim trailing slashes from URL | ||
.replace(/\/+$/, ""); | ||
return Style.build( | ||
config.OPENMAPTILES_URL, | ||
`${baseUrl}/sprites/sprite`, | ||
config.FONT_URL ?? | ||
"https://osm-americana.github.io/fontstack66/{fontstack}/{range}.pbf", | ||
Label.getLocales() | ||
); | ||
} | ||
|
||
function removeAfterLastSlash(str: string): string { | ||
const lastSlashIndex = str.lastIndexOf("/"); | ||
if (lastSlashIndex === -1) { | ||
return str; // return the original string if no slash is found | ||
} | ||
return str.substring(0, lastSlashIndex + 1); | ||
} | ||
|
||
export function loadRTLPlugin(): void { | ||
maplibregl.setRTLTextPlugin( | ||
"https://unpkg.com/@mapbox/[email protected]/mapbox-gl-rtl-text.min.js", | ||
null, | ||
true | ||
); | ||
} | ||
|
||
export function createMap( | ||
window, | ||
shieldDefCallback: (shields: ShieldDefinitions) => void, | ||
options: MapOptions | ||
): Map { | ||
window.maplibregl = maplibregl; | ||
let map: Map = (window.map = new maplibregl.Map(options)); | ||
|
||
const shieldRenderer = new URLShieldRenderer("shields.json", routeParser) | ||
.filterImageID(shieldPredicate) | ||
.filterNetwork(networkPredicate) | ||
.renderOnMaplibreGL(map) | ||
.onShieldDefLoad(shieldDefCallback); | ||
|
||
map.on("styleimagemissing", function (e) { | ||
switch (e.id.split("\n")[0]) { | ||
case "shield": | ||
break; | ||
case "poi": | ||
Poi.missingIconHandler(shieldRenderer, map, e); | ||
break; | ||
default: | ||
console.warn("Image id not recognized:", JSON.stringify(e.id)); | ||
break; | ||
} | ||
}); | ||
return map; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters