Skip to content

Commit

Permalink
wip: do not fail when setting "Circles" layer with non markers data
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Aug 15, 2024
1 parent 9304630 commit e42ed43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
38 changes: 9 additions & 29 deletions umap/static/umap/js/modules/data/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Feature {
}

getUIClass() {
return this.getOption('UIClass') || this.getDefaultUIClass()
return this.getOption('UIClass')
}

getClassName() {
Expand Down Expand Up @@ -560,8 +560,8 @@ class Feature {
properties.lon = center.lng
properties.lng = center.lng
properties.alt = center?.alt
if (typeof this.getMeasure !== 'undefined') {
properties.measure = this.getMeasure()
if (typeof this.ui.getMeasure !== 'undefined') {
properties.measure = this.ui.getMeasure()
}
}
return L.extend(properties, this.properties)
Expand Down Expand Up @@ -601,8 +601,8 @@ export class Point extends Feature {
return { coordinates: GeoJSON.latLngToCoords(latlng), type: 'Point' }
}

getDefaultUIClass() {
return LeafletMarker
getUIClass() {
return super.getUIClass() || LeafletMarker
}

hasGeom() {
Expand Down Expand Up @@ -710,16 +710,6 @@ class Path extends Feature {
]
}

getStyle() {
const options = {}
for (const option of this.ui.getStyleOptions()) {
options[option] = this.getDynamicOption(option)
}
if (options.interactive) options.pointerEvents = 'visiblePainted'
else options.pointerEvents = 'stroke'
return options
}

getBestZoom() {
return this.getOption('zoomTo') || this.map.getBoundsZoom(this.bounds, true)
}
Expand Down Expand Up @@ -805,19 +795,14 @@ export class LineString extends Path {
return !this.coordinates.length
}

getDefaultUIClass() {
return LeafletPolyline
getUIClass() {
return super.getUIClass() || LeafletPolyline
}

isSameClass(other) {
return other instanceof LineString
}

getMeasure(shape) {
const length = L.GeoUtil.lineLength(this.map, shape || this.ui._defaultShape())
return L.GeoUtil.readableDistance(length, this.map.measureTools.getMeasureUnit())
}

toPolygon() {
const geojson = this.toGeoJSON()
geojson.geometry.type = 'Polygon'
Expand Down Expand Up @@ -922,9 +907,9 @@ export class Polygon extends Path {
return !this.coordinates.length || !this.coordinates[0].length
}

getDefaultUIClass() {
getUIClass() {
if (this.getOption('mask')) return MaskPolygon
return LeafletPolygon
return super.getUIClass() || LeafletPolygon
}

isSameClass(other) {
Expand Down Expand Up @@ -956,11 +941,6 @@ export class Polygon extends Path {
return options
}

getMeasure(shape) {
const area = L.GeoUtil.geodesicArea(shape || this.ui._defaultShape())
return L.GeoUtil.readableArea(area, this.map.measureTools.getMeasureUnit())
}

toLineString() {
const geojson = this.toGeoJSON()
delete geojson.id
Expand Down
24 changes: 22 additions & 2 deletions umap/static/umap/js/modules/rendering/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DomUtil,
LineUtil,
latLng,
LatLng,
LatLngBounds,
} from '../../../vendors/leaflet/leaflet-src.esm.js'
import { translate } from '../i18n.js'
Expand Down Expand Up @@ -267,7 +268,7 @@ export const LeafletMarker = Marker.extend({
const PathMixin = {
_onMouseOver: function () {
if (this._map.measureTools?.enabled()) {
this._map.tooltip.open({ content: this.feature.getMeasure(), anchor: this })
this._map.tooltip.open({ content: this.getMeasure(), anchor: this })
} else if (this._map.editEnabled && !this._map.editedFeature) {
this._map.tooltip.open({ content: translate('Click to edit'), anchor: this })
}
Expand Down Expand Up @@ -334,7 +335,7 @@ const PathMixin = {
let items = FeatureMixin.getContextMenuItems.call(this, event)
items.push({
text: translate('Display measure'),
callback: () => Alert.info(this.feature.getMeasure()),
callback: () => Alert.info(this.getMeasure()),
})
if (this._map.editEnabled && !this.feature.isReadOnly() && this.feature.isMulti()) {
items = items.concat(this.getContextMenuMultiItems(event))
Expand Down Expand Up @@ -468,6 +469,12 @@ export const LeafletPolyline = Polyline.extend({
})
return items
},

getMeasure: function (shape) {
// FIXME: compute from data in feature (with TurfJS)
const length = L.GeoUtil.lineLength(this._map, shape || this._defaultShape())
return L.GeoUtil.readableDistance(length, this._map.measureTools.getMeasureUnit())
},
})

export const LeafletPolygon = Polygon.extend({
Expand Down Expand Up @@ -502,6 +509,11 @@ export const LeafletPolygon = Polygon.extend({
startHole: function (event) {
this.enableEdit().newHole(event.latlng)
},

getMeasure: function (shape) {
const area = L.GeoUtil.geodesicArea(shape || this._defaultShape())
return L.GeoUtil.readableArea(area, this._map.measureTools.getMeasureUnit())
},
})
const WORLD = [
latLng([90, 180]),
Expand Down Expand Up @@ -541,6 +553,14 @@ export const MaskPolygon = LeafletPolygon.extend({
export const CircleMarker = BaseCircleMarker.extend({
parentClass: BaseCircleMarker,
includes: [FeatureMixin, PathMixin],
initialize: function (feature, latlng) {
if (Array.isArray(latlng) && !(latlng[0] instanceof Number)) {
// Must be a line or polygon
const bounds = new LatLngBounds(latlng)
latlng = bounds.getCenter()
}
FeatureMixin.initialize.call(this, feature, latlng)
},
getClass: () => CircleMarker,
getStyleOptions: function () {
const options = PathMixin.getStyleOptions.call(this)
Expand Down
4 changes: 2 additions & 2 deletions umap/static/umap/js/umap.controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ U.Editable = L.Editable.extend({
} else {
const tmpLatLngs = e.layer.editor._drawnLatLngs.slice()
tmpLatLngs.push(e.latlng)
measure = e.layer.feature.getMeasure(tmpLatLngs)
measure = e.layer.getMeasure(tmpLatLngs)

if (e.layer.editor._drawnLatLngs.length < e.layer.editor.MIN_VERTEX) {
// when drawing second point
Expand All @@ -1273,7 +1273,7 @@ U.Editable = L.Editable.extend({
}
} else {
// when moving an existing point
measure = e.layer.feature.getMeasure()
measure = e.layer.getMeasure()
}
if (measure) {
if (e.layer instanceof L.Polygon) {
Expand Down

0 comments on commit e42ed43

Please sign in to comment.