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

MultiPoints do not save 'invalid Geometry value' #130

Closed
rukayaj opened this issue Dec 15, 2015 · 1 comment
Closed

MultiPoints do not save 'invalid Geometry value' #130

rukayaj opened this issue Dec 15, 2015 · 1 comment
Labels

Comments

@rukayaj
Copy link
Contributor

rukayaj commented Dec 15, 2015

I get an error "invalid geometry value" when I try to add points to a multipoint field. I have a model like this :

from django.contrib.gis.db import models
class Project(models.Model):
    point_locations = models.MultiPointField(null=True, blank=True)

And I'm using a generic UpdateView and a ModelForm (specifying the LeafletWidget() for the point_locations field). This works beautifully for points and multipolygons, but multipoints gives me the "invalid geometry value" error.

Something seems to be going wrong in leaflet.forms.js in the _serialize function. Changing line 37: var geojson = geom.toGeoJSON(); console.log(geojson); gives something like this:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[19.589996337890625,-33.29609930468822]}}]}

which looks promising, but the next few lines seem to lose this info. Going on in the code, is_generic is false for multipoints, which means that the else is triggered here:

        if (is_multi && is_generic) {
            var flat = {type: 'GeometryCollection', geometries: []};
            for (var i=0; i < geojson.features.length; i++) {
                flat.geometries.push(geojson.features[i].geometry);
            }
            geojson = flat;
        }
        else {
            geojson = geojson.geometry;
        }
        return JSON.stringify(geojson);

and there is no geometry in the geojson object, which causes this function to return 'undefined'. So I have fixed this for myself by adding the following:

        if (is_multi && is_generic) {
            var flat = {type: 'GeometryCollection', geometries: []};
            for (var i=0; i < geojson.features.length; i++) {
                flat.geometries.push(geojson.features[i].geometry);
            }
            geojson = flat;
        }
        // My addition
        else if (this.options.geom_type == 'MULTIPOINT') {
            var flat = {type: 'MultiPoint', coordinates: []};
            for (var i=0; i < geojson.features.length; i++) {
                flat.coordinates.push(geojson.features[i].geometry.coordinates);
            }
            geojson = flat;
        }
        // End my addition
        else {
            geojson = geojson.geometry;
        }
        return JSON.stringify(geojson);

I don't know if what I've done here is correct. This doesn't seem to be an issue anyone else has encountered, perhaps I am doing something wrong somewhere else? My forms work fine for multipolygons and points though, which makes me think this is a bug that's cropped up.

@leplatrem
Copy link
Collaborator

Thanks for reporting this!

@PetrDlouhy committed a fix for geometrycollections in current master. It might be related to your problem.

Could you please a pull-request with the changes you suggest so that we can comment/review/merge?

Thanks a lot!


We would welcome any help for maintaining this library. If you use it and find it useful, please Watch it on Github, help us review contributions or comment issues :) Thousand thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants