Skip to content

Commit

Permalink
Merge pull request #1465 from umap-project/defaultview-latest-path
Browse files Browse the repository at this point in the history
Use bounds instead of center when zooming to Polyline/Polygon
  • Loading branch information
yohanboniface authored Dec 15, 2023
2 parents 8568159 + 0852077 commit a32b680
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
12 changes: 12 additions & 0 deletions umap/static/umap/js/umap.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,18 @@ L.U.PathMixin = {
bounds = bounds || this.map.getBounds()
return bounds.overlaps(this.getBounds())
},

zoomTo: function (e) {
// Use bounds instead of centroid for paths.
e = e || {}
const easing = e.easing !== undefined ? e.easing : this.map.options.easing
if (easing) {
this.map.flyToBounds(this.getBounds(), this.getBestZoom())
} else {
this.map.fitBounds(this.getBounds(), this.getBestZoom() || this.map.getZoom())
}
if (e.callback) e.callback.call(this)
},
}

L.U.Polyline = L.Polyline.extend({
Expand Down
2 changes: 1 addition & 1 deletion umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ L.U.Map.include({
this.renderEditToolbar()
}
this.initShortcuts()
this.onceDatalayersLoaded(function () {
this.onceDataLoaded(function () {
if (L.Util.queryString('share')) this.renderShareBox()
else if (this.options.onLoadPanel === 'databrowser') this.openBrowser()
else if (this.options.onLoadPanel === 'caption') this.displayCaption()
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.68896484375, 48.55297816440071],
"coordinates": [14.68896484375, 48.55297816440071],
},
"properties": {
"_umap_options": {"color": "DarkCyan", "iconClass": "Ball"},
Expand Down
4 changes: 2 additions & 2 deletions umap/tests/integration/test_export_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_umap_export(map, live_server, datalayer, page):
"features": [
{
"geometry": {
"coordinates": [13.68896484375, 48.55297816440071],
"coordinates": [14.68896484375, 48.55297816440071],
"type": "Point",
},
"properties": {
Expand Down Expand Up @@ -85,5 +85,5 @@ def test_csv_export(map, live_server, datalayer, page):
assert (
path.read_text()
== """name,description,Latitude,Longitude
Here,Da place anonymous again 755,48.55297816440071,13.68896484375"""
Here,Da place anonymous again 755,48.55297816440071,14.68896484375"""
)
73 changes: 71 additions & 2 deletions umap/tests/integration/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,80 @@ def test_default_view_latest_without_datalayer_should_use_default_center(
):
datalayer.settings["displayOnLoad"] = False
datalayer.save()
map.settings["defaultView"] = "latest"
map.settings["properties"]["defaultView"] = "latest"
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
# Hash is defined, so map is initialized
expect(page).to_have_url(re.compile(".*#7/.*"))
expect(page).to_have_url(re.compile(r".*#7/48\..+/13\..+"))
layers = page.locator(".umap-browse-datalayers li")
expect(layers).to_have_count(1)


def test_default_view_latest_with_marker(map, live_server, datalayer, page):
map.settings["properties"]["defaultView"] = "latest"
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
# Hash is defined, so map is initialized
expect(page).to_have_url(re.compile(r".*#7/48\..+/14\..+"))
layers = page.locator(".umap-browse-datalayers li")
expect(layers).to_have_count(1)


def test_default_view_latest_with_line(map, live_server, page):
data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "a line"},
"geometry": {
"type": "LineString",
"coordinates": [
[2.12, 49.57],
[3.19, 48.77],
[2.51, 47.55],
[1.08, 49.02],
],
},
}
],
}
DataLayerFactory(map=map, data=data)
map.settings["properties"]["defaultView"] = "latest"
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(page).to_have_url(re.compile(r".*#8/48\..+/2\..+"))
layers = page.locator(".umap-browse-datalayers li")
expect(layers).to_have_count(1)


def test_default_view_latest_with_polygon(map, live_server, page):
data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "a polygon"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[2.12, 49.57],
[1.08, 49.02],
[2.51, 47.55],
[3.19, 48.77],
[2.12, 49.57],
]
],
},
}
],
}
DataLayerFactory(map=map, data=data)
map.settings["properties"]["defaultView"] = "latest"
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(page).to_have_url(re.compile(r".*#8/48\..+/2\..+"))
layers = page.locator(".umap-browse-datalayers li")
expect(layers).to_have_count(1)

Expand Down
2 changes: 1 addition & 1 deletion umap/tests/test_map_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def test_download(client, map, datalayer):
"features": [
{
"geometry": {
"coordinates": [13.68896484375, 48.55297816440071],
"coordinates": [14.68896484375, 48.55297816440071],
"type": "Point",
},
"properties": {
Expand Down

0 comments on commit a32b680

Please sign in to comment.