-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Zoom default table map to account for points #10
Comments
I think the right place to do this is in the datasette-leaflet-freedraw/datasette_leaflet_freedraw/__init__.py Lines 74 to 106 in 3ecd505
Instead, I'm going to have the JavaScript on the page use a |
Partial prototype: let innerSql = Array.from(document.getElementsByTagName("span")).filter(
el => el.innerText == "View and edit SQL"
)[0].parentElement.getAttribute("title")
let outerSql = `with inner as (${innerSql}) select AsGeoJSON(Extent(geometry)) as bounds from inner`;
let queryPath = location.pathname.split("/").slice(0, -1).join("/") + ".json"
fetch(
queryPath + "?sql=" + encodeURIComponent(outerSql) +
"&_shape=array&_json=bounds"
).then(d => d.json()).then(r => console.log(r)) |
I think I then need this: |
This has all got too messy. I'm going to block work on this until there's a cleaner way to get at the necessary information without reading title attributes and similar: |
Here's as far as I got: diff --git a/datasette_leaflet_freedraw/static/datasette-leaflet-freedraw.js b/datasette_leaflet_freedraw/static/datasette-leaflet-freedraw.js
index 16b86b8..754c75b 100644
--- a/datasette_leaflet_freedraw/static/datasette-leaflet-freedraw.js
+++ b/datasette_leaflet_freedraw/static/datasette-leaflet-freedraw.js
@@ -53,6 +53,23 @@ window.addEventListener("load", () => {
});
});
+function zoomMapToExtent(map) {
+ let tableSql = Array.from(document.getElementsByTagName("span")).filter(
+ el => el.innerText == "View and edit SQL"
+ )[0].parentElement.getAttribute("title");
+ let sql = `with inner as (${tableSql}) select AsGeoJSON(Extent(geometry)) as bounds from inner`;
+ let queryPath = location.pathname.split("/").slice(0, -1).join("/") + ".json"
+ fetch(
+ queryPath + "?sql=" + encodeURIComponent(sql) +
+ "&_shape=array&_json=bounds" + location.search.replace(/^\?/, '&')
+ ).then(d => d.json()).then(rows => {
+ if (rows.length == 1) {
+ let bounds = rows[0].bounds;
+ map.fitBounds(L.geoJSON(bounds).getBounds())
+ }
+ });
+}
+
function configureMap(input) {
let div = document.createElement("div");
div.style.marginTop = "1em";
@@ -64,6 +81,12 @@ function configureMap(input) {
zoom: 2,
layers: [tiles],
});
+
+ // If table page, fire off code to zoom to extent of bounds
+ if (datasette.leaflet_freedraw.show_for_table && !input.value) {
+ zoomMapToExtent(map);
+ }
+
let freeDraw = new FreeDraw();
/* If input.value is GeoJSON, add those to the map */
let allPoints = []; Then I realized I needed special code to ensure that I copied over the |
The map here starts at the entire world: https://calands.datasettes.com/calands/CPAD_2020a_SuperUnits
But there are only points in California:
https://calands.datasettes.com/calands?sql=select+AsGeoJSON%28Extent%28geometry%29%29+from+CPAD_2020a_SuperUnits
Returns:
The text was updated successfully, but these errors were encountered: