Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoJSON support #2130

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _includes/head.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@
>
<script src="{{ '/assets/js/theme.js' | relative_url | bust_file_cache }}"></script>
{% endif %}

<!-- GeoJSON support via Leaflet -->
{% if page.map %}
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""
>
{% endif %}
28 changes: 28 additions & 0 deletions _includes/scripts/leaflet.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% if page.map %}
<script
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""
></script>
<script>
/* Create leaflet map as another node and hide the code block, appending the leaflet node after it
this is done to enable retrieving the code again when changing theme between light/dark */
document.querySelectorAll('pre>code.language-geojson').forEach((elem) => {
const jsonData = elem.textContent;
const backup = elem.parentElement;
backup.classList.add('unloaded');
/* create leaflet node */
let mapElement = document.createElement('div');
mapElement.classList.add('map');
backup.after(mapElement);

var map = L.map(mapElement);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(map);
let geoJSON = L.geoJSON(JSON.parse(jsonData)).addTo(map);
map.fitBounds(geoJSON.getBounds());
});
</script>
{% endif %}
1 change: 1 addition & 0 deletions _layouts/default.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
{% include scripts/masonry.liquid %}
{% include scripts/mermaid.liquid %}
{% include scripts/chart.liquid %}
{% include scripts/leaflet.liquid %}
{% include scripts/misc.liquid %}
{% include scripts/badges.liquid %}
{% include scripts/mathjax.liquid %}
Expand Down
93 changes: 93 additions & 0 deletions _posts/2024-01-26-geojson-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
layout: post
title: a post with geojson
date: 2024-01-26 17:57:00
description: this is what included geojson code could look like
tags: formatting charts maps
categories: sample-posts
map: true
---

This is an example post with some [geojson](https://geojson.org/) code. To create your own visualization, go to [geojson.io](https://geojson.io/).

````markdown
```geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
-60.11363029935569,
-2.904625022183211
],
[
-60.11363029935569,
-3.162613728707967
],
[
-59.820894493858034,
-3.162613728707967
],
[
-59.820894493858034,
-2.904625022183211
],
[
-60.11363029935569,
-2.904625022183211
]
]
],
"type": "Polygon"
}
}
]
}
```
````

Which generates:

```geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
-60.11363029935569,
-2.904625022183211
],
[
-60.11363029935569,
-3.162613728707967
],
[
-59.820894493858034,
-3.162613728707967
],
[
-59.820894493858034,
-2.904625022183211
],
[
-60.11363029935569,
-2.904625022183211
]
]
],
"type": "Polygon"
}
}
]
}
```
5 changes: 5 additions & 0 deletions _sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1081,3 +1081,8 @@ nav[data-toggle="toc"] {
#toc-sidebar {
z-index: 1;
}

.map {
width: 100%;
height: 400px;
}
1 change: 1 addition & 0 deletions assets/js/copy_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ codeBlocks.forEach(function (codeBlock) {
if (
(codeBlock.querySelector("pre:not(.lineno)") || codeBlock.querySelector("code")) &&
codeBlock.querySelector("code:not(.language-chart)") &&
codeBlock.querySelector("code:not(.language-geojson)") &&
codeBlock.querySelector("code:not(.language-mermaid)")
) {
// create copy button
Expand Down