Skip to content

Commit

Permalink
fix: leaflets.forms for multipolygon/multipolyline
Browse files Browse the repository at this point in the history
  • Loading branch information
Gagaro committed Mar 22, 2017
1 parent 633b7e4 commit 15356b2
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions leaflet/static/leaflet/leaflet.forms.js
Expand Up @@ -17,6 +17,7 @@ L.FieldStore = L.Class.extend({
var items = typeof(layer.getLayers) == 'function' ? layer.getLayers() : [layer],
is_multi = this.options.is_collection || items.length > 1,
is_generic = this.options.is_generic,
collection_type = this.options.collection_type,
is_empty = items.length === 0;

if (is_empty)
Expand All @@ -43,6 +44,26 @@ L.FieldStore = L.Class.extend({
}
geojson = flat;
}
// Special case for MultiPolyline/MultiPolygon because it was removed from leaflet 1.0
else if (collection_type != 'featureGroup') {
var latlngs = [];
for (var i = 0; i < geojson.features.length; i++) {
var latlng = [];
var coord = geojson.features[i].geometry.coordinates;
if (collection_type == 'polygon') {
coord = coord[0];
}
for (var j = 0; j < coord.length; j++) {
latlng.push([coord[j][1], coord[j][0]]);
}
if (collection_type == 'polygon') {
latlng = [latlng];
}
latlngs.push(latlng);
}
geom = L[collection_type](latlngs);
geojson = geom.toGeoJSON().geometry;
}
// In order to make multipoint work, it seems we need to treat it similarly to the GeometryCollections
else if (this.options.geom_type == 'MULTIPOINT') {
var flat = {type: 'MultiPoint', coordinates: []};
Expand Down Expand Up @@ -84,8 +105,8 @@ L.GeometryField = L.Class.extend({
options.is_polygon = /polygon$/.test(geom_type) || options.is_generic;
options.is_point = /point$/.test(geom_type) || options.is_generic;
options.collection_type = ({
'multilinestring': 'multiPolyline',
'multipolygon': 'multiPolygon',
'multilinestring': 'polyline',
'multipolygon': 'polygon',
})[geom_type] || 'featureGroup';

L.setOptions(this, options);
Expand Down Expand Up @@ -159,15 +180,21 @@ L.GeometryField = L.Class.extend({
var geometry = this.store.load();
if (geometry) {
// Add initial geometry to the map
geometry.addTo(this._map);
if (geometry instanceof L.LayerGroup) {
geometry.eachLayer(function (l) {
this.drawnItems.addLayer(l);
}, this);
}
else if (geometry instanceof L.Polygon || geometry instanceof L.Polyline) {
var latlngs = geometry.getLatLngs();
for (var i = 0; i < latlngs.length; i++) {
this.drawnItems.addLayer(L[this.options.collection_type](latlngs[i]));
}
}
else {
this.drawnItems.addLayer(geometry);
}
this.drawnItems.addTo(this._map);
}
this._setView();
return geometry;
Expand Down Expand Up @@ -210,7 +237,7 @@ L.GeometryField = L.Class.extend({
},

_editionLayer: function () {
var type = this.options.collection_type,
var type = 'featureGroup',
constructor = L[type];
if (typeof(constructor) != 'function') {
throw 'Unsupported geometry type: ' + type;
Expand Down

0 comments on commit 15356b2

Please sign in to comment.